iio: light: add support for veml6031x00 ALS series
Subsystems (4)
Javier Carrasco
maintainerv6 cover iio: light: add support for veml6031x00 ALS series
These ambient light sensors with I2C interface provide two light channels (ALS and IR), high/low threshold alarms with configurable persistence, and a data ready signal.
The devices covered by this driver have the same resolution, and they share most of their functionality. These are the differences between them (note that the x belongs to their names, and it is not a wildcard):
- Device ID: accessible via two 8-byte registers, different values for veml6031x00/veml6031x01 and veml60311x00/veml60311x01.
- I2C address: same grouping, 0x29 and 0x10 I2C addresses.
- AEC qualification: AEC-Q100 for veml6031x00/veml60311x00 and AEC-Q101 for veml6031x01/veml60311x01.
The alarms and the data ready signals share the interrupt pin, and an interrupt status register must be accessed to identify the source. Such multiplexing is not new in IIO, and I have followed existing examples for it. The persistence setting (own attribute) to trigger the alarms uses the pattern that has already been used for the veml6030.
The device configuration is in general documented in the datasheet and the application note. There is an exception, though: the activation of the "active force" mode that is required for the data ready signal must be carried out in two steps even though the affected bits are located in the same register: first ALS_AF (active force mode enable) must be set, and then ALS_TRIG (active force trigger setting) must be enabled. I have added a brief commentary in the code to explain this behavior, which has been confirmed by the manufacturer.
The datasheet specifies the scale and integration time for the ALS channel. Although both settings also affect the measured IR value, no transfer function, accuracy, or calibration is specified for converting the IR output into a physical quantity. The IR channel is therefore intended only as a qualitative indication of the infrared content of the incident light, rather than as a precision measurement channel. Consequently, the driver does not expose scale or integration time as IR-channel attributes.
The only functionality that has not been implemented yet is the x0.66 gain (and its x0.165 counterpart when PD_DIV=1), which makes the gts helpers less usable due to the conversions required. It is indeed an uncommon gain to use (there are x0.5 and x0.125 gains) with no known use-case at the moment that justifies making adjustments to the gts helpers or adding artificial conversions to make it work.
This driver has been tested with the four supported devices separately as well as in pairs where the I2C addresses don't overlap.
To: Jonathan Cameron <[email protected]>
To: Lars-Peter Clausen <[email protected]>
To: Rob Herring <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Conor Dooley <[email protected]>
To: David Lechner <[email protected]>
To: Nuno Sá <[email protected]>
To: Andy Shevchenko <[email protected]>
Changes in v6:
- [1/4] Fix dt-binding title.
- [2/4] Update headers as indicated by Andy and Uwe.
- [2/4] Macro to get selector from gain registers.
- [2/4] Use pm_runtime_get_noresume() (no devres).
- [2/4] Drop <linux/mod_devicetable.h>
- [2/4] Use regmap_update_bits() to set power state.
- [3/4] Add scan info for the non-irq variant.
- [3/4] Fix lock acquisition (direct mode and then scale_lock).
- [3/4] Zero-initialize scan buffer.
- [4/4] Store timestamp before calling iio_trigger_poll_nested().
- [4/4] Move mutex_init and regfield_init before setup_irq().
- [4/4] Add lockdep_assert_held() to veml6031x00_interrupt().
- [4/4] Avoid register read if a spurious interrupt arrives when the device is powered down.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- [2/4] Explicit inclusion of some missing used headers.
- [2/4] Define CONF0 as volatile to account for AF_TRIG (reset by hardware).
- [2/4] Drop integration time and scale for IR channel.
- [2/4] Use unsigned int instead of int where no negative values are possible.
- [2/4] Write a helper to update the gain and use it where needed.
- [2/4] Move i2c_get_match() closer to the data allocation.
- [2/4] Move iio dev initialization out of the runtime pm region.
- [2/4] Lower log level for unknown ID part.
- [2/4] Redefine gains as _INT_MILLI.
- [2/4] Drop pointer to the device in the private structure.
- [2/4] Fix some coding-style issues.
- [4/4] Add second iio_chan_spec to avoid exposing dead sysfs attributes if no irq is available.
- [4/4] Refactor goto calls in functions with guard().
- [3/4] Use IIO_DECLARE_BUFFER_WITH_TS() instead of a custom structure.
- [4/4] Use format instead of deprecated sign for the scan type.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- [1/4] Entry in alphabetical order for MAINTAINERS.
- [2/4] Fix style for device ID tables.
- [2/4] Add missing header mod_devicetable.h and move iio/sysfs.h to
[4/4] for the persistence attribute.
- [2/4] Move IIO_DEV_ACQUIRE_DIRECT_MODE to [3/4] where buffers are
added.
- [3/4] Use test_bit() to check the active scan mask.
- [2/4] Add mutex for operations on scattered register fields.
- [2/4] Add shutdown action after turning the device on.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Move veml6030 fixes to a separate patch stack.
- Use C99 initializers for i2c_device_id.
- Split driver code into multiple patches to ease its review.
- Rework locking to get rid of atomic increment/decrement ops.
- Fix error paths in pm_runtime operations.
- Use IIO_DEV_ACQUIRE_DIRECT_MODE for single read/write ops.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Add commit to fix bug in veml6030.c (channel type when pushing
events) and remove dead code.
- Use gts helpers to simplify operations.
- Drop unused gain_idx.
- Build INT_MASK as an OR operation of the involved bits.
- Format arrays to follow the desired standard for IIO.
- Directly return function result as the last operation within another function instead of 'ret = x; if (ret) return ret; return 0;'.
- Fix some spacing (double space, tab for alignemnt in info struct).
- Use sizeof() for __le16 reg instead of 2.
- Return an error if the part ID could not be read.
- Spell out sd -> shutdown.
- Use devm_mutex_init() instead of mutex_init().
- Avoid using conditional guard, use claim/release instead.
- Access integration times from the global array to get and set the integration time instead of using a switch.
- Simplify read of available periods (persistence).
- Drop IRQF_TRIGGER_FALLING in the threaded irq request.
- Add regmap ranges.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Javier Carrasco (4):
dt-bindings: iio: light: veml6030: add veml6031x00 ALS series
iio: light: add support for veml6031x00 ALS series
iio: light: veml6031x00: add support for triggered buffers
iio: light: veml6031x00: add support for events and trigger
.../bindings/iio/light/vishay,veml6030.yaml | 13 +- MAINTAINERS | 6 + drivers/iio/light/Kconfig | 14 + drivers/iio/light/Makefile | 1 + drivers/iio/light/veml6031x00.c | 1344 ++++++++++++++++++++ 5 files changed, 1377 insertions(+), 1 deletion(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20241109-veml6031x00-aa9463da064a
Best regards,
Signature · 1 line
Javier Carrasco <[email protected]>
Javier Carrasco
maintainerPATCH v6 1/4 dt-bindings: iio: light: veml6030: add veml6031x00 ALS series
These ambient light sensors share their properties with the ones from the same manufacturer that are supported by this bindings.
Note that only two datasheets are provided as every one of them covers two devices (veml6031x00/veml60311x00 and veml6031x01/veml60311x01).
--- .../devicetree/bindings/iio/light/vishay,veml6030.yaml | 13 ++++++++++++- MAINTAINERS | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +12 -1
@@ -4,7 +4,8 @@ $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: VEML3235, VEML3328, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS) +title: VEML3235, VEML3328, VEML6030, VEML6031x00 series, VEML6035 + and VEML7700 Ambient Light Sensors (ALS) maintainers: - Rishi Gupta <[email protected]>
@@ -23,6 +24,8 @@ description: | 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/80007/veml6031x00.pdf + https://www.vishay.com/docs/80008/veml6031x01.pdf https://www.vishay.com/docs/84889/veml6035.pdf https://www.vishay.com/docs/84286/veml7700.pdf
@@ -32,6 +35,10 @@ properties: - vishay,veml3235 - vishay,veml3328 - vishay,veml6030 + - vishay,veml6031x00 + - vishay,veml6031x01 + - vishay,veml60311x00 + - vishay,veml60311x01 - vishay,veml6035 - vishay,veml7700
@@ -69,6 +76,8 @@ allOf: properties: compatible: enum: + - vishay,veml6031x00 + - vishay,veml6031x01 - vishay,veml6035 then: properties:
@@ -82,6 +91,8 @@ allOf: enum: - vishay,veml3235 - vishay,veml3328 + - vishay,veml60311x00 + - vishay,veml60311x01 - vishay,veml7700 then: properties:
MAINTAINERS +5 -0
@@ -28761,6 +28761,11 @@ S: Maintained F: Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml F: drivers/iio/light/veml6030.c +VISHAY VEML6031X00 AMBIENT LIGHT SENSOR DRIVER +M: Javier Carrasco <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml + VISHAY VEML6046X00 RGBIR COLOR SENSOR DRIVER M: Andreas Klinger <[email protected]> S: Maintained
Signature · 1 line
2.43.0
Javier Carrasco
maintainerPATCH v6 2/4 iio: light: add support for veml6031x00 ALS series
In reply to Javier CarrascoThese sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.
This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.
--- MAINTAINERS | 1 + drivers/iio/light/Kconfig | 12 + drivers/iio/light/Makefile | 1 + drivers/iio/light/veml6031x00.c | 696 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 710 insertions(+)
MAINTAINERS +1 -0
@@ -28765,6 +28765,7 @@ VISHAY VEML6031X00 AMBIENT LIGHT SENSOR DRIVER M: Javier Carrasco <[email protected]> S: Maintained F: Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +F: drivers/iio/light/veml6031x00.c VISHAY VEML6046X00 RGBIR COLOR SENSOR DRIVER M: Andreas Klinger <[email protected]>
drivers/iio/light/Kconfig +12 -0
@@ -738,6 +738,18 @@ config VEML6030 To compile this driver as a module, choose M here: the module will be called veml6030. +config VEML6031X00 + tristate "VEML6031X00 ambient light sensor series" + select REGMAP_I2C + select IIO_GTS_HELPER + depends on I2C + help + Say Y here if you want to build a driver for the Vishay VEML6031X00 + ambient light sensor series. + + To compile this driver as a module, choose M here: the + module will be called veml6031x00. + config VEML6040 tristate "VEML6040 RGBW light sensor" select REGMAP_I2C
drivers/iio/light/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-$(CONFIG_VCNL4035) += vcnl4035.o obj-$(CONFIG_VEML3235) += veml3235.o obj-$(CONFIG_VEML3328) += veml3328.o obj-$(CONFIG_VEML6030) += veml6030.o +obj-$(CONFIG_VEML6031X00) += veml6031x00.o obj-$(CONFIG_VEML6040) += veml6040.o obj-$(CONFIG_VEML6046X00) += veml6046x00.o obj-$(CONFIG_VEML6070) += veml6070.o
drivers/iio/light/veml6031x00.c new file +696 -0
696 lines not on this page · raw message
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 10 lines · On Wed, 12 Aug 2026 22:27:41 +0200 Javier Carrasco…
These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.
This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.
Signed-off-by Javier Carrasco <[email protected]>
Hi Javier,
A few minor things inline
Thanks,
Jonathan
Quoted 14 lines
drivers/iio/light/veml6031x00.c new file +8
-0+ +static const struct iio_chan_spec veml6031x00_channels[] = { + { + .type =IIO_LIGHT , + .address =VEML6031X00_REG_ALS_L , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_INT_TIME ) | +BIT (IIO_CHAN_INFO_SCALE ),
drivers/iio/light/veml6031x00.c line 147
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ .info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME) | + BIT(IIO_CHAN_INFO_SCALE), + }, + { + .type = IIO_INTENSITY, + .address = VEML6031X00_REG_IR_L, + .modified = 1, + .channel2 = IIO_MOD_LIGHT_IR, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + }
Trailing comma. Maybe we'll add more after this and they will fit on one line. Yeah it's a stretch but generally always add comma unless it is a specific terminating entry.
Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +5
-0+}; +static int veml6031x00_validate_part_id (struct veml6031x00_data *data) +{ +struct device *dev =regmap_get_device (data->regmap); +int part_id, ret;
drivers/iio/light/veml6031x00.c line 498
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+__le16 regval; + + ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_ID_L , ®val, +sizeof (regval)); +if (ret) +return dev_err_probe (dev, ret,"Failed to read ID\n" ); + + part_id =le16_to_cpu (regval); +if (part_id != data->chip->part_id) +dev_info (dev,"Unknown ID %04x\n" , part_id);
This will trigger on unknown or unexpected ID (i.e one we know about but not the one firmware indicated we should expect). I'd rephrase to include that.
Quoted 61 lines
drivers/iio/light/veml6031x00.c new file +60
-0+ +return 0 ; +} +static int veml6031x00_probe (struct i2c_client *i2c) +{ +struct device *dev = &i2c->dev; +struct veml6031x00_data *data; +struct iio_dev *iio; +int ret; + + iio =devm_iio_device_alloc (dev,sizeof (*data)); +if (!iio) +return -ENOMEM ; + + data =iio_priv (iio); +i2c_set_clientdata (i2c, iio); + + data->chip =i2c_get_match_data (i2c); +if (!data->chip) +return dev_err_probe (dev, -EINVAL ,"Failed to get chip data\n" ); + + data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" ); + + iio->name = data->chip->name; + iio->channels = veml6031x00_channels; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels); + iio->modes =INDIO_DIRECT_MODE ; + iio->info = &veml6031x00_info; + + ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret; + + ret =veml6031x00_regfield_init (data); +if (ret) +return dev_err_probe (dev, ret,"Failed to init regfield\n" ); + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable regulator\n" ); + +/* The device starts in power down mode by default */ + ret =veml6031x00_set_power (data, true); +if (ret) +return dev_err_probe (dev, ret,"Failed to power on the device\n" ); + + ret =devm_add_action_or_reset (dev, veml6031x00_als_shutdown_action, data); +if (ret) +return dev_err_probe (dev, ret,"Failed to add shutdown action\n" ); + +pm_runtime_set_autosuspend_delay (dev,2000 ); +pm_runtime_use_autosuspend (dev); + ret =devm_pm_runtime_set_active_enabled (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable runtime PM\n" ); + +pm_runtime_get_noresume (dev);
drivers/iio/light/veml6031x00.c line 588
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ + ret =veml6031x00_validate_part_id (data); +if (ret) + goto err_pm_put; + + ret =veml6031x00_hw_init (iio); +if (ret) + goto err_pm_put; + +pm_runtime_put_autosuspend (dev);
This and the get_noresume() above smell like they are probably here to ensure a 1->0 transition for the runtime pm reference count and hence kick off the timer
for autosuspend. That shouldn't be necessary as there is code in the driver
core to try a suspend. Look for pm_request_idle() calls in drivers/base/dd.c
Note that we've had sashiko give what we have concluded in the past are false positives on this dance being required.
Given you have device to hand, just probe it and check if it suspends.
I haven't quite had the nerve to yet post a series removing this dance from drivers I can't test!
Quoted 11 lines
drivers/iio/light/veml6031x00.c new file +11
-0+ + ret =devm_iio_device_register (dev, iio); +if (ret) +return dev_err_probe (dev, ret,"Failed to register iio device\n" ); + +return 0 ; + +err_pm_put: +pm_runtime_put_noidle (dev); +return ret; +}
Jonathan Cameron
maintainerQuoted 41 lines · On Thu, 13 Aug 2026 02:04:29 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 10 lines · On Wed, 12 Aug 2026 22:27:41 +0200 Javier Carrasco…
These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.
This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.
Signed-off-by Javier Carrasco <[email protected]>Hi Javier,
A few minor things inline
Thanks,
Jonathan
Quoted 20 lines
drivers/iio/light/veml6031x00.c new file +14
-0+ +static const struct iio_chan_spec veml6031x00_channels[] = { + { + .type =IIO_LIGHT , + .address =VEML6031X00_REG_ALS_L , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_INT_TIME ) | +BIT (IIO_CHAN_INFO_SCALE ), + .info_mask_separate_available =BIT (IIO_CHAN_INFO_INT_TIME ) | +BIT (IIO_CHAN_INFO_SCALE ), + }, + { + .type =IIO_INTENSITY , + .address =VEML6031X00_REG_IR_L ,
Quoted 8 lines
drivers/iio/light/veml6031x00.c line 147
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ .modified = 1, + .channel2 = IIO_MOD_LIGHT_IR, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + }Trailing comma. Maybe we'll add more after this and they will fit on one line. Yeah it's a stretch but generally always add comma unless it is a specific terminating entry.
Ah. Not such a stretch. I'd forgotten timestamp channels. This creates unnecessary noise in the very next patch!
Quoted 3 lines
Quoted 2 lines
+};
Quoted 10 lines · Jonathan Cameron
Quoted 8 lines · On Thu, 13 Aug 2026 02:04:29 +0100 Jonathan Cameron <[email protected]> wrote:
Review
Quoted 1 line · On Wed, 12 Aug 2026 22:27:41 +0200 Javier Carrasco…
(quoted diff) +1
-0+ }Trailing comma. Maybe we'll add more after this and they will fit on one line. Yeah it's a stretch but generally always add comma unless it is a specific terminating entry.
Which is correct suggestion.
Quoted 2 lines
Ah. Not such a stretch. I'd forgotten timestamp channels. This creates unnecessary noise in the very next patch!
Exactly, so trailing comma here saves us from unneeded churn in the next patch.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerOn Thu Aug 13, 2026 at 3:04 AM CEST, Jonathan Cameron wrote:
...
Quoted 74 lines
Quoted 58 lines
drivers/iio/light/veml6031x00.c new file +57
-0+static int veml6031x00_probe (struct i2c_client *i2c) +{ +struct device *dev = &i2c->dev; +struct veml6031x00_data *data; +struct iio_dev *iio; +int ret; + + iio =devm_iio_device_alloc (dev,sizeof (*data)); +if (!iio) +return -ENOMEM ; + + data =iio_priv (iio); +i2c_set_clientdata (i2c, iio); + + data->chip =i2c_get_match_data (i2c); +if (!data->chip) +return dev_err_probe (dev, -EINVAL ,"Failed to get chip data\n" ); + + data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" ); + + iio->name = data->chip->name; + iio->channels = veml6031x00_channels; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels); + iio->modes =INDIO_DIRECT_MODE ; + iio->info = &veml6031x00_info; + + ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret; + + ret =veml6031x00_regfield_init (data); +if (ret) +return dev_err_probe (dev, ret,"Failed to init regfield\n" ); + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable regulator\n" ); + +/* The device starts in power down mode by default */ + ret =veml6031x00_set_power (data, true); +if (ret) +return dev_err_probe (dev, ret,"Failed to power on the device\n" ); + + ret =devm_add_action_or_reset (dev, veml6031x00_als_shutdown_action, data); +if (ret) +return dev_err_probe (dev, ret,"Failed to add shutdown action\n" ); + +pm_runtime_set_autosuspend_delay (dev,2000 ); +pm_runtime_use_autosuspend (dev); + ret =devm_pm_runtime_set_active_enabled (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable runtime PM\n" ); + +pm_runtime_get_noresume (dev);drivers/iio/light/veml6031x00.c line 588
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ + ret =veml6031x00_validate_part_id (data); +if (ret) + goto err_pm_put; + + ret =veml6031x00_hw_init (iio); +if (ret) + goto err_pm_put; + +pm_runtime_put_autosuspend (dev);This and the
get_noresume()above smell like they are probably here to ensure a 1->0 transition for the runtime pm reference count and hence kick off the timer
for autosuspend. That shouldn't be necessary as there is code in the driver
core to try a suspend. Look forpm_request_idle()calls indrivers/base/dd.c
Quoted 7 lines
Note that we've had sashiko give what we have concluded in the past are false positives on this dance being required.
Given you have device to hand, just probe it and check if it suspends.I haven't quite had the nerve to yet post a series removing this dance from drivers I can't test!
I tested that mechanism with several devices of this family, and it worked consistently. I will remove the calls to pm_runtime_get_noresume() and pm_runtime_put_autosuspend(), and with that all the goto jumps in the probe function. I still believe that they are absolutely fine when combined with devm_*() calls, but there is now a better reason to remove them.
I may have some more IIO devices to validate it with different hardware, but that's an experiment that will have to wait for a while... Anyway, right now this dance is suboptimal but harmless, isn't it?
Quoted 11 lines
drivers/iio/light/veml6031x00.c line 599
Quoted 11 lines
drivers/iio/light/veml6031x00.c new file +11
-0+ + ret =devm_iio_device_register (dev, iio); +if (ret) +return dev_err_probe (dev, ret,"Failed to register iio device\n" ); + +return 0 ; + +err_pm_put: +pm_runtime_put_noidle (dev); +return ret; +}
Best regards,
Javier
Quoted 8 lines · Javier Carrasco
These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.
This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.
This paragraph needs to be rephrased. In the current form it suits cover letter and not the commit message. Here, just list the features supported.
The "the subsequent patches of the series." in the commit message is very ambiguous. What patch series? Which patches? Are they landed in the upstream? If yes, which commit IDs? If not, when if ever? Et cetera! Usually it can be simply said "The other features may be implemented later on."
The basic hint (one of) when writing a commit message is to put yourself in the shoes of the unprepared reader who sees it in the Git history for the first time in their life. What will your message give to them?
Besides the above some comments in the ->probe() to be addressed and other minor style issues all over, but in general this looks very good and on track (for v7.4).
...
drivers/iio/light/veml6031x00.c line 111
Quoted 11 lines
drivers/iio/light/veml6031x00.c new file +11
-0+/* +* The shutdown bits (SD and ALS_IR_SD) are in different registers, and both +* must be updated when changing the device power state. +*/ +static int veml6031x00_set_power (struct veml6031x00_data *data,bool state) +{ +int ret; + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_SD , + state ?0 :VEML6031X00_CONF0_SD );
_assign_bits()?
drivers/iio/light/veml6031x00.c line 117
Quoted 6 lines
drivers/iio/light/veml6031x00.c new file +6
-0+if (ret) +return ret; + +return regmap_update_bits (data->regmap,VEML6031X00_REG_CONF1 , +VEML6031X00_CONF1_IR_SD , + state ?0 :VEML6031X00_CONF1_IR_SD );
Ditto.
Quoted 1 line
+}
...
Quoted 23 lines
drivers/iio/light/veml6031x00.c new file +23
-0+static int veml6031x00_set_it (struct iio_dev *iio,int val,int val2) +{ +struct veml6031x00_data *data =iio_priv (iio); +int ret, gain_sel, new_gain, prev_gain, prev_it; +unsigned int gain_reg, it_idx, pd_div4; +bool gain_in_range; + +if (val || !iio_gts_valid_time (&data->gts, val2)) +return -EINVAL ; + +guard (mutex)(&data->scale_lock); + + ret =regmap_field_read (data->rf.it, &it_idx); +if (ret) +return ret; + + ret =regmap_field_read (data->rf.gain, &gain_reg); +if (ret) +return ret; + + ret =regmap_field_read (data->rf.pd_div4, &pd_div4); +if (ret) +return ret;
drivers/iio/light/veml6031x00.c line 292
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ + prev_it =iio_gts_find_int_time_by_sel (&data->gts, it_idx); +if (prev_it <0 ) +return prev_it; + +if (prev_it == val2) +return 0 ; + + prev_gain =iio_gts_find_gain_by_sel (&data->gts, +VEML6031X00_GAIN_SEL (pd_div4, gain_reg));
What about temporary for &data->gts? Say you have local 'gts' variable, this becomes
prev_gain = iio_gts_find_gain_by_sel(gts,VEML6031X00_GAIN_SEL(pd_div4, gain_reg));
drivers/iio/light/veml6031x00.c line 297
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5
-0+if (prev_gain <0 ) +return prev_gain; + + ret =iio_gts_find_new_gain_by_gain_time_min (&data->gts, prev_gain, prev_it, + val2, &new_gain, &gain_in_range);
ret = iio_gts_find_new_gain_by_gain_time_min(gts, prev_gain, prev_it, val2, &new_gain, &gain_in_range);
Quoted 18 lines
drivers/iio/light/veml6031x00.c new file +18
-0+if (ret) +return ret; + +if (!gain_in_range) +dev_dbg (regmap_get_device (data->regmap),"Optimal gain out of range\n" ); + + ret =iio_gts_find_sel_by_int_time (&data->gts, val2); +if (ret <0 ) +return ret; + + ret =regmap_field_write (data->rf.it, ret); +if (ret) +return ret; + + gain_sel =iio_gts_find_sel_by_gain (&data->gts, new_gain); +if (gain_sel <0 ) +return gain_sel; +
drivers/iio/light/veml6031x00.c line 325
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+return veml6031x00_write_gain (data, gain_sel); +} + +static int veml6031x00_set_scale (struct iio_dev *iio,int val,int val2) +{ +struct veml6031x00_data *data =iio_priv (iio); +int gain_sel, it_sel, ret; + + ret =iio_gts_find_gain_time_sel_for_scale (&data->gts, val, val2, + &gain_sel, &it_sel);
In the similar way
ret = iio_gts_find_gain_time_sel_for_scale(gts, val, val2, &gain_sel, &it_sel);
OR (for stricter limits)
ret = iio_gts_find_gain_time_sel_for_scale(gts, val, val2, &gain_sel, &it_sel);
Quoted 11 lines
drivers/iio/light/veml6031x00.c new file +11
-0+if (ret) +return ret; + +guard (mutex)(&data->scale_lock); + + ret =regmap_field_write (data->rf.it, it_sel); +if (ret) +return ret; + +return veml6031x00_write_gain (data, gain_sel); +}
...
drivers/iio/light/veml6031x00.c line 339
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+static int veml6031x00_get_scale (struct veml6031x00_data *data,int *val, +int *val2)
Split this logically
static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val, int *val2)
OR
static int veml6031x00_get_scale(struct veml6031x00_data *data,
int *val, int *val2)
drivers/iio/light/veml6031x00.c line 488
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+static int veml6031x00_validate_part_id (struct veml6031x00_data *data) +{ +struct device *dev =regmap_get_device (data->regmap); +int part_id, ret;
Why is 'part_id' signed?
drivers/iio/light/veml6031x00.c line 492
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +3
-0+__le16 regval; + ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_ID_L , ®val, +sizeof (regval));
With
struct regmap *map = data->regmap; struct device *dev = regmap_get_device(map); unsigned int part_id; __le16 val; int ret; ... ret = regmap_bulk_read(map, VEML6031X00_REG_ID_L, &val, sizeof(val));
Please, revisit the whole series for this type of improvements.
Quoted 9 lines
drivers/iio/light/veml6031x00.c new file +9
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to read ID\n" ); + + part_id =le16_to_cpu (regval); +if (part_id != data->chip->part_id) +dev_info (dev,"Unknown ID %04x\n" , part_id); + +return 0 ; +}
...
Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7
-0+static int veml6031x00_probe (struct i2c_client *i2c) +{ +struct device *dev = &i2c->dev; +struct veml6031x00_data *data; +struct iio_dev *iio; +int ret; +
drivers/iio/light/veml6031x00.c line 538
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ iio =devm_iio_device_alloc (dev,sizeof (*data)); +if (!iio) +return -ENOMEM ; + + data =iio_priv (iio); +i2c_set_clientdata (i2c, iio); + + data->chip =i2c_get_match_data (i2c); +if (!data->chip) +return dev_err_probe (dev, -EINVAL ,"Failed to get chip data\n" );
-ENODATA
drivers/iio/light/veml6031x00.c line 543
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );
Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Quoted 43 lines
drivers/iio/light/veml6031x00.c new file +43
-0+ iio->name = data->chip->name; + iio->channels = veml6031x00_channels; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels); + iio->modes =INDIO_DIRECT_MODE ; + iio->info = &veml6031x00_info; + + ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret; + + ret =veml6031x00_regfield_init (data); +if (ret) +return dev_err_probe (dev, ret,"Failed to init regfield\n" ); + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable regulator\n" ); + +/* The device starts in power down mode by default */ + ret =veml6031x00_set_power (data, true); +if (ret) +return dev_err_probe (dev, ret,"Failed to power on the device\n" ); + + ret =devm_add_action_or_reset (dev, veml6031x00_als_shutdown_action, data); +if (ret) +return dev_err_probe (dev, ret,"Failed to add shutdown action\n" ); + +pm_runtime_set_autosuspend_delay (dev,2000 ); +pm_runtime_use_autosuspend (dev); + ret =devm_pm_runtime_set_active_enabled (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable runtime PM\n" ); + +pm_runtime_get_noresume (dev); + + ret =veml6031x00_validate_part_id (data); +if (ret) + goto err_pm_put; + + ret =veml6031x00_hw_init (iio); +if (ret) + goto err_pm_put; +
drivers/iio/light/veml6031x00.c line 597
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +9
-0+pm_runtime_put_autosuspend (dev); + + ret =devm_iio_device_register (dev, iio); +if (ret) +return dev_err_probe (dev, ret,"Failed to register iio device\n" ); + +return 0 ; +err_pm_put: +pm_runtime_put_noidle (dev);
Hmm... This is usually a red flag to see a goto after devm_*() calls.
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerHello Andy, thank you again for your thorough review.
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Javier Carrasco
These sensors provide two light channels (ALS and IR), I2C communication and a multiplexed interrupt line to signal data ready and configurable threshold alarms.
This first implementation provides basic functionality (measurement configuration, raw reads and ID validation) and defines the different register regions in preparation for extended features in the subsequent patches of the series.
Quoted 8 lines
This paragraph needs to be rephrased. In the current form it suits cover letter and not the commit message. Here, just list the features supported.
The "the subsequent patches of the series." in the commit message is very ambiguous. What patch series? Which patches? Are they landed in the upstream? If yes, which commit IDs? If not, when if ever? Et cetera! Usually it can be simply said "The other features may be implemented later on."
This commit message will be re-worked. Up to v3 the driver was sent as a single patch, and as I split it, it stayed a bit too dependent of what I was sending as separate patches. But there is no real need for it.
...
Quoted 9 lines
drivers/iio/light/veml6031x00.c line 543
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Could you please explain what you are trying to avoid? Even if the debugfs is already enabled for regmap at this point, what is the possible race condition? The IIO device is still not registered at this point, and the registers are set to their right values in _hw_init() after the mutexes were initialized. Moving the mutex initialization a couple of lines towards the top is not an issue, but I would like to understand the reasoning behind.
Quoted 51 lines
Quoted 50 lines
drivers/iio/light/veml6031x00.c new file +50
-0+ iio->name = data->chip->name; + iio->channels = veml6031x00_channels; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels); + iio->modes =INDIO_DIRECT_MODE ; + iio->info = &veml6031x00_info; + + ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret; + + ret =veml6031x00_regfield_init (data); +if (ret) +return dev_err_probe (dev, ret,"Failed to init regfield\n" ); + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable regulator\n" ); + +/* The device starts in power down mode by default */ + ret =veml6031x00_set_power (data, true); +if (ret) +return dev_err_probe (dev, ret,"Failed to power on the device\n" ); + + ret =devm_add_action_or_reset (dev, veml6031x00_als_shutdown_action, data); +if (ret) +return dev_err_probe (dev, ret,"Failed to add shutdown action\n" ); + +pm_runtime_set_autosuspend_delay (dev,2000 ); +pm_runtime_use_autosuspend (dev); + ret =devm_pm_runtime_set_active_enabled (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable runtime PM\n" ); + +pm_runtime_get_noresume (dev); + + ret =veml6031x00_validate_part_id (data); +if (ret) + goto err_pm_put; + + ret =veml6031x00_hw_init (iio); +if (ret) + goto err_pm_put; + +pm_runtime_put_autosuspend (dev); + + ret =devm_iio_device_register (dev, iio); +if (ret) +return dev_err_probe (dev, ret,"Failed to register iio device\n" ); + +return 0 ;
Quoted 5 lines
drivers/iio/light/veml6031x00.c line 597
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+err_pm_put: +pm_runtime_put_noidle (dev);Hmm... This is usually a red flag to see a goto after devm_*() calls.
Could you please elaborate on this? I am aware of the dangers of a goto after the cleanup attribute, but devm_*() calls work on a different scope, not local to the function. What could go wrong? This goto is just a way not to repeat pm_runtime_put_noidle(); return ret; and I am not strongly against repeating these two lines wherever there is a goto, but I am not sure what we are avoiding in this case.
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}
Best regards,
Javier
Quoted 3 lines · Javier Carrasco
Hello Andy, thank you again for your thorough review.
Quoted 1 line · Andy Shevchenko
On Wed, Aug 12, 2026 at 10:27:41PM +0200, Javier Carrasco wrote:
...
Quoted 11 lines
Quoted 7 lines
drivers/iio/light/veml6031x00.c line 543
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Could you please explain what you are trying to avoid? Even if the debugfs is already enabled for regmap at this point, what is the possible race condition?
That's my question to you. If you think there is none, then we are all good. Basically the Q is if that mutex (that is initialised later in the probe) is required for the device IO which might be possible immediately when regmap is registered.
Quoted 5 lines
The IIO device is still not registered at this
point, and the registers are set to their right values in_hw_init()after the mutexes were initialized. Moving the mutex initialization a couple of lines towards the top is not an issue, but I would like to understand the reasoning behind.
User may read (and depending on the driver case writing) the register map. Would it be a problem?
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+ ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret;
...
Quoted 11 lines
Quoted 4 lines
drivers/iio/light/veml6031x00.c line 597
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+err_pm_put: +pm_runtime_put_noidle (dev);Hmm... This is usually a red flag to see a goto after devm_*() calls.
Could you please elaborate on this? I am aware of the dangers of a goto after the cleanup attribute, but devm_*() calls work on a different scope, not local to the function. What could go wrong? This goto is just a way not to repeat
pm_runtime_put_noidle(); return ret; and I am not strongly against repeating these two lines wherever there is a goto, but I am not sure what we are avoiding in this case.
No, the goto here may (or may not) lead to the wrong ordering on the cleanup and/or error paths. It's usually recommended to avoid such a style even if it works without races in that case. That's why when switching to managed resources it's highly recommended to wrap everything to be managed (in case there is no native APIs available) and avoid any flow disruption (goto:s).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerQuoted 24 lines · Andy Shevchenko
Quoted 3 lines · Javier Carrasco
Hello Andy, thank you again for your thorough review.
Quoted 1 line · Andy Shevchenko
On Wed, Aug 12, 2026 at 10:27:41PM +0200, Javier Carrasco wrote:
...
Quoted 11 lines
Quoted 7 lines
drivers/iio/light/veml6031x00.c line 543
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Could you please explain what you are trying to avoid? Even if the debugfs is already enabled for regmap at this point, what is the possible race condition?
That's my question to you. If you think there is none, then we are all good. Basically the Q is if that mutex (that is initialised later in the probe) is required for the device IO which might be possible immediately when regmap is registered.
Quoted 9 lines
Quoted 5 lines
The IIO device is still not registered at this
point, and the registers are set to their right values in_hw_init()after the mutexes were initialized. Moving the mutex initialization a couple of lines towards the top is not an issue, but I would like to understand the reasoning behind.User may read (and depending on the driver case writing) the register map. Would it be a problem?
That is not a problem, as no assumptions about the original values are made.
Quoted 18 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+ ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret;...
Quoted 11 lines
Quoted 4 lines
drivers/iio/light/veml6031x00.c line 597
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+err_pm_put: +pm_runtime_put_noidle (dev);Hmm... This is usually a red flag to see a goto after devm_*() calls.
Could you please elaborate on this? I am aware of the dangers of a goto after the cleanup attribute, but devm_*() calls work on a different scope, not local to the function. What could go wrong? This goto is just a way not to repeat
pm_runtime_put_noidle(); return ret; and I am not strongly against repeating these two lines wherever there is a goto, but I am not sure what we are avoiding in this case.
Quoted 5 lines
No, the goto here may (or may not) lead to the wrong ordering on the cleanup and/or error paths. It's usually recommended to avoid such a style even if it works without races in that case. That's why when switching to managed resources it's highly recommended to wrap everything to be managed (in case there is no native APIs available) and avoid any flow disruption (goto:s).
In this case I can't see any issue with the goto. It only leads to a call to pm_runtime_put_noidle(), and after returning 'ret', the device-managed resources are released. If the goto is removed and a call to pm_runtime_put_noidle() is carried out in every error path, the result is exactly the same as there are no operations within the probe execution path that affect their behavior.
I can imagine that it might be problematic in some special cases, but the use of devm_*() and goto jumps within a function is allowed and used in the official documentation [1], and in several upstream drivers in IIO [2][3] and other subsystems [4]. Of course, all those examples might be bad examples, but I could not find any reference discouraging the use of goto with devm_*().
[1] https://docs.kernel.org/driver-api/usb/writing_musb_glue_layer.html
[2] drivers/iio/accel/kxcjk-1013.c
[3] drivers/iio/pressure/hid-sensor-press.c
[4] drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
Best regards,
Javier
Quoted 5 lines · Javier Carrasco
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Javier Carrasco
Hello Andy, thank you again for your thorough review.
Quoted 1 line · Andy Shevchenko
On Wed, Aug 12, 2026 at 10:27:41PM +0200, Javier Carrasco wrote:
...
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
Quoted 7 lines
drivers/iio/light/veml6031x00.c line 543
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Could you please explain what you are trying to avoid? Even if the debugfs is already enabled for regmap at this point, what is the possible race condition?
That's my question to you. If you think there is none, then we are all good. Basically the Q is if that mutex (that is initialised later in the probe) is required for the device IO which might be possible immediately when regmap is registered.
Quoted 5 lines
The IIO device is still not registered at this
point, and the registers are set to their right values in_hw_init()after the mutexes were initialized. Moving the mutex initialization a couple of lines towards the top is not an issue, but I would like to understand the reasoning behind.
Quoted 5 lines
Quoted 2 lines
User may read (and depending on the driver case writing) the register map. Would it be a problem?
That is not a problem, as no assumptions about the original values are made.
It's not about the values, it's about the *access* HW during that time when there is no mutex available yet.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+ ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret;
...
Quoted 18 lines
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines
drivers/iio/light/veml6031x00.c line 597
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+err_pm_put: +pm_runtime_put_noidle (dev);Hmm... This is usually a red flag to see a goto after devm_*() calls.
Could you please elaborate on this? I am aware of the dangers of a goto after the cleanup attribute, but devm_*() calls work on a different scope, not local to the function. What could go wrong? This goto is just a way not to repeat
pm_runtime_put_noidle(); return ret; and I am not strongly against repeating these two lines wherever there is a goto, but I am not sure what we are avoiding in this case.Quoted 5 lines
No, the goto here may (or may not) lead to the wrong ordering on the cleanup and/or error paths. It's usually recommended to avoid such a style even if it works without races in that case. That's why when switching to managed resources it's highly recommended to wrap everything to be managed (in case there is no native APIs available) and avoid any flow disruption (goto:s).
Quoted 6 lines
In this case I can't see any issue with the goto. It only leads to a call to
pm_runtime_put_noidle(), and after returning 'ret', the device-managed resources are released. If the goto is removed and a call topm_runtime_put_noidle()is carried out in every error path, the result is exactly the same as there are no operations within the probe execution path that affect their behavior.
Right, but again "It's usually recommended to avoid such a style...".
Quoted 8 lines
I can imagine that it might be problematic in some special cases, but the use of devm_*() and goto jumps within a function is allowed and used in the official documentation [1], and in several upstream drivers in IIO [2][3] and other subsystems [4]. Of course, all those examples might be bad examples, but I could not find any reference discouraging the use of goto with devm_*().
[1] https://docs.kernel.org/driver-api/usb/writing_musb_glue_layer.html
It's not kernel-wide documentation. At a brief glance it has more issues than just that one. I would not recommend to refer and use that piece of the documentation without fixing it first.
Quoted 3 lines
[2]
drivers/iio/accel/kxcjk-1013.c
[3]drivers/iio/pressure/hid-sensor-press.c
[4]drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerQuoted 32 lines · Andy Shevchenko
Quoted 5 lines · Javier Carrasco
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Javier Carrasco
Hello Andy, thank you again for your thorough review.
Quoted 1 line · Andy Shevchenko
On Wed, Aug 12, 2026 at 10:27:41PM +0200, Javier Carrasco wrote:
...
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
Quoted 7 lines
drivers/iio/light/veml6031x00.c line 543
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Could you please explain what you are trying to avoid? Even if the debugfs is already enabled for regmap at this point, what is the possible race condition?
That's my question to you. If you think there is none, then we are all good. Basically the Q is if that mutex (that is initialised later in the probe) is required for the device IO which might be possible immediately when regmap is registered.
Quoted 5 lines
The IIO device is still not registered at this
point, and the registers are set to their right values in_hw_init()after the mutexes were initialized. Moving the mutex initialization a couple of lines towards the top is not an issue, but I would like to understand the reasoning behind.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
User may read (and depending on the driver case writing) the register map. Would it be a problem?
That is not a problem, as no assumptions about the original values are made.
It's not about the values, it's about the *access* HW during that time when there is no mutex available yet.
There is no issue with that either. The mutex serializes the access to multiple registers that store the scale to provide a consistent value that can't be manipulated by other accesses (e.g. debugfs). That does not have any meaning as long as the IIO device has been registered, which of course happens after the mutex initialization.
Best regards,
Javier
Jonathan Cameron
maintainerQuoted 81 lines · On Fri, 14 Aug 2026 12:07:03 +0300 Andy Shevchenko…
Quoted 5 lines · Javier Carrasco
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Javier Carrasco
Hello Andy, thank you again for your thorough review.
Quoted 1 line · Andy Shevchenko
On Wed, Aug 12, 2026 at 10:27:41PM +0200, Javier Carrasco wrote:
...
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
Quoted 7 lines
drivers/iio/light/veml6031x00.c line 543
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ data->regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to set regmap\n" );Is debugfs access already enabled for regmap after this call? Perhaps you want mutex to be initialised before that?
Could you please explain what you are trying to avoid? Even if the debugfs is already enabled for regmap at this point, what is the possible race condition?
That's my question to you. If you think there is none, then we are all good. Basically the Q is if that mutex (that is initialised later in the probe) is required for the device IO which might be possible immediately when regmap is registered.
Quoted 5 lines
The IIO device is still not registered at this
point, and the registers are set to their right values in_hw_init()after the mutexes were initialized. Moving the mutex initialization a couple of lines towards the top is not an issue, but I would like to understand the reasoning behind.Quoted 5 lines
Quoted 2 lines
User may read (and depending on the driver case writing) the register map. Would it be a problem?
That is not a problem, as no assumptions about the original values are made.
It's not about the values, it's about the *access* HW during that time when there is no mutex available yet.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+ ret =devm_mutex_init (dev, &data->scale_lock); +if (ret) +return ret;...
Quoted 18 lines
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines
drivers/iio/light/veml6031x00.c line 597
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+err_pm_put: +pm_runtime_put_noidle (dev);Hmm... This is usually a red flag to see a goto after devm_*() calls.
Could you please elaborate on this? I am aware of the dangers of a goto after the cleanup attribute, but devm_*() calls work on a different scope, not local to the function. What could go wrong? This goto is just a way not to repeat
pm_runtime_put_noidle(); return ret; and I am not strongly against repeating these two lines wherever there is a goto, but I am not sure what we are avoiding in this case.Quoted 5 lines
No, the goto here may (or may not) lead to the wrong ordering on the cleanup and/or error paths. It's usually recommended to avoid such a style even if it works without races in that case. That's why when switching to managed resources it's highly recommended to wrap everything to be managed (in case there is no native APIs available) and avoid any flow disruption (goto:s).
Quoted 6 lines
In this case I can't see any issue with the goto. It only leads to a call to
pm_runtime_put_noidle(), and after returning 'ret', the device-managed resources are released. If the goto is removed and a call topm_runtime_put_noidle()is carried out in every error path, the result is exactly the same as there are no operations within the probe execution path that affect their behavior.Right, but again "It's usually recommended to avoid such a style...".
Quoted 6 lines
I can imagine that it might be problematic in some special cases, but the use of devm_*() and goto jumps within a function is allowed and used in the official documentation [1], and in several upstream drivers in IIO [2][3] and other subsystems [4]. Of course, all those examples might be bad examples, but I could not find any reference discouraging the use of goto with devm_*().
Quoted 5 lines
Quoted 1 line
[1] https://docs.kernel.org/driver-api/usb/writing_musb_glue_layer.html
It's not kernel-wide documentation. At a brief glance it has more issues than just that one. I would not recommend to refer and use that piece of the documentation without fixing it first.
FWIW I think the runtime pm handling under the goto is unnecessary anyway (as we don't need to grab a reference anyway) - so with that gone this whole debate becomes moot. (Yes I did put moot in for my own amusement)
J
Quoted 5 lines
Quoted 4 lines
[2]
drivers/iio/accel/kxcjk-1013.c
[3]drivers/iio/pressure/hid-sensor-press.c
[4]drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
Javier Carrasco
maintainerPATCH v6 3/4 iio: light: veml6031x00: add support for triggered buffers
In reply to Javier CarrascoAdd triggered buffer functionality for the two channels the device provides (ALS and IR).
--- drivers/iio/light/Kconfig | 2 + drivers/iio/light/veml6031x00.c | 117 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 1 deletion(-)
drivers/iio/light/Kconfig +2 -0
@@ -742,6 +742,8 @@ config VEML6031X00 tristate "VEML6031X00 ambient light sensor series" select REGMAP_I2C select IIO_GTS_HELPER + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER depends on I2C help Say Y here if you want to build a driver for the Vishay VEML6031X00
drivers/iio/light/veml6031x00.c +116 -1
171 lines not on this page · raw message
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH v6 3/4] iio: light: veml6031x00: add support for triggered buffers
Quoted 4 lines · On Wed, 12 Aug 2026 22:27:42 +0200 Javier Carrasco…
Add triggered buffer functionality for the two channels the device provides (ALS and IR).
Signed-off-by Javier Carrasco <[email protected]>
A very trivial comment inline. Feel free to ignore!
Quoted 11 lines
drivers/iio/light/veml6031x00.c +6
-0+ +static irqreturn_t veml6031x00_trig_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *iio = pf->indio_dev; +struct veml6031x00_data *data =iio_priv (iio);
drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 571
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+ IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2) = { }; +unsigned int i =0 ; +int ch, ret; + +if (test_bit (VEML6031X00_SCAN_ALS , iio->active_scan_mask) && +test_bit (VEML6031X00_SCAN_IR , iio->active_scan_mask)) { + ret =regmap_bulk_read (data->regmap, +VEML6031X00_REG_ALS_L , + scan, +2 *sizeof (*scan));
I don't care that much, but you could combine parameters on one line in a few more places to reduced the code length a little.
e.g.
I'd be tempted to put the two lines above on one line or even
ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_ALS_L, scan, 2 * sizeof(*scan));
Quoted 20 lines
drivers/iio/light/veml6031x00.c +20
-0+if (ret) + goto done; + }else { +iio_for_each_active_channel (iio, ch) { + ret =regmap_bulk_read (data->regmap, + iio->channels[ch].address, + &scan[i++], +sizeof (*scan)); +if (ret) + goto done; + } + } + +iio_push_to_buffers_with_ts (iio, scan,sizeof (scan), pf->timestamp); + +done: +iio_trigger_notify_done (iio->trig); + +return IRQ_HANDLED ; +}
Re: [PATCH v6 3/4] iio: light: veml6031x00: add support for triggered buffers
Quoted 2 lines · Jonathan Cameron
On Wed, 12 Aug 2026 22:27:42 +0200
Javier Carrasco <[email protected]> wrote:
...
Quoted 20 lines
Quoted 5 lines
drivers/iio/light/veml6031x00.c +5
-0+static irqreturn_t veml6031x00_trig_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *iio = pf->indio_dev; +struct veml6031x00_data *data =iio_priv (iio);drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 571
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+ IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2) = { }; +unsigned int i =0 ; +int ch, ret; + +if (test_bit (VEML6031X00_SCAN_ALS , iio->active_scan_mask) && +test_bit (VEML6031X00_SCAN_IR , iio->active_scan_mask)) { + ret =regmap_bulk_read (data->regmap, +VEML6031X00_REG_ALS_L , + scan, +2 *sizeof (*scan));I don't care that much, but you could combine parameters on one line in a few more places to reduced the code length a little.
e.g.
I'd be tempted to put the two lines above on one line or even
Quoted 2 lines
ret = regmap_bulk_read(data->regmap, VEML6031X00_REG_ALS_L, scan, 2 * sizeof(*scan));
I usually suggest to combine that with
struct regmap *map = data->regmap;
so it will become
ret = regmap_bulk_read(map, VEML6031X00_REG_ALS_L, scan, 2 * sizeof(*scan));
Quoted 8 lines
drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 579
Quoted 8 lines
drivers/iio/light/veml6031x00.c +8
-0+if (ret) + goto done; + }else { +iio_for_each_active_channel (iio, ch) { + ret =regmap_bulk_read (data->regmap, + iio->channels[ch].address, + &scan[i++], +sizeof (*scan));
And here it will help
ret = regmap_bulk_read(map, iio->channels[ch].address, &scan[i++], sizeof(*scan));
Quoted 3 lines
drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 582
Quoted 3 lines
drivers/iio/light/veml6031x00.c +3
-0+if (ret) + goto done; + }
But I would even go with
ret = regmap_bulk_read(map, iio->channels[ch].address, &scan[i], sizeof(scan[i])); if (ret) goto done; i++;
Quoted 9 lines
Quoted 9 lines
drivers/iio/light/veml6031x00.c +9
-0+ } + +iio_push_to_buffers_with_ts (iio, scan,sizeof (scan), pf->timestamp); + +done: +iio_trigger_notify_done (iio->trig); + +return IRQ_HANDLED ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerPATCH v6 4/4 iio: light: veml6031x00: add support for events and trigger
In reply to Javier CarrascoThe device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.
--- drivers/iio/light/veml6031x00.c | 545 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 539 insertions(+), 6 deletions(-)
drivers/iio/light/veml6031x00.c +539 -6
@@ -25,6 +25,9 @@#include <asm/byteorder.h> #include <linux/iio/iio.h> +#include <linux/iio/events.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger.h> #include <linux/iio/iio-gts-helper.h> #include <linux/iio/triggered_buffer.h> #include <linux/iio/trigger_consumer.h>
@@ -32,16 +35,28 @@/* Device registers */ #define VEML6031X00_REG_CONF0 0x00#define VEML6031X00_REG_CONF1 0x01 +#define VEML6031X00_REG_WH_L 0x04 +#define VEML6031X00_REG_WH_H 0x05 +#define VEML6031X00_REG_WL_L 0x06 +#define VEML6031X00_REG_WL_H 0x07#define VEML6031X00_REG_ALS_L 0x10#define VEML6031X00_REG_ALS_H 0x11#define VEML6031X00_REG_IR_L 0x12#define VEML6031X00_REG_IR_H 0x13#define VEML6031X00_REG_ID_L 0x14#define VEML6031X00_REG_ID_H 0x15 +#define VEML6031X00_REG_INT 0x17/* Bit masks for specific functionality */ #define VEML6031X00_CONF0_SD BIT(0) +#define VEML6031X00_CONF0_AF_TRIG BIT(2) +#define VEML6031X00_CONF0_AF BIT(3)#define VEML6031X00_CONF1_IR_SD BIT(7) +#define VEML6031X00_INT_TH_H BIT(1) +#define VEML6031X00_INT_TH_L BIT(2) +#define VEML6031X00_INT_DRDY BIT(3) +#define VEML6031X00_INT_MASK \ + (VEML6031X00_INT_TH_L | VEML6031X00_INT_TH_H | VEML6031X00_INT_DRDY)#define VEML6031X00_GAIN_SEL (pd_div4, gain) (((pd_div4) << 2) | (gain))
@@ -53,8 +68,10 @@ enum veml6031x00_scan {struct veml6031x00_rf {struct regmap_field *gain; +struct regmap_field *int_en;struct regmap_field *it;struct regmap_field *pd_div4; +struct regmap_field *pers; };struct veml6031x00_chip {
@@ -65,6 +82,7 @@ struct veml6031x00_chip {struct veml6031x00_data {struct iio_gts gts;struct regmap *regmap; +struct iio_trigger *trig;struct veml6031x00_rf rf;const struct veml6031x00_chip *chip;/*
@@ -73,6 +91,14 @@ struct veml6031x00_data {* consistent set. */ struct mutex scale_lock; +/* +* Serialize access to irq enable/disable by events and trigger +* (shared line). +*/ +struct mutex irq_lock; +int int_users; +bool ev_en; +bool trig_en; };static const struct iio_itime_sel_mul veml6031x00_it_sel[] = {
@@ -107,6 +133,17 @@ static const struct iio_gain_sel_pair veml6031x00_gain_sel[] = { GAIN_SCALE_GAIN(16, VEML6031X00_SEL_GAIN_2_000), }; +static IIO_CONST_ATTR(in_illuminance_thresh_either_period_available, "1 2 4 8"); + +static struct attribute *veml6031x00_event_attributes[] = { + &iio_const_attr_in_illuminance_thresh_either_period_available.dev_attr.attr, +NULL +}; + +static const struct attribute_group veml6031x00_event_attr_group = { + .attrs = veml6031x00_event_attributes, +}; +/* * The shutdown bits (SD and ALS_IR_SD) are in different registers, and both * must be updated when changing the device power state.
@@ -137,6 +174,23 @@ static void veml6031x00_als_shutdown_action(void *data) "Failed to shut down device: %d\n", ret); } +static const struct iio_event_spec veml6031x00_event_spec[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ), + }, { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ), + }, { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_EITHER , + .mask_separate =BIT (IIO_EV_INFO_PERIOD ) | +BIT (IIO_EV_INFO_ENABLE ), + }, +}; +static const struct iio_chan_spec veml6031x00_channels[] = { { .type =IIO_LIGHT ,
@@ -171,9 +225,47 @@ static const struct iio_chan_spec veml6031x00_channels[] = { IIO_CHAN_SOFT_TIMESTAMP(VEML6031X00_SCAN_TIMESTAMP), }; +static const struct iio_chan_spec veml6031x00_channels_irq[] = { + { + .type =IIO_LIGHT , + .address =VEML6031X00_REG_ALS_L , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ) | +BIT (IIO_CHAN_INFO_INT_TIME ) | +BIT (IIO_CHAN_INFO_SCALE ), + .info_mask_separate_available =BIT (IIO_CHAN_INFO_INT_TIME ) | +BIT (IIO_CHAN_INFO_SCALE ), + .event_spec = veml6031x00_event_spec, + .num_event_specs =ARRAY_SIZE (veml6031x00_event_spec), + .scan_index =VEML6031X00_SCAN_ALS , + .scan_type = { + .format =IIO_SCAN_FORMAT_UNSIGNED_INT , + .realbits =16 , + .storagebits =16 , + .endianness =IIO_LE , + }, + }, + { + .type =IIO_INTENSITY , + .address =VEML6031X00_REG_IR_L , + .modified =1 , + .channel2 =IIO_MOD_LIGHT_IR , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .scan_index =VEML6031X00_SCAN_IR , + .scan_type = { + .format =IIO_SCAN_FORMAT_UNSIGNED_INT , + .realbits =16 , + .storagebits =16 , + .endianness =IIO_LE , + }, + }, +IIO_CHAN_SOFT_TIMESTAMP (VEML6031X00_SCAN_TIMESTAMP ), +}; +static const struct regmap_range veml6031x00_readable_ranges[] = {regmap_reg_range (VEML6031X00_REG_CONF0 ,VEML6031X00_REG_CONF1 ), +regmap_reg_range (VEML6031X00_REG_WH_L ,VEML6031X00_REG_WL_H ),regmap_reg_range (VEML6031X00_REG_ALS_L ,VEML6031X00_REG_ID_H ), +regmap_reg_range (VEML6031X00_REG_INT ,VEML6031X00_REG_INT ), };static const struct regmap_access_table veml6031x00_readable_table = {
@@ -182,7 +274,7 @@ static const struct regmap_access_table veml6031x00_readable_table = { };static const struct regmap_range veml6031x00_writable_ranges[] = { -regmap_reg_range (VEML6031X00_REG_CONF0 ,VEML6031X00_REG_CONF1 ), +regmap_reg_range (VEML6031X00_REG_CONF0 ,VEML6031X00_REG_WL_H ), };static const struct regmap_access_table veml6031x00_writable_table = {
@@ -194,6 +286,7 @@ static const struct regmap_range veml6031x00_volatile_ranges[] = {/* AF_TRIG in CONF0 is volatile */ regmap_reg_range (VEML6031X00_REG_CONF0 ,VEML6031X00_REG_CONF0 ),regmap_reg_range (VEML6031X00_REG_ALS_L ,VEML6031X00_REG_IR_H ), + regmap_reg_range(VEML6031X00_REG_INT, VEML6031X00_REG_INT), }; static const struct regmap_access_table veml6031x00_volatile_table = {
@@ -201,6 +294,15 @@ static const struct regmap_access_table veml6031x00_volatile_table = { .n_yes_ranges = ARRAY_SIZE(veml6031x00_volatile_ranges), }; +static const struct regmap_range veml6031x00_precious_ranges[] = { +regmap_reg_range (VEML6031X00_REG_INT ,VEML6031X00_REG_INT ), +}; + +static const struct regmap_access_table veml6031x00_precious_table = { + .yes_ranges = veml6031x00_precious_ranges, + .n_yes_ranges =ARRAY_SIZE (veml6031x00_precious_ranges), +}; +static const struct regmap_config veml6031x00_regmap_config = { .name ="veml6031x00_regmap" , .reg_bits =8 ,
@@ -208,13 +310,20 @@ static const struct regmap_config veml6031x00_regmap_config = { .rd_table = &veml6031x00_readable_table, .wr_table = &veml6031x00_writable_table, .volatile_table = &veml6031x00_volatile_table, - .max_register = VEML6031X00_REG_ID_H, + .precious_table = &veml6031x00_precious_table, + .max_register = VEML6031X00_REG_INT, .cache_type = REGCACHE_MAPLE, }; +static const struct reg_field veml6031x00_rf_int_en = +REG_FIELD (VEML6031X00_REG_CONF0 ,1 ,1 ); +static const struct reg_field veml6031x00_rf_it =REG_FIELD (VEML6031X00_REG_CONF0 ,4 ,6 ); +static const struct reg_field veml6031x00_rf_pers = +REG_FIELD (VEML6031X00_REG_CONF1 ,1 ,2 ); +static const struct reg_field veml6031x00_rf_gain =REG_FIELD (VEML6031X00_REG_CONF1 ,3 ,4 );
@@ -237,11 +346,21 @@ static int veml6031x00_regfield_init(struct veml6031x00_data *data)return PTR_ERR (rm_field); rf->it = rm_field; + rm_field =devm_regmap_field_alloc (dev, data->regmap, veml6031x00_rf_int_en); +if (IS_ERR (rm_field)) +return PTR_ERR (rm_field); + rf->int_en = rm_field; + rm_field =devm_regmap_field_alloc (dev, data->regmap, veml6031x00_rf_pd_div4);if (IS_ERR (rm_field))return PTR_ERR (rm_field); rf->pd_div4 = rm_field; + rm_field =devm_regmap_field_alloc (dev, data->regmap, veml6031x00_rf_pers); +if (IS_ERR (rm_field)) +return PTR_ERR (rm_field); + rf->pers = rm_field; +return 0 ; }
@@ -340,6 +459,31 @@ static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2)return veml6031x00_write_gain (data, gain_sel); } +static int veml6031x00_read_period (struct iio_dev *iio,int *val) +{ +struct veml6031x00_data *data =iio_priv (iio); +unsigned int regval; +int ret; + + ret =regmap_field_read (data->rf.pers, ®val); +if (ret) +return ret; + + *val =BIT (regval); + +return IIO_VAL_INT ; +} + +static int veml6031x00_write_period (struct iio_dev *iio,int val) +{ +struct veml6031x00_data *data =iio_priv (iio); + +if (val <0 || val >8 ||hweight8 (val) !=1 ) +return -EINVAL ; + +return regmap_field_write (data->rf.pers,ffs (val) -1 ); +} +static int veml6031x00_set_scale (struct iio_dev *iio,int val,int val2) {struct veml6031x00_data *data =iio_priv (iio);
@@ -394,6 +538,53 @@ static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val,return IIO_VAL_INT_PLUS_NANO ; } +static int veml6031x00_read_th (struct iio_dev *iio,int *val,int *val2,int dir) +{ +struct veml6031x00_data *data =iio_priv (iio); +__le16 regval; +int ret; + +if (dir ==IIO_EV_DIR_RISING ) + ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_WH_L , + ®val,sizeof (regval)); +else + ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_WL_L , + ®val,sizeof (regval)); +if (ret) +return ret; + + *val =le16_to_cpu (regval); + +return IIO_VAL_INT ; +} + +static int veml6031x00_write_th (struct iio_dev *iio,int val,int val2,int dir) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +__le16 regval; +int ret; + +if (val <0 || val >U16_MAX || val2) +return -EINVAL ; + + regval =cpu_to_le16 (val); + +if (dir ==IIO_EV_DIR_RISING ) { + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WH_L , + ®val,sizeof (regval)); +if (ret) +dev_dbg (dev,"Failed to set high threshold %d\n" , ret); + }else { + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WL_L , + ®val,sizeof (regval)); +if (ret) +dev_dbg (dev,"Failed to set low threshold %d\n" , ret); + } + +return ret; +} +static int veml6031x00_single_read (struct iio_dev *iio,enum iio_chan_type type,int *val) {
@@ -507,13 +698,221 @@ static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, } } +static int veml6031x00_set_interrupt (struct veml6031x00_data *data,bool state) + __must_hold(&data->irq_lock) +{ +int ret; + +lockdep_assert_held (&data->irq_lock); + +if (state) { + data->int_users++; +if (data->int_users >1 ) +return 0 ; + }else { + data->int_users--; +if (data->int_users >0 ) +return 0 ; + } + + ret =regmap_field_write (data->rf.int_en, state); +if (ret) { +if (state) + data->int_users--; +else + data->int_users++; + } + +return ret; +} + +static int veml6031x00_read_event_val (struct iio_dev *iio, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info, +int *val,int *val2) +{ +switch (type) { +case IIO_EV_TYPE_THRESH : +if (dir ==IIO_EV_DIR_EITHER && info ==IIO_EV_INFO_PERIOD ) +return veml6031x00_read_period (iio, val); + +return veml6031x00_read_th (iio, val, val2, dir); +default : +return -EINVAL ; + } +} + +static int veml6031x00_write_event_val (struct iio_dev *iio, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info, +int val,int val2) +{ +switch (info) { +case IIO_EV_INFO_VALUE : +return veml6031x00_write_th (iio, val, val2, dir); +case IIO_EV_INFO_PERIOD : +return veml6031x00_write_period (iio, val); +default : +return -EINVAL ; + } +} + +static int veml6031x00_read_event_config (struct iio_dev *iio, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir) +{ +struct veml6031x00_data *data =iio_priv (iio); + +guard (mutex)(&data->irq_lock); + +return data->ev_en; +} + +static int veml6031x00_write_event_config (struct iio_dev *iio, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +bool state) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + +guard (mutex)(&data->irq_lock); + +/* avoid multiple increments/decrements from one source */ +if (state == data->ev_en) +return 0 ; + +if (state) { + ret =pm_runtime_resume_and_get (dev); +if (ret) +return ret; + } + + ret =veml6031x00_set_interrupt (data, state); +if (ret) { +if (state) +pm_runtime_put_autosuspend (dev); +return ret; + } + + data->ev_en = state; + +if (!state) +pm_runtime_put_autosuspend (dev); + +return 0 ; +} + +static void veml6031x00_disable_event_action (void *arg) +{ +struct veml6031x00_data *data = arg; +struct device *dev =regmap_get_device (data->regmap); +int ret; + +guard (mutex)(&data->irq_lock); + +if (!data->ev_en) +return ; + + ret =veml6031x00_set_interrupt (data, false); +if (ret) +dev_err (dev,"Failed to disable events: %d\n" , ret); + + data->ev_en = false; +pm_runtime_put_autosuspend (dev); +} +static const struct iio_info veml6031x00_info = { .read_raw = veml6031x00_read_raw, .read_avail = veml6031x00_read_avail, .write_raw = veml6031x00_write_raw, .write_raw_get_fmt = veml6031x00_write_raw_get_fmt, + .read_event_value = veml6031x00_read_event_val, + .write_event_value = veml6031x00_write_event_val, + .read_event_config = veml6031x00_read_event_config, + .write_event_config = veml6031x00_write_event_config, + .event_attrs = &veml6031x00_event_attr_group, }; +static const struct iio_info veml6031x00_info_no_irq = { + .read_raw = veml6031x00_read_raw, + .read_avail = veml6031x00_read_avail, + .write_raw = veml6031x00_write_raw, + .write_raw_get_fmt = veml6031x00_write_raw_get_fmt, +}; + +static irqreturn_t veml6031x00_irq (int irq,void *private) +{ +struct iio_dev *iio = private; +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +s64 timestamp; +unsigned int regval; +int ret; +bool trigger_poll; + + ret =pm_runtime_get_if_active (dev); +if (ret <=0 ) +return IRQ_NONE ; + + scoped_guard(mutex, &data->irq_lock) { + ret =regmap_read (data->regmap,VEML6031X00_REG_INT , ®val); +if (ret) { +dev_dbg (dev,"Failed to read interrupt register %d\n" , ret); +pm_runtime_put (dev); +return IRQ_NONE ; + } + +if (!(regval &VEML6031X00_INT_MASK )) { +pm_runtime_put (dev); +return IRQ_NONE ; + } + + timestamp =iio_get_time_ns (iio); + +if ((regval & (VEML6031X00_INT_TH_H |VEML6031X00_INT_TH_L )) && + data->ev_en) { +if (regval &VEML6031X00_INT_TH_H ) +iio_push_event (iio, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_RISING ), + timestamp); +if (regval &VEML6031X00_INT_TH_L ) +iio_push_event (iio, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + } + + trigger_poll = (regval &VEML6031X00_INT_DRDY ) && data->trig_en; + } + +/* +* iio_trigger_poll_nested() must be called with irq_lock released: +* it runs trig_handler() synchronously in this thread, which calls +* reenable() on completion, and that callback also takes irq_lock. +* iio_pollfunc_store_time() is not called for our own trigger, so the +* timestamp is stored here. +*/ +if (trigger_poll) { + iio->pollfunc->timestamp = timestamp; +iio_trigger_poll_nested (data->trig); + } + +pm_runtime_put (dev); + +return IRQ_HANDLED ; +} +static int veml6031x00_buffer_preenable (struct iio_dev *iio) {struct veml6031x00_data *data =iio_priv (iio);
@@ -549,11 +948,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)return 0 ; } +static int veml6031x00_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct iio_dev *iio =iio_trigger_get_drvdata (trig); +struct veml6031x00_data *data =iio_priv (iio); +int ret; + +guard (mutex)(&data->irq_lock); + +if (state == data->trig_en) +return 0 ; + + ret =veml6031x00_set_interrupt (data, state); +if (ret) +return ret; + +/* The AF bit must be updated before updating AF_TRIG */ + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , state)); +if (ret) { +veml6031x00_set_interrupt (data, !state); + +return ret; + } + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +if (ret) { +regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , !state)); +veml6031x00_set_interrupt (data, !state); + +return ret; + } + + data->trig_en = state; + +return 0 ; +} +static const struct iio_buffer_setup_ops veml6031x00_buffer_setup_ops = { .preenable = veml6031x00_buffer_preenable, .postdisable = veml6031x00_buffer_postdisable, }; +static void veml6031x00_trigger_reenable (struct iio_trigger *trig) +{ +struct iio_dev *iio =iio_trigger_get_drvdata (trig); +struct veml6031x00_data *data =iio_priv (iio); +int ret; + +guard (mutex)(&data->irq_lock); + +if (!data->trig_en) +return ; + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , true)); +if (ret) +dev_err (regmap_get_device (data->regmap), +"Failed to reenable trigger: %d\n" , ret); +} + +static const struct iio_trigger_ops veml6031x00_trigger_ops = { + .validate_device = iio_trigger_validate_own_device, + .set_trigger_state = veml6031x00_set_trigger_state, + .reenable = veml6031x00_trigger_reenable, +}; +static irqreturn_t veml6031x00_trig_handler (int irq,void *p) {struct iio_poll_func *pf = p;
@@ -612,7 +1078,8 @@ static int veml6031x00_hw_init(struct iio_dev *iio) {struct veml6031x00_data *data =iio_priv (iio);struct device *dev =regmap_get_device (data->regmap); -int ret; +int ret, val; +__le16 regval;/* Max resolution = 6.9632 lx/cnt for gain = 0.125 and IT = 3.125ms */ ret = devm_iio_init_iio_gts(dev, 6, 963200000,
@@ -624,9 +1091,54 @@ static int veml6031x00_hw_init(struct iio_dev *iio)if (ret)return dev_err_probe (dev, ret,"Failed to init IIO GTS\n" ); + regval =0 ; + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WL_L , ®val, +sizeof (regval)); +if (ret) +return dev_err_probe (dev, ret,"Failed to set low threshold\n" ); + + regval =cpu_to_le16 (U16_MAX ); + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WH_L , ®val, +sizeof (regval)); +if (ret) +return dev_err_probe (dev, ret,"Failed to set high threshold\n" ); + + ret =regmap_field_write (data->rf.int_en,0 ); +if (ret) +return ret; + + ret =regmap_read (data->regmap,VEML6031X00_REG_INT , &val); +if (ret) +return dev_err_probe (dev, ret,"Failed to clear interrupts\n" ); +return 0 ; } +static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig) +return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops; +iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig); + +return devm_request_threaded_irq (dev, i2c->irq, +NULL , veml6031x00_irq, +IRQF_ONESHOT , iio->name, iio); +} +static int veml6031x00_probe (struct i2c_client *i2c) {struct device *dev = &i2c->dev;
@@ -651,15 +1163,16 @@ static int veml6031x00_probe(struct i2c_client *i2c) "Failed to set regmap\n"); iio->name = data->chip->name; - iio->channels = veml6031x00_channels; - iio->num_channels =ARRAY_SIZE (veml6031x00_channels); iio->modes =INDIO_DIRECT_MODE ; - iio->info = &veml6031x00_info; ret =devm_mutex_init (dev, &data->scale_lock);if (ret)return ret; + ret =devm_mutex_init (dev, &data->irq_lock); +if (ret) +return ret; + ret =veml6031x00_regfield_init (data);if (ret)return dev_err_probe (dev, ret,"Failed to init regfield\n" );
@@ -685,6 +1198,12 @@ static int veml6031x00_probe(struct i2c_client *i2c)pm_runtime_get_noresume (dev); + ret =devm_add_action_or_reset (dev, veml6031x00_disable_event_action, data); +if (ret) { +dev_err_probe (dev, ret,"Failed to add event cleanup action\n" ); + goto err_pm_put; + } + ret =veml6031x00_validate_part_id (data);if (ret) goto err_pm_put;
@@ -693,6 +1212,20 @@ static int veml6031x00_probe(struct i2c_client *i2c)if (ret) goto err_pm_put; +if (i2c->irq) { + iio->channels = veml6031x00_channels_irq; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels_irq); + ret =veml6031x00_setup_irq (i2c, iio); +if (ret) + goto err_pm_put; + + iio->info = &veml6031x00_info; + }else { + iio->channels = veml6031x00_channels; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels); + iio->info = &veml6031x00_info_no_irq; + } +pm_runtime_put_autosuspend (dev); ret = devm_iio_triggered_buffer_setup(dev, iio,
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Asynchronous `pm_runtime_put_autosuspend()` is used in a devres cleanup action immediately…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Asynchronous pm_runtime_put_autosuspend() is used in a devres cleanup action immediately before pm_runtime_disable(), violating PM subsystem guidelines.
- [Medium] Module and trigger device reference leak in the probe error path due to iio_trigger_get().
- [Medium] Unintended I2C read of the configuration register during trigger handler due to iterating over the soft timestamp channel.
--
--- Patch [4]: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger --- commit 2fab19b731ba4590b28fdcf71e13c66950cf30a9
Author: Javier Carrasco <[email protected]>
iio: light: veml6031x00: add support for events and trigger
This commit adds support for events and data-ready triggers using a shared interrupt line for the VEML6031X00 light sensor.
Quoted 4 lines
drivers/iio/light/veml6031x00.c +0
-0
[ ... ]
Quoted 7 lines
drivers/iio/light/veml6031x00.c +7
-0+static void veml6031x00_disable_event_action (void *arg) +{ +struct veml6031x00_data *data = arg; +struct device *dev =regmap_get_device (data->regmap); +int ret; + +guard (mutex)(&data->irq_lock);
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 829
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+ +if (!data->ev_en) +return ; + + ret =veml6031x00_set_interrupt (data, false); +if (ret) +dev_err (dev,"Failed to disable events: %d\n" , ret); + + data->ev_en = false; +pm_runtime_put_autosuspend (dev);
[Severity: Medium]
Will this asynchronous PM put be cancelled during device removal?
Since this function is registered as a devres action via devm_add_action_or_reset(), it will execute right before pm_runtime_disable() on the unbind path.
The PM subsystem guidelines indicate that pm_runtime_put_sync() should be used when pm_runtime_disable() or device removal follows immediately, as pending async idle/suspend work can be cancelled.
Quoted 1 line
+}
[ ... ]
Quoted 12 lines
drivers/iio/light/veml6031x00.c +12
-0+static irqreturn_t veml6031x00_trig_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *iio = pf->indio_dev; +struct veml6031x00_data *data =iio_priv (iio); + IIO_DECLARE_BUFFER_WITH_TS(__le16, scan, 2) = { }; +unsigned int i =0 ; +int ch, ret; + +if (test_bit (VEML6031X00_SCAN_ALS , iio->active_scan_mask) && +test_bit (VEML6031X00_SCAN_IR , iio->active_scan_mask)) { + ret = regmap_bulk_read(data->regmap,
drivers/iio/light/veml6031x00.c
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+VEML6031X00_REG_ALS_L , + scan, + 2 * sizeof(*scan)); +if (ret) + goto done; + }else { +iio_for_each_active_channel (iio, ch) { + ret = regmap_bulk_read(data->regmap, + iio->channels[ch].address, + &scan[i++],
[Severity: Medium]
Does this loop perform an unintended I2C read when the soft timestamp channel is active?
Since IIO_CHAN_SOFT_TIMESTAMP sets the channel address to 0, if the timestamp channel is in the active scan mask, this loop will call regmap_bulk_read() with address 0 (VEML6031X00_REG_CONF0).
Should the loop explicitly skip the timestamp channel, or check if the channel has a valid I2C register address?
Quoted 13 lines
drivers/iio/light/veml6031x00.c +13
-0+ sizeof(*scan)); +if (ret) + goto done; + } + } + +iio_push_to_buffers_with_ts (iio, scan,sizeof (scan), pf->timestamp); + +done: +iio_trigger_notify_done (iio->trig); + +return IRQ_HANDLED ; +}
[ ... ]
Quoted 12 lines
drivers/iio/light/veml6031x00.c +12
-0+static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig) +return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops;
drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1138
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig); + + return devm_request_threaded_irq(dev, i2c->irq, + NULL, veml6031x00_irq,
[Severity: Medium]
Is there a reference leak here if devm_request_threaded_irq() fails?
If the IRQ request fails, the probe will abort. Since devm_iio_triggered_buffer_setup() has not been called yet, the INDIO_BUFFER_TRIGGERED flag won't be set in iio->modes.
During cleanup, iio_dev_release() skips iio_device_unregister_trigger_consumer() if that flag is missing, permanently leaking the trigger device and module reference acquired by iio_trigger_get().
Quoted 2 lines
drivers/iio/light/veml6031x00.c +2
-0+ IRQF_ONESHOT, iio->name, iio); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Jonathan Cameron
maintainerRe: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
In reply to Javier CarrascoQuoted 6 lines · On Wed, 12 Aug 2026 22:27:43 +0200 Javier Carrasco…
The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.
Signed-off-by Javier Carrasco <[email protected]>
Trivial only.
Quoted 16 lines
--- drivers/iio/light/veml6031x00.c | 545 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 539 insertions(+), 6 deletions(-)drivers/iio/light/veml6031x00.c +6
-0+ +static int veml6031x00_write_event_config (struct iio_dev *iio, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +bool state)
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 790
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + +guard (mutex)(&data->irq_lock); + +/* avoid multiple increments/decrements from one source */ +if (state == data->ev_en) +return 0 ;
There is not a lot of shared code in here between state true/false I'd split as two helpers.
if (state) ret = veml6031x00_event_enable(); else ret = veml6031x00_even_disable();
Quoted 50 lines
drivers/iio/light/veml6031x00.c +44
-0+ +if (state) { + ret =pm_runtime_resume_and_get (dev); +if (ret) +return ret; + } + + ret =veml6031x00_set_interrupt (data, state); +if (ret) { +if (state) +pm_runtime_put_autosuspend (dev); +return ret; + } + + data->ev_en = state; + +if (!state) +pm_runtime_put_autosuspend (dev); + +return 0 ; +}@@ -549,11 +948,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)return 0 ; } +static int veml6031x00_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct iio_dev *iio =iio_trigger_get_drvdata (trig); +struct veml6031x00_data *data =iio_priv (iio); +int ret; + +guard (mutex)(&data->irq_lock); + +if (state == data->trig_en) +return 0 ; + + ret =veml6031x00_set_interrupt (data, state); +if (ret) +return ret; + +/* The AF bit must be updated before updating AF_TRIG */ + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , state)); +if (ret) { +veml6031x00_set_interrupt (data, !state); + +return ret;
drivers/iio/light/veml6031x00.c static int veml6031x00_buffer_postdisable(struct iio_dev *iio) · line 983
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+ } + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +if (ret) { +regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , !state)); +veml6031x00_set_interrupt (data, !state);
This dance vs a goto is I guess due to the mutex. I'd clean it up by using a helper function for the stuff done under the guard(). The helper can do goto based cleanup and avoid repetition plus reduce chance of missing cleaning something up on error. The outer function can still use guard().
Quoted 19 lines
drivers/iio/light/veml6031x00.c +17
-0+ +return ret; + } + + data->trig_en = state; + +return 0 ; +} +static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig)
drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1135
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops; +iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig);
Sashiko is correct that we loose a reference here on error and right now there is no IIO core infrastructure to solve this
Why are we setting a default trigger? Userspace tools should be
fine looking for a data ready trigger, or choosing a different one if they would prefer. Added advantage of not setting it here is the reference count issue goes away :)
Quoted 5 lines
drivers/iio/light/veml6031x00.c +5
-0+ +return devm_request_threaded_irq (dev, i2c->irq, +NULL , veml6031x00_irq, +IRQF_ONESHOT , iio->name, iio); +}
Javier Carrasco
maintainerRe: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
Hi Jonathan, thank you for your review to the series.
Quoted 44 lines · Jonathan Cameron
Quoted 41 lines · On Wed, 12 Aug 2026 22:27:43 +0200 Javier Carrasco…
The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.
Signed-off-by Javier Carrasco <[email protected]>drivers/iio/light/veml6031x00.c +31
-0@@ -549,11 +948,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)return 0 ; } +static int veml6031x00_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct iio_dev *iio =iio_trigger_get_drvdata (trig); +struct veml6031x00_data *data =iio_priv (iio); +int ret; + +guard (mutex)(&data->irq_lock); + +if (state == data->trig_en) +return 0 ; + + ret =veml6031x00_set_interrupt (data, state); +if (ret) +return ret; + +/* The AF bit must be updated before updating AF_TRIG */ + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , state)); +if (ret) { +veml6031x00_set_interrupt (data, !state); + +return ret; + } + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +if (ret) { + regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0, + VEML6031X00_CONF0_AF,
Quoted 9 lines
drivers/iio/light/veml6031x00.c static int veml6031x00_buffer_postdisable(struct iio_dev *iio) · line 983
Quoted 2 lines
drivers/iio/light/veml6031x00.c +2
-0+ FIELD_PREP(VEML6031X00_CONF0_AF, !state)); +veml6031x00_set_interrupt (data, !state);This dance vs a goto is I guess due to the mutex. I'd clean it up by using a helper function for the stuff done under the
guard(). The helper can do goto based cleanup and avoid repetition plus reduce chance of missing cleaning something up on error. The outer function can still useguard().
Yes, that was the reason why some code was duplicated. I will add a helper function with the __must_hold() annotation and lockdep_assert_held().
Quoted 33 lines
Quoted 19 lines
drivers/iio/light/veml6031x00.c +17
-0+ +return ret; + } + + data->trig_en = state; + +return 0 ; +} +static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig)drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1135
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops; +iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig);Sashiko is correct that we loose a reference here on error and right now there is no IIO core infrastructure to solve this
Quoted 5 lines
Why are we setting a default trigger? Userspace tools should be
fine looking for a data ready trigger, or choosing a different one if they would prefer. Added advantage of not setting it here is the reference count issue goes away :)
I will drop iio->trig = iio_trigger_get(data->trig) for v7.
I added it because it is (or at least, it was) a common practice in many IIO drivers to assign their own trigger, and in the end it is by far the most common use case. Of course, we're not going to touch existing drivers to remove that, but is it then something to be advised against in the future unless there is a good reason for it?
Thanks and best regards,
Javier
Jonathan Cameron
maintainerRe: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
Quoted 100 lines · On Thu, 13 Aug 2026 14:43:26 +0200 "Javier Carrasco"…
Hi Jonathan, thank you for your review to the series.
Quoted 44 lines · Jonathan Cameron
Quoted 41 lines · On Wed, 12 Aug 2026 22:27:43 +0200 Javier Carrasco…
The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.
Signed-off-by Javier Carrasco <[email protected]>drivers/iio/light/veml6031x00.c +31
-0@@ -549,11 +948,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)return 0 ; } +static int veml6031x00_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct iio_dev *iio =iio_trigger_get_drvdata (trig); +struct veml6031x00_data *data =iio_priv (iio); +int ret; + +guard (mutex)(&data->irq_lock); + +if (state == data->trig_en) +return 0 ; + + ret =veml6031x00_set_interrupt (data, state); +if (ret) +return ret; + +/* The AF bit must be updated before updating AF_TRIG */ + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , state)); +if (ret) { +veml6031x00_set_interrupt (data, !state); + +return ret; + } + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +if (ret) { + regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0, + VEML6031X00_CONF0_AF,Quoted 9 lines
drivers/iio/light/veml6031x00.c static int veml6031x00_buffer_postdisable(struct iio_dev *iio) · line 983
Quoted 2 lines
drivers/iio/light/veml6031x00.c +2
-0+ FIELD_PREP(VEML6031X00_CONF0_AF, !state)); +veml6031x00_set_interrupt (data, !state);This dance vs a goto is I guess due to the mutex. I'd clean it up by using a helper function for the stuff done under the
guard(). The helper can do goto based cleanup and avoid repetition plus reduce chance of missing cleaning something up on error. The outer function can still useguard().Yes, that was the reason why some code was duplicated. I will add a helper function with the
__must_hold()annotation andlockdep_assert_held().Quoted 33 lines
Quoted 19 lines
drivers/iio/light/veml6031x00.c +17
-0+ +return ret; + } + + data->trig_en = state; + +return 0 ; +} +static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig)drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1135
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops; +iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig);Sashiko is correct that we loose a reference here on error and right now there is no IIO core infrastructure to solve this
Quoted 5 lines
Why are we setting a default trigger? Userspace tools should be
fine looking for a data ready trigger, or choosing a different one if they would prefer. Added advantage of not setting it here is the reference count issue goes away :)
Quoted 7 lines
I will drop
iio->trig=iio_trigger_get(data->trig)for v7.I added it because it is (or at least, it was) a common practice in many IIO drivers to assign their own trigger, and in the end it is by far the most common use case. Of course, we're not going to touch existing drivers to remove that, but is it then something to be advised against in the future unless there is a good reason for it?
It makes sense if they also 'require' that trigger - but generally if a driver has dealt with the potential extra complexity of allowing other triggers that is because they want to use them. As such a default has no particular benefit.
I probably let a few defaults in over the years where this wasn't the case though :(
Jonathan
Quoted 3 lines
Thanks and best regards,
Javier
Javier Carrasco
maintainerRe: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
Quoted 111 lines · Jonathan Cameron
Quoted 100 lines · On Thu, 13 Aug 2026 14:43:26 +0200 "Javier Carrasco"…
Hi Jonathan, thank you for your review to the series.
Quoted 44 lines · Jonathan Cameron
Quoted 41 lines · On Wed, 12 Aug 2026 22:27:43 +0200 Javier Carrasco…
The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.
Signed-off-by Javier Carrasco <[email protected]>drivers/iio/light/veml6031x00.c +31
-0@@ -549,11 +948,78 @@ static int veml6031x00_buffer_postdisable(struct iio_dev *iio)return 0 ; } +static int veml6031x00_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct iio_dev *iio =iio_trigger_get_drvdata (trig); +struct veml6031x00_data *data =iio_priv (iio); +int ret; + +guard (mutex)(&data->irq_lock); + +if (state == data->trig_en) +return 0 ; + + ret =veml6031x00_set_interrupt (data, state); +if (ret) +return ret; + +/* The AF bit must be updated before updating AF_TRIG */ + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , state)); +if (ret) { +veml6031x00_set_interrupt (data, !state); + +return ret; + } + + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +if (ret) { + regmap_update_bits(data->regmap, VEML6031X00_REG_CONF0, + VEML6031X00_CONF0_AF,Quoted 9 lines
drivers/iio/light/veml6031x00.c static int veml6031x00_buffer_postdisable(struct iio_dev *iio) · line 983
Quoted 2 lines
drivers/iio/light/veml6031x00.c +2
-0+ FIELD_PREP(VEML6031X00_CONF0_AF, !state)); +veml6031x00_set_interrupt (data, !state);This dance vs a goto is I guess due to the mutex. I'd clean it up by using a helper function for the stuff done under the
guard(). The helper can do goto based cleanup and avoid repetition plus reduce chance of missing cleaning something up on error. The outer function can still useguard().Yes, that was the reason why some code was duplicated. I will add a helper function with the
__must_hold()annotation andlockdep_assert_held().Quoted 33 lines
Quoted 19 lines
drivers/iio/light/veml6031x00.c +17
-0+ +return ret; + } + + data->trig_en = state; + +return 0 ; +} +static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig)drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1135
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops; +iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig);Sashiko is correct that we loose a reference here on error and right now there is no IIO core infrastructure to solve this
Quoted 5 lines
Why are we setting a default trigger? Userspace tools should be
fine looking for a data ready trigger, or choosing a different one if they would prefer. Added advantage of not setting it here is the reference count issue goes away :)Quoted 7 lines
I will drop
iio->trig=iio_trigger_get(data->trig)for v7.I added it because it is (or at least, it was) a common practice in many IIO drivers to assign their own trigger, and in the end it is by far the most common use case. Of course, we're not going to touch existing drivers to remove that, but is it then something to be advised against in the future unless there is a good reason for it?
Quoted 7 lines
It makes sense if they also 'require' that trigger - but generally if a driver has dealt with the potential extra complexity of allowing other triggers that is because they want to use them. As such a default has no particular benefit.
I probably let a few defaults in over the years where this wasn't the case though :(
I bet there are way worse sins ;)
Quoted 6 lines
Jonathan
Quoted 3 lines
Thanks and best regards,
Javier
Re: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
In reply to Javier CarrascoQuoted 4 lines · Javier Carrasco
The device provides a shared interrupt line for to notify events and data ready, which can be used as a trigger. The interrupt line is not a requirement for the device to work. Implement variants for the cases whether the interrupt line is provided or not.
...
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 706
Quoted 6 lines
drivers/iio/light/veml6031x00.c +6
-0+static int veml6031x00_set_interrupt (struct veml6031x00_data *data,bool state) + __must_hold(&data->irq_lock) +{ +int ret; + +lockdep_assert_held (&data->irq_lock);
This needs another include.
Quoted 20 lines
drivers/iio/light/veml6031x00.c +20
-0+if (state) { + data->int_users++; +if (data->int_users >1 ) +return 0 ; + }else { + data->int_users--; +if (data->int_users >0 ) +return 0 ; + } + + ret =regmap_field_write (data->rf.int_en, state); +if (ret) { +if (state) + data->int_users--; +else + data->int_users++; + } + +return ret; +}
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WL_L, ®val, + sizeof(regval));
I always suggest to go with logical split.
ret = regmap_bulk_write(data->regmap, VEML6031X00_REG_WL_L, ®val, sizeof(regval));
With shortened name and
struct regmap *map = data->regmap;
this can be reduced to
ret = regmap_bulk_write(map, VEML6031X00_REG_WL_L, &val, sizeof(val));
which is perfectly under 80 limit.
drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1102
Quoted 6 lines
drivers/iio/light/veml6031x00.c +6
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to set low threshold\n" ); + + regval =cpu_to_le16 (U16_MAX ); + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WH_L , ®val, +sizeof (regval));
Ditto and so on...
Quoted 2 lines
(quoted diff) +2
-0+ if (ret) + return dev_err_probe(dev, ret, "Failed to set high threshold\n");
...
Quoted 12 lines
drivers/iio/light/veml6031x00.c +12
-0+static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev =regmap_get_device (data->regmap); +int ret; + + data->trig =devm_iio_trigger_alloc (dev,"%s-drdy%d" , + iio->name,iio_device_id (iio)); +if (!data->trig) +return -ENOMEM ; + + data->trig->ops = &veml6031x00_trigger_ops;
drivers/iio/light/veml6031x00.c static int veml6031x00_hw_init(struct iio_dev *iio) · line 1138
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+iio_trigger_set_drvdata (data->trig, iio); + + ret =devm_iio_trigger_register (dev, data->trig); +if (ret) +return ret; + + iio->trig =iio_trigger_get (data->trig); + + return devm_request_threaded_irq(dev, i2c->irq, + NULL, veml6031x00_irq,
return devm_request_threaded_irq(dev, i2c->irq, NULL, veml6031x00_irq,
...is perfectly one line and keeps be split logically.
Quoted 2 lines
drivers/iio/light/veml6031x00.c +2
-0+ IRQF_ONESHOT, iio->name, iio); +}
...
drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1204
Quoted 6 lines
drivers/iio/light/veml6031x00.c +4
-0pm_runtime_get_noresume (dev); + ret =devm_add_action_or_reset (dev, veml6031x00_disable_event_action, data); +if (ret) { +dev_err_probe (dev, ret,"Failed to add event cleanup action\n" ); + goto err_pm_put;
No, this is (I am pretty sure) wrong. It's a red flag if one sees a goto after devm.
Quoted 1 line
(quoted diff) +1
-0+ }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerRe: [PATCH v6 4/4] iio: light: veml6031x00: add support for events and trigger
In reply to Javier CarrascoQuoted 2 lines
static int
veml6031x00_probe(struct i2c_client *i2c){
...
drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1206
Quoted 6 lines
drivers/iio/light/veml6031x00.c +6
-0+ ret =devm_add_action_or_reset (dev, veml6031x00_disable_event_action, data); +if (ret) { +dev_err_probe (dev, ret,"Failed to add event cleanup action\n" ); + goto err_pm_put; + } +
I just realized that this action is added without taking into account if an interrupt is registered or not. It passes because the function checks if the events are enabled, but it is unnecessary if there is no interrupt in the first place. I will move it right after veml6031x00_setup_irq(), and when at it, I will take the whole IIO device initialization to a dedicated function to make it more readable.
Quoted 14 lines
drivers/iio/light/veml6031x00.c +7
-0ret =veml6031x00_validate_part_id (data);if (ret) goto err_pm_put;@@ -693,6 +1212,20 @@ static int veml6031x00_probe(struct i2c_client *i2c)if (ret) goto err_pm_put; +if (i2c->irq) { + iio->channels = veml6031x00_channels_irq; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels_irq); + ret =veml6031x00_setup_irq (i2c, iio); +if (ret) + goto err_pm_put; +
drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 1231
Quoted 10 lines
drivers/iio/light/veml6031x00.c +7
-0+ iio->info = &veml6031x00_info; + }else { + iio->channels = veml6031x00_channels; + iio->num_channels =ARRAY_SIZE (veml6031x00_channels); + iio->info = &veml6031x00_info_no_irq; + } +pm_runtime_put_autosuspend (dev); ret = devm_iio_triggered_buffer_setup(dev, iio,
Best regards,
Javier
Jonathan Cameron
maintainerQuoted 3 lines · On Wed, 12 Aug 2026 22:27:39 +0200 Javier Carrasco…
These ambient light sensors with I2C interface provide two light channels (ALS and IR), high/low threshold alarms with configurable persistence, and a data ready signal.
https://sashiko.dev/#/patchset/20260812-veml6031x00-v6-0-7eef6e4ce290%40gmail.com
Seems to have one plausible point that is driver specific. The first one about the devm action calling pm_runtime_put_sync() rather than the autosuspend version.
The others are a false positive (the one about the timestamp channel) and the iio_trigger reference count thing that is real but needs some work in the core to fix. That one affects a bunch of drivers unfortunately :(
Jonathan