iio: light: add support for veml6031x00 ALS series
Subsystems (4)
Javier Carrasco
maintainerv3 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 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: Rishi Gupta <[email protected]>
To: David Lechner <[email protected]>
To: Nuno Sá <[email protected]>
To: Andy Shevchenko <[email protected]>
To: Matti Vaittinen <[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 | 23 +- MAINTAINERS | 6 + drivers/iio/light/Kconfig | 14 + drivers/iio/light/Makefile | 1 + drivers/iio/light/veml6031x00.c | 1197 ++++++++++++++++++++ 5 files changed, 1240 insertions(+), 1 deletion(-)
---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20241109-veml6031x00-aa9463da064a
Best regards,
Signature · 1 line
Javier Carrasco <[email protected]>
Javier Carrasco
maintainerPATCH v3 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).
--- .../bindings/iio/light/vishay,veml6030.yaml | 23 +++++++++++++++++++++- MAINTAINERS | 5 +++++ 2 files changed, 27 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +22 -1
@@ -4,7 +4,9 @@ $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: VEML3235, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS) +title: + VEML3235, VEML6030, VEML6031x00 series, VEML6035 and VEML7700 Ambient + Light Sensors (ALS) maintainers: - Rishi Gupta <[email protected]>
@@ -22,12 +24,18 @@ description: | Specifications about the sensors can be found at: https://www.vishay.com/docs/80131/veml3235.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 properties: compatible: enum: + - vishay,veml6031x00 + - vishay,veml6031x01 + - vishay,veml60311x00 + - vishay,veml60311x01 - vishay,veml3235 - vishay,veml6030 - vishay,veml6035
@@ -67,6 +75,8 @@ allOf: properties: compatible: enum: + - vishay,veml6031x00 + - vishay,veml6031x01 - vishay,veml6035 then: properties:
@@ -79,12 +89,23 @@ allOf: compatible: enum: - vishay,veml3235 + - vishay,veml60311x00 + - vishay,veml60311x01 - vishay,veml7700 then: properties: reg: enum: - 0x10 + + - if: + properties: + compatible: + enum: + - vishay,veml3235 + - vishay,veml7700 + then: + properties: interrupts: false additionalProperties: false
MAINTAINERS +5 -0
@@ -28381,6 +28381,11 @@ S: Maintained F: Documentation/devicetree/bindings/iio/light/vishay,veml6046x00.yaml F: drivers/iio/light/veml6046x00.c +VISHAY VEML6031X00 AMBIENT LIGHT SENSOR DRIVER +M: Javier Carrasco <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml + VISHAY VEML6075 UVA AND UVB LIGHT SENSOR DRIVER M: Javier Carrasco <[email protected]> S: Maintained
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v3 1/4] dt-bindings: iio: light: veml6030: add veml6031x00 ALS series
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The new entry in the MAINTAINERS file breaks alphabetical sorting. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The new entry in the MAINTAINERS file breaks alphabetical sorting. --
commit c75afbde2cfc8b7f8986709a43e0d1e7b6d9ebe5
Author: Javier Carrasco <[email protected]>
dt-bindings: iio: light: veml6030: add veml6031x00 ALS series
This commit updates the veml6030 bindings to include the veml6031x00 ambient light sensor series. It adds the new compatible strings and creates a new entry in the MAINTAINERS file for the driver.
MAINTAINERS S: Maintained · line 28384
Quoted 9 lines
MAINTAINERS +1
-0@@ -28381,6 +28381,11 @@ S: Maintained F: Documentation/devicetree/bindings/iio/light/vishay,veml6046x00.yaml F: drivers/iio/light/veml6046x00.c +VISHAY VEML6031X00 AMBIENT LIGHT SENSOR DRIVER
[Severity: Low]
This isn't a bug, but does this insertion break the alphabetical sorting of the MAINTAINERS file?
Since "VEML6031" comes before "VEML6046", should this new block be placed above the VISHAY VEML6046X00 entry to maintain the standard lexicographical ordering?
Quoted 5 lines
MAINTAINERS +4
-0+M: Javier Carrasco <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml + VISHAY VEML6075 UVA AND UVB LIGHT SENSOR DRIVER
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Javier Carrasco
maintainerPATCH v3 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 | 657 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 671 insertions(+)
MAINTAINERS +1 -0
@@ -28385,6 +28385,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 VEML6075 UVA AND UVB LIGHT SENSOR DRIVER M: Javier Carrasco <[email protected]>
drivers/iio/light/Kconfig +12 -0
@@ -713,6 +713,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
@@ -66,6 +66,7 @@ obj-$(CONFIG_VCNL4000) += vcnl4000.o obj-$(CONFIG_VCNL4035) += vcnl4035.o obj-$(CONFIG_VEML3235) += veml3235.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 +657 -0
657 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] Mixing `devm_pm_runtime_get_noresume()` with manual `pm_runtime_put_autosuspend()` causes a PM…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Mixing devm_pm_runtime_get_noresume() with manual pm_runtime_put_autosuspend() causes a PM usage counter underflow.
- [High] veml6031x00_write_raw() modifies hardware registers over I2C without acquiring a runtime PM reference.
- [High] The shutdown devres action performs I2C writes without ensuring the device and its parent are resumed.
- [Medium] Missing locks in veml6031x00_read_raw() for IIO_CHAN_INFO_SCALE and IIO_CHAN_INFO_INT_TIME allow torn reads of configuration fields.
- [Medium] Incorrect scale attribute exposure for the IR channel and failure to mark shared hardware attributes.
- [Medium] The driver unconditionally sleeps for the integration time on every raw read, introducing unnecessary latency.
--
commit 02aa1fda2e77e393230904b3d122bdbb99445c9f
Author: Javier Carrasco <[email protected]>
iio: light: add support for veml6031x00 ALS series
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 6 lines
drivers/iio/light/veml6031x00.c new file +0
-0@@ -0,0 +1,657 @@
[ ... ]
Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7
-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_SCALE ), + .info_mask_shared_by_all =BIT (IIO_CHAN_INFO_INT_TIME ),
drivers/iio/light/veml6031x00.c line 138
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_separate_available = 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 ) | + BIT(IIO_CHAN_INFO_SCALE),
[Severity: Medium]
Does this code expose hardware gain globally or independently per channel?
If hardware gain (gain and pd_div4) is globally applied to the entire chip, writing to a separate attribute must not silently alter another channel's state. Should shared hardware settings be exposed via info_mask_shared_by_all instead?
Additionally, the scale value returned for the IR channel is computed using a lux-to-count base which might be physically meaningless for raw IR intensity.
[ ... ]
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+static int veml6031x00_single_read (struct iio_dev *iio,enum iio_chan_type type, +int *val) +{
[ ... ]
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5
-0+IIO_DEV_ACQUIRE_DIRECT_MODE (iio, claim); +if (IIO_DEV_ACQUIRE_FAILED (claim)) +return -EBUSY ; + +PM_RUNTIME_ACQUIRE_AUTOSUSPEND (data->dev, pm);
drivers/iio/light/veml6031x00.c line 379
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ ret =PM_RUNTIME_ACQUIRE_ERR (&pm); +if (ret) +return ret; + + ret =veml6031x00_get_it (data, &it_usec); +if (ret <0 ) +return ret; + +/* integration time + 10 % to ensure completion */ +fsleep (it_usec + (it_usec /10 ));
[Severity: Medium]
Does this unconditionally sleep for the integration time on every raw read?
Since the device measures continuously when active, this delay is likely only needed if the device was just resumed from sleep. Forcing this delay on every read unnecessarily throttles the maximum polling read rate.
Could this be optimized by manually calling pm_runtime_get_sync() and checking its return value (which returns 1 if already active) to apply the sleep only when strictly necessary?
[ ... ]
Quoted 4 lines
drivers/iio/light/veml6031x00.c new file +4
-0+static int veml6031x00_read_raw (struct iio_dev *iio, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{
drivers/iio/light/veml6031x00.c line 402
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+struct veml6031x00_data *data =iio_priv (iio); + +switch (mask) { +case IIO_CHAN_INFO_RAW : +return veml6031x00_single_read (iio, chan->type, val); +case IIO_CHAN_INFO_INT_TIME : + *val =0 ; +return veml6031x00_get_it (data, val2); +case IIO_CHAN_INFO_SCALE : +return veml6031x00_get_scale (data, val, val2);
[Severity: Medium]
Could concurrent reads and writes cause torn reads of configuration fields here?
When handling IIO_CHAN_INFO_SCALE or IIO_CHAN_INFO_INT_TIME, this function does not acquire the IIO direct mode lock or any other mutex. It performs multiple non-atomic regmap_field_read() operations (e.g., reading both gain and pd_div4 in veml6031x00_get_scale()).
If a sysfs read interleaves with a write, the read might fetch a transient, inconsistent mix of old and new register bits.
[ ... ]
drivers/iio/light/veml6031x00.c line 437
Quoted 13 lines
drivers/iio/light/veml6031x00.c new file +13
-0+static int veml6031x00_write_raw (struct iio_dev *iio, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +IIO_DEV_ACQUIRE_DIRECT_MODE (iio, claim); +if (IIO_DEV_ACQUIRE_FAILED (claim)) +return -EBUSY ; + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : +return veml6031x00_set_it (iio, val, val2); +case IIO_CHAN_INFO_SCALE : +return veml6031x00_set_scale (iio, val, val2);
[Severity: High]
Does this code modify hardware registers over I2C without acquiring a runtime PM reference?
Unlike veml6031x00_single_read(), this function directly writes to the sensor's configuration registers without a PM reference. Because the regmap is not configured with .pm_runtime = true, I2C transactions may be attempted while the client device (or its parent controller) is runtime suspended, which can trigger bus transfer errors.
[ ... ]
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+static int veml6031x00_probe (struct i2c_client *i2c) +{
[ ... ]
drivers/iio/light/veml6031x00.c line 536
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+ 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" );
[Severity: High]
Can this shutdown devres action perform I2C writes while the device is suspended?
Because this devres action is registered before devm_pm_runtime_set_active_enabled(), on driver unbind, PM is disabled and the device state is forced to suspended before the shutdown action executes due to devres LIFO execution order.
The shutdown action then attempts to perform I2C writes to the shutdown register. Accessing the I2C bus while the device state is suspended can cause bus errors or hangs.
[ ... ]
Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7
-0+ ret =devm_pm_runtime_set_active_enabled (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable runtime PM\n" ); + + ret =devm_pm_runtime_get_noresume (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to get runtime PM\n" );
[ ... ]
drivers/iio/light/veml6031x00.c line 567
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5
-0+ ret =veml6031x00_hw_init (iio); +if (ret) +return ret; + +pm_runtime_put_autosuspend (dev);
[Severity: High]
Does mixing devm_pm_runtime_get_noresume() with manual pm_runtime_put_autosuspend() cause a PM usage counter underflow?
The devm_pm_runtime_get_noresume(dev) call increments the PM usage counter and registers a devres action to decrement it on driver unbind.
However, the explicit pm_runtime_put_autosuspend(dev) decrements the usage counter to its pre-get state. When the driver is subsequently removed (or if probe fails after the put), the devres action will blindly decrement the counter a second time, resulting in an underflow.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Javier Carrasco
maintainerQuoted 8 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Mixingdevm_pm_runtime_get_noresume()with manualpm_runtime_put_autosuspend()causes a PM usage counter underflow.
- [High]veml6031x00_write_raw()modifies hardware registers over I2C without acquiring a runtime PM reference.
- [High] The shutdown devres action performs I2C writes without ensuring the device and its parent are resumed.
- [Medium] Missing locks inveml6031x00_read_raw()forIIO_CHAN_INFO_SCALEandIIO_CHAN_INFO_INT_TIMEallow torn reads of configuration fields.
- [Medium] Incorrect scale attribute exposure for the IR channel and failure to mark shared hardware attributes.
- [Medium] The driver unconditionally sleeps for the integration time on every raw read, introducing unnecessary latency.
--
...
Quoted 18 lines
drivers/iio/light/veml6031x00.c line 437
Quoted 13 lines
drivers/iio/light/veml6031x00.c new file +13
-0+static int veml6031x00_write_raw (struct iio_dev *iio, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +IIO_DEV_ACQUIRE_DIRECT_MODE (iio, claim); +if (IIO_DEV_ACQUIRE_FAILED (claim)) +return -EBUSY ; + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : +return veml6031x00_set_it (iio, val, val2); +case IIO_CHAN_INFO_SCALE : +return veml6031x00_set_scale (iio, val, val2);[Severity: High]
Does this code modify hardware registers over I2C without acquiring a runtime PM reference?
Quoted 6 lines
Unlike
veml6031x00_single_read(), this function directly writes to the sensor's configuration registers without a PM reference. Because the regmap is not configured with .pm_runtime = true, I2C transactions may be attempted while the client device (or its parent controller) is runtime suspended, which can trigger bus transfer errors.
The registers are always accessible as long as the device is powered, both in normal operation and power down. Therefore, this function cannot be compared to veml6031x00_single_read() that acquires a PM reference to carry out a measurement, and the write operations work fine.
Quoted 44 lines
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+static int veml6031x00_probe (struct i2c_client *i2c) +{[ ... ]
drivers/iio/light/veml6031x00.c line 536
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+ 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" );[Severity: High]
Can this shutdown devres action perform I2C writes while the device is suspended?Because this devres action is registered before
devm_pm_runtime_set_active_enabled(), on driver unbind, PM is disabled and the device state is forced to suspended before the shutdown action executes due to devres LIFO execution order.The shutdown action then attempts to perform I2C writes to the shutdown register. Accessing the I2C bus while the device state is suspended can cause bus errors or hangs.
[ ... ]
Quoted 7 lines
drivers/iio/light/veml6031x00.c new file +7
-0+ ret =devm_pm_runtime_set_active_enabled (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable runtime PM\n" ); + + ret =devm_pm_runtime_get_noresume (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to get runtime PM\n" );[ ... ]
drivers/iio/light/veml6031x00.c line 567
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5
-0+ ret =veml6031x00_hw_init (iio); +if (ret) +return ret; + +pm_runtime_put_autosuspend (dev);[Severity: High]
Does mixingdevm_pm_runtime_get_noresume()with manualpm_runtime_put_autosuspend()cause a PM usage counter underflow?
Quoted 7 lines
The
devm_pm_runtime_get_noresume(dev)call increments the PM usage counter and registers a devres action to decrement it on driver unbind.However, the explicit
pm_runtime_put_autosuspend(dev)decrements the usage counter to its pre-get state. When the driver is subsequently removed (or if probe fails after the put), the devres action will blindly decrement the counter a second time, resulting in an underflow.
I believe Sashiko is misinterpreting how this pattern works (not introduced by me, it is already available upstream in drivers/spi/atmel-quadspi.c). Decrementing the usage counter is fine because the devres action uses pm_runtime_put_noidle(), which in turns uses atomic_add_unless(usage_count, -1, 0) to avoid decrementing the usage_count if it is already 0. Sashiko has reported the same issue in other patches of this series.
Best,
Javier
Jonathan Cameron
maintainerQuoted 10 lines · On Sun, 24 May 2026 23:53:56 +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]>
A few things inline. Biggest one is that device driver specific state needs local well documented locking. Here the whole complex gain handling means there are a bunch of register field where the accesses to each set of them need to appear atomic.
Quoted 6 lines
drivers/iio/light/veml6031x00.c new file +0
-0
drivers/iio/light/veml6031x00.c line 10
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0@@ -0,0 +1,657 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* VEML6031X00 Ambient Light Sensor +* +* Copyright (c) 2026, Javier Carrasco <[email protected]> +*/ + +#include <linux/bitfield.h> +#include <linux/i2c.h> +#include <linux/module.h>
mod_devicetable.h is missing. Please check again for others.
drivers/iio/light/veml6031x00.c line 15
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +5
-0+#include <linux/pm_runtime.h> +#include <linux/regmap.h> +#include <linux/units.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h>
This isn't needed yet - bring it in when it is.
Quoted 14 lines
drivers/iio/light/veml6031x00.c new file +13
-0+#include <linux/iio/iio-gts-helper.h> +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); +if (ret) +return ret; + + ret =regmap_field_write (data->rf.it, it_sel); +if (ret)
drivers/iio/light/veml6031x00.c line 314
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+return ret; + + ret =regmap_field_write (data->rf.pd_div4, gain_sel >>2 ); +if (ret) +return ret; + +return regmap_field_write (data->rf.gain, gain_sel &0x03 ); +} + +static int veml6031x00_get_scale(struct veml6031x00_data *data, int *val,
One of the few thing sashiko raised on this patch that seems reasonable is this is doing a bunch of accesses to fields that are updated via other paths. It may make sense to ensure we get a consistent set and don't manage to hit the middle of a write elsewhere - such as in set_scale()
Given that this is a driver specific thing rather than a state transition matter, a local mutex should be used to ensure these are serialized. Using the direct mode claim is relying on an internal implementation detail (that there is a serializing lock in there).
Quoted 41 lines
drivers/iio/light/veml6031x00.c new file +41
-0+ int *val2) +{ +int gain, it, gain_reg, pd_div4, it_reg, ret, sel; + + 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; + + sel = (pd_div4 <<2 ) | gain_reg; + gain =iio_gts_find_gain_by_sel (&data->gts, sel); +if (gain <0 ) +return gain; + + ret =regmap_field_read (data->rf.it, &it_reg); +if (ret) +return ret; + + it =iio_gts_find_int_time_by_sel (&data->gts, it_reg); +if (it <0 ) +return it; + + ret =iio_gts_get_scale (&data->gts, gain, it, val, val2); +if (ret) +return ret; + +return IIO_VAL_INT_PLUS_NANO ; +} + +static int veml6031x00_single_read (struct iio_dev *iio,enum iio_chan_type type, +int *val) +{ +struct veml6031x00_data *data =iio_priv (iio); +int addr, it_usec, ret; +__le16 reg; + +switch (type) { +case IIO_LIGHT :
drivers/iio/light/veml6031x00.c line 365
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ addr =VEML6031X00_REG_ALS_L ; +break ; +case IIO_INTENSITY : + addr =VEML6031X00_REG_IR_L ; +break ; +default : +return -EINVAL ; + } + +IIO_DEV_ACQUIRE_DIRECT_MODE (iio, claim);
Doing this before you have added any means to get into any other mode is premature. This claim belongs in the next patch.
Quoted 38 lines
drivers/iio/light/veml6031x00.c new file +38
-0+if (IIO_DEV_ACQUIRE_FAILED (claim)) +return -EBUSY ; + +PM_RUNTIME_ACQUIRE_AUTOSUSPEND (data->dev, pm); + ret =PM_RUNTIME_ACQUIRE_ERR (&pm); +if (ret) +return ret; + + ret =veml6031x00_get_it (data, &it_usec); +if (ret <0 ) +return ret; + +/* integration time + 10 % to ensure completion */ +fsleep (it_usec + (it_usec /10 )); + + ret =regmap_bulk_read (data->regmap, addr, ®,sizeof (reg)); +if (ret) +return ret; + + *val =le16_to_cpu (reg); +return IIO_VAL_INT ; +} + +static int veml6031x00_read_raw (struct iio_dev *iio, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct veml6031x00_data *data =iio_priv (iio); + +switch (mask) { +case IIO_CHAN_INFO_RAW : +return veml6031x00_single_read (iio, chan->type, val); +case IIO_CHAN_INFO_INT_TIME : + *val =0 ; +return veml6031x00_get_it (data, val2); +case IIO_CHAN_INFO_SCALE : +return veml6031x00_get_scale (data, val, val2); +default :
drivers/iio/light/veml6031x00.c line 429
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +9
-0+return -EINVAL ; + } +} + +static int veml6031x00_write_raw (struct iio_dev *iio, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +IIO_DEV_ACQUIRE_DIRECT_MODE (iio, claim);
As above.
Quoted 39 lines
drivers/iio/light/veml6031x00.c new file +38
-0+if (IIO_DEV_ACQUIRE_FAILED (claim)) +return -EBUSY ; + +switch (mask) { +case IIO_CHAN_INFO_INT_TIME : +return veml6031x00_set_it (iio, val, val2); +case IIO_CHAN_INFO_SCALE : +return veml6031x00_set_scale (iio, val, val2); +default : +return -EINVAL ; + } +} + +static int veml6031x00_probe (struct i2c_client *i2c) +{ +struct device *dev = &i2c->dev; +struct veml6031x00_data *data; +struct iio_dev *iio; +struct regmap *regmap; +int ret; + + regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap), +"Failed to set regmap\n" ); + + iio =devm_iio_device_alloc (dev,sizeof (*data)); +if (!iio) +return -ENOMEM ; + + data =iio_priv (iio); +i2c_set_clientdata (i2c, iio); + data->dev = dev; + data->regmap = regmap; + + ret =veml6031x00_regfield_init (data); +if (ret) +return dev_err_probe (dev, ret,"Failed to init regfield\n" );
drivers/iio/light/veml6031x00.c line 534
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to enable regulator\n" ); + + data->chip =i2c_get_match_data (i2c); +if (!data->chip) +return dev_err_probe (dev, -EINVAL ,"Failed to get chip data\n" ); + + ret =devm_add_action_or_reset (dev, veml6031x00_als_shutdown_action, data);
Why is shutdown cleanup registered before the power_on? If that has to happen then add a comment on why here. Otherwise move it down a few lines.
Quoted 48 lines
drivers/iio/light/veml6031x00.c new file +47
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to add shutdown action\n" ); + +/* The device starts in power down mode by default */ + ret =veml6031x00_als_power_on (data); +if (ret) +return dev_err_probe (dev, ret,"Failed to power on the device\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" ); + + ret =devm_pm_runtime_get_noresume (dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to get runtime PM\n" ); + + ret =veml6031x00_validate_part_id (data); +if (ret) +return ret; + + 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 =veml6031x00_hw_init (iio); +if (ret) +return ret; + +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 ; +} + +static const struct of_device_id veml6031x00_of_match[] = { + { + .compatible ="vishay,veml6031x00" , + .data = &veml6031x00_chip, + }, + {
drivers/iio/light/veml6031x00.c line 628
Quoted 10 lines
drivers/iio/light/veml6031x00.c new file +10
-0+ .compatible = "vishay,veml6031x01", + .data = &veml6031x01_chip, + }, + { + .compatible = "vishay,veml60311x00", + .data = &veml60311x00_chip, + }, + { + .compatible = "vishay,veml60311x01", + .data = &veml60311x01_chip,
These would actually end up shorter than the i2c_device_id ones below if they were on one line. Pick a style and use it for both tables. I prefer this one a little.
Quoted 30 lines
drivers/iio/light/veml6031x00.c new file +29
-0+ }, + { } +}; +MODULE_DEVICE_TABLE (of, veml6031x00_of_match); + +static const struct i2c_device_id veml6031x00_id[] = { + { .name ="veml6031x00" , .driver_data = (kernel_ulong_t)&veml6031x00_chip }, + { .name ="veml6031x01" , .driver_data = (kernel_ulong_t)&veml6031x01_chip }, + { .name ="veml60311x00" , .driver_data = (kernel_ulong_t)&veml60311x00_chip }, + { .name ="veml60311x01" , .driver_data = (kernel_ulong_t)&veml60311x01_chip }, + { } +}; +MODULE_DEVICE_TABLE (i2c, veml6031x00_id); + +static struct i2c_driver veml6031x00_driver = { + .driver = { + .name ="veml6031x00" , + .of_match_table = veml6031x00_of_match, + .pm =pm_ptr (&veml6031x00_pm_ops), + }, + .probe = veml6031x00_probe, + .id_table = veml6031x00_id, +}; +module_i2c_driver (veml6031x00_driver); + +MODULE_AUTHOR ("Javier Carrasco <[email protected]>" ); +MODULE_DESCRIPTION ("VEML6031X00 Ambient Light Sensor" ); +MODULE_LICENSE ("GPL" ); +MODULE_IMPORT_NS ("IIO_GTS_HELPER" );
Jonathan Cameron
maintainerQuoted 12 lines · On Tue, 26 May 2026 18:59:33 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sun, 24 May 2026 23:53:56 +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.
Quoted 6 lines
Quoted 1 line
Signed-off-by Javier Carrasco <[email protected]>A few things inline. Biggest one is that device driver specific state needs local well documented locking. Here the whole complex gain handling means there are a bunch of register field where the accesses to each set of them need to appear atomic.
The sashiko comment on underflowing pm counters also looks plausible so check that as well.
Javier Carrasco
maintainerQuoted 15 lines · Jonathan Cameron
Quoted 12 lines · On Tue, 26 May 2026 18:59:33 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sun, 24 May 2026 23:53:56 +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.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Signed-off-by Javier Carrasco <[email protected]>A few things inline. Biggest one is that device driver specific state needs local well documented locking. Here the whole complex gain handling means there are a bunch of register field where the accesses to each set of them need to appear atomic.
The sashiko comment on underflowing pm counters also looks plausible so check that as well.
Hi Jonathan, thanks for your review to this and the other patches of the series. I will go through all your comments within the next days.
I replied to its comment on the PM underflow, but as I only replied to it, and then I forwarded the message, it might have gone unseen.
I believe Sashiko is misinterpreting how this pattern works (not introduced by me, it is already available upstream in drivers/spi/atmel-quadspi.c). Decrementing the usage counter is fine because the devres action uses pm_runtime_put_noidle(), which in turns uses atomic_add_unless(usage_count, -1, 0) to avoid decrementing the usage_count if it is already 0. Otherwise the devm_ variant would be unusable because it would not be possible to put the device in autosuspend for the same reason Sashiko is complaining.
Best regards,
Javier
Quoted 2 lines · Jonathan Cameron
On Sun, 24 May 2026 23:53:56 +0200
Javier Carrasco <[email protected]> wrote:
...
Quoted 5 lines
drivers/iio/light/veml6031x00.c line 10
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+#include <linux/bitfield.h> +#include <linux/i2c.h> +#include <linux/module.h>
mod_devicetable.his missing. Please check again for others.
Yep. Please, follow IWYU principle.
Quoted 3 lines
drivers/iio/light/veml6031x00.c line 13
Quoted 3 lines
drivers/iio/light/veml6031x00.c new file +3
-0+#include <linux/pm_runtime.h> +#include <linux/regmap.h> +#include <linux/units.h>
Also add a blank line here to group out the linux/iio/*.
Quoted 5 lines
drivers/iio/light/veml6031x00.c line 15
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> This isn't needed yet - bring it in when it is.
Quoted 1 line
+#include <
linux/iio/iio-gts-helper.h>
...
Quoted 5 lines
drivers/iio/light/veml6031x00.c line 511
Quoted 5 lines
drivers/iio/light/veml6031x00.c new file +4
-0+ regmap =devm_regmap_init_i2c (i2c, &veml6031x00_regmap_config); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap), +"Failed to set regmap\n" );
It's okay to have it on a single line.
...
Quoted 2 lines
drivers/iio/light/veml6031x00.c line 520
Quoted 2 lines
drivers/iio/light/veml6031x00.c new file +2
-0+ data->dev = dev; + data->regmap = regmap;
This is not needed. Use one of them.
...
I have a déjà vu that I have given already same comments. Why are they not addressed?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Jonathan Cameron
On Sun, 24 May 2026 23:53:56 +0200
Javier Carrasco <[email protected]> wrote:
...
Quoted 2 lines
I have a déjà vu that I have given already same comments. Why are they not addressed?
Okay, it's v3, I reviewed already v4.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Javier Carrasco
maintainerPATCH v3 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 | 106 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+)
drivers/iio/light/Kconfig +2 -0
@@ -717,6 +717,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 +106 -0
143 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v3 3/4] iio: light: veml6031x00: add support for triggered buffers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The trigger handler does not skip the soft timestamp channel, leading to an out-of-bounds array…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The trigger handler does not skip the soft timestamp channel, leading to an out-of-bounds array write and an invalid I2C read.
- [Medium] Runtime PM usage counter underflow on device unbind due to mixing devm_pm_runtime_get_noresume() with a manual pm_runtime_put_autosuspend(). --
commit 7b07348868ecc638382ae3a2ca8a3bea1a0c8680
Author: Javier Carrasco <[email protected]>
iio: light: veml6031x00: add support for triggered buffers
Add triggered buffer functionality for the two channels the device provides (ALS and IR).
Quoted 4 lines
drivers/iio/light/veml6031x00.c +0
-0
[ ... ]
Quoted 3 lines
drivers/iio/light/veml6031x00.c +0
-0@@ -461,6 +484,82 @@ static const struct iio_info veml6031x00_info = { .write_raw_get_fmt = veml6031x00_write_raw_get_fmt, };
[ ... ]
Quoted 14 lines
drivers/iio/light/veml6031x00.c +14
-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); +int ch, ret, i =0 ; +struct { +__le16 chans[2 ]; +aligned_s64 timestamp; + } scan = { }; + +if (*iio->active_scan_mask == (BIT (VEML6031X00_SCAN_ALS ) | +BIT (VEML6031X00_SCAN_IR ))) { + ret = regmap_bulk_read(data->regmap,
drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 549
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+VEML6031X00_REG_ALS_L , + &scan.chans, sizeof(scan.chans)); +if (ret) + goto done; + }else { +iio_for_each_active_channel (iio, ch) { + ret =regmap_bulk_read (data->regmap, + iio->channels[ch].address, + &scan.chans[i++], +sizeof (*scan.chans));
[Severity: High]
Does this loop properly handle the timestamp channel?
If the user enables the timestamp channel alongside ALS and IR, the timestamp bit is included in iio->active_scan_mask. The optimization check above will evaluate to false, falling back to this else block.
Inside this loop, the timestamp channel isn't skipped. Since the soft timestamp channel has no assigned address, does it default to 0 and cause an unintended I2C read of the configuration register?
Additionally, scan.chans is defined as an array of 2 elements. Could this cause an out-of-bounds write to scan.chans[2] as i increments, corrupting the struct padding or timestamp field?
Quoted 12 lines
drivers/iio/light/veml6031x00.c +12
-0+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 ; +}
[ ... ]
drivers/iio/light/veml6031x00.c static int veml6031x00_probe(struct i2c_client *i2c) · line 673
Quoted 12 lines
drivers/iio/light/veml6031x00.c +7
-0@@ -564,6 +663,13 @@ static int veml6031x00_probe(struct i2c_client *i2c)if (ret)return ret; + ret =devm_iio_triggered_buffer_setup (dev, iio,NULL , + veml6031x00_trig_handler, + &veml6031x00_buffer_setup_ops); +if (ret) +return dev_err_probe (dev, ret, +"Failed to register triggered buffer\n" ); +pm_runtime_put_autosuspend (dev);
[Severity: Medium]
Can this manual pm_runtime_put_autosuspend() cause a usage counter underflow on device unbind?
The driver acquires a runtime PM reference earlier using devm_pm_runtime_get_noresume(), which registers a devres cleanup action to decrement the usage counter on unbind.
With this manual put in probe, will the devres unbind action drop the counter below zero and trigger a warning in the PM core?
Quoted 2 lines
drivers/iio/light/veml6031x00.c +0
-0ret =devm_iio_device_register (dev, iio);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Jonathan Cameron
maintainerRe: [PATCH v3 3/4] iio: light: veml6031x00: add support for triggered buffers
In reply to Javier CarrascoQuoted 4 lines · On Sun, 24 May 2026 23:53:57 +0200 Javier Carrasco…
Add triggered buffer functionality for the two channels the device provides (ALS and IR).
Signed-off-by Javier Carrasco <[email protected]>
Trivial stuff only.
Quoted 23 lines
drivers/iio/light/veml6031x00.c +18
-0+ +static int veml6031x00_buffer_postdisable (struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); + +pm_runtime_put_autosuspend (data->dev); + +return 0 ; +} + +static const struct iio_buffer_setup_ops veml6031x00_buffer_setup_ops = { + .preenable = veml6031x00_buffer_preenable, + .postdisable = veml6031x00_buffer_postdisable, +}; + +static irqreturn_t veml6031x00_trig_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p;
drivers/iio/light/veml6031x00.c static const struct iio_info veml6031x00_info = { · line 538
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+struct iio_dev *iio = pf->indio_dev; +struct veml6031x00_data *data =iio_priv (iio); +int ch, ret, i =0 ; +struct { +__le16 chans[2 ]; +aligned_s64 timestamp; + } scan = { }; + +if (*iio->active_scan_mask == (BIT (VEML6031X00_SCAN_ALS ) | +BIT (VEML6031X00_SCAN_IR ))) {
I'd prefer we always treat that as a bitmap and do the more costly check test_bit(VEML6041X00_SCAN_ALS, iio->active_scan_mask) && test_bit(VEML6041X00_SCAN_IR, iio->active_scan_mask)
Quoted 23 lines
drivers/iio/light/veml6031x00.c +23
-0+ ret =regmap_bulk_read (data->regmap, +VEML6031X00_REG_ALS_L , + &scan.chans,sizeof (scan.chans)); +if (ret) + goto done; + }else { +iio_for_each_active_channel (iio, ch) { + ret =regmap_bulk_read (data->regmap, + iio->channels[ch].address, + &scan.chans[i++], +sizeof (*scan.chans)); +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 ; +}
Javier Carrasco
maintainerPATCH v3 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 | 442 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 438 insertions(+), 4 deletions(-)
drivers/iio/light/veml6031x00.c +438 -4
@@ -7,12 +7,16 @@#include <linux/bitfield.h> #include <linux/i2c.h> +#include <linux/interrupt.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/pm_runtime.h> #include <linux/regmap.h> #include <linux/units.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>
@@ -20,16 +24,29 @@/* 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)enum veml6031x00_scan {VEML6031X00_SCAN_ALS ,
@@ -39,8 +56,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 {
@@ -52,8 +71,14 @@ struct veml6031x00_data {struct device *dev;struct iio_gts gts;struct regmap *regmap; +struct iio_trigger *trig;struct veml6031x00_rf rf;const struct veml6031x00_chip *chip; +/* serialize access to irq enable/disable by events and trigger */ +struct mutex lock; +int int_users; +bool ev_en; +bool trig_en; };static const struct iio_itime_sel_mul veml6031x00_it_sel[] = {
@@ -88,6 +113,17 @@ static const struct iio_gain_sel_pair veml6031x00_gain_sel[] = { GAIN_SCALE_GAIN(16, VEML6031X00_SEL_MILLI_GAIN_X2000), }; +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, +}; +/* * Two shutdown bits (SD and ALS_IR_SD) must be cleared to power on * the device.
@@ -127,6 +163,23 @@ static void veml6031x00_als_shutdown_action(void *data)veml6031x00_als_shutdown (data); } +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 ,
@@ -136,6 +189,8 @@ static const struct iio_chan_spec veml6031x00_channels[] = { .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), .info_mask_separate_available = 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 = { .sign = 'u',
@@ -166,7 +221,9 @@ static const struct iio_chan_spec veml6031x00_channels[] = {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 = {
@@ -175,7 +232,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 = {
@@ -185,6 +242,7 @@ static const struct regmap_access_table veml6031x00_writable_table = {static const struct regmap_range veml6031x00_volatile_ranges[] = {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 = {
@@ -192,6 +250,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 ,
@@ -199,13 +266,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 );
@@ -224,6 +298,11 @@ static int veml6031x00_regfield_init(struct veml6031x00_data *data)return PTR_ERR (rm_field); rf->gain = rm_field; + rm_field =devm_regmap_field_alloc (dev, 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, regmap, veml6031x00_rf_it);if (IS_ERR (rm_field))return PTR_ERR (rm_field);
@@ -234,6 +313,11 @@ static int veml6031x00_regfield_init(struct veml6031x00_data *data)return PTR_ERR (rm_field); rf->pd_div4 = rm_field; + rm_field =devm_regmap_field_alloc (dev, regmap, veml6031x00_rf_pers); +if (IS_ERR (rm_field)) +return PTR_ERR (rm_field); + rf->pers = rm_field; +return 0 ; }
@@ -313,6 +397,30 @@ static int veml6031x00_set_it(struct iio_dev *iio, int val, int val2)return regmap_field_write (data->rf.gain, gain_sel &0x03 ); } +static int veml6031x00_read_period (struct iio_dev *iio,int *val) +{ +struct veml6031x00_data *data =iio_priv (iio); +int ret, reg; + + ret =regmap_field_read (data->rf.pers, ®); +if (ret) +return ret; + + *val =1 << reg; + +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);
@@ -367,6 +475,51 @@ 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 reg; +int ret; + +if (dir ==IIO_EV_DIR_RISING ) + ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_WH_L , + ®,sizeof (reg)); +else + ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_WL_L , + ®,sizeof (reg)); +if (ret) +return ret; + + *val =le16_to_cpu (reg); + +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 = data->dev; +__le16 reg =cpu_to_le16 (val); +int ret; + +if (val <0 || val >U16_MAX || val2) +return -EINVAL ; + +if (dir ==IIO_EV_DIR_RISING ) { + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WH_L , + ®,sizeof (reg)); +if (ret) +dev_dbg (dev,"Failed to set high threshold %d\n" , ret); + }else { + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WL_L , + ®,sizeof (reg)); +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) {
@@ -477,13 +630,189 @@ 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->lock) +{ +int ret; + +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->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); +int ret; + +guard (mutex)(&data->lock); + +/* avoid multiple increments/decrements from one source */ +if (state == data->ev_en) +return 0 ; + +if (state) { + ret =pm_runtime_resume_and_get (data->dev); +if (ret) +return ret; + } + + ret =veml6031x00_set_interrupt (data, state); +if (ret) { +if (state) +pm_runtime_put_autosuspend (data->dev); +return ret; + } + + data->ev_en = state; + +if (!state) +pm_runtime_put_autosuspend (data->dev); + +return 0 ; +} +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, }; +/* AF_TRIG is reset by hardware, but the rest of the fields are persistent */ +static int veml6031x00_set_af_trig (struct veml6031x00_data *data,bool state) +{ +regcache_drop_region (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_REG_CONF0 ); + +return regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +} + +static irqreturn_t veml6031x00_interrupt (int irq,void *private) +{ +struct iio_dev *iio = private; +struct veml6031x00_data *data =iio_priv (iio); +s64 timestamp; +int ret, reg; + + ret =regmap_read (data->regmap,VEML6031X00_REG_INT , ®); +if (ret) { +dev_err (data->dev, +"Failed to read interrupt register %d\n" , ret); +return IRQ_NONE ; + } + +if (!(reg &VEML6031X00_INT_MASK )) +return IRQ_NONE ; + +guard (mutex)(&data->lock); + +if ((reg & (VEML6031X00_INT_TH_H |VEML6031X00_INT_TH_L )) && data->ev_en) { + timestamp =iio_get_time_ns (iio); + +if (reg &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 (reg &VEML6031X00_INT_TH_L ) +iio_push_event (iio,IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + } + +if ((reg &VEML6031X00_INT_DRDY ) && data->trig_en) { +iio_trigger_poll_nested (data->trig); + ret =veml6031x00_set_af_trig (data, true); +if (ret) +dev_err (data->dev,"Failed to set trigger %d\n" , ret); + } + +return IRQ_HANDLED ; +} +static int veml6031x00_buffer_preenable (struct iio_dev *iio) {struct veml6031x00_data *data =iio_priv (iio);
@@ -518,11 +847,54 @@ 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->lock); + +if (state == data->trig_en) +return 0 ; + + ret =veml6031x00_set_interrupt (data, state); +if (ret) +return ret; + +/* The AF bit must be set before setting AF_TRIG */ + ret =regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , state)); +if (ret) + goto err_disable_interrupt; + + ret =veml6031x00_set_af_trig (data, state); +if (ret) + goto err_clear_af; + + data->trig_en = state; + +return 0 ; + +err_clear_af: +regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF , +FIELD_PREP (VEML6031X00_CONF0_AF , !state)); +err_disable_interrupt: +veml6031x00_set_interrupt (data, !state); +return ret; +} +static const struct iio_buffer_setup_ops veml6031x00_buffer_setup_ops = { .preenable = veml6031x00_buffer_preenable, .postdisable = veml6031x00_buffer_postdisable, }; +static const struct iio_trigger_ops veml6031x00_trigger_ops = { + .set_trigger_state = veml6031x00_set_trigger_state, +}; +static irqreturn_t veml6031x00_trig_handler (int irq,void *p) {struct iio_poll_func *pf = p;
@@ -581,7 +953,8 @@ static int veml6031x00_hw_init(struct iio_dev *iio) {struct veml6031x00_data *data =iio_priv (iio);struct device *dev = data->dev; -int ret; +int ret, val; +__le16 reg;/* Max resolution = 6.9632 lx/cnt for gain = 0.125 and IT = 3.125ms */ ret = devm_iio_init_iio_gts(dev, 6, 963200000,
@@ -593,6 +966,54 @@ static int veml6031x00_hw_init(struct iio_dev *iio)if (ret)return dev_err_probe (dev, ret,"failed to init iio gts\n" ); + reg =0 ; + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WL_L , ®,sizeof (reg)); +if (ret) +return dev_err_probe (dev, ret,"Failed to set low threshold\n" ); + + reg =cpu_to_le16 (U16_MAX ); + ret =regmap_bulk_write (data->regmap,VEML6031X00_REG_WH_L , ®,sizeof (reg)); +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 = data->dev; +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); + ret =devm_request_threaded_irq (dev, i2c->irq,NULL , + veml6031x00_interrupt, +IRQF_ONESHOT , + iio->name, iio); +if (ret) +return dev_err_probe (dev, ret,"Failed to request irq %d\n" , + i2c->irq); +return 0 ; }
@@ -618,6 +1039,10 @@ static int veml6031x00_probe(struct i2c_client *i2c) data->dev = dev; data->regmap = regmap; + ret =devm_mutex_init (dev, &data->lock); +if (ret) +return ret; + ret =veml6031x00_regfield_init (data);if (ret)return dev_err_probe (dev, ret,"Failed to init regfield\n" );
@@ -657,12 +1082,21 @@ static int veml6031x00_probe(struct i2c_client *i2c) iio->channels = veml6031x00_channels; iio->num_channels =ARRAY_SIZE (veml6031x00_channels); iio->modes =INDIO_DIRECT_MODE ; - iio->info = &veml6031x00_info; ret =veml6031x00_hw_init (iio);if (ret)return ret; +if (i2c->irq) { + ret =veml6031x00_setup_irq (i2c, iio); +if (ret) +return ret; + + iio->info = &veml6031x00_info; + }else { + iio->info = &veml6031x00_info_no_irq; + } + ret =devm_iio_triggered_buffer_setup (dev, iio,NULL , veml6031x00_trig_handler, &veml6031x00_buffer_setup_ops);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v3 4/4] iio: light: veml6031x00: add support for events and trigger
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
New issues:
- [High] Cache pollution of AF_TRIG causes unintended conversions and unnecessary I2C reads.
- [High] The trigger reference acquired via iio_trigger_get() is leaked on probe error paths and driver unbind.
- [High] PM runtime usage counter leaks if the driver unbinds while an event is enabled.
- [High] Sysfs read/write functions and trigger operations access hardware without acquiring a PM runtime reference.
- [High] Out-of-bounds array write in veml6031x00_trig_handler when the timestamp channel is enabled.
- [Medium] Dead event sysfs attributes are exposed when no IRQ is present.
Pre-existing issues:
- [High] Unbalanced double-put of the runtime PM usage counter on driver unbind.
- [High] The shutdown action accesses I2C registers after PM runtime has been disabled. --
commit cbdd69d1454af4788d5d18a23d3fb6854c9981e9
Author: Javier Carrasco <[email protected]>
iio: light: veml6031x00: add support for events and trigger
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.
Quoted 4 lines
drivers/iio/light/veml6031x00.c +0
-0
[ ... ]
drivers/iio/light/veml6031x00.c static const struct iio_chan_spec veml6031x00_channels[] = { · line 193
Quoted 6 lines
drivers/iio/light/veml6031x00.c +2
-0@@ -136,6 +189,8 @@ static const struct iio_chan_spec veml6031x00_channels[] = { .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), + .event_spec = veml6031x00_event_spec, + .num_event_specs = ARRAY_SIZE(veml6031x00_event_spec),
[Severity: Medium]
Will this expose dead event sysfs attributes when no IRQ is present?
When i2c->irq is 0, the driver registers veml6031x00_info_no_irq which lacks event callbacks. However, this channel array unconditionally defines event_spec and num_event_specs. The IIO core will parse these and expose event sysfs files. Reading or writing these files will fail with -EINVAL because the callbacks are NULL, which violates the sysfs API contract.
[ ... ]
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 665
Quoted 7 lines
drivers/iio/light/veml6031x00.c +7
-0+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) +{
[Severity: High]
Are PM runtime references needed here before accessing hardware?
This function, as well as veml6031x00_write_event_val() and veml6031x00_set_trigger_state(), performs I2C accesses via regmap without first calling pm_runtime_resume_and_get(). If the device is runtime-suspended and its PM domain has powered it off, will these I2C accesses fail or time out?
[ ... ]
Quoted 7 lines
drivers/iio/light/veml6031x00.c +7
-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) +{ +struct veml6031x00_data *data =iio_priv (iio);
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 722
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+int ret; + +guard (mutex)(&data->lock); + +/* avoid multiple increments/decrements from one source */ +if (state == data->ev_en) +return 0 ; + +if (state) { + ret =pm_runtime_resume_and_get (data->dev);
[Severity: High]
Could this leak the PM runtime usage counter if the driver unbinds?
Enabling an event increments the PM runtime usage counter here. If the driver is unbound via sysfs while an event is enabled, the IIO core does not automatically disable the event. Since there is no unbind cleanup mechanism to release the pending PM reference when data->ev_en is true, does this keep the device powered on indefinitely?
[ ... ]
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 770
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+/* AF_TRIG is reset by hardware, but the rest of the fields are persistent */ +static int veml6031x00_set_af_trig (struct veml6031x00_data *data,bool state) +{ +regcache_drop_region (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_REG_CONF0 ); + +return regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +}
[Severity: High]
Can this cause unintentional active force conversions due to cache pollution?
This sets the self-clearing AF_TRIG bit using regmap_update_bits(). Because CONF0 is not marked as a volatile register, regmap_update_bits() writes AF_TRIG=1 and caches it. Any subsequent read-modify-write on CONF0 (e.g., toggling the interrupt or changing integration time) will read the cached AF_TRIG=1 and write it back to hardware, unintentionally triggering an active force conversion and causing hardware state corruption.
[ ... ]
Quoted 13 lines
static
irqreturn_tveml6031x00_trig_handler(int irq, void *p){drivers/iio/light/veml6031x00.c +0
-0struct iio_poll_func *pf = p;struct iio_dev *iio = pf->indio_dev;struct veml6031x00_data *data =iio_priv (iio);int ch, ret, i =0 ;struct {__le16 chans[2 ];aligned_s64 timestamp; } scan = { };if (*iio->active_scan_mask == (BIT (VEML6031X00_SCAN_ALS ) |BIT (VEML6031X00_SCAN_IR ))) {
drivers/iio/light/veml6031x00.c
Quoted 10 lines
drivers/iio/light/veml6031x00.c +0
-0ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_ALS_L , &scan.chans,sizeof (scan.chans));if (ret) goto done; }else {iio_for_each_active_channel (iio, ch) { ret = regmap_bulk_read(data->regmap, iio->channels[ch].address, &scan.chans[i++],
[Severity: High]
Does this loop cause an out-of-bounds array write if the timestamp channel is enabled?
The iio_for_each_active_channel() loop does not exclude the soft timestamp channel. If the user enables ALS, IR, and TIMESTAMP channels, the loop iterates 3 times. However, the scan.chans array is declared as __le16 chans[2]. Processing the timestamp channel increments the index i to 2 and writes to scan.chans[2], causing an out-of-bounds memory write.
Furthermore, because the timestamp channel's address is 0, this incorrectly attempts a bogus I2C read from VEML6031X00_REG_CONF0.
[ ... ]
Quoted 9 lines
drivers/iio/light/veml6031x00.c +9
-0+static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev = data->dev; +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 1008
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);
[Severity: High]
Is the trigger reference acquired here leaked on driver unbind?
Calling iio_trigger_get() increments the trigger device's refcount and the driver module's refcount. If the driver fails probe afterwards or is unbound later, iio_trigger_put() is never called because there is no devres action registered for it and no custom remove callback. Does this permanently leak the trigger device reference and prevent the module from being unloaded?
[ ... ]
Quoted 1 line
drivers/iio/light/veml6031x00.c +0
-0@@ -618,6 +1039,10 @@ static int veml6031x00_probe(struct i2c_client *i2c)
[ ... ]
Quoted 1 line
ret =
devm_add_action_or_reset(dev, veml6031x00_als_shutdown_action, data);
[Severity: High]
This is a pre-existing issue, but does this shutdown action access I2C registers after PM runtime has been disabled?
This action is registered via devm_add_action_or_reset() before PM runtime is enabled. On teardown, devm actions run in reverse, meaning PM runtime is disabled before the shutdown action runs. The shutdown action then attempts I2C writes while PM runtime is disabled. If the device was suspended, it cannot be resumed, potentially causing I2C transfer failures.
[ ... ]
Quoted 1 line
pm_runtime_put_autosuspend(dev);
[Severity: High]
This is a pre-existing issue, but is there an unbalanced double-put of the runtime PM usage counter on driver unbind?
The probe function registers a cleanup action to drop the PM reference via devm_pm_runtime_get_noresume(dev). However, this explicit put at the end of probe means the devm action will drop the reference a second time on unbind, causing a PM usage counter underflow that breaks the PM runtime state machine.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Javier Carrasco
maintainerRe: [PATCH v3 4/4] iio: light: veml6031x00: add support for events and trigger
Quoted 42 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
New issues:
- [High] Cache pollution ofAF_TRIGcauses unintended conversions and unnecessary I2C reads.
- [High] The trigger reference acquired viaiio_trigger_get()is leaked on probe error paths and driver unbind.
- [High] PM runtime usage counter leaks if the driver unbinds while an event is enabled.
- [High] Sysfs read/write functions and trigger operations access hardware without acquiring a PM runtime reference.
- [High] Out-of-bounds array write inveml6031x00_trig_handlerwhen the timestamp channel is enabled.
- [Medium] Dead event sysfs attributes are exposed when no IRQ is present.Pre-existing issues:
- [High] Unbalanced double-put of the runtime PM usage counter on driver unbind.
- [High] The shutdown action accesses I2C registers after PM runtime has been disabled. --commit
cbdd69d1454af4788d5d18a23d3fb6854c9981e9
Author: Javier Carrasco <[email protected]>iio: light: veml6031x00: add support for events and trigger
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.
Quoted 4 lines
drivers/iio/light/veml6031x00.c +0
-0[ ... ]
drivers/iio/light/veml6031x00.c static const struct iio_chan_spec veml6031x00_channels[] = { · line 193
Quoted 6 lines
drivers/iio/light/veml6031x00.c +2
-0@@ -136,6 +189,8 @@ static const struct iio_chan_spec veml6031x00_channels[] = { .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), + .event_spec = veml6031x00_event_spec, + .num_event_specs = ARRAY_SIZE(veml6031x00_event_spec),[Severity: Medium]
Will this expose dead event sysfs attributes when no IRQ is present?
Quoted 8 lines
When
i2c->irqis 0, the driver registersveml6031x00_info_no_irqwhich lacks event callbacks. However, this channel array unconditionally definesevent_specandnum_event_specs. The IIO core will parse these and expose event sysfs files. Reading or writing these files will fail with-EINVALbecause the callbacks are NULL, which violates the sysfs API contract.[ ... ]
@Jonathan: is this how it really works? How can I then dynamically define even_spec to account for it?
Quoted 11 lines
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 665
Quoted 7 lines
drivers/iio/light/veml6031x00.c +7
-0+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) +{[Severity: High]
Are PM runtime references needed here before accessing hardware?
Quoted 8 lines
This function, as well as
veml6031x00_write_event_val()andveml6031x00_set_trigger_state(), performs I2C accesses via regmap without first callingpm_runtime_resume_and_get(). If the device is runtime-suspended and its PM domain has powered it off, will these I2C accesses fail or time out?[ ... ]
As I said for [2/4], reading registers in shutdown mode is absolutely fine.
Quoted 21 lines
Quoted 7 lines
drivers/iio/light/veml6031x00.c +7
-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) +{ +struct veml6031x00_data *data =iio_priv (iio);drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 722
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+int ret; + +guard (mutex)(&data->lock); + +/* avoid multiple increments/decrements from one source */ +if (state == data->ev_en) +return 0 ; + +if (state) { + ret =pm_runtime_resume_and_get (data->dev);[Severity: High]
Could this leak the PM runtime usage counter if the driver unbinds?
Quoted 8 lines
Enabling an event increments the PM runtime usage counter here. If the driver is unbound via sysfs while an event is enabled, the IIO core does not automatically disable the event. Since there is no unbind cleanup mechanism to release the pending PM reference when
data->ev_enis true, does this keep the device powered on indefinitely?[ ... ]
@Jonathan: Does the IIO core let the event enabled? What is the approach to clear it then?
Quoted 14 lines
drivers/iio/light/veml6031x00.c static int veml6031x00_write_raw_get_fmt(struct iio_dev *indio_dev, · line 770
Quoted 10 lines
drivers/iio/light/veml6031x00.c +10
-0+/* AF_TRIG is reset by hardware, but the rest of the fields are persistent */ +static int veml6031x00_set_af_trig (struct veml6031x00_data *data,bool state) +{ +regcache_drop_region (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_REG_CONF0 ); + +return regmap_update_bits (data->regmap,VEML6031X00_REG_CONF0 , +VEML6031X00_CONF0_AF_TRIG , +FIELD_PREP (VEML6031X00_CONF0_AF_TRIG , state)); +}[Severity: High]
Can this cause unintentional active force conversions due to cache pollution?
Quoted 9 lines
This sets the self-clearing
AF_TRIGbit usingregmap_update_bits(). Because CONF0 is not marked as a volatile register,regmap_update_bits()writes AF_TRIG=1 and caches it. Any subsequent read-modify-write on CONF0 (e.g., toggling the interrupt or changing integration time) will read the cached AF_TRIG=1 and write it back to hardware, unintentionally triggering an active force conversion and causing hardware state corruption.[ ... ]
I will define the CONF0 register as volatile to account for this.
Quoted 34 lines
Quoted 13 lines
static
irqreturn_tveml6031x00_trig_handler(int irq, void *p){drivers/iio/light/veml6031x00.c +0
-0struct iio_poll_func *pf = p;struct iio_dev *iio = pf->indio_dev;struct veml6031x00_data *data =iio_priv (iio);int ch, ret, i =0 ;struct {__le16 chans[2 ];aligned_s64 timestamp; } scan = { };if (*iio->active_scan_mask == (BIT (VEML6031X00_SCAN_ALS ) |BIT (VEML6031X00_SCAN_IR ))) {drivers/iio/light/veml6031x00.c
Quoted 10 lines
drivers/iio/light/veml6031x00.c +0
-0ret =regmap_bulk_read (data->regmap,VEML6031X00_REG_ALS_L , &scan.chans,sizeof (scan.chans));if (ret) goto done; }else {iio_for_each_active_channel (iio, ch) { ret = regmap_bulk_read(data->regmap, iio->channels[ch].address, &scan.chans[i++],[Severity: High]
Does this loop cause an out-of-bounds array write if the timestamp channel is enabled?The
iio_for_each_active_channel()loop does not exclude the soft timestamp channel. If the user enables ALS, IR, and TIMESTAMP channels, the loop iterates 3 times. However, the scan.chans array is declared as__le16chans[2]. Processing the timestamp channel increments the index i to 2 and writes to scan.chans[2], causing an out-of-bounds memory write.
Quoted 5 lines
Furthermore, because the timestamp channel's address is 0, this incorrectly attempts a bogus I2C read from
VEML6031X00_REG_CONF0.[ ... ]
Not sure if this is a real issue, and the address for channel 0 is not CONF0 because it is defined in iio_chan_spec veml6031x00_channels.address.
Quoted 23 lines
Quoted 9 lines
drivers/iio/light/veml6031x00.c +9
-0+static int veml6031x00_setup_irq (struct i2c_client *i2c,struct iio_dev *iio) +{ +struct veml6031x00_data *data =iio_priv (iio); +struct device *dev = data->dev; +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 1008
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);[Severity: High]
Is the trigger reference acquired here leaked on driver unbind?
Quoted 8 lines
Calling
iio_trigger_get()increments the trigger device's refcount and the driver module's refcount. If the driver fails probe afterwards or is unbound later,iio_trigger_put()is never called because there is no devres action registered for it and no custom remove callback. Does this permanently leak the trigger device reference and prevent the module from being unloaded?[ ... ]
Same here. I believe that only industrialio-trigger.c makes use of iio_trigger_put().
Quoted 24 lines
Quoted 1 line
drivers/iio/light/veml6031x00.c +0
-0@@ -618,6 +1039,10 @@ static int veml6031x00_probe(struct i2c_client *i2c)[ ... ]
Quoted 1 line
ret =
devm_add_action_or_reset(dev, veml6031x00_als_shutdown_action, data);[Severity: High]
This is a pre-existing issue, but does this shutdown action access I2C registers after PM runtime has been disabled?This action is registered via
devm_add_action_or_reset()before PM runtime is enabled. On teardown, devm actions run in reverse, meaning PM runtime is disabled before the shutdown action runs. The shutdown action then attempts I2C writes while PM runtime is disabled. If the device was suspended, it cannot be resumed, potentially causing I2C transfer failures.[ ... ]
Quoted 1 line
pm_runtime_put_autosuspend(dev);[Severity: High]
This is a pre-existing issue, but is there an unbalanced double-put of the runtime PM usage counter on driver unbind?
Quoted 5 lines
The probe function registers a cleanup action to drop the PM reference via
devm_pm_runtime_get_noresume(dev). However, this explicit put at the end of probe means the devm action will drop the reference a second time on unbind, causing a PM usage counter underflow that breaks the PM runtime state machine.
As I said for [2/4], there won't be any counter underflow that way.
Best,
Javier