Skip to contentSkip to conversation

iio: light: fix scale in veml6030

PATCH Superseded Javier Carrasco 3 participants 10 messages Jan 19, 2025 – Jan 25, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Javier Carrasco

author maintainer
#0 lore raw

v2 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.

To ease the usage of the iio-gts selectors, patches to support regfields and caching has been included.

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 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 (4):
iio: light: veml6030: extend regmap to support regfields
iio: light: veml6030: extend regmap to support caching
iio: gts-helper: export iio_gts_get_total_gain()
iio: light: veml6030: fix scale to conform to ABI

 drivers/iio/industrialio-gts-helper.c |   3 +-
 drivers/iio/light/Kconfig             |   1 +
 drivers/iio/light/veml6030.c          | 608 +++++++++++++++++-----------------
 include/linux/iio/iio-gts-helper.h    |   1 +
 4 files changed, 299 insertions(+), 314 deletions(-)

---
base-commit: 577a66e2e634f712384c57a98f504c44ea4b47da
change-id: 20241231-veml6030-scale-8142f387e7e6

Best regards,

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

PATCH v2 1/4 iio: light: veml6030: extend regmap to support regfields

Add support for regfields as well to simplify register operations, taking into account the different fields for the veml6030/veml7700 and veml6035.

---
 drivers/iio/light/veml6030.c | 95 ++++++++++++++++++++++++++++++++------------
 1 file changed, 70 insertions(+), 25 deletions(-)
drivers/iio/light/veml6030.c +70 -25
index 9b71825eea9bee2146be17ed2f30f5a8f7ad37e3..8e4eb8b0c19276635567d4eed74f4d402c3205fa 100644
@@ -59,18 +59,31 @@
 #define VEML6035_INT_CHAN     BIT(3)
 #define VEML6035_CHAN_EN      BIT(2)
 
+/* Regfields */
+#define VEML6030_GAIN_RF      REG_FIELD(VEML6030_REG_ALS_CONF, 11, 12)
+#define VEML6030_IT_RF        REG_FIELD(VEML6030_REG_ALS_CONF, 6, 9)
+
+#define VEML6035_GAIN_RF      REG_FIELD(VEML6030_REG_ALS_CONF, 10, 12)
+
 enum veml6030_scan {
 	VEML6030_SCAN_ALS,
 	VEML6030_SCAN_WH,
 	VEML6030_SCAN_TIMESTAMP,
 };
 
+struct veml6030_rf {
+	struct regmap_field *it;
+	struct regmap_field *gain;
+};
+
 struct veml603x_chip {
 	const char *name;
 	const int(*scale_vals)[][2];
 	const int num_scale_vals;
 	const struct iio_chan_spec *channels;
 	const int num_channels;
+	const struct reg_field gain_rf;
+	const struct reg_field it_rf;
 	int (*hw_init)(struct iio_dev *indio_dev, struct device *dev);
 	int (*set_info)(struct iio_dev *indio_dev);
 	int (*set_als_gain)(struct iio_dev *indio_dev, int val, int val2);
@@ -91,6 +104,7 @@ struct veml603x_chip {
 struct veml6030_data {
 	struct i2c_client *client;
 	struct regmap *regmap;
+	struct veml6030_rf rf;
 	int cur_resolution;
 	int cur_gain;
 	int cur_integration_time;
@@ -330,17 +344,17 @@ static const struct regmap_config veml6030_regmap_config = {
 static int veml6030_get_intgrn_tm(struct iio_dev *indio_dev,
 						int *val, int *val2)
 {
-	int ret, reg;
+	int it_idx, ret;
 	struct veml6030_data *data = iio_priv(indio_dev);
 
-	ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
+	ret = regmap_field_read(data->rf.it, &it_idx);
 	if (ret) {
 		dev_err(&data->client->dev,
 				"can't read als conf register %d\n", ret);
 		return ret;
 	}
 
-	switch ((reg >> 6) & 0xF) {
+	switch (it_idx) {
 	case 0:
 		*val2 = 100000;
 		break;
@@ -405,8 +419,7 @@ static int veml6030_set_intgrn_tm(struct iio_dev *indio_dev,
 		return -EINVAL;
 	}
 
-	ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
-					VEML6030_ALS_IT, new_int_time);
+	ret = regmap_field_write(data->rf.it, new_int_time);
 	if (ret) {
 		dev_err(&data->client->dev,
 				"can't update als integration time %d\n", ret);
@@ -510,23 +523,22 @@ static int veml6030_set_als_gain(struct iio_dev *indio_dev,
 	struct veml6030_data *data = iio_priv(indio_dev);
 
 	if (val == 0 && val2 == 125000) {
-		new_gain = 0x1000; /* 0x02 << 11 */
+		new_gain = 0x01;
 		gain_idx = 3;
 	} else if (val == 0 && val2 == 250000) {
-		new_gain = 0x1800;
+		new_gain = 0x11;
 		gain_idx = 2;
 	} else if (val == 1 && val2 == 0) {
 		new_gain = 0x00;
 		gain_idx = 1;
 	} else if (val == 2 && val2 == 0) {
-		new_gain = 0x800;
+		new_gain = 0x01;
 		gain_idx = 0;
 	} else {
 		return -EINVAL;
 	}
 
-	ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
-					VEML6030_ALS_GAIN, new_gain);
+	ret = regmap_field_write(data->rf.gain, new_gain);
 	if (ret) {
 		dev_err(&data->client->dev,
 				"can't set als gain %d\n", ret);
@@ -544,30 +556,31 @@ static int veml6035_set_als_gain(struct iio_dev *indio_dev, int val, int val2)
 	struct veml6030_data *data = iio_priv(indio_dev);
 
 	if (val == 0 && val2 == 125000) {
-		new_gain = VEML6035_SENS;
+		new_gain = FIELD_GET(VEML6035_GAIN_M, VEML6035_SENS);
 		gain_idx = 5;
 	} else if (val == 0 && val2 == 250000) {
-		new_gain = VEML6035_SENS | VEML6035_GAIN;
+		new_gain = FIELD_GET(VEML6035_GAIN_M, VEML6035_SENS |
+				      VEML6035_GAIN);
 		gain_idx = 4;
 	} else if (val == 0 && val2 == 500000) {
-		new_gain = VEML6035_SENS | VEML6035_GAIN |
-			VEML6035_DG;
+		new_gain = FIELD_GET(VEML6035_GAIN_M, VEML6035_SENS |
+				      VEML6035_GAIN | VEML6035_DG);
 		gain_idx = 3;
 	} else if (val == 1 && val2 == 0) {
 		new_gain = 0x0000;
 		gain_idx = 2;
 	} else if (val == 2 && val2 == 0) {
-		new_gain = VEML6035_GAIN;
+		new_gain = FIELD_GET(VEML6035_GAIN_M, VEML6035_GAIN);
 		gain_idx = 1;
 	} else if (val == 4 && val2 == 0) {
-		new_gain = VEML6035_GAIN | VEML6035_DG;
+		new_gain = FIELD_GET(VEML6035_GAIN_M, VEML6035_GAIN |
+				      VEML6035_DG);
 		gain_idx = 0;
 	} else {
 		return -EINVAL;
 	}
 
-	ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF,
-				 VEML6035_GAIN_M, new_gain);
+	ret = regmap_field_write(data->rf.gain, new_gain);
 	if (ret) {
 		dev_err(&data->client->dev, "can't set als gain %d\n", ret);
 		return ret;
@@ -581,17 +594,17 @@ static int veml6035_set_als_gain(struct iio_dev *indio_dev, int val, int val2)
 static int veml6030_get_als_gain(struct iio_dev *indio_dev,
 						int *val, int *val2)
 {
-	int ret, reg;
+	int gain, ret;
 	struct veml6030_data *data = iio_priv(indio_dev);
 
-	ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
+	ret = regmap_field_read(data->rf.gain, &gain);
 	if (ret) {
 		dev_err(&data->client->dev,
 				"can't read als conf register %d\n", ret);
 		return ret;
 	}
 
-	switch ((reg >> 11) & 0x03) {
+	switch (gain) {
 	case 0:
 		*val = 1;
 		*val2 = 0;
@@ -617,17 +630,17 @@ static int veml6030_get_als_gain(struct iio_dev *indio_dev,
 
 static int veml6035_get_als_gain(struct iio_dev *indio_dev, int *val, int *val2)
 {
-	int ret, reg;
+	int gain, ret;
 	struct veml6030_data *data = iio_priv(indio_dev);
 
-	ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, &reg);
+	ret = regmap_field_read(data->rf.gain, &gain);
 	if (ret) {
 		dev_err(&data->client->dev,
-			"can't read als conf register %d\n", ret);
+				"can't read als conf register %d\n", ret);
 		return ret;
 	}
 
-	switch (FIELD_GET(VEML6035_GAIN_M, reg)) {
+	switch (gain) {
 	case 0:
 		*val = 1;
 		*val2 = 0;
@@ -990,6 +1003,27 @@ static int veml7700_set_info(struct iio_dev *indio_dev)
 	return 0;
 }
 
+static int veml6030_regfield_init(struct iio_dev *indio_dev)
+{
+	struct veml6030_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = &data->client->dev;
+	struct regmap_field *rm_field;
+	struct veml6030_rf *rf = &data->rf;
+
+	rm_field = devm_regmap_field_alloc(dev, regmap, data->chip->it_rf);
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->it = rm_field;
+
+	rm_field = devm_regmap_field_alloc(dev, regmap, data->chip->gain_rf);
+	if (IS_ERR(rm_field))
+		return PTR_ERR(rm_field);
+	rf->gain = rm_field;
+
+	return 0;
+}
+
 /*
  * Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2,
  * persistence to 1 x integration time and the threshold
@@ -1143,6 +1177,11 @@ static int veml6030_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = veml6030_regfield_init(indio_dev);
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "failed to init regfields\n");
+
 	ret = data->chip->hw_init(indio_dev, &client->dev);
 	if (ret < 0)
 		return ret;
@@ -1191,6 +1230,8 @@ static const struct veml603x_chip veml6030_chip = {
 	.num_scale_vals = ARRAY_SIZE(veml6030_scale_vals),
 	.channels = veml6030_channels,
 	.num_channels = ARRAY_SIZE(veml6030_channels),
+	.gain_rf = VEML6030_GAIN_RF,
+	.it_rf = VEML6030_IT_RF,
 	.hw_init = veml6030_hw_init,
 	.set_info = veml6030_set_info,
 	.set_als_gain = veml6030_set_als_gain,
@@ -1203,6 +1244,8 @@ static const struct veml603x_chip veml6035_chip = {
 	.num_scale_vals = ARRAY_SIZE(veml6035_scale_vals),
 	.channels = veml6030_channels,
 	.num_channels = ARRAY_SIZE(veml6030_channels),
+	.gain_rf = VEML6035_GAIN_RF,
+	.it_rf = VEML6030_IT_RF,
 	.hw_init = veml6035_hw_init,
 	.set_info = veml6030_set_info,
 	.set_als_gain = veml6035_set_als_gain,
@@ -1215,6 +1258,8 @@ static const struct veml603x_chip veml7700_chip = {
 	.num_scale_vals = ARRAY_SIZE(veml6030_scale_vals),
 	.channels = veml7700_channels,
 	.num_channels = ARRAY_SIZE(veml7700_channels),
+	.gain_rf = VEML6030_GAIN_RF,
+	.it_rf = VEML6030_IT_RF,
 	.hw_init = veml6030_hw_init,
 	.set_info = veml7700_set_info,
 	.set_als_gain = veml6030_set_als_gain,
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v2 2/4 iio: light: veml6030: extend regmap to support caching

In reply to Javier Carrasco

The configuration registers are not volatile and are not affected by read operations (i.e. not precious), making them suitable to be cached in order to reduce the number of accesses to the device.

Add support for caching (RBTREE type).

---
 drivers/iio/light/veml6030.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
drivers/iio/light/veml6030.c +31 -0
index 8e4eb8b0c19276635567d4eed74f4d402c3205fa..3afd4bb1ad53157f7ec63a35f51e5a96f7b019fd 100644
@@ -333,12 +333,43 @@ static const struct iio_chan_spec veml7700_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(VEML6030_SCAN_TIMESTAMP),
 };
 
+static const struct regmap_range veml6030_readable_ranges[] = {
+	regmap_reg_range(VEML6030_REG_ALS_CONF, VEML6030_REG_ALS_INT),
+};
+
+static const struct regmap_access_table veml6030_readable_table = {
+	.yes_ranges = veml6030_readable_ranges,
+	.n_yes_ranges = ARRAY_SIZE(veml6030_readable_ranges),
+};
+
+static const struct regmap_range veml6030_writable_ranges[] = {
+	regmap_reg_range(VEML6030_REG_ALS_CONF, VEML6030_REG_ALS_PSM),
+};
+
+static const struct regmap_access_table veml6030_writable_table = {
+	.yes_ranges = veml6030_writable_ranges,
+	.n_yes_ranges = ARRAY_SIZE(veml6030_writable_ranges),
+};
+
+static const struct regmap_range veml6030_volatile_ranges[] = {
+	regmap_reg_range(VEML6030_REG_ALS_DATA, VEML6030_REG_WH_DATA),
+};
+
+static const struct regmap_access_table veml6030_volatile_table = {
+	.yes_ranges = veml6030_volatile_ranges,
+	.n_yes_ranges = ARRAY_SIZE(veml6030_volatile_ranges),
+};
+
 static const struct regmap_config veml6030_regmap_config = {
 	.name = "veml6030_regmap",
 	.reg_bits = 8,
 	.val_bits = 16,
 	.max_register = VEML6030_REG_ALS_INT,
 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
+	.rd_table = &veml6030_readable_table,
+	.wr_table = &veml6030_writable_table,
+	.volatile_table = &veml6030_volatile_table,
+	.cache_type = REGCACHE_RBTREE,
 };
 
 static int veml6030_get_intgrn_tm(struct iio_dev *indio_dev,
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v2 3/4 iio: gts-helper: export iio_gts_get_total_gain()

In reply to 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 the IIO_LIGHT channel compared to acquiring the scale in NANO.

---
 drivers/iio/industrialio-gts-helper.c | 3 ++-
 include/linux/iio/iio-gts-helper.h    | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +2 -1
index d70ebe3bf7742906960c6af22102682c5b8dcdfa..6ec434338411d09fffa1db52046a7df9b34c89bf 100644
@@ -950,7 +950,7 @@ 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)
+int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
 {
 	const struct iio_itime_sel_mul *itime;
 
@@ -966,6 +966,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
index e5de7a124bad6eb65414df364f84e81301b0690b..66f830ab9b49b566d549c7b5b8291d42a0825b96 100644
@@ -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
maintainer
#4 lore raw

Re: [PATCH v2 3/4] iio: gts-helper: export iio_gts_get_total_gain()

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 the IIO_LIGHT channel compared to acquiring the scale in NANO.

Hi Javier,

This is fine but could you please go the extra mile and add a doc to the function? I'd like to have kerneldoc for all exported functions.

Quoted 38 lines
---
  drivers/iio/industrialio-gts-helper.c | 3 ++-
  include/linux/iio/iio-gts-helper.h    | 1 +
  2 files changed, 3 insertions(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +2 -1
index d70ebe3bf7742906960c6af22102682c5b8dcdfa..6ec434338411d09fffa1db52046a7df9b34c89bf 100644
@@ -950,7 +950,7 @@ 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)
+int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
  {
  	const struct iio_itime_sel_mul *itime;
 
@@ -966,6 +966,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
index e5de7a124bad6eb65414df364f84e81301b0690b..66f830ab9b49b566d549c7b5b8291d42a0825b96 100644
@@ -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
author maintainer
#5 lore raw

Re: [PATCH v2 3/4] iio: gts-helper: export iio_gts_get_total_gain()

Quoted 5 lines · Matti Vaittinen
Quoted 4 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 the IIO_LIGHT channel compared to acquiring the scale in NANO.

Quoted 8 lines
Quoted 2 lines

Hi Javier,

This is fine but could you please go the extra mile and add a doc to the function? I'd like to have kerneldoc for all exported functions.

Hi Matti,

Sure, I will add that for v3.

Thanks for your feedback and best regards,
Javier Carrasco

author maintainer
#6 lore raw

PATCH v2 4/4 iio: light: veml6030: 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 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.

Fixes 7b779f573c48 ("iio: light: add driver for veml6030 ambient light sensor")
---
 drivers/iio/light/Kconfig    |   1 +
 drivers/iio/light/veml6030.c | 528 ++++++++++++++++++-------------------------
 2 files changed, 218 insertions(+), 311 deletions(-)
drivers/iio/light/Kconfig +1 -0
index e34e551eef3e8db006de56724ce3873c07b3360a..eb7f56eaeae07c8b021dc7c0db87f46b44ed44d7 100644
@@ -683,6 +683,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
maintainer Acked-by on v2 4/4
#7 lore raw

Re: [PATCH v2 4/4] iio: light: veml6030: fix scale to conform to ABI

Quoted 11 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 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.

Quoted 2 lines
Fixes 7b779f573c48 ("iio: light: add driver for veml6030 ambient light sensor")

Hi Javier,

Sorry for the delay - been a bit busy lately. I didn't do a thorough review but the total_gain (and rest of the GTS) usage looked good to me :)

maintainer
#8 lore raw
In reply to Javier Carrasco
Quoted 29 lines · On Sun, 19 Jan 2025 18:31:57 +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.

To ease the usage of the iio-gts selectors, patches to support regfields and caching has been included.

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.

Whilst we might consider a backport eventually. This is a huge patch to do that with. Hence I'll take this the slow way for next merge window.

For now I've applied patches 1 and 2 in the interest of nibbling away at what we will need to see again ;)

Jonathan

Quoted 25 lines

---
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 (4):
iio: light: veml6030: extend regmap to support regfields
iio: light: veml6030: extend regmap to support caching
iio: gts-helper: export iio_gts_get_total_gain()
iio: light: veml6030: fix scale to conform to ABI

 drivers/iio/industrialio-gts-helper.c |   3 +-
 drivers/iio/light/Kconfig             |   1 +
 drivers/iio/light/veml6030.c          | 608 +++++++++++++++++-----------------
 include/linux/iio/iio-gts-helper.h    |   1 +
 4 files changed, 299 insertions(+), 314 deletions(-)

---
base-commit: 577a66e2e634f712384c57a98f504c44ea4b47da
change-id: 20241231-veml6030-scale-8142f387e7e6

Best regards,

author maintainer
#9 lore raw
Quoted 38 lines · Jonathan Cameron
Quoted 35 lines · On Sun, 19 Jan 2025 18:31:57 +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.

To ease the usage of the iio-gts selectors, patches to support regfields and caching has been included.

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.

Quoted 9 lines
Quoted 1 line

Whilst we might consider a backport eventually. This is a huge patch to do that with. Hence I'll take this the slow way for next merge window.

For now I've applied patches 1 and 2 in the interest of nibbling away at what we will need to see again ;)

Jonathan

Thanks, Jonathan.

Could you please tell me where you applied them, so I
can rebase onto that branch? I did not find them in linux-next/master, iio/testing or iio/togreg. Or should they show up in one of those within the next hours/days?

Best regards,
Javier Carrasco.