Skip to contentSkip to conversation

iio: light: add support for veml3235

PATCH Superseded Javier Carrasco 3 participants 5 messages Oct 16, 2024 – Oct 19, 2024

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Javier Carrasco

author maintainer
#0 lore raw

cover iio: light: add support for veml3235

This series adds support for the Vishay veml3235 ambient light sensor with I2C protocol interface.

I attempted to add support for this device in the existing veml6030 driver, as it shares some operating principles with the supported devices. But given that the veml3235 has different register addresses, bit arrangements, and limited functionality, it ended up making most of the driver kind of device-agnostic.

Instead, the proposed driver is based on the recently updated veml6030 with multiple simplifications and a few clean ups (e.g. regfields, right definition of shared-by-all info masks, which can't be modified in veml6030 as it breaks the ABI).

On the other hand, the dt-bindings can be recycled as there is no real reason to add new ones. From a dt-bindings point of view it resembles the already supported veml7700. But if for whatever reason new bindings would be preferred, I am willing to provide them in further versions.

---
Javier Carrasco (2):
dt-bindings: iio: light: veml6030: add veml3235
iio: light: add support for veml3235

 .../bindings/iio/light/vishay,veml6030.yaml        |   5 +-
 MAINTAINERS                                        |   6 +
 drivers/iio/light/Kconfig                          |  11 +
 drivers/iio/light/Makefile                         |   1 +
 drivers/iio/light/veml3235.c                       | 534 +++++++++++++++++++++
 5 files changed, 556 insertions(+), 1 deletion(-)

---
base-commit: b852e1e7a0389ed6168ef1d38eb0bad71a6b11e8
change-id: 20241007-veml3235-0a38265e9bae

Best regards,

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

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

The veml3235 is another Vishay ambient light sensor that shares similar properties with the other sensors covered by this bindings. In this case, only the compatible, reg, and vdd-supply properties are required, and the device does not have an interrupt line, like the already supported veml7700.

---
 Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml +4 -1
index 53b55575efd3..4ea69f1fdd63 100644
@@ -4,7 +4,7 @@
 $id: http://devicetree.org/schemas/iio/light/vishay,veml6030.yaml#
 $schema: http://devicetree.org/meta-schemas/core.yaml#
 
-title: VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
+title: VEML3235, VEML6030, VEML6035 and VEML7700 Ambient Light Sensors (ALS)
 
 maintainers:
   - Rishi Gupta <[email protected]>
@@ -20,6 +20,7 @@ description: |
   whenever configured threshold is crossed.
 
   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/84889/veml6035.pdf
     https://www.vishay.com/docs/84286/veml7700.pdf
@@ -27,6 +28,7 @@ description: |
 properties:
   compatible:
     enum:
+      - vishay,veml3235
       - vishay,veml6030
       - vishay,veml6035
       - vishay,veml7700
@@ -76,6 +78,7 @@ allOf:
       properties:
         compatible:
           enum:
+            - vishay,veml3235
             - vishay,veml7700
     then:
       properties:
Signature · 1 line
2.43.0
Acked-by on v1 1/2
#2 lore raw

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

Quoted 10 lines · Javier Carrasco

The veml3235 is another Vishay ambient light sensor that shares similar properties with the other sensors covered by this bindings. In this case, only the compatible, reg, and vdd-supply properties are required, and the device does not have an interrupt line, like the already supported veml7700.

---
 Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Best regards,
Krzysztof

author maintainer
#3 lore raw

PATCH 2/2 iio: light: add support for veml3235

In reply to Javier Carrasco

The Vishay veml3235 is a low-power ambient light sensor with I2C interface. It provides a minimum detectable intensity of 0.0021 lx/cnt, configurable integration time and gain, and an additional white channel to distinguish between different light sources.

---
 MAINTAINERS                  |   6 +
 drivers/iio/light/Kconfig    |  11 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/veml3235.c | 534 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 552 insertions(+)
MAINTAINERS +6 -0
index 5ed860685cd1..ae7cb38fb64e 100644
@@ -24798,6 +24798,12 @@ S:	Maintained
 F:	drivers/input/serio/userio.c
 F:	include/uapi/linux/userio.h
 
+VISHAY VEML3235 AMBIENT LIGHT SENSOR DRIVER
+M:	Javier Carrasco <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml
+F:	drivers/iio/light/veml3235.c
+
 VISHAY VEML6030 AMBIENT LIGHT SENSOR DRIVER
 M:	Javier Carrasco <[email protected]>
 S:	Maintained
drivers/iio/light/Kconfig +11 -0
index c38b3c603ab4..c6b7ed0c61a1 100644
@@ -670,6 +670,17 @@ config VCNL4035
 	  To compile this driver as a module, choose M here: the
 	  module will be called vcnl4035.
 
+config VEML3235
+	tristate "VEML3235 ambient light sensor"
+	select REGMAP_I2C
+	depends on I2C
+	help
+	  Say Y here if you want to build a driver for the Vishay VEML3235
+	  ambient light sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called veml3235.
+
 config VEML6030
 	tristate "VEML6030 and VEML6035 ambient light sensors"
 	select REGMAP_I2C
drivers/iio/light/Makefile +1 -0
index 321010fc0b93..f14a37442712 100644
@@ -62,6 +62,7 @@ obj-$(CONFIG_TSL4531)		+= tsl4531.o
 obj-$(CONFIG_US5182D)		+= us5182d.o
 obj-$(CONFIG_VCNL4000)		+= vcnl4000.o
 obj-$(CONFIG_VCNL4035)		+= vcnl4035.o
+obj-$(CONFIG_VEML3235)		+= veml3235.o
 obj-$(CONFIG_VEML6030)		+= veml6030.o
 obj-$(CONFIG_VEML6040)		+= veml6040.o
 obj-$(CONFIG_VEML6070)		+= veml6070.o
drivers/iio/light/veml3235.c new file +534 -0

534 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw
Quoted 6 lines · On Wed, 16 Oct 2024 23:39:32 +0200 Javier Carrasco…

The Vishay veml3235 is a low-power ambient light sensor with I2C interface. It provides a minimum detectable intensity of 0.0021 lx/cnt, configurable integration time and gain, and an additional white channel to distinguish between different light sources.

A few minor things inline but looking good in general to me.

Quoted 21 lines
drivers/iio/light/veml3235.c new file +15 -0
new file mode 100644
index 000000000000..9bda42379456
@@ -0,0 +1,534 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * VEML3235 Ambient Light Sensor
+ *
+ * Copyright (c) 2024, Javier Carrasco <[email protected]>
+ *
+ * Datasheet: https://www.vishay.com/docs/80131/veml3235.pdf
+ * Appnote-80222: https://www.vishay.com/docs/80222/designingveml3235.pdf
+ */
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/iio/iio.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>

drivers/iio/light/veml3235.c line 25

Quoted 10 lines
drivers/iio/light/veml3235.c new file +10 -0
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+
+#define VEML3235_REG_CONF       0x00
+#define VEML3235_REG_WH_DATA    0x04
+#define VEML3235_REG_ALS_DATA   0x05
+#define VEML3235_REG_ID         0x09
+
+#define VEML3235_SD             BIT(0)
+#define VEML3235_SD0            BIT(8)

Name these so it's obvious they are in REG_CONF.
#define VEML3235_CONF_SD
etc.

drivers/iio/light/veml3235.c line 202

Quoted 5 lines
drivers/iio/light/veml3235.c new file +5 -0
+
+static int veml3235_set_gain(struct iio_dev *indio_dev, int val, int val2)
+{
+	struct veml3235_data *data = iio_priv(indio_dev);
+	int ret, new_gain, gain_idx;
	if (val2 !== 0)
		return -EINVAL;

	switch (val)
.. 
	default:
		return -EINVAL;
Quoted 42 lines
drivers/iio/light/veml3235.c new file +41 -0
+
+	if (val == 1 && val2 == 0) {
+		new_gain = 0x00;
+		gain_idx = 3;
+	} else if (val == 2 && val2 == 0) {
+		new_gain = 0x01;
+		gain_idx = 2;
+	} else if (val == 4 && val2 == 0) {
+		new_gain = 0x03;
+		gain_idx = 1;
+	} else if (val == 8 && val2 == 0) {
+		new_gain = 0x07;
+		gain_idx = 0;
+	} else {
+		return -EINVAL;
+	}
+
+	ret = regmap_field_write(data->rf.gain, new_gain);
+	if (ret) {
+		dev_err(data->dev, "failed to set gain: %d\n", ret);
+		return ret;
+	}
+
+	if (data->gain < gain_idx)
+		data->resolution <<= gain_idx - data->gain;
+	else if (data->gain > gain_idx)
+		data->resolution >>= data->gain - gain_idx;
+
+	data->gain = gain_idx;
+
+	return 0;
+}
 
+static int veml3235_read_avail(struct iio_dev *indio_dev,
+			       struct iio_chan_spec const *chan,
+			       const int **vals, int *type, int *length,
+			       long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_INT_TIME:
+		*vals = (int *)&veml3235_it_times;
+		*length = 2 * ARRAY_SIZE(veml3235_it_times);

drivers/iio/light/veml3235.c line 328

Quoted 10 lines
drivers/iio/light/veml3235.c new file +10 -0
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
+	case IIO_CHAN_INFO_SCALE:
+		*vals = (int *)&veml3235_scale_vals;
+		*length = ARRAY_SIZE(veml3235_scale_vals);
+		*type = IIO_VAL_INT;
+		return IIO_AVAIL_LIST;
+	}
+
+	return -EINVAL;

Push this into a default: as it lets static analysis tools be sure you intended to only handle those two cases.

drivers/iio/light/veml3235.c line 354

Quoted 12 lines
drivers/iio/light/veml3235.c new file +11 -0
+}
 
+static int veml3235_read_id(struct veml3235_data *data)
+{
+	int ret, reg;
+
+	ret = regmap_field_read(data->rf.id,  &reg);
+	if (ret)
+		return dev_err_probe(data->dev, ret, "failed to read ID\n");
+
+	if (reg != 0x35)
+		return dev_err_probe(data->dev, -ENODEV, "Unknown ID %d\n", reg);

This looks to be breaking any potential use of fallback dt compatibles for future devices that are fully backwards compatible.

Should just be a dev_info() to log that we aren't sure what the device is then carry on anyway on basis the firmware tables should be correct.

Quoted 3 lines
drivers/iio/light/veml3235.c new file +3 -0
+
+	return 0;
+}