Skip to contentSkip to conversation

iio: light: fix scale in veml3235 and add helpers to iio-gts

PATCH Applied Javier Carrasco 2 participants 5 messages Dec 30, 2024 – Jan 4, 2025

Subsystems (3)
lore
Share & tools
lore

Javier Carrasco

author maintainer
#0 lore raw

v3 cover iio: light: fix scale in veml3235 and add helpers to iio-gts

This series addresses an issue in the veml3235 that was inherited from an older driver (veml6030, not covered here but probably addressed after discussing this series), where the scale is does not follow ABI.

To simplify the gain/integration time handling, the iio-gts helpers have been used. And to further simplify the process, two new helpers have been proposed to address repetitive patterns that are found in all users of iio-gts.

The additions to iio-gts are wrappers around existing helpers, and I have tried to keep their names short, as adding more prefixes to the existing functions looked too cumbersome and inconvenient to follow the 80-char/line recommendation. I have not added any test for the new helpers because I would prefer to discuss them first.

This series has been tested with a veml3235sl under all supported gains and integration times as well as with a few unsupported values to make sure the operations fail in those cases.

---
Changes in v3:
- Rename iio_gts_find_gain_sel_in_times() to iio_gts_find_gain_time_sel_for_scale() and update its documentation.
- Rebase onto iio/testing, apply tags and drop applied patches.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- industrialio-gts-helper.c: explicitly return -EINVAL if iio_gts_find_new_gain_vy_gain_time_min() fails for the minimum gain fallback.
- industrialio-gts-helper.c: check the non-zero value of ret to continue
to the next iteration in iio_gts_find_gan_sel_in_times().
- veml3235.c: drop code style fixes and move them to a separate patch.
- veml3235.c: add patch to support regmap cache to simplify value
updates.
- veml3235.c: put 'val' back in the arguments of set_it() to drop the
diff, and check it internally as before.
- veml3235.c: fix typo in comment.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Javier Carrasco (2):
iio: gts-helper: add helpers to ease searches of gain_sel and new_gain
iio: veml3235: fix scale to conform to ABI

 drivers/iio/industrialio-gts-helper.c |  77 +++++++++++
 drivers/iio/light/Kconfig             |   1 +
 drivers/iio/light/veml3235.c          | 237 ++++++++++++++++++----------------
 include/linux/iio/iio-gts-helper.h    |   6 +
 4 files changed, 213 insertions(+), 108 deletions(-)

---
base-commit: 627f3c41ca005398cf1e86a312c5043147ec7ea6
change-id: 20241215-veml3235_scale-62de98c7b5fa

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/2 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.

---
 drivers/iio/industrialio-gts-helper.c | 77 +++++++++++++++++++++++++++++++++++
 include/linux/iio/iio-gts-helper.h    |  6 +++
 2 files changed, 83 insertions(+)
drivers/iio/industrialio-gts-helper.c +77 -0
index 3b5a998150623ba43c2f015c2c5d8a757743b893..d70ebe3bf7742906960c6af22102682c5b8dcdfa 100644
@@ -915,6 +915,41 @@ int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel
 }
 EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_sel_for_scale_using_time, "IIO_GTS_HELPER");
 
+/**
+ * iio_gts_find_gain_time_sel_for_scale - Fetch gain and time selectors for scale
+ * @gts:	Gain time scale descriptor
+ * @scale_int:	Integral part of the scale (typically val1)
+ * @scale_nano:	Fractional part of the scale (nano or ppb)
+ * @gain_sel:	Pointer to value where gain selector is stored.
+ * @time_sel:	Pointer to value where time selector is stored.
+ *
+ * Wrapper around iio_gts_find_gain_for_scale_using_time() to fetch the
+ * gain and time selectors for a given scale.
+ *
+ * Return: 0 on success and -EINVAL on error.
+ */
+int iio_gts_find_gain_time_sel_for_scale(struct iio_gts *gts, int scale_int,
+					 int scale_nano, int *gain_sel,
+					 int *time_sel)
+{
+	int i, ret;
+
+	for (i = 0; i < gts->num_itime; i++) {
+		*time_sel = gts->itime_table[i].sel;
+		ret = iio_gts_find_gain_sel_for_scale_using_time(gts, *time_sel,
+								 scale_int,
+								 scale_nano,
+								 gain_sel);
+		if (ret)
+			continue;
+
+		return 0;
+	}
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_time_sel_for_scale, "IIO_GTS_HELPER");
+
 static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
 {
 	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,
 }
 EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_old_gain_time, "IIO_GTS_HELPER");
 
+/**
+ * iio_gts_find_new_gain_by_gain_time_min - compensate for time change
+ * @gts:	Gain time scale descriptor
+ * @old_gain:	Previously set gain
+ * @old_time:	Selector corresponding previously set time
+ * @new_time:	Selector corresponding new time to be set
+ * @new_gain:	Pointer to value where new gain is to be written
+ * @in_range:	Indicate if the @new_gain was in the range of
+ *		supported gains.
+ *
+ * Wrapper around iio_gts_find_new_gain_by_old_gain_time() that tries to
+ * set an optimal value if no exact match was found, defaulting to the
+ * minimum gain to avoid saturations if the optimal value is not in the
+ * range of supported gains.
+ *
+ * Return: 0 on success and a negative value if no gain was found.
+ */
+int iio_gts_find_new_gain_by_gain_time_min(struct iio_gts *gts, int old_gain,
+					   int old_time, int new_time,
+					   int *new_gain, bool *in_range)
+{
+	int ret;
+
+	*in_range = true;
+	ret = iio_gts_find_new_gain_by_old_gain_time(gts, old_gain, old_time,
+						     new_time, new_gain);
+	if (*new_gain < 0)
+		return -EINVAL;
+
+	if (ret) {
+		*new_gain = iio_find_closest_gain_low(gts, *new_gain, in_range);
+		if (*new_gain < 0) {
+			*new_gain = iio_gts_get_min_gain(gts);
+			if (*new_gain < 0)
+				return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_gain_time_min, "IIO_GTS_HELPER");
+
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Matti Vaittinen <[email protected]>");
 MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers");
include/linux/iio/iio-gts-helper.h +6 -0
index 9cb6c80dea716cb80b69bee5277230bafe1c9a95..e5de7a124bad6eb65414df364f84e81301b0690b 100644
@@ -188,6 +188,9 @@ int iio_gts_total_gain_to_scale(struct iio_gts *gts, int total_gain,
 int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel,
 					       int scale_int, int scale_nano,
 					       int *gain_sel);
+int iio_gts_find_gain_time_sel_for_scale(struct iio_gts *gts, int scale_int,
+					 int scale_nano, int *gain_sel,
+					 int *time_sel);
 int iio_gts_get_scale(struct iio_gts *gts, int gain, int time, int *scale_int,
 		      int *scale_nano);
 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,
 int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain,
 					   int old_time, int new_time,
 					   int *new_gain);
+int iio_gts_find_new_gain_by_gain_time_min(struct iio_gts *gts, int old_gain,
+					   int old_time, int new_time,
+					   int *new_gain, bool *in_range);
 int iio_gts_avail_times(struct iio_gts *gts,  const int **vals, int *type,
 			int *length);
 int iio_gts_all_avail_scales(struct iio_gts *gts, const int **vals, int *type,
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v3 2/2 iio: veml3235: fix scale to conform to ABI

In reply to Javier Carrasco

The current scale is not ABI-compliant as it is just the sensor gain instead of the value that acts as a multiplier to be applied to the raw value (there is no offset).

Use the iio-gts helpers to obtain the proper scale values according to the gain and integration time to match the resolution tables from the datasheet. When at it, use 'scale' instead of 'gain' consistently for the get/set functions to avoid misunderstandings.

Fixes c5a23f80c164 ("iio: light: add support for veml3235")
---
 drivers/iio/light/Kconfig    |   1 +
 drivers/iio/light/veml3235.c | 237 +++++++++++++++++++++++--------------------
 2 files changed, 130 insertions(+), 108 deletions(-)
drivers/iio/light/Kconfig +1 -0
index 4897258e3da5519f2757e5f105552596dc4473cf..e34e551eef3e8db006de56724ce3873c07b3360a 100644
@@ -669,6 +669,7 @@ config VCNL4035
 config VEML3235
 	tristate "VEML3235 ambient light sensor"
 	select REGMAP_I2C
+	select IIO_GTS_HELPER
 	depends on I2C
 	help
 	  Say Y here if you want to build a driver for the Vishay VEML3235
drivers/iio/light/veml3235.c +129 -108

333 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#3 lore raw

Re: [PATCH v3 2/2] iio: veml3235: fix scale to conform to ABI

Quoted 9 lines · Javier Carrasco

The current scale is not ABI-compliant as it is just the sensor gain instead of the value that acts as a multiplier to be applied to the raw value (there is no offset).

Use the iio-gts helpers to obtain the proper scale values according to the gain and integration time to match the resolution tables from the datasheet. When at it, use 'scale' instead of 'gain' consistently for the get/set functions to avoid misunderstandings.

Quoted 8 lines
Fixes c5a23f80c164 ("iio: light: add support for veml3235")
---
 drivers/iio/light/Kconfig    |   1 +
 drivers/iio/light/veml3235.c | 237 +++++++++++++++++++++++--------------------
 2 files changed, 130 insertions(+), 108 deletions(-)

@Jonathan, if you apply this patch in its current form, could you please fix the message from "iio: veml3235:..." to "iio: light: veml3235:..."? I just noticed that the "light:" bit is missing.
I can send a new version if that's preferred.

The applied patches from this series did not have this issue.

Thanks and best regards,
Javier Carrasco

maintainer Applied
#4 lore raw

Re: [PATCH v3 2/2] iio: veml3235: fix scale to conform to ABI

Quoted 19 lines · On Tue, 31 Dec 2024 09:39:03 +0100 "Javier Carrasco"…
Quoted 9 lines · Javier Carrasco

The current scale is not ABI-compliant as it is just the sensor gain instead of the value that acts as a multiplier to be applied to the raw value (there is no offset).

Use the iio-gts helpers to obtain the proper scale values according to the gain and integration time to match the resolution tables from the datasheet. When at it, use 'scale' instead of 'gain' consistently for the get/set functions to avoid misunderstandings.

Quoted 8 lines
Fixes c5a23f80c164 ("iio: light: add support for veml3235")
---
 drivers/iio/light/Kconfig    |   1 +
 drivers/iio/light/veml3235.c | 237 +++++++++++++++++++++++--------------------
 2 files changed, 130 insertions(+), 108 deletions(-)
Quoted 6 lines

@Jonathan, if you apply this patch in its current form, could you please fix the message from "iio: veml3235:..." to "iio: light: veml3235:..."? I just noticed that the "light:" bit is missing.
I can send a new version if that's preferred.

The applied patches from this series did not have this issue.

Applied to the togreg branch of iio.git and tidied patch names as suggested. As normal pushed out briefly as testing for 0-day to have fun with it.

Thanks,

Jonathan

Quoted 3 lines

Thanks and best regards,
Javier Carrasco