iio: light: fix scale in veml6030
Subsystems (3)
Javier Carrasco
maintainerv3 cover iio: light: fix scale in veml6030
This series follows a similar approach as recently used for the veml3235 by using iio-gts to manage the scale as stated in the ABI. In its current form, the driver exposes the hardware gain instead of the multiplier for the raw value to obtain a value in lux.
Although this driver and the veml3235 have many similarities, there are two main differences in this series compared to the one used to fix the other driver:
- The veml6030 has fractional gains, which are not supported by the iio-gts helpers. My first attempt was adding support for them, but that made the whole iio-gts implementation more complex, cumbersome, and the risk of affecting existing clients was not negligible. Instead, a x8 factor has been used for the hardware gain to present the minimum value (x0.125) as x1, keeping linearity. The scales iio-gts generates are therefore right without any extra conversion, and they match the values provided in the different datasheets.
- This driver included a processed value for the ambient light, maybe because the scale did not follow the ABI and the conversion was not direct. To avoid breaking userspace, the functionality has been kept, but of course using the fixed scales. In order to ease the calculations, iio_gts_get_total_gain() has been exported to avoid working directly with the scale in NANO, that would require 64-bit operations.
This issue has been present since the original implementation, and it affects all devices it supports.
This series has been tested with a veml7700 (same gains as veml6030) and a veml6035 with positive results.
---
Changes in v3:
- Rebase onto iio/testing and drop [1/4], [2/4] (applied).
- iio-gts: document exported function.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Rename SEL_GAIN to indicate they are in MILLI.
- Split first patch (regfields and chaching).
- Use regfield structs in chip struct instead of function pointer.
- Use total gain to derive scale, avoiding 64-bit divisions.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Javier Carrasco (2):
iio: gts-helper: export iio_gts_get_total_gain()
iio: light: veml6030: fix scale to conform to ABI
drivers/iio/industrialio-gts-helper.c | 11 +- drivers/iio/light/Kconfig | 1 + drivers/iio/light/veml6030.c | 528 ++++++++++++++-------------------- include/linux/iio/iio-gts-helper.h | 1 + 4 files changed, 229 insertions(+), 312 deletions(-)
---
base-commit: ed2010907caa1c838d2e565d67bbc08fe023663e
change-id: 20241231-veml6030-scale-8142f387e7e6
Best regards,
Signature · 1 line
Javier Carrasco <[email protected]>
Javier Carrasco
maintainerPATCH v3 1/2 iio: gts-helper: export iio_gts_get_total_gain()
Export this function in preparation for the fix in veml6030.c, where the total gain can be used to ease the calculation of the processed value of the IIO_LIGHT channel compared to acquiring the scale in NANO.
--- drivers/iio/industrialio-gts-helper.c | 11 ++++++++++- include/linux/iio/iio-gts-helper.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +10 -1
@@ -1026,7 +1026,15 @@ int iio_gts_find_gain_time_sel_for_scale(struct iio_gts *gts, int scale_int, }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) +/** +* iio_gts_get_total_gain - Fetch total gain for given HW-gain and time +* @gts: Gain time scale descriptor +* @gain: HW-gain for which the total gain is searched for +* @time: Integration time for which the total gain is searched for +* +* Return: total gain on success and -EINVAL on error. +*/ +int iio_gts_get_total_gain (struct iio_gts *gts,int gain,int time) {const struct iio_itime_sel_mul *itime;
@@ -1042,6 +1050,7 @@ static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)return gain * itime->mul; } +EXPORT_SYMBOL_NS_GPL (iio_gts_get_total_gain,"IIO_GTS_HELPER" );static int iio_gts_get_scale_linear (struct iio_gts *gts,int gain,int time,u64 *scale)
include/linux/iio/iio-gts-helper.h +1 -0
@@ -208,5 +208,6 @@ int iio_gts_all_avail_scales(struct iio_gts *gts, const int **vals, int *type, int *length);int iio_gts_avail_scales_for_time (struct iio_gts *gts,int time,const int **vals,int *type,int *length); +int iio_gts_get_total_gain (struct iio_gts *gts,int gain,int time);#endif
Signature · 1 line
2.43.0
Re: [PATCH v3 1/2] iio: gts-helper: export iio_gts_get_total_gain()
Thanks Javier!
Quoted 6 lines · Javier Carrasco
Export this function in preparation for the fix in
veml6030.c, where the total gain can be used to ease the calculation of the processed value of theIIO_LIGHTchannel compared to acquiring the scale in NANO.Suggested-by Matti Vaittinen <[email protected]>Signed-off-by Javier Carrasco <[email protected]>
Yours,
-- Matti
Javier Carrasco
maintainerPATCH v3 2/2 iio: light: veml6030: fix scale to conform to ABI
In reply to Javier CarrascoThe 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 and drop dedicated variables to store the current values of the integration time, gain and resolution. When at it, use 'scale' instead of 'gain' consistently for the get/set functions to avoid misunderstandings.
--- drivers/iio/light/Kconfig | 1 + drivers/iio/light/veml6030.c | 528 ++++++++++++++++++------------------------- 2 files changed, 218 insertions(+), 311 deletions(-)
drivers/iio/light/Kconfig +1 -0
@@ -694,6 +694,7 @@ config VEML6030 select REGMAP_I2C select IIO_BUFFER select IIO_TRIGGERED_BUFFER + select IIO_GTS_HELPER depends on I2C help Say Y here if you want to build a driver for the Vishay VEML6030
drivers/iio/light/veml6030.c +217 -311
731 lines not on this page · raw message
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 26 lines · On Mon, 27 Jan 2025 20:30:21 +0100 Javier Carrasco…
This series follows a similar approach as recently used for the veml3235 by using iio-gts to manage the scale as stated in the ABI. In its current form, the driver exposes the hardware gain instead of the multiplier for the raw value to obtain a value in lux.
Although this driver and the veml3235 have many similarities, there are two main differences in this series compared to the one used to fix the other driver:
- The veml6030 has fractional gains, which are not supported by the iio-gts helpers. My first attempt was adding support for them, but that made the whole iio-gts implementation more complex, cumbersome, and the risk of affecting existing clients was not negligible. Instead, a x8 factor has been used for the hardware gain to present the minimum value (x0.125) as x1, keeping linearity. The scales iio-gts generates are therefore right without any extra conversion, and they match the values provided in the different datasheets.
- This driver included a processed value for the ambient light, maybe because the scale did not follow the ABI and the conversion was not direct. To avoid breaking userspace, the functionality has been kept, but of course using the fixed scales. In order to ease the calculations,
iio_gts_get_total_gain()has been exported to avoid working directly with the scale in NANO, that would require 64-bit operations.
Quoted 7 lines
This issue has been present since the original implementation, and it affects all devices it supports.
This series has been tested with a veml7700 (same gains as veml6030) and a veml6035 with positive results.
Signed-off-by Javier Carrasco <[email protected]>
Applied to the togreg branch of iio.git. Initially pushed out as testing.
I've rebased on char-misc, but will almost certainly rebase once more on rc1 once available.
Quoted 28 lines
---
Changes in v3:
- Rebase onto iio/testing and drop [1/4], [2/4] (applied).
- iio-gts: document exported function.
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- RenameSEL_GAINto indicate they are in MILLI.
- Split first patch (regfields and chaching).
- Use regfield structs in chip struct instead of function pointer.
- Use total gain to derive scale, avoiding 64-bit divisions.
- Link to v1: https://lore.kernel.org/r/[email protected]---
Javier Carrasco (2):
iio: gts-helper: exportiio_gts_get_total_gain()
iio: light: veml6030: fix scale to conform to ABIdrivers/iio/industrialio-gts-helper.c | 11 +- drivers/iio/light/Kconfig | 1 + drivers/iio/light/veml6030.c | 528 ++++++++++++++-------------------- include/linux/iio/iio-gts-helper.h | 1 + 4 files changed, 229 insertions(+), 312 deletions(-)---
base-commit:ed2010907caa1c838d2e565d67bbc08fe023663e
change-id: 20241231-veml6030-scale-8142f387e7e6Best regards,