Skip to contentSkip to conversation

dt-bindings: iio: light: isl76682: Document ISL76682

PATCH Superseded Marek Vasut 3 participants 6 messages Nov 18, 2023 – Nov 20, 2023

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 dt-bindings: iio: light: isl76682: Document ISL76682

The ISL76682 is very basic ALS which only supports ALS or IR mode in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any other fancy functionality. Document it as trivial device.

---
Cc: Alexander Stein <[email protected]>
Cc: Andre Werner <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Bjorn Helgaas <[email protected]>
Cc: Conor Dooley <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Krzysztof Kozlowski <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: Luca Ceresoli <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Matti Vaittinen <[email protected]>
Cc: Naresh Solanki <[email protected]>
Cc: Patrick Rudolph <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Stefan Windfeldt-Prytz <[email protected]>
Cc: Vincent Tremblay <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add AB from Conor

---
 Documentation/devicetree/bindings/trivial-devices.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/trivial-devices.yaml +2 -0
index c3190f2a168a2..27164e9219276 100644
@@ -179,6 +179,8 @@ properties:
           - isil,isl29030
             # Intersil ISL68137 Digital Output Configurable PWM Controller
           - isil,isl68137
+            # Intersil ISL76682 Ambient Light Sensor
+          - isil,isl76682
             # Linear Technology LTC2488
           - lineartechnology,ltc2488
             # 5 Bit Programmable, Pulse-Width Modulator
Signature · 1 line
2.42.0
#1 lore raw

PATCH v2 2/2 iio: light: isl76682: Add ISL76682 driver

The ISL76682 is very basic ALS which only supports ALS or IR mode in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any other fancy functionality.

---
Cc: Alexander Stein <[email protected]>
Cc: Andre Werner <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Bjorn Helgaas <[email protected]>
Cc: Conor Dooley <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Krzysztof Kozlowski <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: Luca Ceresoli <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Matti Vaittinen <[email protected]>
Cc: Naresh Solanki <[email protected]>
Cc: Patrick Rudolph <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Stefan Windfeldt-Prytz <[email protected]>
Cc: Vincent Tremblay <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: - Overhaul the driver
- Cache the entire 8-bit command register instead of parts of it and build up and rewrite the entire register if necessary
- Fix illumination scale, add intensity scale, add integration time

---
 drivers/iio/light/Kconfig    |   8 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/isl76682.c | 336 +++++++++++++++++++++++++++++++++++
 3 files changed, 345 insertions(+)
 create mode 100644 drivers/iio/light/isl76682.c
drivers/iio/light/Kconfig +8 -0
index 45edba797e4c7..124c45f509754 100644
@@ -252,6 +252,14 @@ config ISL29125
 	  To compile this driver as a module, choose M here: the module will be
 	  called isl29125.
 
+config ISL76682
+	tristate "Intersil ISL76682 Light Sensor"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  Provides driver for the Intersil's ISL76682 device. This driver
+	  supports the sysfs interface to get the ALS and IR intensity.
+
 config HID_SENSOR_ALS
 	depends on HID_SENSOR_HUB
 	select IIO_BUFFER
drivers/iio/light/Makefile +1 -0
index c0db4c4c36ec9..09fa585f3109f 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_IQS621_ALS)	+= iqs621-als.o
 obj-$(CONFIG_SENSORS_ISL29018)	+= isl29018.o
 obj-$(CONFIG_SENSORS_ISL29028)	+= isl29028.o
 obj-$(CONFIG_ISL29125)		+= isl29125.o
+obj-$(CONFIG_ISL76682)		+= isl76682.o
 obj-$(CONFIG_JSA1212)		+= jsa1212.o
 obj-$(CONFIG_SENSORS_LM3533)	+= lm3533-als.o
 obj-$(CONFIG_LTR501)		+= ltr501.o
drivers/iio/light/isl76682.c new file +336 -0

336 lines not on this page · raw message

Signature · 1 line
2.42.0
maintainer
#2 lore raw

Re: [PATCH v2 2/2] iio: light: isl76682: Add ISL76682 driver

Quoted 5 lines · On Sat, 18 Nov 2023 01:06:10 +0100 Marek Vasut <[email protected]> wrote:

The ISL76682 is very basic ALS which only supports ALS or IR mode in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any other fancy functionality.

Hi Marek

A few comments inline,

Jonathan

Quoted 12 lines
 drivers/iio/light/Kconfig    |   8 +
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/isl76682.c | 336 +++++++++++++++++++++++++++++++++++
 3 files changed, 345 insertions(+)
 create mode 100644 drivers/iio/light/isl76682.c
drivers/iio/light/Kconfig +0 -0
index 45edba797e4c7..124c45f509754 100644
@@ -252,6 +252,14 @@ config ISL29125
 	  To compile this driver as a module, choose M here: the module will be

drivers/iio/light/Kconfig config ISL29125 · line 262

Quoted 10 lines
drivers/iio/light/Kconfig +8 -0
 	  called isl29125.
 
+config ISL76682
+	tristate "Intersil ISL76682 Light Sensor"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  Provides driver for the Intersil's ISL76682 device. This driver
+	  supports the sysfs interface to get the ALS and IR intensity.
+

One of the bots tends to moan about short descriptions + it can be useful to let someone know the module name. Hence I'd add the usual boilerplate description for that here.

Quoted 13 lines
drivers/iio/light/isl76682.c new file +7 -0
new file mode 100644
index 0000000000000..64ec8b8fe71ee
@@ -0,0 +1,336 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * IIO driver for the light sensor ISL76682.
+ * ISL76682 is Ambient Light Sensor
+ *
+ * Copyright (c) 2023 Marek Vasut <[email protected]>
+ */

drivers/iio/light/isl76682.c line 17

Quoted 10 lines
drivers/iio/light/isl76682.c new file +10 -0
+
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/regmap.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>

After changes below, this sysfs.h include should not be needed.

drivers/iio/light/isl76682.c line 28

Quoted 11 lines
drivers/iio/light/isl76682.c new file +11 -0
+
+#define ISL76682_REG_COMMAND			0x00
+
+#define ISL76682_COMMAND_EN			BIT(7)
+#define ISL76682_COMMAND_MODE_CONTINUOUS	BIT(6)
+#define ISL76682_COMMAND_LIGHT_IR		BIT(5)
+
+#define ISL76682_COMMAND_RANGE_LUX_1K		0
+#define ISL76682_COMMAND_RANGE_LUX_4K		(1 << 0)
+#define ISL76682_COMMAND_RANGE_LUX_16K		(2 << 0)
+#define ISL76682_COMMAND_RANGE_LUX_64K		(3 << 0)

As you are writing these into a field with a mask, 0, 1, 2, 3 should be fine as the shift is done by FIELD_PREP()

drivers/iio/light/isl76682.c line 42

Quoted 4 lines
drivers/iio/light/isl76682.c new file +3 -0
+#define ISL76682_COMMAND_RANGE_LUX_MASK		GENMASK(1, 0)
 
+struct isl76682_chip {
+	struct mutex			lock;

A lock needs a comment on what scope it covers. Be as specific as you can as you need to distinguish it from the locks in regmap or the underlying bus code.

Quoted 13 lines
drivers/iio/light/isl76682.c new file +12 -0
+	struct regmap			*regmap;
+	u8				range;
+	u8				command;
+};
 
+static int isl76682_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long mask)
+{
+	struct isl76682_chip *chip = iio_priv(indio_dev);
+	u8 range;
+
+	if (chan->type != IIO_LIGHT)

drivers/iio/light/isl76682.c line 90

Quoted 10 lines
drivers/iio/light/isl76682.c new file +10 -0
+		return -EINVAL;
+
+	if (mask != IIO_CHAN_INFO_SCALE)
+		return -EINVAL;
+
+	if (val != 0)
+		return -EINVAL;
+
+	if (chan->type == IIO_LIGHT) {
+		if (val2 == 15000)		/* 0.015 ... 1000 lux */

As below, I'd prefer these to be lookups in the same table used for read_avail() callback.

Quoted 15 lines
drivers/iio/light/isl76682.c new file +15 -0
+			range = ISL76682_COMMAND_RANGE_LUX_1K;
+		else if (val2 == 60000)		/* 0.060 ... 4000 lux */
+			range = ISL76682_COMMAND_RANGE_LUX_4K;
+		else if (val2 == 240000)	/* 0.240 ... 16000 lux */
+			range = ISL76682_COMMAND_RANGE_LUX_16K;
+		else if (val2 == 960000)	/* 0.960 ... 64000 lux */
+			range = ISL76682_COMMAND_RANGE_LUX_64K;
+		else
+			return -EINVAL;
+	} else if (chan->type == IIO_INTENSITY) {
+		if (val2 == 10500)		/* 0.0105 .. 1000 lux */
+			range = ISL76682_COMMAND_RANGE_LUX_1K;
+		else if (val2 == 42000)		/* 0.042 ... 4000 lux */
+			range = ISL76682_COMMAND_RANGE_LUX_4K;
+		else if (val2 == 168000)	/* 0.168 ... 16000 lux */

drivers/iio/light/isl76682.c line 115

Quoted 10 lines
drivers/iio/light/isl76682.c new file +10 -0
+			range = ISL76682_COMMAND_RANGE_LUX_16K;
+		else if (val2 == 673000)	/* 0.673 ... 64000 lux */
+			range = ISL76682_COMMAND_RANGE_LUX_64K;
+		else
+			return -EINVAL;
+	} else {
+		return -EINVAL;
+	}
+
+	mutex_lock(&chip->lock);

scoped_guard(mutex, &chip->lock); or guard(mutex)(&chip->lock); nice here as well.

Quoted 4 lines
drivers/iio/light/isl76682.c new file +4 -0
+	chip->range = range;
+	mutex_unlock(&chip->lock);
+
+	return 0;

drivers/iio/light/isl76682.c line 129

Quoted 10 lines
drivers/iio/light/isl76682.c new file +10 -0
+}
+
+static int isl76682_read_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int *val, int *val2, long mask)
+{
+	struct isl76682_chip *chip = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	mutex_lock(&chip->lock);

guard(mutex)(&chip->lock);

Will allow you to make direct returns instead of breaking to where the unlock is, generally simplifying the code flow.

Quoted 59 lines
drivers/iio/light/isl76682.c new file +59 -0
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		switch (chan->type) {
+		case IIO_LIGHT:
+			ret = isl76682_get(chip, false, val);
+			break;
+		case IIO_INTENSITY:
+			ret = isl76682_get(chip, true, val);
+			break;
+		default:
+			break;
+		}
+
+		if (ret < 0)
+			break;
+
+		ret = IIO_VAL_INT;
+		break;
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0;
+		switch (chan->type) {
+		case IIO_LIGHT:
+			if (chip->range == ISL76682_COMMAND_RANGE_LUX_1K)
+				*val2 = 15000;
+			else if (chip->range == ISL76682_COMMAND_RANGE_LUX_4K)
+				*val2 = 60000;
+			else if (chip->range == ISL76682_COMMAND_RANGE_LUX_16K)
+				*val2 = 240000;
+			else if (chip->range == ISL76682_COMMAND_RANGE_LUX_64K)
+				*val2 = 960000;
+			else
+				break;
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		case IIO_INTENSITY:
+			if (chip->range == ISL76682_COMMAND_RANGE_LUX_1K)
+				*val2 = 10500;
+			else if (chip->range == ISL76682_COMMAND_RANGE_LUX_4K)
+				*val2 = 42000;
+			else if (chip->range == ISL76682_COMMAND_RANGE_LUX_16K)
+				*val2 = 168000;
+			else if (chip->range == ISL76682_COMMAND_RANGE_LUX_64K)
+				*val2 = 673000;
+			else
+				break;
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	mutex_unlock(&chip->lock);
+
+	return ret;

drivers/iio/light/isl76682.c line 198

Quoted 10 lines
drivers/iio/light/isl76682.c new file +10 -0
+}
+
+static IIO_CONST_ATTR(in_illuminance_scale_available, "0.015 0.06 0.24 0.96");
+static IIO_CONST_ATTR(in_intensity_scale_available, "0.0105 0.042 0.168 0.673");
+static IIO_CONST_ATTR(integration_time_available, "0.090");	/* 90 ms */
+
+static struct attribute *isl76682_attributes[] = {
+	&iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
+	&iio_const_attr_in_intensity_scale_available.dev_attr.attr,
+	&iio_const_attr_integration_time_available.dev_attr.attr,

Please use the read_avail() callback and matching bitmaps for these. Doing them with custom attributes is something we are trying to move away from as it prevents any sensible in kernel access to the values and tends to end up with duplication of data (and potential inconsistencies) given you also have to match against the values.

There are lots of older drivers that predate that callback being added that we haven't converted over yet.

Quoted 3 lines
drivers/iio/light/isl76682.c new file +3 -0
+	NULL,
+};
+

...

drivers/iio/light/isl76682.c line 307

Quoted 8 lines
drivers/iio/light/isl76682.c new file +8 -0
+static void isl76682_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct isl76682_chip *chip = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+
+	isl76682_clear_configure_reg(chip);

Use a devm_add_action_or_reset() call with a custom callback to unwind this automatically... As they you can use devm_iio_device_register() and get rid of the need for any remove function at all.

I am a little curious as to why it needs to be called here as it doesn't obviously match a call in probe that is being unwound. As such a comment on 'why' would also be good.

Quoted 2 lines
drivers/iio/light/isl76682.c new file +2 -0
+}
+
maintainer
#3 lore raw
In reply to Marek Vasut
Quoted 6 lines · On Sat, 18 Nov 2023 01:06:09 +0100 Marek Vasut <[email protected]> wrote:

The ISL76682 is very basic ALS which only supports ALS or IR mode in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any other fancy functionality. Document it as trivial device.

I'm always a bit in 2 minds about trivial-devices as it doesn't include a basic power supply. In this case vdd which I'd prefer to see documented even on such a simple device as this. Not everyone is going to leave it powered up all the time!

Still I'm not that fussed. Maybe it's worth allowing vdd-supply in trivial-devices.yaml?

Jonathan

Quoted 40 lines

---
Cc: Alexander Stein <[email protected]>
Cc: Andre Werner <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Bjorn Helgaas <[email protected]>
Cc: Conor Dooley <[email protected]>
Cc: Fabio Estevam <[email protected]>
Cc: Guenter Roeck <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Krzysztof Kozlowski <[email protected]>
Cc: Lars-Peter Clausen <[email protected]>
Cc: Luca Ceresoli <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Matti Vaittinen <[email protected]>
Cc: Naresh Solanki <[email protected]>
Cc: Patrick Rudolph <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Stefan Windfeldt-Prytz <[email protected]>
Cc: Vincent Tremblay <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
V2: Add AB from Conor

---
 Documentation/devicetree/bindings/trivial-devices.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/trivial-devices.yaml +2 -0
index c3190f2a168a2..27164e9219276 100644
@@ -179,6 +179,8 @@ properties:
           - isil,isl29030
             # Intersil ISL68137 Digital Output Configurable PWM Controller
           - isil,isl68137
+            # Intersil ISL76682 Ambient Light Sensor
+          - isil,isl76682
             # Linear Technology LTC2488
           - lineartechnology,ltc2488
             # 5 Bit Programmable, Pulse-Width Modulator
#4 lore raw
Quoted 10 lines · Jonathan Cameron
Quoted 6 lines · On Sat, 18 Nov 2023 01:06:09 +0100 Marek Vasut <[email protected]> wrote:

The ISL76682 is very basic ALS which only supports ALS or IR mode in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any other fancy functionality. Document it as trivial device.

Quoted 7 lines

I'm always a bit in 2 minds about trivial-devices as it doesn't include a basic power supply. In this case vdd which I'd prefer to see documented even on such a simple device as this. Not everyone is going to leave it powered up all the time!

Still I'm not that fussed. Maybe it's worth allowing vdd-supply in trivial-devices.yaml?

The hardware I have is powered all the time, so I'd be adding completely untested code which is no less useless for me (at least right now). Lets postpone this until there is a need for it, can we ?

I addressed all the other comments in 2/2.

#5 lore raw
Quoted 16 lines · On Sun, 19 Nov 2023 19:08:45 +0100 Marek Vasut <[email protected]> wrote:
Quoted 15 lines · Jonathan Cameron
Quoted 6 lines · On Sat, 18 Nov 2023 01:06:09 +0100 Marek Vasut <[email protected]> wrote:

The ISL76682 is very basic ALS which only supports ALS or IR mode in four ranges, 1k/4k/16k/64k LUX. There is no IRQ support or any other fancy functionality. Document it as trivial device.

I'm always a bit in 2 minds about trivial-devices as it doesn't include a basic power supply. In this case vdd which I'd prefer to see documented even on such a simple device as this. Not everyone is going to leave it powered up all the time!

Quoted 6 lines
Quoted 2 lines

Still I'm not that fussed. Maybe it's worth allowing vdd-supply in trivial-devices.yaml?

The hardware I have is powered all the time, so I'd be adding completely untested code which is no less useless for me (at least right now). Lets postpone this until there is a need for it, can we ?

It's untested but trivial code (one call and an error code check) but fair enough. Previous experience says that we'll soon see it added... Its probably the most common follow up patch for a new driver.

Quoted 3 lines

I addressed all the other comments in 2/2.