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)
39 if (scale > full || !scale)
42 if (U64_MAX - full < scale) {
43 /* Risk of overflow */
44 if (full - scale < scale)
51 while (full > scale * (u64)tmp)
58 * gain_get_scale_fraction - get the gain or time based on scale and known one
60 * @max: Maximum linearized scale. As an example, when scale is created
61 * in magnitude of NANOs and max scale is 64.1 - The linearized
62 * scale is 64 100 000 000.
63 * @scale: Linearized scale to compute the gain/time for.
64 * @known: Either integration time or gain depending on which one is known
65 * @unknown: Pointer to variable where the computed gain/time is stored
67 * Internal helper for computing unknown fraction of total gain.
68 * Compute either gain or time based on scale and either the gain or time
69 * depending on which one is known.
71 * Return: 0 on success.
73 static int gain_get_scale_fraction(const u64 max, u64 scale, int known,
78 tot_gain = iio_gts_get_gain(max, scale);
82 *unknown = tot_gain / known;
84 /* We require total gain to be exact multiple of known * unknown */
85 if (!*unknown || *unknown * known != tot_gain)
91 static int iio_gts_delinearize(u64 lin_scale, unsigned long scaler,
92 int *scale_whole, int *scale_nano)
102 frac = do_div(lin_scale, scaler);
104 *scale_whole = lin_scale;
105 *scale_nano = frac * (NANO / scaler);
110 static int iio_gts_linearize(int scale_whole, int scale_nano,
111 unsigned long scaler, u64 *lin_scale)
114 * Expect scale to be (mostly) NANO or MICRO. Divide divider instead of
115 * multiplication followed by division to avoid overflow.
117 if (scaler > NANO || !scaler)
120 *lin_scale = (u64)scale_whole * (u64)scaler +
121 (u64)(scale_nano / (NANO / scaler));
127 * iio_gts_total_gain_to_scale - convert gain to scale
128 * @gts: Gain time scale descriptor
129 * @total_gain: the gain to be converted
130 * @scale_int: Pointer to integral part of the scale (typically val1)
131 * @scale_nano: Pointer to fractional part of the scale (nano or ppb)
133 * Convert the total gain value to scale. NOTE: This does not separate gain
134 * generated by HW-gain or integration time. It is up to caller to decide what
135 * part of the total gain is due to integration time and what due to HW-gain.
137 * Return: 0 on success. Negative errno on failure.
139 int iio_gts_total_gain_to_scale(struct iio_gts *gts, int total_gain,
140 int *scale_int, int *scale_nano)
144 tmp = gts->max_scale;
146 do_div(tmp, total_gain);
148 return iio_gts_delinearize(tmp, NANO, scale_int, scale_nano);
150 EXPORT_SYMBOL_NS_GPL(iio_gts_total_gain_to_scale, IIO_GTS_HELPER);
153 * iio_gts_purge_avail_scale_table - free-up the available scale tables
154 * @gts: Gain time scale descriptor
156 * Free the space reserved by iio_gts_build_avail_scale_table().
158 static void iio_gts_purge_avail_scale_table(struct iio_gts *gts)
162 if (gts->per_time_avail_scale_tables) {
163 for (i = 0; i < gts->num_itime; i++)
164 kfree(gts->per_time_avail_scale_tables[i]);
166 kfree(gts->per_time_avail_scale_tables);
167 gts->per_time_avail_scale_tables = NULL;
170 kfree(gts->avail_all_scales_table);
171 gts->avail_all_scales_table = NULL;
173 gts->num_avail_all_scales = 0;
176 static int iio_gts_gain_cmp(const void *a, const void *b)
178 return *(int *)a - *(int *)b;
181 static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
183 int ret, i, j, new_idx, time_idx;
187 for (i = 0; i < gts->num_itime; i++) {
189 * Sort the tables for nice output and for easier finding of
192 sort(gains[i], gts->num_hwgain, sizeof(int), iio_gts_gain_cmp,
195 /* Convert gains to scales */
196 for (j = 0; j < gts->num_hwgain; j++) {
197 ret = iio_gts_total_gain_to_scale(gts, gains[i][j],
199 &scales[i][2 * j + 1]);
205 gain_bytes = array_size(gts->num_hwgain, sizeof(int));
206 all_gains = kcalloc(gts->num_itime, gain_bytes, GFP_KERNEL);
211 * We assume all the gains for same integration time were unique.
212 * It is likely the first time table had greatest time multiplier as
213 * the times are in the order of preference and greater times are
214 * usually preferred. Hence we start from the last table which is likely
215 * to have the smallest total gains.
217 time_idx = gts->num_itime - 1;
218 memcpy(all_gains, gains[time_idx], gain_bytes);
219 new_idx = gts->num_hwgain;
222 for (j = 0; j < gts->num_hwgain; j++) {
223 int candidate = gains[time_idx][j];
226 if (candidate > all_gains[new_idx - 1]) {
227 all_gains[new_idx] = candidate;
232 for (chk = 0; chk < new_idx; chk++)
233 if (candidate <= all_gains[chk])
236 if (candidate == all_gains[chk])
239 memmove(&all_gains[chk + 1], &all_gains[chk],
240 (new_idx - chk) * sizeof(int));
241 all_gains[chk] = candidate;
246 gts->avail_all_scales_table = kcalloc(new_idx, 2 * sizeof(int),
248 if (!gts->avail_all_scales_table) {
252 gts->num_avail_all_scales = new_idx;
254 for (i = 0; i < gts->num_avail_all_scales; i++) {
255 ret = iio_gts_total_gain_to_scale(gts, all_gains[i],
256 >s->avail_all_scales_table[i * 2],
257 >s->avail_all_scales_table[i * 2 + 1]);
260 kfree(gts->avail_all_scales_table);
261 gts->num_avail_all_scales = 0;
273 * iio_gts_build_avail_scale_table - create tables of available scales
274 * @gts: Gain time scale descriptor
276 * Build the tables which can represent the available scales based on the
277 * originally given gain and time tables. When both time and gain tables are
278 * given this results:
279 * 1. A set of tables representing available scales for each supported
281 * 2. A single table listing all the unique scales that any combination of
282 * supported gains and times can provide.
284 * NOTE: Space allocated for the tables must be freed using
285 * iio_gts_purge_avail_scale_table() when the tables are no longer needed.
287 * Return: 0 on success.
289 static int iio_gts_build_avail_scale_table(struct iio_gts *gts)
291 int **per_time_gains, **per_time_scales, i, j, ret = -ENOMEM;
293 per_time_gains = kcalloc(gts->num_itime, sizeof(*per_time_gains), GFP_KERNEL);
297 per_time_scales = kcalloc(gts->num_itime, sizeof(*per_time_scales), GFP_KERNEL);
298 if (!per_time_scales)
301 for (i = 0; i < gts->num_itime; i++) {
302 per_time_scales[i] = kcalloc(gts->num_hwgain, 2 * sizeof(int),
304 if (!per_time_scales[i])
307 per_time_gains[i] = kcalloc(gts->num_hwgain, sizeof(int),
309 if (!per_time_gains[i]) {
310 kfree(per_time_scales[i]);
314 for (j = 0; j < gts->num_hwgain; j++)
315 per_time_gains[i][j] = gts->hwgain_table[j].gain *
316 gts->itime_table[i].mul;
319 ret = gain_to_scaletables(gts, per_time_gains, per_time_scales);
323 kfree(per_time_gains);
324 gts->per_time_avail_scale_tables = per_time_scales;
330 kfree(per_time_scales[i]);
331 kfree(per_time_gains[i]);
333 kfree(per_time_scales);
335 kfree(per_time_gains);
340 static void iio_gts_us_to_int_micro(int *time_us, int *int_micro_times,
345 for (i = 0; i < num_times; i++) {
346 int_micro_times[i * 2] = time_us[i] / 1000000;
347 int_micro_times[i * 2 + 1] = time_us[i] % 1000000;
352 * iio_gts_build_avail_time_table - build table of available integration times
353 * @gts: Gain time scale descriptor
355 * Build the table which can represent the available times to be returned
356 * to users using the read_avail-callback.
358 * NOTE: Space allocated for the tables must be freed using
359 * iio_gts_purge_avail_time_table() when the tables are no longer needed.
361 * Return: 0 on success.
363 static int iio_gts_build_avail_time_table(struct iio_gts *gts)
365 int *times, i, j, idx = 0, *int_micro_times;
370 times = kcalloc(gts->num_itime, sizeof(int), GFP_KERNEL);
374 /* Sort times from all tables to one and remove duplicates */
375 for (i = gts->num_itime - 1; i >= 0; i--) {
376 int new = gts->itime_table[i].time_us;
378 if (times[idx] < new) {
383 for (j = 0; j <= idx; j++) {
384 if (times[j] > new) {
385 memmove(×[j + 1], ×[j],
386 (idx - j) * sizeof(int));
393 /* create a list of times formatted as list of IIO_VAL_INT_PLUS_MICRO */
394 int_micro_times = kcalloc(idx, sizeof(int) * 2, GFP_KERNEL);
395 if (int_micro_times) {
397 * This is just to survive a unlikely corner-case where times in
398 * the given time table were not unique. Else we could just
399 * trust the gts->num_itime.
401 gts->num_avail_time_tables = idx;
402 iio_gts_us_to_int_micro(times, int_micro_times, idx);
405 gts->avail_time_tables = int_micro_times;
408 if (!int_micro_times)
415 * iio_gts_purge_avail_time_table - free-up the available integration time table
416 * @gts: Gain time scale descriptor
418 * Free the space reserved by iio_gts_build_avail_time_table().
420 static void iio_gts_purge_avail_time_table(struct iio_gts *gts)
422 if (gts->num_avail_time_tables) {
423 kfree(gts->avail_time_tables);
424 gts->avail_time_tables = NULL;
425 gts->num_avail_time_tables = 0;
430 * iio_gts_build_avail_tables - create tables of available scales and int times
431 * @gts: Gain time scale descriptor
433 * Build the tables which can represent the available scales and available
434 * integration times. Availability tables are built based on the originally
435 * given gain and given time tables.
437 * When both time and gain tables are
438 * given this results:
439 * 1. A set of sorted tables representing available scales for each supported
441 * 2. A single sorted table listing all the unique scales that any combination
442 * of supported gains and times can provide.
443 * 3. A sorted table of supported integration times
445 * After these tables are built one can use the iio_gts_all_avail_scales(),
446 * iio_gts_avail_scales_for_time() and iio_gts_avail_times() helpers to
447 * implement the read_avail operations.
449 * NOTE: Space allocated for the tables must be freed using
450 * iio_gts_purge_avail_tables() when the tables are no longer needed.
452 * Return: 0 on success.
454 static int iio_gts_build_avail_tables(struct iio_gts *gts)
458 ret = iio_gts_build_avail_scale_table(gts);
462 ret = iio_gts_build_avail_time_table(gts);
464 iio_gts_purge_avail_scale_table(gts);
470 * iio_gts_purge_avail_tables - free-up the availability tables
471 * @gts: Gain time scale descriptor
473 * Free the space reserved by iio_gts_build_avail_tables(). Frees both the
474 * integration time and scale tables.
476 static void iio_gts_purge_avail_tables(struct iio_gts *gts)
478 iio_gts_purge_avail_time_table(gts);
479 iio_gts_purge_avail_scale_table(gts);
482 static void devm_iio_gts_avail_all_drop(void *res)
484 iio_gts_purge_avail_tables(res);
488 * devm_iio_gts_build_avail_tables - manged add availability tables
489 * @dev: Pointer to the device whose lifetime tables are bound
490 * @gts: Gain time scale descriptor
492 * Build the tables which can represent the available scales and available
493 * integration times. Availability tables are built based on the originally
494 * given gain and given time tables.
496 * When both time and gain tables are given this results:
497 * 1. A set of sorted tables representing available scales for each supported
499 * 2. A single sorted table listing all the unique scales that any combination
500 * of supported gains and times can provide.
501 * 3. A sorted table of supported integration times
503 * After these tables are built one can use the iio_gts_all_avail_scales(),
504 * iio_gts_avail_scales_for_time() and iio_gts_avail_times() helpers to
505 * implement the read_avail operations.
507 * The tables are automatically released upon device detach.
509 * Return: 0 on success.
511 static int devm_iio_gts_build_avail_tables(struct device *dev,
516 ret = iio_gts_build_avail_tables(gts);
520 return devm_add_action_or_reset(dev, devm_iio_gts_avail_all_drop, gts);
523 static int sanity_check_time(const struct iio_itime_sel_mul *t)
525 if (t->sel < 0 || t->time_us < 0 || t->mul <= 0)
531 static int sanity_check_gain(const struct iio_gain_sel_pair *g)
533 if (g->sel < 0 || g->gain <= 0)
539 static int iio_gts_sanity_check(struct iio_gts *gts)
543 if (!gts->num_hwgain && !gts->num_itime)
546 for (t = 0; t < gts->num_itime; t++) {
547 ret = sanity_check_time(>s->itime_table[t]);
552 for (g = 0; g < gts->num_hwgain; g++) {
553 ret = sanity_check_gain(>s->hwgain_table[g]);
558 for (g = 0; g < gts->num_hwgain; g++) {
559 for (t = 0; t < gts->num_itime; t++) {
562 gain = gts->hwgain_table[g].gain;
563 mul = gts->itime_table[t].mul;
565 if (check_mul_overflow(gain, mul, &res))
573 static int iio_init_iio_gts(int max_scale_int, int max_scale_nano,
574 const struct iio_gain_sel_pair *gain_tbl, int num_gain,
575 const struct iio_itime_sel_mul *tim_tbl, int num_times,
580 memset(gts, 0, sizeof(*gts));
582 ret = iio_gts_linearize(max_scale_int, max_scale_nano, NANO,
587 gts->hwgain_table = gain_tbl;
588 gts->num_hwgain = num_gain;
589 gts->itime_table = tim_tbl;
590 gts->num_itime = num_times;
592 return iio_gts_sanity_check(gts);
596 * devm_iio_init_iio_gts - Initialize the gain-time-scale helper
597 * @dev: Pointer to the device whose lifetime gts resources are
599 * @max_scale_int: integer part of the maximum scale value
600 * @max_scale_nano: fraction part of the maximum scale value
601 * @gain_tbl: table describing supported gains
602 * @num_gain: number of gains in the gain table
603 * @tim_tbl: table describing supported integration times. Provide
604 * the integration time table sorted so that the preferred
605 * integration time is in the first array index. The search
607 * iio_gts_find_time_and_gain_sel_for_scale() start search
608 * from first provided time.
609 * @num_times: number of times in the time table
610 * @gts: pointer to the helper struct
612 * Initialize the gain-time-scale helper for use. Note, gains, times, selectors
613 * and multipliers must be positive. Negative values are reserved for error
614 * checking. The total gain (maximum gain * maximum time multiplier) must not
615 * overflow int. The allocated resources will be released upon device detach.
617 * Return: 0 on success.
619 int devm_iio_init_iio_gts(struct device *dev, int max_scale_int, int max_scale_nano,
620 const struct iio_gain_sel_pair *gain_tbl, int num_gain,
621 const struct iio_itime_sel_mul *tim_tbl, int num_times,
626 ret = iio_init_iio_gts(max_scale_int, max_scale_nano, gain_tbl,
627 num_gain, tim_tbl, num_times, gts);
631 return devm_iio_gts_build_avail_tables(dev, gts);
633 EXPORT_SYMBOL_NS_GPL(devm_iio_init_iio_gts, IIO_GTS_HELPER);
636 * iio_gts_all_avail_scales - helper for listing all available scales
637 * @gts: Gain time scale descriptor
638 * @vals: Returned array of supported scales
639 * @type: Type of returned scale values
640 * @length: Amount of returned values in array
642 * Return: a value suitable to be returned from read_avail or a negative error.
644 int iio_gts_all_avail_scales(struct iio_gts *gts, const int **vals, int *type,
647 if (!gts->num_avail_all_scales)
650 *vals = gts->avail_all_scales_table;
651 *type = IIO_VAL_INT_PLUS_NANO;
652 *length = gts->num_avail_all_scales * 2;
654 return IIO_AVAIL_LIST;
656 EXPORT_SYMBOL_NS_GPL(iio_gts_all_avail_scales, IIO_GTS_HELPER);
659 * iio_gts_avail_scales_for_time - list scales for integration time
660 * @gts: Gain time scale descriptor
661 * @time: Integration time for which the scales are listed
662 * @vals: Returned array of supported scales
663 * @type: Type of returned scale values
664 * @length: Amount of returned values in array
666 * Drivers which do not allow scale setting to change integration time can
667 * use this helper to list only the scales which are valid for given integration
670 * Return: a value suitable to be returned from read_avail or a negative error.
672 int iio_gts_avail_scales_for_time(struct iio_gts *gts, int time,
673 const int **vals, int *type, int *length)
677 for (i = 0; i < gts->num_itime; i++)
678 if (gts->itime_table[i].time_us == time)
681 if (i == gts->num_itime)
684 *vals = gts->per_time_avail_scale_tables[i];
685 *type = IIO_VAL_INT_PLUS_NANO;
686 *length = gts->num_hwgain * 2;
688 return IIO_AVAIL_LIST;
690 EXPORT_SYMBOL_NS_GPL(iio_gts_avail_scales_for_time, IIO_GTS_HELPER);
693 * iio_gts_avail_times - helper for listing available integration times
694 * @gts: Gain time scale descriptor
695 * @vals: Returned array of supported times
696 * @type: Type of returned scale values
697 * @length: Amount of returned values in array
699 * Return: a value suitable to be returned from read_avail or a negative error.
701 int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type,
704 if (!gts->num_avail_time_tables)
707 *vals = gts->avail_time_tables;
708 *type = IIO_VAL_INT_PLUS_MICRO;
709 *length = gts->num_avail_time_tables * 2;
711 return IIO_AVAIL_LIST;
713 EXPORT_SYMBOL_NS_GPL(iio_gts_avail_times, IIO_GTS_HELPER);
716 * iio_gts_find_sel_by_gain - find selector corresponding to a HW-gain
717 * @gts: Gain time scale descriptor
718 * @gain: HW-gain for which matching selector is searched for
720 * Return: a selector matching given HW-gain or -EINVAL if selector was
723 int iio_gts_find_sel_by_gain(struct iio_gts *gts, int gain)
727 for (i = 0; i < gts->num_hwgain; i++)
728 if (gts->hwgain_table[i].gain == gain)
729 return gts->hwgain_table[i].sel;
733 EXPORT_SYMBOL_NS_GPL(iio_gts_find_sel_by_gain, IIO_GTS_HELPER);
736 * iio_gts_find_gain_by_sel - find HW-gain corresponding to a selector
737 * @gts: Gain time scale descriptor
738 * @sel: selector for which matching HW-gain is searched for
740 * Return: a HW-gain matching given selector or -EINVAL if HW-gain was not
743 int iio_gts_find_gain_by_sel(struct iio_gts *gts, int sel)
747 for (i = 0; i < gts->num_hwgain; i++)
748 if (gts->hwgain_table[i].sel == sel)
749 return gts->hwgain_table[i].gain;
753 EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_by_sel, IIO_GTS_HELPER);
756 * iio_gts_get_min_gain - find smallest valid HW-gain
757 * @gts: Gain time scale descriptor
759 * Return: The smallest HW-gain -EINVAL if no HW-gains were in the tables.
761 int iio_gts_get_min_gain(struct iio_gts *gts)
763 int i, min = -EINVAL;
765 for (i = 0; i < gts->num_hwgain; i++) {
766 int gain = gts->hwgain_table[i].gain;
771 min = min(min, gain);
776 EXPORT_SYMBOL_NS_GPL(iio_gts_get_min_gain, IIO_GTS_HELPER);
779 * iio_find_closest_gain_low - Find the closest lower matching gain
780 * @gts: Gain time scale descriptor
781 * @gain: HW-gain for which the closest match is searched
782 * @in_range: indicate if the @gain was actually in the range of
785 * Search for closest supported gain that is lower than or equal to the
786 * gain given as a parameter. This is usable for drivers which do not require
787 * user to request exact matching gain but rather for rounding to a supported
788 * gain value which is equal or lower (setting lower gain is typical for
789 * avoiding saturation)
791 * Return: The closest matching supported gain or -EINVAL if @gain
792 * was smaller than the smallest supported gain.
794 int iio_find_closest_gain_low(struct iio_gts *gts, int gain, bool *in_range)
801 for (i = 0; i < gts->num_hwgain; i++) {
802 if (gain == gts->hwgain_table[i].gain) {
807 if (gain > gts->hwgain_table[i].gain) {
809 diff = gain - gts->hwgain_table[i].gain;
812 int tmp = gain - gts->hwgain_table[i].gain;
821 * We found valid HW-gain which is greater than
822 * reference. So, unless we return a failure below we
823 * will have found an in-range gain
828 /* The requested gain was smaller than anything we support */
835 return gts->hwgain_table[best].gain;
837 EXPORT_SYMBOL_NS_GPL(iio_find_closest_gain_low, IIO_GTS_HELPER);
839 static int iio_gts_get_int_time_gain_multiplier_by_sel(struct iio_gts *gts,
842 const struct iio_itime_sel_mul *time;
844 time = iio_gts_find_itime_by_sel(gts, sel);
852 * iio_gts_find_gain_for_scale_using_time - Find gain by time and scale
853 * @gts: Gain time scale descriptor
854 * @time_sel: Integration time selector corresponding to the time gain is
856 * @scale_int: Integral part of the scale (typically val1)
857 * @scale_nano: Fractional part of the scale (nano or ppb)
858 * @gain: Pointer to value where gain is stored.
860 * In some cases the light sensors may want to find a gain setting which
861 * corresponds given scale and integration time. Sensors which fill the
862 * gain and time tables may use this helper to retrieve the gain.
864 * Return: 0 on success. -EINVAL if gain matching the parameters is not
867 static int iio_gts_find_gain_for_scale_using_time(struct iio_gts *gts, int time_sel,
868 int scale_int, int scale_nano,
874 ret = iio_gts_linearize(scale_int, scale_nano, NANO, &scale_linear);
878 ret = iio_gts_get_int_time_gain_multiplier_by_sel(gts, time_sel);
884 ret = gain_get_scale_fraction(gts->max_scale, scale_linear, mul, gain);
888 if (!iio_gts_valid_gain(gts, *gain))
895 * iio_gts_find_gain_sel_for_scale_using_time - Fetch gain selector.
896 * @gts: Gain time scale descriptor
897 * @time_sel: Integration time selector corresponding to the time gain is
899 * @scale_int: Integral part of the scale (typically val1)
900 * @scale_nano: Fractional part of the scale (nano or ppb)
901 * @gain_sel: Pointer to value where gain selector is stored.
903 * See iio_gts_find_gain_for_scale_using_time() for more information
905 int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel,
906 int scale_int, int scale_nano,
911 ret = iio_gts_find_gain_for_scale_using_time(gts, time_sel, scale_int,
916 ret = iio_gts_find_sel_by_gain(gts, gain);
924 EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_sel_for_scale_using_time, IIO_GTS_HELPER);
926 static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
928 const struct iio_itime_sel_mul *itime;
930 if (!iio_gts_valid_gain(gts, gain))
936 itime = iio_gts_find_itime_by_time(gts, time);
940 return gain * itime->mul;
943 static int iio_gts_get_scale_linear(struct iio_gts *gts, int gain, int time,
949 total_gain = iio_gts_get_total_gain(gts, gain, time);
953 tmp = gts->max_scale;
955 do_div(tmp, total_gain);
963 * iio_gts_get_scale - get scale based on integration time and HW-gain
964 * @gts: Gain time scale descriptor
965 * @gain: HW-gain for which the scale is computed
966 * @time: Integration time for which the scale is computed
967 * @scale_int: Integral part of the scale (typically val1)
968 * @scale_nano: Fractional part of the scale (nano or ppb)
970 * Compute scale matching the integration time and HW-gain given as parameter.
972 * Return: 0 on success.
974 int iio_gts_get_scale(struct iio_gts *gts, int gain, int time, int *scale_int,
980 ret = iio_gts_get_scale_linear(gts, gain, time, &lin_scale);
984 return iio_gts_delinearize(lin_scale, NANO, scale_int, scale_nano);
986 EXPORT_SYMBOL_NS_GPL(iio_gts_get_scale, IIO_GTS_HELPER);
989 * iio_gts_find_new_gain_sel_by_old_gain_time - compensate for time change
990 * @gts: Gain time scale descriptor
991 * @old_gain: Previously set gain
992 * @old_time_sel: Selector corresponding previously set time
993 * @new_time_sel: Selector corresponding new time to be set
994 * @new_gain: Pointer to value where new gain is to be written
996 * We may want to mitigate the scale change caused by setting a new integration
997 * time (for a light sensor) by also updating the (HW)gain. This helper computes
998 * new gain value to maintain the scale with new integration time.
1000 * Return: 0 if an exactly matching supported new gain was found. When a
1001 * non-zero value is returned, the @new_gain will be set to a negative or
1002 * positive value. The negative value means that no gain could be computed.
1003 * Positive value will be the "best possible new gain there could be". There
1004 * can be two reasons why finding the "best possible" new gain is not deemed
1005 * successful. 1) This new value cannot be supported by the hardware. 2) The new
1006 * gain required to maintain the scale would not be an integer. In this case,
1007 * the "best possible" new gain will be a floored optimal gain, which may or
1008 * may not be supported by the hardware.
1010 int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts,
1011 int old_gain, int old_time_sel,
1012 int new_time_sel, int *new_gain)
1014 const struct iio_itime_sel_mul *itime_old, *itime_new;
1020 itime_old = iio_gts_find_itime_by_sel(gts, old_time_sel);
1024 itime_new = iio_gts_find_itime_by_sel(gts, new_time_sel);
1028 ret = iio_gts_get_scale_linear(gts, old_gain, itime_old->time_us,
1033 ret = gain_get_scale_fraction(gts->max_scale, scale, itime_new->mul,
1038 if (!iio_gts_valid_gain(gts, *new_gain))
1043 EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_sel_by_old_gain_time, IIO_GTS_HELPER);
1046 * iio_gts_find_new_gain_by_old_gain_time - compensate for time change
1047 * @gts: Gain time scale descriptor
1048 * @old_gain: Previously set gain
1049 * @old_time: Selector corresponding previously set time
1050 * @new_time: Selector corresponding new time to be set
1051 * @new_gain: Pointer to value where new gain is to be written
1053 * We may want to mitigate the scale change caused by setting a new integration
1054 * time (for a light sensor) by also updating the (HW)gain. This helper computes
1055 * new gain value to maintain the scale with new integration time.
1057 * Return: 0 if an exactly matching supported new gain was found. When a
1058 * non-zero value is returned, the @new_gain will be set to a negative or
1059 * positive value. The negative value means that no gain could be computed.
1060 * Positive value will be the "best possible new gain there could be". There
1061 * can be two reasons why finding the "best possible" new gain is not deemed
1062 * successful. 1) This new value cannot be supported by the hardware. 2) The new
1063 * gain required to maintain the scale would not be an integer. In this case,
1064 * the "best possible" new gain will be a floored optimal gain, which may or
1065 * may not be supported by the hardware.
1067 int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain,
1068 int old_time, int new_time,
1071 const struct iio_itime_sel_mul *itime_new;
1077 itime_new = iio_gts_find_itime_by_time(gts, new_time);
1081 ret = iio_gts_get_scale_linear(gts, old_gain, old_time, &scale);
1085 ret = gain_get_scale_fraction(gts->max_scale, scale, itime_new->mul,
1090 if (!iio_gts_valid_gain(gts, *new_gain))
1095 EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_old_gain_time, IIO_GTS_HELPER);
1097 MODULE_LICENSE("GPL");
1098 MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
1099 MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers");