1 // SPDX-License-Identifier: GPL-2.0-only
2 /* gain-time-scale conversion helpers for IIO light sensors
4 * Copyright (c) 2023 Matti Vaittinen <mazziesaccount@gmail.com>
7 #include <linux/device.h>
8 #include <linux/errno.h>
9 #include <linux/export.h>
10 #include <linux/minmax.h>
11 #include <linux/module.h>
12 #include <linux/overflow.h>
13 #include <linux/slab.h>
14 #include <linux/sort.h>
15 #include <linux/types.h>
16 #include <linux/units.h>
18 #include <linux/iio/iio-gts-helper.h>
19 #include <linux/iio/types.h>
22 * iio_gts_get_gain - Convert scale to total gain
24 * Internal helper for converting scale to total gain.
26 * @max: Maximum linearized scale. As an example, when scale is created
27 * in magnitude of NANOs and max scale is 64.1 - The linearized
28 * scale is 64 100 000 000.
29 * @scale: Linearized scale to compute the gain for.
31 * Return: (floored) gain corresponding to the scale. -EINVAL if scale
34 static int iio_gts_get_gain(const u64 max, const u64 scale)
38 if (scale > full || !scale)
41 return div64_u64(full, scale);
45 * gain_get_scale_fraction - get the gain or time based on scale and known one
47 * @max: Maximum linearized scale. As an example, when scale is created
48 * in magnitude of NANOs and max scale is 64.1 - The linearized
49 * scale is 64 100 000 000.
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
56 * depending on which one is known.
58 * Return: 0 on success.
60 static int gain_get_scale_fraction(const u64 max, u64 scale, int known,
65 tot_gain = iio_gts_get_gain(max, scale);
69 *unknown = tot_gain / known;
71 /* We require total gain to be exact multiple of known * unknown */
72 if (!*unknown || *unknown * known != tot_gain)
78 static int iio_gts_delinearize(u64 lin_scale, unsigned long scaler,
79 int *scale_whole, int *scale_nano)
89 frac = do_div(lin_scale, scaler);
91 *scale_whole = lin_scale;
92 *scale_nano = frac * (NANO / scaler);
97 static int iio_gts_linearize(int scale_whole, int scale_nano,
98 unsigned long scaler, u64 *lin_scale)
101 * Expect scale to be (mostly) NANO or MICRO. Divide divider instead of
102 * multiplication followed by division to avoid overflow.
104 if (scaler > NANO || !scaler)
107 *lin_scale = (u64)scale_whole * (u64)scaler +
108 (u64)(scale_nano / (NANO / scaler));
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
117 * @scale_int: Pointer to integral part of the scale (typically val1)
118 * @scale_nano: Pointer to fractional part of the scale (nano or ppb)
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.
124 * Return: 0 on success. Negative errno on failure.
126 int iio_gts_total_gain_to_scale(struct iio_gts *gts, int total_gain,
127 int *scale_int, int *scale_nano)
131 tmp = gts->max_scale;
133 do_div(tmp, total_gain);
135 return iio_gts_delinearize(tmp, NANO, scale_int, scale_nano);
137 EXPORT_SYMBOL_NS_GPL(iio_gts_total_gain_to_scale, IIO_GTS_HELPER);
140 * iio_gts_purge_avail_scale_table - free-up the available scale tables
141 * @gts: Gain time scale descriptor
143 * Free the space reserved by iio_gts_build_avail_scale_table().
145 static void iio_gts_purge_avail_scale_table(struct iio_gts *gts)
149 if (gts->per_time_avail_scale_tables) {
150 for (i = 0; i < gts->num_itime; i++)
151 kfree(gts->per_time_avail_scale_tables[i]);
153 kfree(gts->per_time_avail_scale_tables);
154 gts->per_time_avail_scale_tables = NULL;
157 kfree(gts->avail_all_scales_table);
158 gts->avail_all_scales_table = NULL;
160 gts->num_avail_all_scales = 0;
163 static int iio_gts_gain_cmp(const void *a, const void *b)
165 return *(int *)a - *(int *)b;
168 static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
170 int ret, i, j, new_idx, time_idx;
174 for (i = 0; i < gts->num_itime; i++) {
176 * Sort the tables for nice output and for easier finding of
179 sort(gains[i], gts->num_hwgain, sizeof(int), iio_gts_gain_cmp,
182 /* Convert gains to scales */
183 for (j = 0; j < gts->num_hwgain; j++) {
184 ret = iio_gts_total_gain_to_scale(gts, gains[i][j],
186 &scales[i][2 * j + 1]);
192 gain_bytes = array_size(gts->num_hwgain, sizeof(int));
193 all_gains = kcalloc(gts->num_itime, gain_bytes, GFP_KERNEL);
198 * We assume all the gains for same integration time were unique.
199 * It is likely the first time table had greatest time multiplier as
200 * the times are in the order of preference and greater times are
201 * usually preferred. Hence we start from the last table which is likely
202 * to have the smallest total gains.
204 time_idx = gts->num_itime - 1;
205 memcpy(all_gains, gains[time_idx], gain_bytes);
206 new_idx = gts->num_hwgain;
209 for (j = 0; j < gts->num_hwgain; j++) {
210 int candidate = gains[time_idx][j];
213 if (candidate > all_gains[new_idx - 1]) {
214 all_gains[new_idx] = candidate;
219 for (chk = 0; chk < new_idx; chk++)
220 if (candidate <= all_gains[chk])
223 if (candidate == all_gains[chk])
226 memmove(&all_gains[chk + 1], &all_gains[chk],
227 (new_idx - chk) * sizeof(int));
228 all_gains[chk] = candidate;
233 gts->avail_all_scales_table = kcalloc(new_idx, 2 * sizeof(int),
235 if (!gts->avail_all_scales_table) {
239 gts->num_avail_all_scales = new_idx;
241 for (i = 0; i < gts->num_avail_all_scales; i++) {
242 ret = iio_gts_total_gain_to_scale(gts, all_gains[i],
243 >s->avail_all_scales_table[i * 2],
244 >s->avail_all_scales_table[i * 2 + 1]);
247 kfree(gts->avail_all_scales_table);
248 gts->num_avail_all_scales = 0;
260 * iio_gts_build_avail_scale_table - create tables of available scales
261 * @gts: Gain time scale descriptor
263 * Build the tables which can represent the available scales based on the
264 * originally given gain and time tables. When both time and gain tables are
265 * given this results:
266 * 1. A set of tables representing available scales for each supported
268 * 2. A single table listing all the unique scales that any combination of
269 * supported gains and times can provide.
271 * NOTE: Space allocated for the tables must be freed using
272 * iio_gts_purge_avail_scale_table() when the tables are no longer needed.
274 * Return: 0 on success.
276 static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
278 int **per_time_gains, **per_time_scales, i, j, ret = -ENOMEM;
280 per_time_gains = kcalloc(gts->num_itime, sizeof(*per_time_gains), GFP_KERNEL);
284 per_time_scales = kcalloc(gts->num_itime, sizeof(*per_time_scales), GFP_KERNEL);
285 if (!per_time_scales)
288 for (i = 0; i < gts->num_itime; i++) {
289 per_time_scales[i] = kcalloc(gts->num_hwgain, 2 * sizeof(int),
291 if (!per_time_scales[i])
294 per_time_gains[i] = kcalloc(gts->num_hwgain, sizeof(int),
296 if (!per_time_gains[i]) {
297 kfree(per_time_scales[i]);
301 for (j = 0; j < gts->num_hwgain; j++)
302 per_time_gains[i][j] = gts->hwgain_table[j].gain *
303 gts->itime_table[i].mul;
306 ret = gain_to_scaletables(gts, per_time_gains, per_time_scales);
310 kfree(per_time_gains);
311 gts->per_time_avail_scale_tables = per_time_scales;
317 kfree(per_time_scales[i]);
318 kfree(per_time_gains[i]);
320 kfree(per_time_scales);
322 kfree(per_time_gains);
327 static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
332 for (i = 0; i < num_times; i++) {
333 int_micro_times[i * 2] = time_us[i] / 1000000;
334 int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
339 * iio_gts_build_avail_time_table - build table of available integration times
340 * @gts: Gain time scale descriptor
342 * Build the table which can represent the available times to be returned
343 * to users using the read_avail-callback.
345 * NOTE: Space allocated for the tables must be freed using
346 * iio_gts_purge_avail_time_table() when the tables are no longer needed.
348 * Return: 0 on success.
350 static int iio_gts_build_avail_time_table(struct iio_gts *gts)
352 int *times, i, j, idx = 0, *int_micro_times;
357 times = kcalloc(gts->num_itime, sizeof(int), GFP_KERNEL);
361 /* Sort times from all tables to one and remove duplicates */
362 for (i = gts->num_itime - 1; i >= 0; i--) {
363 int new = gts->itime_table[i].time_us;
365 if (times[idx] < new) {
370 for (j = 0; j <= idx; j++) {
371 if (times[j] > new) {
372 memmove(×[j + 1], ×[j],
373 (idx - j) * sizeof(int));
380 /* create a list of times formatted as list of IIO_VAL_INT_PLUS_MICRO */
381 int_micro_times = kcalloc(idx, sizeof(int) * 2, GFP_KERNEL);
382 if (int_micro_times) {
384 * This is just to survive a unlikely corner-case where times in
385 * the given time table were not unique. Else we could just
386 * trust the gts->num_itime.
388 gts->num_avail_time_tables = idx;
389 iio_gts_us_to_int_micro(times, int_micro_times, idx);
392 gts->avail_time_tables = int_micro_times;
395 if (!int_micro_times)
402 * iio_gts_purge_avail_time_table - free-up the available integration time table
403 * @gts: Gain time scale descriptor
405 * Free the space reserved by iio_gts_build_avail_time_table().
407 static void iio_gts_purge_avail_time_table(struct iio_gts *gts)
409 if (gts->num_avail_time_tables) {
410 kfree(gts->avail_time_tables);
411 gts->avail_time_tables = NULL;
412 gts->num_avail_time_tables = 0;
417 * iio_gts_build_avail_tables - create tables of available scales and int times
418 * @gts: Gain time scale descriptor
420 * Build the tables which can represent the available scales and available
421 * integration times. Availability tables are built based on the originally
422 * given gain and given time tables.
424 * When both time and gain tables are
425 * given this results:
426 * 1. A set of sorted tables representing available scales for each supported
428 * 2. A single sorted table listing all the unique scales that any combination
429 * of supported gains and times can provide.
430 * 3. A sorted table of supported integration times
432 * After these tables are built one can use the iio_gts_all_avail_scales(),
433 * iio_gts_avail_scales_for_time() and iio_gts_avail_times() helpers to
434 * implement the read_avail operations.
436 * NOTE: Space allocated for the tables must be freed using
437 * iio_gts_purge_avail_tables() when the tables are no longer needed.
439 * Return: 0 on success.
441 static int iio_gts_build_avail_tables(struct iio_gts *gts)
445 ret = iio_gts_build_avail_scale_table(gts);
449 ret = iio_gts_build_avail_time_table(gts);
451 iio_gts_purge_avail_scale_table(gts);
457 * iio_gts_purge_avail_tables - free-up the availability tables
458 * @gts: Gain time scale descriptor
460 * Free the space reserved by iio_gts_build_avail_tables(). Frees both the
461 * integration time and scale tables.
463 static void iio_gts_purge_avail_tables(struct iio_gts *gts)
465 iio_gts_purge_avail_time_table(gts);
466 iio_gts_purge_avail_scale_table(gts);
469 static void devm_iio_gts_avail_all_drop(void *res)
471 iio_gts_purge_avail_tables(res);
475 * devm_iio_gts_build_avail_tables - manged add availability tables
476 * @dev: Pointer to the device whose lifetime tables are bound
477 * @gts: Gain time scale descriptor
479 * Build the tables which can represent the available scales and available
480 * integration times. Availability tables are built based on the originally
481 * given gain and given time tables.
483 * When both time and gain tables are given this results:
484 * 1. A set of sorted tables representing available scales for each supported
486 * 2. A single sorted table listing all the unique scales that any combination
487 * of supported gains and times can provide.
488 * 3. A sorted table of supported integration times
490 * After these tables are built one can use the iio_gts_all_avail_scales(),
491 * iio_gts_avail_scales_for_time() and iio_gts_avail_times() helpers to
492 * implement the read_avail operations.
494 * The tables are automatically released upon device detach.
496 * Return: 0 on success.
498 static int devm_iio_gts_build_avail_tables(struct device *dev,
503 ret = iio_gts_build_avail_tables(gts);
507 return devm_add_action_or_reset(dev, devm_iio_gts_avail_all_drop, gts);
510 static int sanity_check_time(const struct iio_itime_sel_mul *t)
512 if (t->sel < 0 || t->time_us < 0 || t->mul <= 0)
518 static int sanity_check_gain(const struct iio_gain_sel_pair *g)
520 if (g->sel < 0 || g->gain <= 0)
526 static int iio_gts_sanity_check(struct iio_gts *gts)
530 if (!gts->num_hwgain && !gts->num_itime)
533 for (t = 0; t < gts->num_itime; t++) {
534 ret = sanity_check_time(>s->itime_table[t]);
539 for (g = 0; g < gts->num_hwgain; g++) {
540 ret = sanity_check_gain(>s->hwgain_table[g]);
545 for (g = 0; g < gts->num_hwgain; g++) {
546 for (t = 0; t < gts->num_itime; t++) {
549 gain = gts->hwgain_table[g].gain;
550 mul = gts->itime_table[t].mul;
552 if (check_mul_overflow(gain, mul, &res))
560 static int iio_init_iio_gts(int max_scale_int, int max_scale_nano,
561 const struct iio_gain_sel_pair *gain_tbl, int num_gain,
562 const struct iio_itime_sel_mul *tim_tbl, int num_times,
567 memset(gts, 0, sizeof(*gts));
569 ret = iio_gts_linearize(max_scale_int, max_scale_nano, NANO,
574 gts->hwgain_table = gain_tbl;
575 gts->num_hwgain = num_gain;
576 gts->itime_table = tim_tbl;
577 gts->num_itime = num_times;
579 return iio_gts_sanity_check(gts);
583 * devm_iio_init_iio_gts - Initialize the gain-time-scale helper
584 * @dev: Pointer to the device whose lifetime gts resources are
586 * @max_scale_int: integer part of the maximum scale value
587 * @max_scale_nano: fraction part of the maximum scale value
588 * @gain_tbl: table describing supported gains
589 * @num_gain: number of gains in the gain table
590 * @tim_tbl: table describing supported integration times. Provide
591 * the integration time table sorted so that the preferred
592 * integration time is in the first array index. The search
594 * iio_gts_find_time_and_gain_sel_for_scale() start search
595 * from first provided time.
596 * @num_times: number of times in the time table
597 * @gts: pointer to the helper struct
599 * Initialize the gain-time-scale helper for use. Note, gains, times, selectors
600 * and multipliers must be positive. Negative values are reserved for error
601 * checking. The total gain (maximum gain * maximum time multiplier) must not
602 * overflow int. The allocated resources will be released upon device detach.
604 * Return: 0 on success.
606 int devm_iio_init_iio_gts(struct device *dev, int max_scale_int, int max_scale_nano,
607 const struct iio_gain_sel_pair *gain_tbl, int num_gain,
608 const struct iio_itime_sel_mul *tim_tbl, int num_times,
613 ret = iio_init_iio_gts(max_scale_int, max_scale_nano, gain_tbl,
614 num_gain, tim_tbl, num_times, gts);
618 return devm_iio_gts_build_avail_tables(dev, gts);
620 EXPORT_SYMBOL_NS_GPL(devm_iio_init_iio_gts, IIO_GTS_HELPER);
623 * iio_gts_all_avail_scales - helper for listing all available scales
624 * @gts: Gain time scale descriptor
625 * @vals: Returned array of supported scales
626 * @type: Type of returned scale values
627 * @length: Amount of returned values in array
629 * Return: a value suitable to be returned from read_avail or a negative error.
631 int iio_gts_all_avail_scales(struct iio_gts *gts, const int **vals, int *type,
634 if (!gts->num_avail_all_scales)
637 *vals = gts->avail_all_scales_table;
638 *type = IIO_VAL_INT_PLUS_NANO;
639 *length = gts->num_avail_all_scales * 2;
641 return IIO_AVAIL_LIST;
643 EXPORT_SYMBOL_NS_GPL(iio_gts_all_avail_scales, IIO_GTS_HELPER);
646 * iio_gts_avail_scales_for_time - list scales for integration time
647 * @gts: Gain time scale descriptor
648 * @time: Integration time for which the scales are listed
649 * @vals: Returned array of supported scales
650 * @type: Type of returned scale values
651 * @length: Amount of returned values in array
653 * Drivers which do not allow scale setting to change integration time can
654 * use this helper to list only the scales which are valid for given integration
657 * Return: a value suitable to be returned from read_avail or a negative error.
659 int iio_gts_avail_scales_for_time(struct iio_gts *gts, int time,
660 const int **vals, int *type, int *length)
664 for (i = 0; i < gts->num_itime; i++)
665 if (gts->itime_table[i].time_us == time)
668 if (i == gts->num_itime)
671 *vals = gts->per_time_avail_scale_tables[i];
672 *type = IIO_VAL_INT_PLUS_NANO;
673 *length = gts->num_hwgain * 2;
675 return IIO_AVAIL_LIST;
677 EXPORT_SYMBOL_NS_GPL(iio_gts_avail_scales_for_time, IIO_GTS_HELPER);
680 * iio_gts_avail_times - helper for listing available integration times
681 * @gts: Gain time scale descriptor
682 * @vals: Returned array of supported times
683 * @type: Type of returned scale values
684 * @length: Amount of returned values in array
686 * Return: a value suitable to be returned from read_avail or a negative error.
688 int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type,
691 if (!gts->num_avail_time_tables)
694 *vals = gts->avail_time_tables;
695 *type = IIO_VAL_INT_PLUS_MICRO;
696 *length = gts->num_avail_time_tables * 2;
698 return IIO_AVAIL_LIST;
700 EXPORT_SYMBOL_NS_GPL(iio_gts_avail_times, IIO_GTS_HELPER);
703 * iio_gts_find_sel_by_gain - find selector corresponding to a HW-gain
704 * @gts: Gain time scale descriptor
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)
714 for (i = 0; i < gts->num_hwgain; i++)
715 if (gts->hwgain_table[i].gain == gain)
716 return gts->hwgain_table[i].sel;
720 EXPORT_SYMBOL_NS_GPL(iio_gts_find_sel_by_gain, IIO_GTS_HELPER);
723 * iio_gts_find_gain_by_sel - find HW-gain corresponding to a selector
724 * @gts: Gain time scale descriptor
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
730 int iio_gts_find_gain_by_sel(struct iio_gts *gts, int sel)
734 for (i = 0; i < gts->num_hwgain; i++)
735 if (gts->hwgain_table[i].sel == sel)
736 return gts->hwgain_table[i].gain;
740 EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_by_sel, IIO_GTS_HELPER);
743 * iio_gts_get_min_gain - find smallest valid HW-gain
744 * @gts: Gain time scale descriptor
746 * Return: The smallest HW-gain -EINVAL if no HW-gains were in the tables.
748 int iio_gts_get_min_gain(struct iio_gts *gts)
750 int i, min = -EINVAL;
752 for (i = 0; i < gts->num_hwgain; i++) {
753 int gain = gts->hwgain_table[i].gain;
758 min = min(min, gain);
763 EXPORT_SYMBOL_NS_GPL(iio_gts_get_min_gain, IIO_GTS_HELPER);
766 * iio_find_closest_gain_low - Find the closest lower matching gain
767 * @gts: Gain time scale descriptor
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
776 * avoiding saturation)
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)
788 for (i = 0; i < gts->num_hwgain; i++) {
789 if (gain == gts->hwgain_table[i].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
809 * reference. So, unless we return a failure below we
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;
824 EXPORT_SYMBOL_NS_GPL(iio_find_closest_gain_low, IIO_GTS_HELPER);
826 static int iio_gts_get_int_time_gain_multiplier_by_sel(struct iio_gts *gts,
829 const struct iio_itime_sel_mul *time;
831 time = iio_gts_find_itime_by_sel(gts, sel);
839 * iio_gts_find_gain_for_scale_using_time - Find gain by time and scale
840 * @gts: Gain time scale descriptor
841 * @time_sel: Integration time selector corresponding to the time gain is
843 * @scale_int: Integral part of the scale (typically val1)
844 * @scale_nano: Fractional part of the scale (nano or ppb)
845 * @gain: Pointer to value where gain is stored.
847 * In some cases the light sensors may want to find a gain setting which
848 * corresponds given scale and integration time. Sensors which fill the
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
854 static int iio_gts_find_gain_for_scale_using_time(struct iio_gts *gts, int time_sel,
855 int scale_int, int scale_nano,
861 ret = iio_gts_linearize(scale_int, scale_nano, NANO, &scale_linear);
865 ret = iio_gts_get_int_time_gain_multiplier_by_sel(gts, time_sel);
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.
883 * @gts: Gain time scale descriptor
884 * @time_sel: Integration time selector corresponding to the time gain is
886 * @scale_int: Integral part of the scale (typically val1)
887 * @scale_nano: Fractional part of the scale (nano or ppb)
888 * @gain_sel: Pointer to value where gain selector is stored.
890 * See iio_gts_find_gain_for_scale_using_time() for more information
892 int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel,
893 int scale_int, int scale_nano,
898 ret = iio_gts_find_gain_for_scale_using_time(gts, time_sel, scale_int,
903 ret = iio_gts_find_sel_by_gain(gts, gain);
911 EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_sel_for_scale_using_time, IIO_GTS_HELPER);
913 static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
915 const struct iio_itime_sel_mul *itime;
917 if (!iio_gts_valid_gain(gts, gain))
923 itime = iio_gts_find_itime_by_time(gts, time);
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);
940 tmp = gts->max_scale;
942 do_div(tmp, total_gain);
950 * iio_gts_get_scale - get scale based on integration time and HW-gain
951 * @gts: Gain time scale descriptor
952 * @gain: HW-gain for which the scale is computed
953 * @time: Integration time for which the scale is computed
954 * @scale_int: Integral part of the scale (typically val1)
955 * @scale_nano: Fractional part of the scale (nano or ppb)
957 * Compute scale matching the integration time and HW-gain given as parameter.
959 * Return: 0 on success.
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);
971 return iio_gts_delinearize(lin_scale, NANO, scale_int, scale_nano);
973 EXPORT_SYMBOL_NS_GPL(iio_gts_get_scale, IIO_GTS_HELPER);
976 * iio_gts_find_new_gain_sel_by_old_gain_time - compensate for time change
977 * @gts: Gain time scale descriptor
978 * @old_gain: Previously set gain
979 * @old_time_sel: Selector corresponding previously set time
980 * @new_time_sel: Selector corresponding new time to be set
981 * @new_gain: Pointer to value where new gain is to be written
983 * We may want to mitigate the scale change caused by setting a new integration
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
988 * non-zero value is returned, the @new_gain will be set to a negative or
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
992 * successful. 1) This new value cannot be supported by the hardware. 2) The new
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
995 * may not be supported by the hardware.
997 int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts,
998 int old_gain, int old_time_sel,
999 int new_time_sel, int *new_gain)
1001 const struct iio_itime_sel_mul *itime_old, *itime_new;
1007 itime_old = iio_gts_find_itime_by_sel(gts, old_time_sel);
1011 itime_new = iio_gts_find_itime_by_sel(gts, new_time_sel);
1015 ret = iio_gts_get_scale_linear(gts, old_gain, itime_old->time_us,
1020 ret = gain_get_scale_fraction(gts->max_scale, scale, itime_new->mul,
1025 if (!iio_gts_valid_gain(gts, *new_gain))
1030 EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_sel_by_old_gain_time, IIO_GTS_HELPER);
1033 * iio_gts_find_new_gain_by_old_gain_time - compensate for time change
1034 * @gts: Gain time scale descriptor
1035 * @old_gain: Previously set gain
1036 * @old_time: Selector corresponding previously set time
1037 * @new_time: Selector corresponding new time to be set
1038 * @new_gain: Pointer to value where new gain is to be written
1040 * We may want to mitigate the scale change caused by setting a new integration
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
1045 * non-zero value is returned, the @new_gain will be set to a negative or
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
1049 * successful. 1) This new value cannot be supported by the hardware. 2) The new
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
1052 * may not be supported by the hardware.
1054 int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain,
1055 int old_time, int new_time,
1058 const struct iio_itime_sel_mul *itime_new;
1064 itime_new = iio_gts_find_itime_by_time(gts, new_time);
1068 ret = iio_gts_get_scale_linear(gts, old_gain, old_time, &scale);
1072 ret = gain_get_scale_fraction(gts->max_scale, scale, itime_new->mul,
1077 if (!iio_gts_valid_gain(gts, *new_gain))
1082 EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_old_gain_time, IIO_GTS_HELPER);
1084 MODULE_LICENSE("GPL");
1085 MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
1086 MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers");