Skip to contentSkip to conversation

iio: light: vcnl4000: add regulator support

PATCH In review Erikas Bitovtas 4 participants 13 messages Mar 20 – Mar 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover iio: light: vcnl4000: add regulator support

This patch series introduces support for voltage supply, I2C and cathode regulators. This fixes an issue where if a regulator is shared between the proximity sensor and some other device, and the other device is powered off, the proximity sensor would be powered off as well.

One of the commits includes a Reported-by: tag without a Closes: tag - the report was done outside in a Matrix channel. A link to access the report requires sign-in, therefore it was left out.

---
Changes in v6:
- Added descriptions to the regulators in the binding.
- Fixed the dt_binding_check bug in the example.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Renamed vddio-supply to vio-supply.
- Added parentheses to function calls in a variable usage commit message.
- Moved the redundant "proximity-near-level" check removal into its own
patch.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Reworded device tree bindings commit into imperative mood and
clarified where do supplies come from.
- Moved data->chip_spec->set_power_state() calls from init functions to
probe.
- Removed explicit less than 0 checks for ret where unnecessary.
- Moved the patch that adds regulators to the end of the series.
- Added an explanation for removing duplicate prints in probe.
- Fixed indentation for devm_regulator_bulk_get_enable() and i2c_smbus
writes in vcnl4200_init().
- Removed a redundant check for "proximity-near-level" device property.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Added a more detailed description for supplies in the dt-bindings commit.
- Separated sorting includes into a commit of its own.
- Replaced all occurrences of mutex_init with its device-managed
counterpart.
- Moved client->dev variable declaration into a commit for adding
regulators.
- Removed redundant dev_err messages in probe function.
- Replaced all direct usages of client->dev and data->client into usages
by variable.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Removed double quotes in includes.
- Reordered includes alphabetically.
- Enabled regulators before the mutex is initialized.
- Replaced direct usage of &client->dev with a variable.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Erikas Bitovtas (8):
dt-bindings: iio: light: vcnl4000: add regulators
iio: light: vcnl4000: sort includes by their name
iio: light: vcnl4000: move power enablement from init to probe
iio: light: vcnl4000: replace mutex_init() with devm_mutex_init()
iio: light: vcnl4000: remove error messages for trigger and irq
iio: light: vcnl4000: use variables for I2C client and device instances
iio: light: vcnl4000: remove redundant check for proximity-near-level
iio: light: vcnl4000: add support for regulators

 .../bindings/iio/light/vishay,vcnl4000.yaml        | 14 ++++
 drivers/iio/light/vcnl4000.c                       | 90 ++++++++++++----------
 2 files changed, 63 insertions(+), 41 deletions(-)

---
base-commit: 785f0eb2f85decbe7c1ef9ae922931f0194ffc2e
change-id: 20260310-vcnl4000-regulators-bcf1b8a01ce6

Best regards,

Signature · 1 line
Erikas Bitovtas <[email protected]>
#1 lore raw

PATCH v6 1/8 dt-bindings: iio: light: vcnl4000: add regulators

These sensors can accept 2 supplies - one for the sensor and one for IR LED [1]. Add supply properties for the sensor - 2 for the sensors and one external, for their open drain interrupt line, to ensure the sensor is powered on before proceeding with setup.

[1] https://www.vishay.com/docs/84274/vcnl4040.pdf

---
 .../devicetree/bindings/iio/light/vishay,vcnl4000.yaml     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/iio/light/vishay,vcnl4000.yaml +14 -0
index 2ba4d5de4ec4..516afef7a545 100644
@@ -33,6 +33,17 @@ properties:
   interrupts:
     maxItems: 1
 
+  vdd-supply:
+    description: Regulator providing power to the "VDD" pin.
+
+  vio-supply:
+    description: Regulator providing power for pull-up of the I/O lines.
+      Does not connect to the sensor directly, but is needed for the
+      correct operation of the I2C and interrupt lines.
+
+  vled-supply:
+    description: Regulator providing power to the IR anode pin.
+
   reg:
     maxItems: 1
 
@@ -54,6 +65,9 @@ examples:
             compatible = "vishay,vcnl4200";
             reg = <0x51>;
             proximity-near-level = <220>;
+            vdd-supply = <&reg_vdd>;
+            vio-supply = <&reg_vio>;
+            vled-supply = <&reg_vled>;
         };
     };
 ...
Signature · 1 line
2.53.0
Reviewed-by on v6 1/8
#2 lore raw

Re: [PATCH v6 1/8] dt-bindings: iio: light: vcnl4000: add regulators

Quoted 5 lines · Erikas Bitovtas

These sensors can accept 2 supplies - one for the sensor and one for IR LED [1]. Add supply properties for the sensor - 2 for the sensors and one external, for their open drain interrupt line, to ensure the sensor is powered on before proceeding with setup.

Quoted 7 lines

[1] https://www.vishay.com/docs/84274/vcnl4040.pdf

---
 .../devicetree/bindings/iio/light/vishay,vcnl4000.yaml     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

Best regards,
Krzysztof

maintainer
#3 lore raw

Re: [PATCH v6 1/8] dt-bindings: iio: light: vcnl4000: add regulators

In reply to Erikas Bitovtas
Quoted 18 lines · On Fri, 20 Mar 2026 18:45:36 +0200 Erikas Bitovtas <[email protected]> wrote:

These sensors can accept 2 supplies - one for the sensor and one for IR LED [1]. Add supply properties for the sensor - 2 for the sensors and one external, for their open drain interrupt line, to ensure the sensor is powered on before proceeding with setup.

[1] https://www.vishay.com/docs/84274/vcnl4040.pdf

---
 .../devicetree/bindings/iio/light/vishay,vcnl4000.yaml     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Documentation/devicetree/bindings/iio/light/vishay,vcnl4000.yaml +0 -0
index 2ba4d5de4ec4..516afef7a545 100644

Documentation/devicetree/bindings/iio/light/vishay,vcnl4000.yaml properties: · line 42

Quoted 10 lines
Documentation/devicetree/bindings/iio/light/vishay,vcnl4000.yaml +7 -0
@@ -33,6 +33,17 @@ properties:
   interrupts:
     maxItems: 1
 
+  vdd-supply:
+    description: Regulator providing power to the "VDD" pin.
+
+  vio-supply:
+    description: Regulator providing power for pull-up of the I/O lines.
+      Does not connect to the sensor directly, but is needed for the
+      correct operation of the I2C and interrupt lines.

It's optional so I guess it doesn't matter, but whether this supply is needed of not is down to whether the SoC allows for pull up on the interrupt and I2C lines. Some SoCs provide this (though it's been a while since I used one that did.).

In other cases an external pull up is needed. It would feel odd to provide a supply for the pull up it was all hidden in the SoC.

Anyhow, optional so we aren't forcing this anyway.

Jonathan

Quoted 18 lines
Documentation/devicetree/bindings/iio/light/vishay,vcnl4000.yaml +7 -0
+
+  vled-supply:
+    description: Regulator providing power to the IR anode pin.
+
   reg:
     maxItems: 1
 
@@ -54,6 +65,9 @@ examples:
             compatible = "vishay,vcnl4200";
             reg = <0x51>;
             proximity-near-level = <220>;
+            vdd-supply = <&reg_vdd>;
+            vio-supply = <&reg_vio>;
+            vled-supply = <&reg_vled>;
         };
     };
...
#4 lore raw

PATCH v6 2/8 iio: light: vcnl4000: sort includes by their name

In reply to Erikas Bitovtas

Sort include headers by file name for better readability.

---
 drivers/iio/light/vcnl4000.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/light/vcnl4000.c +4 -4
index 5e03c3d8874b..939ff2d65105 100644
@@ -18,12 +18,12 @@
  */
 
 #include <linux/bitfield.h>
-#include <linux/module.h>
-#include <linux/i2c.h>
-#include <linux/err.h>
 #include <linux/delay.h>
-#include <linux/pm_runtime.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
 #include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
 #include <linux/units.h>
 
 #include <linux/iio/buffer.h>
Signature · 1 line
2.53.0
#5 lore raw

PATCH v6 3/8 iio: light: vcnl4000: move power enablement from init to probe

In reply to Erikas Bitovtas

Given both vcnl4000_init() and vcnl4200_init() end with dev->chip_spec->set_power_state(), they can be called once from the probe to enable the sensors. Move the set_power_state function from init and call it after init function in probe.

---
 drivers/iio/light/vcnl4000.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/light/vcnl4000.c +5 -5
index 939ff2d65105..287ccd89cfb2 100644
@@ -280,7 +280,7 @@ static int vcnl4000_init(struct vcnl4000_data *data)
 	data->rev = ret & 0xf;
 	data->al_scale = 250000;
 
-	return data->chip_spec->set_power_state(data, true);
+	return 0;
 };
 
 static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en)
@@ -425,10 +425,6 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 	if (ret < 0)
 		return ret;
 
-	ret = data->chip_spec->set_power_state(data, true);
-	if (ret < 0)
-		return ret;
-
 	return 0;
 };
 
@@ -2003,6 +1999,10 @@ static int vcnl4000_probe(struct i2c_client *client)
 	if (ret < 0)
 		return ret;
 
+	ret = data->chip_spec->set_power_state(data, true);
+	if (ret)
+		return ret;
+
 	dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
 		data->chip_spec->prod, data->rev);
 
Signature · 1 line
2.53.0
#6 lore raw

PATCH v6 4/8 iio: light: vcnl4000: replace mutex_init() with devm_mutex_init()

In reply to Erikas Bitovtas

Replace mutex_init() used across the driver with its device-managed counterpart, so all assigned mutexes get destroyed.

---
 drivers/iio/light/vcnl4000.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/iio/light/vcnl4000.c +14 -3
index 287ccd89cfb2..cd7e6ee42cc5 100644
@@ -356,6 +356,8 @@ static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on)
 
 static int vcnl4200_init(struct vcnl4000_data *data)
 {
+	struct i2c_client *client = data->client;
+	struct device *dev = &client->dev;
 	int ret, id;
 	u16 regval;
 
@@ -400,8 +402,14 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 	}
 	data->al_scale = data->chip_spec->ulux_step;
 	data->ps_scale = 16;
-	mutex_init(&data->vcnl4200_al.lock);
-	mutex_init(&data->vcnl4200_ps.lock);
+
+	ret = devm_mutex_init(dev, &data->vcnl4200_al.lock);
+	if (ret)
+		return ret;
+
+	ret = devm_mutex_init(dev, &data->vcnl4200_ps.lock);
+	if (ret)
+		return ret;
 
 	/* Use 16 bits proximity sensor readings */
 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
@@ -1979,6 +1987,7 @@ static int vcnl4010_probe_trigger(struct iio_dev *indio_dev)
 static int vcnl4000_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
+	struct device *dev = &client->dev;
 	struct vcnl4000_data *data;
 	struct iio_dev *indio_dev;
 	int ret;
@@ -1993,7 +2002,9 @@ static int vcnl4000_probe(struct i2c_client *client)
 	data->id = id->driver_data;
 	data->chip_spec = &vcnl4000_chip_spec_cfg[data->id];
 
-	mutex_init(&data->vcnl4000_lock);
+	ret = devm_mutex_init(dev, &data->vcnl4000_lock);
+	if (ret)
+		return ret;
 
 	ret = data->chip_spec->init(data);
 	if (ret < 0)
Signature · 1 line
2.53.0
#7 lore raw

PATCH v6 5/8 iio: light: vcnl4000: remove error messages for trigger and irq

In reply to Erikas Bitovtas

The error code is available in the log after return. In our case, attaching a triggered buffer can only fail if we are out of memory, as no other buffer is being attached. Remove duplicate error messages to reduce noise in dmesg.

---
 drivers/iio/light/vcnl4000.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/iio/light/vcnl4000.c +2 -7
index cd7e6ee42cc5..76aee16d479b 100644
@@ -2033,11 +2033,8 @@ static int vcnl4000_probe(struct i2c_client *client)
 						      NULL,
 						      data->chip_spec->trig_buffer_func,
 						      data->chip_spec->buffer_setup_ops);
-		if (ret < 0) {
-			dev_err(&client->dev,
-				"unable to setup iio triggered buffer\n");
+		if (ret < 0)
 			return ret;
-		}
 	}
 
 	if (client->irq && data->chip_spec->irq_thread) {
@@ -2047,10 +2044,8 @@ static int vcnl4000_probe(struct i2c_client *client)
 						IRQF_ONESHOT,
 						"vcnl4000_irq",
 						indio_dev);
-		if (ret < 0) {
-			dev_err(&client->dev, "irq request failed\n");
+		if (ret < 0)
 			return ret;
-		}
 
 		ret = vcnl4010_probe_trigger(indio_dev);
 		if (ret < 0)
Signature · 1 line
2.53.0
#8 lore raw

PATCH v6 6/8 iio: light: vcnl4000: use variables for I2C client and device instances

In reply to Erikas Bitovtas

After moving data->client and client->dev into variables of their own, replace all instances of data->client and client->dev being used in vcnl4200_init() and vcnl4000_probe() by the said variables to reduce clutter.

---
 drivers/iio/light/vcnl4000.c | 35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)
drivers/iio/light/vcnl4000.c +16 -19
index 76aee16d479b..34b52725aff6 100644
@@ -361,14 +361,14 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 	int ret, id;
 	u16 regval;
 
-	ret = i2c_smbus_read_word_data(data->client, VCNL4200_DEV_ID);
+	ret = i2c_smbus_read_word_data(client, VCNL4200_DEV_ID);
 	if (ret < 0)
 		return ret;
 
 	id = ret & 0xff;
 
 	if (id != VCNL4200_PROD_ID) {
-		ret = i2c_smbus_read_word_data(data->client, VCNL4040_DEV_ID);
+		ret = i2c_smbus_read_word_data(client, VCNL4040_DEV_ID);
 		if (ret < 0)
 			return ret;
 
@@ -378,7 +378,7 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 			return -ENODEV;
 	}
 
-	dev_dbg(&data->client->dev, "device id 0x%x", id);
+	dev_dbg(dev, "device id 0x%x", id);
 
 	data->rev = (ret >> 8) & 0xf;
 	data->ps_int = 0;
@@ -412,24 +412,22 @@ static int vcnl4200_init(struct vcnl4000_data *data)
 		return ret;
 
 	/* Use 16 bits proximity sensor readings */
-	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
+	ret = i2c_smbus_read_word_data(client, VCNL4200_PS_CONF1);
 	if (ret < 0)
 		return ret;
 
 	regval = ret | VCNL4040_PS_CONF2_PS_HD;
-	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1,
-					regval);
+	ret = i2c_smbus_write_word_data(client, VCNL4200_PS_CONF1, regval);
 	if (ret < 0)
 		return ret;
 
 	/* Align proximity sensor sample rate to 16 bits data width */
-	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF3);
+	ret = i2c_smbus_read_word_data(client, VCNL4200_PS_CONF3);
 	if (ret < 0)
 		return ret;
 
 	regval = ret | VCNL4040_CONF3_PS_SAMPLE_16BITS;
-	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF3,
-					regval);
+	ret = i2c_smbus_write_word_data(client, VCNL4200_PS_CONF3, regval);
 	if (ret < 0)
 		return ret;
 
@@ -1992,7 +1990,7 @@ static int vcnl4000_probe(struct i2c_client *client)
 	struct iio_dev *indio_dev;
 	int ret;
 
-	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
 	if (!indio_dev)
 		return -ENOMEM;
 
@@ -2014,7 +2012,7 @@ static int vcnl4000_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
+	dev_dbg(dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
 		data->chip_spec->prod, data->rev);
 
 	if (device_property_read_u32(&client->dev, "proximity-near-level",
@@ -2029,8 +2027,7 @@ static int vcnl4000_probe(struct i2c_client *client)
 
 	if (data->chip_spec->trig_buffer_func &&
 	    data->chip_spec->buffer_setup_ops) {
-		ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
-						      NULL,
+		ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
 						      data->chip_spec->trig_buffer_func,
 						      data->chip_spec->buffer_setup_ops);
 		if (ret < 0)
@@ -2038,8 +2035,8 @@ static int vcnl4000_probe(struct i2c_client *client)
 	}
 
 	if (client->irq && data->chip_spec->irq_thread) {
-		ret = devm_request_threaded_irq(&client->dev, client->irq,
-						NULL, data->chip_spec->irq_thread,
+		ret = devm_request_threaded_irq(dev, client->irq, NULL,
+						data->chip_spec->irq_thread,
 						IRQF_TRIGGER_FALLING |
 						IRQF_ONESHOT,
 						"vcnl4000_irq",
@@ -2052,7 +2049,7 @@ static int vcnl4000_probe(struct i2c_client *client)
 			return ret;
 	}
 
-	ret = pm_runtime_set_active(&client->dev);
+	ret = pm_runtime_set_active(dev);
 	if (ret < 0)
 		goto fail_poweroff;
 
@@ -2060,9 +2057,9 @@ static int vcnl4000_probe(struct i2c_client *client)
 	if (ret < 0)
 		goto fail_poweroff;
 
-	pm_runtime_enable(&client->dev);
-	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4000_SLEEP_DELAY_MS);
-	pm_runtime_use_autosuspend(&client->dev);
+	pm_runtime_enable(dev);
+	pm_runtime_set_autosuspend_delay(dev, VCNL4000_SLEEP_DELAY_MS);
+	pm_runtime_use_autosuspend(dev);
 
 	return 0;
 fail_poweroff:
Signature · 1 line
2.53.0
#9 lore raw

PATCH v6 7/8 iio: light: vcnl4000: remove redundant check for proximity-near-level

In reply to Erikas Bitovtas

The data->near_level variable is already assigned 0 during devm_kzalloc(), therefore checking if the property is present and then assigning it 0 is redundant. Remove the check for device tree property and let it fail silently if it is missing or invalid.

---
 drivers/iio/light/vcnl4000.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/iio/light/vcnl4000.c +1 -3
index 34b52725aff6..0a4d82679cfe 100644
@@ -2015,9 +2015,7 @@ static int vcnl4000_probe(struct i2c_client *client)
 	dev_dbg(dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
 		data->chip_spec->prod, data->rev);
 
-	if (device_property_read_u32(&client->dev, "proximity-near-level",
-				     &data->near_level))
-		data->near_level = 0;
+	device_property_read_u32(dev, "proximity-near-level", &data->near_level);
 
 	indio_dev->info = data->chip_spec->info;
 	indio_dev->channels = data->chip_spec->channels;
Signature · 1 line
2.53.0
#10 lore raw

PATCH v6 8/8 iio: light: vcnl4000: add support for regulators

In reply to Erikas Bitovtas

Add supply, I2C and cathode voltage regulators to the sensor and enable them. This keeps the sensor powered on even after its only supply shared by another device shuts down.

---
 drivers/iio/light/vcnl4000.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/iio/light/vcnl4000.c +7 -0
index 0a4d82679cfe..9650dbc41f2b 100644
@@ -24,6 +24,7 @@
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
 #include <linux/units.h>
 
 #include <linux/iio/buffer.h>
@@ -1985,6 +1986,7 @@ static int vcnl4010_probe_trigger(struct iio_dev *indio_dev)
 static int vcnl4000_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
+	const char * const regulator_names[] = { "vdd", "vio", "vled" };
 	struct device *dev = &client->dev;
 	struct vcnl4000_data *data;
 	struct iio_dev *indio_dev;
@@ -2000,6 +2002,11 @@ static int vcnl4000_probe(struct i2c_client *client)
 	data->id = id->driver_data;
 	data->chip_spec = &vcnl4000_chip_spec_cfg[data->id];
 
+	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
+					     regulator_names);
+	if (ret)
+		return ret;
+
 	ret = devm_mutex_init(dev, &data->vcnl4000_lock);
 	if (ret)
 		return ret;
Signature · 1 line
2.53.0
Reviewed-by on the v6 series
#11 lore raw
In reply to Erikas Bitovtas
Quoted 8 lines · Erikas Bitovtas

This patch series introduces support for voltage supply, I2C and cathode regulators. This fixes an issue where if a regulator is shared between the proximity sensor and some other device, and the other device is powered off, the proximity sensor would be powered off as well.

One of the commits includes a Reported-by: tag without a Closes: tag - the report was done outside in a Matrix channel. A link to access the report requires sign-in, therefore it was left out.

For the series, except the DT binding, I haven't even opened that mail. Also you can add Suggested-by to the selected patches.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#12 lore raw
Quoted 6 lines · On Fri, 20 Mar 2026 21:35:01 +0200 Andy Shevchenko…
Quoted 5 lines · Erikas Bitovtas

This patch series introduces support for voltage supply, I2C and cathode regulators. This fixes an issue where if a regulator is shared between the proximity sensor and some other device, and the other device is powered off, the proximity sensor would be powered off as well.

Quoted 9 lines
Quoted 3 lines

One of the commits includes a Reported-by: tag without a Closes: tag - the report was done outside in a Matrix channel. A link to access the report requires sign-in, therefore it was left out.

For the series, except the DT binding, I haven't even opened that mail. Also you can add Suggested-by to the selected patches.

You do like to challenge b4! :) Hand removed the tag it added for the DT patch.

Series applied to the testing branch of iio.git that I'll push out as togreg once initial build tests are in etc.

Thanks,

Jonathan