Skip to contentSkip to conversation

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

PATCH Superseded Marek Vasut 2 participants 6 messages Nov 19, 2023 – Nov 21, 2023

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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
V3: No change

---
 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 v3 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
V3: - Limit the read data to 16bit ADC range
- Update Kconfig description
- Update macros, drop bitshifts
- Switch over to table lookup for lux ranges
- Switch over to .read_avail instead of attributes
- Use guard where applicable
- Drop remove function in favor of reset action

---
 drivers/iio/light/Kconfig    |  15 ++
 drivers/iio/light/Makefile   |   1 +
 drivers/iio/light/isl76682.c | 354 +++++++++++++++++++++++++++++++++++
 3 files changed, 370 insertions(+)
 create mode 100644 drivers/iio/light/isl76682.c
drivers/iio/light/Kconfig +15 -0
index 45edba797e4c7..9e8cdc091556d 100644
@@ -252,6 +252,21 @@ 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
+	  Say Y here if you want to build a driver for the Intersil ISL76682
+	  Ambient Light Sensor and IR Intensity sensor. This driver provides
+	  the readouts via standard IIO sysfs and device interface. Both ALS
+	  illuminance and IR illuminance are provided raw with separate scale
+	  setting which can be configured via sysfs, the default scale is 1000
+	  lux, other options are 4000/16000/64000 lux.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called isl76682.
+
 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 +354 -0

354 lines not on this page · raw message

Signature · 1 line
2.42.0
#2 lore raw

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

Quoted 3 lines · Marek Vasut

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.

...

drivers/iio/light/isl76682.c line 14

Quoted 6 lines
drivers/iio/light/isl76682.c new file +6 -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>

This is incomplete.

At least array_size.h, bits.h, cleanup.h, types.h are missing.

...

Quoted 1 line

+#define ISL76682_ADC_MAX 0xffff

Wouldn't the (BIT(16) - 1) be better in order to show that this is limited by a bit field capacity. Also you can use FIELD_MAX().

...

drivers/iio/light/isl76682.c line 130

Quoted 7 lines
drivers/iio/light/isl76682.c new file +6 -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;

This is less maintainable, use it directly (see below). Ah, the value is even not used!

Quoted 9 lines
drivers/iio/light/isl76682.c new file +9 -0
+	int i;
+
+	if (chan->type != IIO_LIGHT && chan->type != IIO_INTENSITY)
+		return -EINVAL;
+
+	guard(mutex)(&chip->lock);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:

drivers/iio/light/isl76682.c line 148

Quoted 10 lines
drivers/iio/light/isl76682.c new file +9 -0
+		switch (chan->type) {
+		case IIO_LIGHT:
+			ret = isl76682_get(chip, false, val);
+			return (ret < 0) ? ret : IIO_VAL_INT;
+		case IIO_INTENSITY:
+			ret = isl76682_get(chip, true, val);
+			return (ret < 0) ? ret : IIO_VAL_INT;
+		default:
 
+			break;
			return -EINVAL;
Quoted 9 lines
drivers/iio/light/isl76682.c new file +9 -0
+		}
+
+		return -EINVAL;
+	case IIO_CHAN_INFO_SCALE:
+		for (i = 0; i < ARRAY_SIZE(isl76682_range_table); i++) {
+			if (chip->range != isl76682_range_table[i].range)
+				continue;
+
+			*val = 0;

...

drivers/iio/light/isl76682.c line 163

Quoted 6 lines
drivers/iio/light/isl76682.c new file +6 -0
+			if (chan->type == IIO_LIGHT)
+				*val2 = isl76682_range_table[i].als;
+			else if (chan->type == IIO_INTENSITY)
+				*val2 = isl76682_range_table[i].ir;
+			else
+				return -EINVAL;

Why not switch-case for consistency's sake?

drivers/iio/light/isl76682.c line 176

Quoted 13 lines
drivers/iio/light/isl76682.c new file +12 -0
+			return IIO_VAL_INT_PLUS_MICRO;
+		}
+		return -EINVAL;
+	case IIO_CHAN_INFO_INT_TIME:
+		*val = 0;
+		*val2 = ISL76682_INT_TIME_US;
+		return IIO_VAL_INT_PLUS_MICRO;
 
+	default:
+		break;
+	}
+
+	return -EINVAL;

Move it to default.

Quoted 1 line

+}

...

drivers/iio/light/isl76682.c line 183

Quoted 5 lines
drivers/iio/light/isl76682.c new file +5 -0
+static int illuminance_scale_available[] = {
+	0, 15000,
+	0, 60000,
+	0, 240000,
+	0, 960000

Leave trailing comma as it's not a terminator and will be an additional burden if this list is going to be expanded in the future.

drivers/iio/light/isl76682.c line 190

Quoted 7 lines
drivers/iio/light/isl76682.c new file +7 -0
+};
+
+static int intensity_scale_available[] = {
+	0, 10500,
+	0, 42000,
+	0, 168000,
+	0, 673000

Ditto.

Quoted 1 line

+};

...

Quoted 9 lines
drivers/iio/light/isl76682.c new file +9 -0
+static int isl76682_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_SCALE:
+		if (chan->type == IIO_LIGHT) {
+			*vals = illuminance_scale_available;

drivers/iio/light/isl76682.c line 213

Quoted 10 lines
drivers/iio/light/isl76682.c new file +10 -0
+			*length = ARRAY_SIZE(illuminance_scale_available);
+			*type = IIO_VAL_INT_PLUS_MICRO;
+			return IIO_AVAIL_LIST;
+		} else if (chan->type == IIO_INTENSITY) {
+			*vals = intensity_scale_available;
+			*length = ARRAY_SIZE(intensity_scale_available);
+			*type = IIO_VAL_INT_PLUS_MICRO;
+			return IIO_AVAIL_LIST;
+		}
+		return -EINVAL;

Same Q: why not switch-case?

drivers/iio/light/isl76682.c line 223

Quoted 11 lines
drivers/iio/light/isl76682.c new file +10 -0
+	case IIO_CHAN_INFO_INT_TIME:
+		*vals = integration_time_available;
+		*length = ARRAY_SIZE(integration_time_available);
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		return IIO_AVAIL_LIST;
 
+	default:
+		break;
+	}
+
+	return -EINVAL;

Move it to default.

Quoted 1 line

+}

...

drivers/iio/light/isl76682.c line 257

Quoted 10 lines
drivers/iio/light/isl76682.c new file +9 -0
+static int isl76682_clear_configure_reg(struct isl76682_chip *chip)
+{
+	struct device *dev = regmap_get_device(chip->regmap);
+	int ret;
+
+	ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, 0x0);
+	if (ret < 0)
+		dev_err(dev, "Error %d clearing the CONFIGURE register\n", ret);
 
+	chip->command = 0;

Even in an error case? Is it a problem?

Quoted 2 lines
(quoted diff) +2 -0
+	return ret;
+}

...

drivers/iio/light/isl76682.c line 264

Quoted 3 lines
drivers/iio/light/isl76682.c new file +3 -0
+static void isl76682_reset_action(void *data)
+{
+	isl76682_clear_configure_reg((struct isl76682_chip *)data);

Redundant casting, just name the field as you line, seems "chip" is what being used elsewhere.

Quoted 1 line

+}

...

drivers/iio/light/isl76682.c line 301

Quoted 1 line
drivers/iio/light/isl76682.c new file +1 -0
+	i2c_set_clientdata(client, indio_dev);

How is this being used?

...

drivers/iio/light/isl76682.c line 308

Quoted 5 lines
drivers/iio/light/isl76682.c new file +5 -0
+	chip->regmap = devm_regmap_init_i2c(client, &isl76682_regmap_config);
+	if (IS_ERR(chip->regmap)) {
+		return dev_err_probe(dev, PTR_ERR(chip->regmap),
+				     "Error initializing regmap\n");
+	}

Redundant {}

Also can be rewritten as

	chip->regmap = devm_regmap_init_i2c(client, &isl76682_regmap_config);
	ret = PTR_ERR_OR_ZERO(chip->regmap);
	if (ret)
		return dev_err_probe(dev, ret, "Error initializing regmap\n");

...

drivers/iio/light/isl76682.c line 330

Quoted 2 lines
drivers/iio/light/isl76682.c new file +2 -0
+static const struct i2c_device_id isl76682_id[] = {
+	{"isl76682", 0},

Unneeded 0.

Quoted 2 lines
(quoted diff) +2 -0
+	{}
+};

...

drivers/iio/light/isl76682.c line 337

Quoted 3 lines
drivers/iio/light/isl76682.c new file +3 -0
+static const struct of_device_id isl76682_of_match[] = {
+	{ .compatible = "isil,isl76682", },
+	{ },

Remove comma from the terminator.

Quoted 1 line

+};

...

Quoted 1 line

+

Redundant blank line.

Quoted 1 line

+module_i2c_driver(isl76682_driver);

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

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

On 11/20/23 13:02, Andy Shevchenko wrote:

[...]

Quoted 9 lines
Quoted 8 lines
drivers/iio/light/isl76682.c new file +8 -0
+static int isl76682_clear_configure_reg(struct isl76682_chip *chip)
+{
+	struct device *dev = regmap_get_device(chip->regmap);
+	int ret;
+
+	ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, 0x0);
+	if (ret < 0)
+		dev_err(dev, "Error %d clearing the CONFIGURE register\n", ret);
Quoted 3 lines

drivers/iio/light/isl76682.c line 257

Quoted 1 line
drivers/iio/light/isl76682.c new file +1 -0
+	chip->command = 0;

Even in an error case? Is it a problem?

I added a comment in V4, if the I2C communication fails, hopefully the next time user reads data the command register will be rewritten again and the communication would have succeeded by then (assuming this was some weird glitch on the I2C bus). So this is best effort attempt to recover from that.

Quoted 6 lines

drivers/iio/light/isl76682.c line 337

Quoted 3 lines
drivers/iio/light/isl76682.c new file +3 -0
+static const struct of_device_id isl76682_of_match[] = {
+	{ .compatible = "isil,isl76682", },
+	{ },

Remove comma from the terminator.

Quoted 9 lines
Quoted 1 line

+};

...

Quoted 1 line

+

Redundant blank line.

Quoted 1 line

+module_i2c_driver(isl76682_driver);

That ^ newline is above the module_i2c_driver or below it ?

I removed the one below .

#4 lore raw

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

Quoted 1 line · Marek Vasut

On 11/20/23 13:02, Andy Shevchenko wrote:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/iio/light/isl76682.c new file +8 -0
+static int isl76682_clear_configure_reg(struct isl76682_chip *chip)
+{
+	struct device *dev = regmap_get_device(chip->regmap);
+	int ret;
+
+	ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, 0x0);
+	if (ret < 0)
+		dev_err(dev, "Error %d clearing the CONFIGURE register\n", ret);
Quoted 8 lines
Quoted 3 lines

drivers/iio/light/isl76682.c line 257

Quoted 1 line
drivers/iio/light/isl76682.c new file +1 -0
+	chip->command = 0;

Even in an error case? Is it a problem?

I added a comment in V4, if the I2C communication fails, hopefully the next time user reads data the command register will be rewritten again and the communication would have succeeded by then (assuming this was some weird glitch on the I2C bus). So this is best effort attempt to recover from that.

OK.

...

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

+

Redundant blank line.

Quoted 1 line

+module_i2c_driver(isl76682_driver);

That ^ newline is above the module_i2c_driver or below it ?

I removed the one below .

Hmm... Comment was clearly about above one (as you see a single + there).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

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

Quoted 31 lines · Andy Shevchenko
Quoted 1 line · Marek Vasut

On 11/20/23 13:02, Andy Shevchenko wrote:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/iio/light/isl76682.c new file +8 -0
+static int isl76682_clear_configure_reg(struct isl76682_chip *chip)
+{
+	struct device *dev = regmap_get_device(chip->regmap);
+	int ret;
+
+	ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, 0x0);
+	if (ret < 0)
+		dev_err(dev, "Error %d clearing the CONFIGURE register\n", ret);
Quoted 8 lines
Quoted 3 lines

drivers/iio/light/isl76682.c line 257

Quoted 1 line
drivers/iio/light/isl76682.c new file +1 -0
+	chip->command = 0;

Even in an error case? Is it a problem?

I added a comment in V4, if the I2C communication fails, hopefully the next time user reads data the command register will be rewritten again and the communication would have succeeded by then (assuming this was some weird glitch on the I2C bus). So this is best effort attempt to recover from that.

OK.

...

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+

Redundant blank line.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

+module_i2c_driver(isl76682_driver);

That ^ newline is above the module_i2c_driver or below it ?

I removed the one below .

Hmm... Comment was clearly about above one (as you see a single + there).

I'll fix that up in V5 then, will wait a bit for further feedback before posting that one.