Skip to contentSkip to conversation

iio: light: veml3328: add support for new sensor

PATCH Superseded Joshua Crofts 3 participants 14 messages May 30 – Jun 2

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Joshua Crofts

author maintainer
#0 lore raw

v3 cover iio: light: veml3328: add support for new sensor

This patch series adds support for the Vishay VEML3328 RGBCIR light sensor. The sensor communicates via I2C (SMBus compatible) and provides 5 types of 16-bit measurements: red, green, blue, clear and infrared.

Reasons for adding a new driver:
- Existing Vishay drivers in the kernel do not cover sensors that handle RGBC and IR simultaneously.
- The register map and configuration differ from other Vishay light sensors currently supported by IIO.

Testing:
- Tested on a Raspberry Pi 4 using a VEML3328 breakout board.

Datasheet:
https://www.vishay.com/docs/84968/veml3328.pdf

---
Changes in v2:
- Add additional IIO_LIGHT channel for ambient light sensing
- Remove separate dt binding file and added veml3328 entry to veml6030 yaml
- Move driver to PM_RUNTIME_ACQUIRE_AUTOSUSPEND() macro
- Add missing headers
- Remov redundant mutex as regmap handles it itself
- Use regmap_set/clear_bits() instead of regmap_update_bits()
- Removed redundant dev pointer
- Edit commit messages
- Various code style cleanups
- Link to v1: https://patch.msgid.link/[email protected]

Changes in v3:
- Add 2D array of precomputed scale values based on integration time
- Fix chan_spec masks
- Simplify read/write/avail callbacks

---
Joshua Crofts (2):
dt-bindings: iio: light: veml6030: add veml3328
iio: light: veml3328: add support for new device

 .../bindings/iio/light/vishay,veml6030.yaml        |   5 +-
 MAINTAINERS                                        |   5 +
 drivers/iio/light/Kconfig                          |  11 +
 drivers/iio/light/Makefile                         |   1 +
 drivers/iio/light/veml3328.c                       | 413 +++++++++++++++++++++
 5 files changed, 434 insertions(+), 1 deletion(-)

---
base-commit: 7b84b1e9dd850a5c9b55e27daa4ecdc2dd5b3431
change-id: 20260530-veml3328-d2ccb26d02c7

Best regards,

Signature · 1 line
Joshua Crofts <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/2 dt-bindings: iio: light: veml6030: add veml3328

The Vishay VEML3328 is an RGBCIR light sensor that shares similar devicetree properties as other existing VEMLxxxx sensors in the kernel.

---
 Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +4 -1
index 4ea69f1fdd63..0041e1db6838 100644
@@ -4,7 +4,7 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: VEML3235, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
 
 maintainers:
   - Rishi Gupta <[email protected]>
@@ -21,6 +21,7 @@ description: |
 
   Specifications about the sensors can be found at:
     https://www.vishay.com/docs/80131/veml3235.pdf
+    https://www.vishay.com/docs/84968/veml3328.pdf
     https://www.vishay.com/docs/84366/veml6030.pdf
     https://www.vishay.com/docs/84889/veml6035.pdf
     https://www.vishay.com/docs/84286/veml7700.pdf
@@ -29,6 +30,7 @@ properties:
   compatible:
     enum:
       - vishay,veml3235
+      - vishay,veml3328
       - vishay,veml6030
       - vishay,veml6035
       - vishay,veml7700
@@ -79,6 +81,7 @@ allOf:
         compatible:
           enum:
             - vishay,veml3235
+            - vishay,veml3328
             - vishay,veml7700
     then:
       properties:
Signature · 1 line
2.54.0
author maintainer
#3 lore raw

PATCH v3 2/2 iio: light: veml3328: add support for new device

In reply to Joshua Crofts

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible).

Also add a new entry for said driver into Kconfig and Makefile.

Assisted-by Gemini:3.1-Pro
---
 MAINTAINERS                  |   5 +
 drivers/iio/light/Kconfig    |  11 ++
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/veml3328.c | 413 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 430 insertions(+)
MAINTAINERS +5 -0
index 9e9457c7bba6..a893a1a1181e 100644
@@ -28402,6 +28402,11 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml
 F:	drivers/iio/light/veml3235.c
 
+VISHAY VEML3328 RGB IR LIGHT SENSOR DRIVER
+M:	Joshua Crofts <[email protected]>
+S:	Maintained
+F:	drivers/iio/light/veml3328.c
+
 VISHAY VEML6030 AMBIENT LIGHT SENSOR DRIVER
 M:	Javier Carrasco <[email protected]>
 S:	Maintained
drivers/iio/light/Kconfig +11 -0
index da4807a3fd3d..ef36824f312f 100644
@@ -713,6 +713,17 @@ config VEML3235
 	  To compile this driver as a module, choose M here: the
 	  module will be called veml3235.
 
+config VEML3328
+	tristate "VEML3328 RGBCIR light sensor"
+	select REGMAP_I2C
+	depends on I2C
+	help
+	  Say Y here if you want to build a driver for the Vishay VEML3328
+	  RGB IR light sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called veml3328
+
 config VEML6030
 	tristate "VEML6030 and VEML6035 ambient light sensors"
 	select REGMAP_I2C
drivers/iio/light/Makefile +1 -0
index 39e62dfc10c7..64e354c49ed8 100644
@@ -66,6 +66,7 @@ obj-$(CONFIG_US5182D)		+= us5182d.o
 obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
 obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
 obj-$(CONFIG_VEML3235)		+= veml3235.o
+obj-$(CONFIG_VEML3328)		+= veml3328.o
 obj-$(CONFIG_VEML6030)		+= veml6030.o
 obj-$(CONFIG_VEML6040)		+= veml6040.o
 obj-$(CONFIG_VEML6046X00)	+= veml6046x00.o
drivers/iio/light/veml3328.c new file +413 -0

413 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v3 1/2] dt-bindings: iio: light: veml6030: add veml3328

In reply to Joshua Crofts
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] The runtime PM autosuspend timer is not started in probe, leaving the device powered on…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The runtime PM autosuspend timer is not started in probe, leaving the device powered on indefinitely.
- [Medium] Concurrent sysfs writes to integration time and scale can leave the hardware in an inconsistent state.
- [Low] Jumping over a variable initialization with the __cleanup__ attribute in a switch statement causes a compiler error. --

commit e6504b133c6cc43d3687ab0ce437962c17f98107
Author: Joshua Crofts <[email protected]>

iio: light: veml3328: add support for new device

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible). Also add a new entry for said driver into Kconfig and Makefile.

Quoted 5 lines
drivers/iio/light/veml3328.c new file +0 -0
new file mode 100644
index 0000000000000..1def67fd9b51d

[ ... ]

Quoted 10 lines
drivers/iio/light/veml3328.c new file +10 -0
+static int veml3328_read_avail(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       const int **vals, int *type, int *length,
+			       long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(data->regmap);
+	unsigned int reg_val;
+	int ret, it_inx;

drivers/iio/light/veml3328.c

Quoted 10 lines
drivers/iio/light/veml3328.c new file +10 -0
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		*length = ARRAY_SIZE(veml3328_it_times) * 2;
+		*vals = (const int *)veml3328_it_times;
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+
+	case IIO_CHAN_INFO_SCALE:
+		PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);

[Severity: Low]
Does this jump over a variable initialization and cause a build regression?

The PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND() macro declares pm with the __cleanup__ attribute. When mask does not match IIO_CHAN_INFO_INT_TIME or IIO_CHAN_INFO_SCALE, the execution jumps to the default: label, which is physically after the pm declaration.

Jumping over a variable initialization with the __cleanup__ attribute causes a compilation regression in GCC.

Could we wrap this case in a scoped block { ... } to prevent this?

Quoted 21 lines
drivers/iio/light/veml3328.c new file +21 -0
+		ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+		if (ret)
+			return ret;
+
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		*length = 8;
+		*vals = (const int *)veml3328_scale_vals[it_inx];
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+
+	default:
+		return -EINVAL;
+	}
+}

[ ... ]

Quoted 17 lines
drivers/iio/light/veml3328.c new file +17 -0
+static int veml3328_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(regmap);
+	unsigned int reg_val;
+	int i, it_inx;
+	int ret;
+
+	PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);
+	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+	if (ret)
+		return ret;
+
+	switch (mask) {

[ ... ]

drivers/iio/light/veml3328.c

Quoted 6 lines
drivers/iio/light/veml3328.c new file +6 -0
+	case IIO_CHAN_INFO_SCALE:
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);

[Severity: Medium]
Is there a race condition here that causes a regression during concurrent sysfs writes?

This performs a lockless read-modify-write operation for IIO_CHAN_INFO_SCALE. Because veml3328_write_raw() lacks driver-level locking (like a mutex), a concurrent sysfs write to IIO_CHAN_INFO_INT_TIME could modify the hardware's integration time index immediately after it is read here.

If that happens, the gain index calculated and applied below will be based on a stale it_inx, leaving the hardware in an inconsistent configuration.

Quoted 20 lines
drivers/iio/light/veml3328.c new file +20 -0
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		for (i = 0; i < 4; i++) {
+			if (val == veml3328_scale_vals[it_inx][i * 2] &&
+			    val2 == veml3328_scale_vals[it_inx][i * 2 + 1])
+				break;
+		}
+
+		if (i == 4)
+			return -EINVAL;
+
+		return regmap_update_bits(regmap, VEML3328_REG_CONF,
+					  VEML3328_CONF_GAIN_MASK,
+					  FIELD_PREP(VEML3328_CONF_GAIN_MASK, i));
+
+	default:
+		return -EINVAL;
+	}
+}

[ ... ]

Quoted 7 lines
drivers/iio/light/veml3328.c new file +7 -0
+static int veml3328_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct veml3328_data *data;
+	struct iio_dev *indio_dev;
+	unsigned int reg_val;
+	int ret;

[ ... ]

drivers/iio/light/veml3328.c

Quoted 5 lines
drivers/iio/light/veml3328.c new file +5 -0
+	pm_runtime_set_active(dev);
+	pm_runtime_set_autosuspend_delay(dev, 2000);
+	pm_runtime_use_autosuspend(dev);
+
+	ret = devm_pm_runtime_enable(dev);

[Severity: Medium]
Are we missing a call to start the autosuspend timer here, causing a power regression?

The sensor is physically powered on via veml3328_power_up(), and runtime PM is initialized as active and enabled.

Since the usage count remains at 0 and the driver does not invoke pm_runtime_idle() or a matched pm_runtime_get/put cycle at the end of probe, the autosuspend timer is never scheduled.

Does this leave the device permanently powered on until a userspace application accesses it?

Quoted 5 lines
drivers/iio/light/veml3328.c new file +5 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
+
+	return devm_iio_device_register(dev, indio_dev);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#4 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

In reply to Joshua Crofts
Quoted 9 lines · Joshua Crofts

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible).

Also add a new entry for said driver into Kconfig and Makefile.

Assisted-by Gemini:3.1-Pro

---

Sashiko had a few remarks, two are bogus IMO, but it does mention that the device isn't put to sleep after probing...

https://sashiko.dev/#/patchset/20260530-veml3328-v3-0-dd562eaee8d9%40gmail.com

Signature · 3 lines
Kind regards

CJD
#6 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

In reply to Joshua Crofts
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Power management flaw: the sensor is left powered on indefinitely after probe. - [Medium] Race…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Power management flaw: the sensor is left powered on indefinitely after probe.
- [Medium] Race condition in write_raw allowing corrupted scale and integration time configuration.
- [Low] Build failure due to variable declaration immediately following a case label without an enclosing block. --

commit e6504b133c6cc43d3687ab0ce437962c17f98107
Author: Joshua Crofts <[email protected]>

iio: light: veml3328: add support for new device

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible). Also add a new entry for said driver into Kconfig and Makefile.

Quoted 4 lines
drivers/iio/light/veml3328.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 11 lines
drivers/iio/light/veml3328.c new file +11 -0
+static int veml3328_read_avail(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       const int **vals, int *type, int *length,
+			       long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(data->regmap);
+	unsigned int reg_val;
+	int ret, it_inx;
+

drivers/iio/light/veml3328.c line 215

Quoted 10 lines
drivers/iio/light/veml3328.c new file +10 -0
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		*length = ARRAY_SIZE(veml3328_it_times) * 2;
+		*vals = (const int *)veml3328_it_times;
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+
+	case IIO_CHAN_INFO_SCALE:
+		PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);
+		ret = PM_RUNTIME_ACQUIRE_ERR(&pm);

[Severity: Low]
Since PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND() expands to a variable declaration with a __cleanup__ attribute, does placing it immediately after the case label cause a build failure?

In C, a label cannot be directly followed by a declaration. Additionally, without an enclosing block, does the variable's scope extend to the end of the switch statement, causing the compiler to emit an error because jumping to the default case bypasses the initialization?

Quoted 6 lines
drivers/iio/light/veml3328.c new file +6 -0
+		if (ret)
+			return ret;
+
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;

[ ... ]

Quoted 18 lines
drivers/iio/light/veml3328.c new file +18 -0
+static int veml3328_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(regmap);
+	unsigned int reg_val;
+	int i, it_inx;
+	int ret;
+
+	PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);
+	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+	if (ret)
+		return ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:

[ ... ]

Quoted 11 lines
drivers/iio/light/veml3328.c new file +11 -0
+	case IIO_CHAN_INFO_SCALE:
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		for (i = 0; i < 4; i++) {
+			if (val == veml3328_scale_vals[it_inx][i * 2] &&

drivers/iio/light/veml3328.c line 290

Quoted 10 lines
drivers/iio/light/veml3328.c new file +10 -0
+			    val2 == veml3328_scale_vals[it_inx][i * 2 + 1])
+				break;
+		}
+
+		if (i == 4)
+			return -EINVAL;
+
+		return regmap_update_bits(regmap, VEML3328_REG_CONF,
+					  VEML3328_CONF_GAIN_MASK,
+					  FIELD_PREP(VEML3328_CONF_GAIN_MASK, i));

[Severity: Medium]
Is this read-modify-write sequence protected against concurrent changes?

Since there is no driver-private mutex or a call to iio_device_claim_direct_mode() here, could a concurrent sysfs write to IIO_CHAN_INFO_INT_TIME alter the integration time in hardware between the initial read and the final update?

If this race occurs, does it cause the driver to apply a gain value calculated for a stale integration time, leaving the sensor in an inconsistent operational state?

Quoted 5 lines
drivers/iio/light/veml3328.c new file +5 -0
+
+	default:
+		return -EINVAL;
+	}
+}

[ ... ]

Quoted 7 lines
drivers/iio/light/veml3328.c new file +7 -0
+static int veml3328_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct veml3328_data *data;
+	struct iio_dev *indio_dev;
+	unsigned int reg_val;
+	int ret;

[ ... ]

drivers/iio/light/veml3328.c line 352

Quoted 5 lines
drivers/iio/light/veml3328.c new file +5 -0
+	pm_runtime_set_active(dev);
+	pm_runtime_set_autosuspend_delay(dev, 2000);
+	pm_runtime_use_autosuspend(dev);
+
+	ret = devm_pm_runtime_enable(dev);

[Severity: Medium]
Does this leave the sensor powered on indefinitely after probe?

Since the driver's usage count starts at 0 and no explicit idle check is triggered (such as by calling pm_runtime_idle() or performing a get_noresume() and put_autosuspend() cycle), the PM core is not prompted to start the autosuspend timer.

Will the device remain in the RPM_ACTIVE state and draw full power after boot until the first user-space interaction initiates a suspend cycle?

Quoted 5 lines
drivers/iio/light/veml3328.c new file +5 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
+
+	return devm_iio_device_register(dev, indio_dev);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

In reply to Joshua Crofts
Quoted 7 lines · On Sat, 30 May 2026 19:23:53 +0200 Joshua Crofts <[email protected]>…
Quoted 6 lines · Joshua Crofts

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible).

Also add a new entry for said driver into Kconfig and Makefile.

Quoted 9 lines
Quoted 3 lines
Assisted-by Gemini:3.1-Pro

---

Sashiko had a few remarks, two are bogus IMO, but it does mention that the device isn't put to sleep after probing...

https://sashiko.dev/#/patchset/20260530-veml3328-v3-0-dd562eaee8d9%40gmail.com

I vaguely recall looking into this years ago and being convinced that it would autosuspend. Easiest is probably to verify if it does by just testing the device.

However that might rely on turning runtime pm on before calling use_autosuspend whereas you have it in a different order here.

One of the other sashiko comments is correct.. I'll reply to the patch.

maintainer
#7 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

In reply to Joshua Crofts
Quoted 7 lines · On Sat, 30 May 2026 19:06:47 +0200 Joshua Crofts <[email protected]>…

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible).

Also add a new entry for said driver into Kconfig and Makefile.

Assisted-by Gemini:3.1-Pro

Hi Joshua,

I didn't find anything extras so this is just some more explanation for the Sashiko comments.

Give we are approaching the end of the cycle, feel free to do a new version quicker than normal if you are sure on what needs changing.

Jonathan

Quoted 75 lines
drivers/iio/light/veml3328.c new file +69 -0
new file mode 100644
index 000000000000..1def67fd9b51
 
+static int veml3328_read_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int *val, int *val2, long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(regmap);
+	unsigned int reg_val;
+	int it_inx, gain_inx;
+	int ret;
+
+	PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);
+	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+	if (ret)
+		return ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		ret = regmap_read(regmap, chan->address, &reg_val);
+		if (ret)
+			return ret;
+
+		*val = reg_val;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_INT_TIME:
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		*val = veml3328_it_times[it_inx][0];
+		*val2 = veml3328_it_times[it_inx][1];
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_CHAN_INFO_SCALE:
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);
+		gain_inx = FIELD_GET(VEML3328_CONF_GAIN_MASK, reg_val);
+
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times) || gain_inx >= 4)
+			return -EINVAL;
+
+		/* Stride by 2 through the flattened array to match (val, val2) */
+		*val = veml3328_scale_vals[it_inx][gain_inx * 2];
+		*val2 = veml3328_scale_vals[it_inx][gain_inx * 2 + 1];
+
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int veml3328_read_avail(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       const int **vals, int *type, int *length,
+			       long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(data->regmap);
+	unsigned int reg_val;

drivers/iio/light/veml3328.c line 213

Quoted 10 lines
drivers/iio/light/veml3328.c new file +10 -0
+	int ret, it_inx;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		*length = ARRAY_SIZE(veml3328_it_times) * 2;
+		*vals = (const int *)veml3328_it_times;
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+
+	case IIO_CHAN_INFO_SCALE:

Under the hood, like all the cleanup.h magic, this instantiates a local variable - the cleanup scope is tided to that and scope for switch statements is a funny thing - it's not per case, but rather the whole switch. Hence need to add {} to define the scope for this case block.

Otherwise you enter this switch via another path and the local variable is unassigned, but the __free() that cleans it up is set. Hence you get a use of undefined variable.

Quoted 48 lines
drivers/iio/light/veml3328.c new file +48 -0
+		PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);
+		ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+		if (ret)
+			return ret;
+
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		*length = 8;
+		*vals = (const int *)veml3328_scale_vals[it_inx];
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int veml3328_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
+{
+	struct veml3328_data *data = iio_priv(indio_dev);
+	struct regmap *regmap = data->regmap;
+	struct device *dev = regmap_get_device(regmap);
+	unsigned int reg_val;
+	int i, it_inx;
+	int ret;
+
+	PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(dev, pm);
+	ret = PM_RUNTIME_ACQUIRE_ERR(&pm);
+	if (ret)
+		return ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		if (val != 0)
+			return -EINVAL;
+
+		for (i = 0; i < ARRAY_SIZE(veml3328_it_times); i++) {
+			if (veml3328_it_times[i][1] == val2)
+				break;
+		}

drivers/iio/light/veml3328.c line 271

Quoted 10 lines
drivers/iio/light/veml3328.c new file +10 -0
+
+		if (i == ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		return regmap_update_bits(regmap, VEML3328_REG_CONF,
+					  VEML3328_CONF_IT_MASK,
+					  FIELD_PREP(VEML3328_CONF_IT_MASK, i));
+
+	case IIO_CHAN_INFO_SCALE:
+		ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);

Hmm. The comment made by sashiko on this is also possibly correct.
The write to INT_TIME can race with the write to INFO_SCALE. Whilst that's not a normal use model (one thread would typically set up all the parameters) you could end up with picking a scale based on an int time that isn't the current one. I'd just throw a guard(mutex)(&data->lock) above this whole switch so we don't have to think about it. (and a mutex in data, plus initialization etc).

Quoted 28 lines
drivers/iio/light/veml3328.c new file +28 -0
+		if (ret)
+			return ret;
+
+		it_inx = FIELD_GET(VEML3328_CONF_IT_MASK, reg_val);
+		if (it_inx >= ARRAY_SIZE(veml3328_it_times))
+			return -EINVAL;
+
+		for (i = 0; i < 4; i++) {
+			if (val == veml3328_scale_vals[it_inx][i * 2] &&
+			    val2 == veml3328_scale_vals[it_inx][i * 2 + 1])
+				break;
+		}
+
+		if (i == 4)
+			return -EINVAL;
+
+		return regmap_update_bits(regmap, VEML3328_REG_CONF,
+					  VEML3328_CONF_GAIN_MASK,
+					  FIELD_PREP(VEML3328_CONF_GAIN_MASK, i));
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info veml3328_info = {
+	.read_raw = veml3328_read_raw,
+	.write_raw = veml3328_write_raw,

drivers/iio/light/veml3328.c line 350

Quoted 10 lines
drivers/iio/light/veml3328.c new file +9 -0
+	.read_avail = veml3328_read_avail,
+};
+
+static int veml3328_probe(struct i2c_client *client)
+{
 
+
+	pm_runtime_set_active(dev);
+	pm_runtime_set_autosuspend_delay(dev, 2000);
+	pm_runtime_use_autosuspend(dev);

As per other thread check this results in power down.
If it doesn't, try moving autosupend calls after devm_pm_runtime_enable() and see if that is enough. Otherwise, you can either manipulate the counters or I think just call pm_runtime_idle() to force it off immediately.

Quoted 7 lines
drivers/iio/light/veml3328.c new file +7 -0
+
+	ret = devm_pm_runtime_enable(dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
+
+	return devm_iio_device_register(dev, indio_dev);
+}
author maintainer
#8 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

Quoted 11 lines · Jonathan Cameron
Quoted 7 lines · On Sat, 30 May 2026 19:06:47 +0200 Joshua Crofts <[email protected]>…

Add support for the Vishay VEML3328 RGB/IR light sensor communicating via I2C (SMBus compatible).

Also add a new entry for said driver into Kconfig and Makefile.

Assisted-by Gemini:3.1-Pro
Quoted 7 lines

Hi Joshua,

I didn't find anything extras so this is just some more explanation for the Sashiko comments.

Give we are approaching the end of the cycle, feel free to do a new version quicker than normal if you are sure on what needs changing.

Considering these are relatively simple fixes, I'll probably send a v4 today (and hope that it will be the final iteration).

Quoted 10 lines
Quoted 1 line

+ case IIO_CHAN_INFO_SCALE:

Under the hood, like all the cleanup.h magic, this instantiates a local variable - the cleanup scope is tided to that and scope for switch statements is a funny thing - it's not per case, but rather the whole switch. Hence need to add {} to define the scope for this case block.

Otherwise you enter this switch via another path and the local variable is unassigned, but the __free() that cleans it up is set. Hence you get a use of undefined variable.

Okay, thanks for the clarification.

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     case IIO_CHAN_INFO_SCALE:
+             ret = regmap_read(regmap, VEML3328_REG_CONF, &reg_val);

Hmm. The comment made by sashiko on this is also possibly correct.
The write to INT_TIME can race with the write to INFO_SCALE. Whilst that's not a normal use model (one thread would typically set up all the parameters) you could end up with picking a scale based on an int time that isn't the current one. I'd just throw a guard(mutex)(&data->lock) above this whole switch so we don't have to think about it. (and a mutex in data, plus initialization etc).

Heh, my first iteration used guard, so we've come full circle :)

Quoted 9 lines

drivers/iio/light/veml3328.c line 350

Quoted 4 lines
drivers/iio/light/veml3328.c new file +4 -0
+
+     pm_runtime_set_active(dev);
+     pm_runtime_set_autosuspend_delay(dev, 2000);
+     pm_runtime_use_autosuspend(dev);

As per other thread check this results in power down.
If it doesn't, try moving autosupend calls after devm_pm_runtime_enable() and see if that is enough. Otherwise, you can either manipulate the counters or I think just call pm_runtime_idle() to force it off immediately.

Sure, that seems reasonable. Testing pm_runtime stuff on a Pi 4 is a bit more painful though, as the LTS kernel doesn't support the PM_RUNTIME_ACQUIRE macros - they were introduced in kernel version 6.19 (which I found out the hard way!)

Thanks

Signature · 3 lines
Kind regards

CJD
author maintainer
#9 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

In reply to Jonathan Cameron
Quoted 8 lines · Jonathan Cameron

drivers/iio/light/veml3328.c line 350

Quoted 3 lines
drivers/iio/light/veml3328.c new file +3 -0
+     pm_runtime_set_active(dev);
+     pm_runtime_set_autosuspend_delay(dev, 2000);
+     pm_runtime_use_autosuspend(dev);

As per other thread check this results in power down.
If it doesn't, try moving autosupend calls after devm_pm_runtime_enable() and see if that is enough. Otherwise, you can either manipulate the counters or I think just call pm_runtime_idle() to force it off immediately.

Just to confirm, I've tested this on the actual hardware and it did autosuspend after probe, so I'm going to say that Sashiko is a bit off for once.

Signature · 3 lines
Kind regards

CJD
maintainer
#10 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

Quoted 5 lines · On Sun, 31 May 2026 18:44:43 +0200 Joshua Crofts <[email protected]>…
Quoted 4 lines · Jonathan Cameron
Quoted 3 lines
drivers/iio/light/veml3328.c new file +3 -0
+     pm_runtime_set_active(dev);
+     pm_runtime_set_autosuspend_delay(dev, 2000);
+     pm_runtime_use_autosuspend(dev);  
Quoted 7 lines
Quoted 4 lines

As per other thread check this results in power down.
If it doesn't, try moving autosupend calls after devm_pm_runtime_enable() and see if that is enough. Otherwise, you can either manipulate the counters or I think just call pm_runtime_idle() to force it off immediately.

Just to confirm, I've tested this on the actual hardware and it did autosuspend after probe, so I'm going to say that Sashiko is a bit off for once.

If you have time to figure out the path that causes that, it would be excellent to have it recorded. I spent a few minutes the other day on this and couldn't find it. Maybe ask AI to figure it out :)

J

Quoted 1 line
author maintainer
#11 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

Quoted 4 lines · Jonathan Cameron

If you have time to figure out the path that causes that, it would be excellent to have it recorded. I spent a few minutes the other day on this and couldn't find it. Maybe ask AI to figure it out :)

Maybe, just maybe if I'm not lazy enough to do it :)

Signature · 3 lines
Kind regards

CJD
author maintainer
#12 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

Quoted 6 lines · Joshua Crofts
Quoted 4 lines · Jonathan Cameron

If you have time to figure out the path that causes that, it would be excellent to have it recorded. I spent a few minutes the other day on this and couldn't find it. Maybe ask AI to figure it out :)

Briefly analyzing this with AI (Gemini 3.1 Pro, just like Sashiko). I noticed that
in drivers/base/dd.c the specific driver probe function is called and then pm_request_idle() is called after it in _driver_probe_device(), which is probably
the reason the sensor is suspended after probe even though I didn't explicitly call
pm_runtime_put_autosuspend() (odd that this behaviour in the actual driver base).

To err on the side of caution, I'd add a pm_runtime_get_noresume() call before setting up autosuspend and a pm_runtime_mark_last_busy()/pm_runtime_get_autosupend()
combo before registering the device. Technically Sashiko is correct, this isn't explicitly done in the driver, nevertheless the driver base actually handles this
if pm_runtime is correctly set up and callbacks are defined.

Jonathan - up to you if you want to fix up the driver since it's already committed
or if I should send a patch (I guess it isn't that critical, but it keeps Sashiko quiet
in future patches).

Signature · 3 lines
Kind regards

CJD
maintainer
#13 lore raw

Re: [PATCH v3 2/2] iio: light: veml3328: add support for new device

Quoted 8 lines · On Tue, 2 Jun 2026 10:14:59 +0200 Joshua Crofts <[email protected]>…
Quoted 6 lines · Joshua Crofts
Quoted 4 lines · Jonathan Cameron

If you have time to figure out the path that causes that, it would be excellent to have it recorded. I spent a few minutes the other day on this and couldn't find it. Maybe ask AI to figure it out :)

Quoted 9 lines

Briefly analyzing this with AI (Gemini 3.1 Pro, just like Sashiko). I noticed that
in drivers/base/dd.c the specific driver probe function is called and then pm_request_idle() is called after it in _driver_probe_device(), which is probably
the reason the sensor is suspended after probe even though I didn't explicitly call
pm_runtime_put_autosuspend() (odd that this behaviour in the actual driver base).

It kind of makes sense as it's basically a now you are finished, should this be going to sleep call. I never thought of looking there.
I did some archaeology and via 3 refactors / bug fixes that call goes all the way back to the introduction of runtime PM 17 years ago.

Quoted 9 lines

To err on the side of caution, I'd add a pm_runtime_get_noresume() call before setting up autosuspend and a pm_runtime_mark_last_busy()/pm_runtime_get_autosupend()
combo before registering the device. Technically Sashiko is correct, this isn't explicitly done in the driver, nevertheless the driver base actually handles this
if pm_runtime is correctly set up and callbacks are defined.

Quoted 5 lines

Jonathan - up to you if you want to fix up the driver since it's already committed
or if I should send a patch (I guess it isn't that critical, but it keeps Sashiko quiet
in future patches).

It is fine as it stands - lets just keep replying to tell sashiko it is wrong until it learns :)

Arguably we should be ripping out some of the dance that is going on in current drivers so as to remove unnecessary complexity. I'm nervous to do that on anything we can't test.

Thanks for looking into this!

Jonathan

Quoted 1 line