Skip to contentSkip to conversation

Support ROHM BU27008 RGB sensor

PATCH Superseded Matti Vaittinen 4 participants 15 messages May 3, 2023 – May 15, 2023

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Matti Vaittinen

author maintainer
#0 lore raw

v4 cover Support ROHM BU27008 RGB sensor

Add support for ROHM BU27008 RGB sensor.

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

This series supports reading the RGBC and IR channels using IIO framework. However, only two of the BC+IR can be enabled at the same time. Series adds also support for scale and integration time configuration, where scale consists of impact of both the integration time and hardware gain. The gain and time support is backed by the newly introduced IIO GTS helper. This series depends on GTS helper patches added in BU27034 support series which is already merged in iio/togreg which this series is based on.

The hardware allows configuring gain setting by writing a 5-bit gain selector value to a register. Part of the gain setting is common for all channels (RGBC + IR) but part of the selector value can be set separately for RGBC and IR:

MODE_CONTROL2 REG:
bit 7 6 5 4 3 2 1 0
-----------------------------------------------------------------

|	RGB	selector		|
+---------------------------------------+
-----------------------------------------------------------------
| high bits IR	|			| low bits IR selector	|
+---------------+			+-----------------------+

In theory it would be possible to set certain separate gain values for RGBC and IR channels, but this gets pretty confusing because there are a few 'unsupported' selector values. If only RGBC or IR was set, some extra handling should be done to prevent the other channel from getting unsupported value due to change in high-bits. Furthermore, allowing the channels to be set different gain values (in some cases when gains are such the HW supports it) would make the cases where also integration time is changed to achieve correct scale ... interesting. It might also be confusing for user to try predicting when setting different scales succeeds and when it does not. Furthermore, if for example the scale setting for RGBC caused IR selector to be invalid - it could also cause the IR scale to "jump" very far from previous value.

To make the code simpler and more predictable for users, the current logic is as follows:

1. Prevent setting IR scale. (My assumption is IR is less used than RGBC)
2. When RGBC scale is set, set also the IR-selector to the same value. This prevents unsupported selector values and makes the IR scale changes predictable.

The 2) could mean we effectively have the same scale for all channels. Unfortunately, the HW design is slightly peculiar and selector 0 means gain 1X on RGBC but gain 2X on IR. Rest of the selectors equal same gain values on RGBC and IR. The result is that while changin selector from 0 => 1 causes RGBC gain to go from 1X => 4X, it causes IR gain to go from 2X => 4X.

So, the driver provides separate scale entries for all channels (also RGB and C will have separate gain entries because these channels are of same type as IR channel). This makes it possible for user applications to go read the scales for all channels after setting scale for one (in order to detect the IR scale difference).

Having the separate IR scale entry which applications can read to detect "arbitrary scale changes" makes it possible for applications to be written so they can cope if we need to implement the 'allow setting some different gains for IR and RGBC' - later.

Finally, the scales_available is also provided for all other channels except the IR channel, which does not allow the scale to be changed.

The sensor provides a data-ready IRQ and the driver implements a triggered buffer mode using this IRQ as a trigger.

Finally, the series introduces generic iio_validate_own_trigger() helper which can be used as a validate_trigger callback for drivers which require the trigger and iio-device to be parented by same device. The KX022A driver is converted to use this new callback instead of rolling it's own function. The new helper and KX022A can be merged in as independent changes if need be.

Revision history
v3 => v4:
bu27008 driver fixes
- Drop thread from device IRQ handler
- Styling and some minor improvements
- Use kernel-doc for enums
- Correctly order entries in Makefile
v2 => v3:
dt-bindings:
- No changes
iio_validate_own_trigger:
- subject fix
bu27008:
- Mostly styling based on comments from Andy and Andi

More accurate changelog in individual patches

v1 => v2:
dt-bindings:
- Fix issues pointed by Krzysztof.
bu27008 driver:
- Fix issues pointed by Jonathan
Add new helper for validating own trigger

More accurate changelog in individual patches

---

Matti Vaittinen (5):
dt-bindings: iio: light: ROHM BU27008
iio: trigger: Add simple trigger_validation helper
iio: kx022a: Use new iio_validate_own_trigger()
iio: light: ROHM BU27008 color sensor
MAINTAINERS: Add ROHM BU27008

 .../bindings/iio/light/rohm,bu27008.yaml      |  49 +
 MAINTAINERS                                   |   3 +-
 drivers/iio/accel/kionix-kx022a.c             |  13 +-
 drivers/iio/industrialio-trigger.c            |  22 +-
 drivers/iio/light/Kconfig                     |  14 +
 drivers/iio/light/Makefile                    |   1 +
 drivers/iio/light/rohm-bu27008.c              | 993 ++++++++++++++++++
 include/linux/iio/trigger.h                   |   1 +
 8 files changed, 1082 insertions(+), 14 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/light/rohm,bu27008.yaml
 create mode 100644 drivers/iio/light/rohm-bu27008.c

base-commit: 7fcbd72176076c44b47e8f68f0223c02c411f420

Signature · 14 lines
2.40.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#1 lore raw

PATCH v4 1/5 dt-bindings: iio: light: ROHM BU27008

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Add BU27008 dt-bindings.

---
Revision history:
v2 => No changes

v1 => v2:
- fix binding file name
- fix binding id
- drop unnecessary '|' from description

---
 .../bindings/iio/light/rohm,bu27008.yaml      | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/rohm,bu27008.yaml
Documentation/devicetree/bindings/iio/light/rohm,bu27008.yaml new file +49 -0
new file mode 100644
index 000000000000..4f66fd47b016
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/rohm,bu27008.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ROHM BU27008 color sensor
+
+maintainers:
+  - Matti Vaittinen <[email protected]>
+
+description:
+  The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear
+  and IR) with four configurable channels. Red and green being always
+  available and two out of the rest three (blue, clear, IR) can be
+  selected to be simultaneously measured. Typical application is adjusting
+  LCD backlight of TVs, mobile phones and tablet PCs.
+
+properties:
+  compatible:
+    const: rohm,bu27008
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply: true
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    i2c {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      light-sensor@38 {
+        compatible = "rohm,bu27008";
+        reg = <0x38>;
+      };
+    };
+
+...
Signature · 14 lines
2.40.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#2 lore raw

PATCH v4 2/5 iio: trigger: Add simple trigger_validation helper

In reply to Matti Vaittinen

Some triggers can only be attached to the IIO device that corresponds to the same physical device. Implement generic helper which can be used as a validate_trigger callback for such devices.

---
Revision history
v2: => v3:
- Fix title (space after iio:)
v2: New patch

---
 drivers/iio/industrialio-trigger.c | 22 +++++++++++++++++++++-
 include/linux/iio/trigger.h        |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)
drivers/iio/industrialio-trigger.c +21 -1
index 784dc1e00310..c616297aa754 100644
@@ -322,7 +322,7 @@ int iio_trigger_attach_poll_func(struct iio_trigger *trig,
 	 * this is the case if the IIO device and the trigger device share the
 	 * same parent device.
 	 */
-	if (pf->indio_dev->dev.parent == trig->dev.parent)
+	if (iio_validate_own_trigger(pf->indio_dev, trig))
 		trig->attached_own_device = true;
 
 	return ret;
@@ -728,6 +728,26 @@ bool iio_trigger_using_own(struct iio_dev *indio_dev)
 }
 EXPORT_SYMBOL(iio_trigger_using_own);
 
+/**
+ * iio_validate_own_trigger - Check if a trigger and IIO device belong to
+ *  the same device
+ * @idev: the IIO device to check
+ * @trig: The IIO trigger to check
+ *
+ * This function can be used as the validate_trigger callback for triggers that
+ * can only be attached to their own device.
+ *
+ * Return: 0 if both the trigger and the IIO device belong to the same
+ * device, -EINVAL otherwise.
+ */
+int iio_validate_own_trigger(struct iio_dev *idev, struct iio_trigger *trig)
+{
+	if (idev->dev.parent != trig->dev.parent)
+		return -EINVAL;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(iio_validate_own_trigger);
+
 /**
  * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
  *  the same device
include/linux/iio/trigger.h +1 -0
index 51f52c5c6092..bce3b1788199 100644
@@ -171,6 +171,7 @@ void iio_trigger_free(struct iio_trigger *trig);
  */
 bool iio_trigger_using_own(struct iio_dev *indio_dev);
 
+int iio_validate_own_trigger(struct iio_dev *idev, struct iio_trigger *trig);
 int iio_trigger_validate_own_device(struct iio_trigger *trig,
 				     struct iio_dev *indio_dev);
 
Signature · 14 lines
2.40.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#4 lore raw

PATCH v4 3/5 iio: kx022a: Use new iio_validate_own_trigger()

In reply to Matti Vaittinen

The new generic iio_validate_own_trigger() can be used as validate_trigger callback for verifying the used trigger belongs to same device as the iio_dev.

Use the generic function instead of rolling own one.

---
Revision history
v2: New patch

---
 drivers/iio/accel/kionix-kx022a.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/iio/accel/kionix-kx022a.c +1 -12
index f98393d74666..09814881f513 100644
@@ -516,17 +516,6 @@ static int kx022a_read_raw(struct iio_dev *idev,
 	return -EINVAL;
 };
 
-static int kx022a_validate_trigger(struct iio_dev *idev,
-				   struct iio_trigger *trig)
-{
-	struct kx022a_data *data = iio_priv(idev);
-
-	if (data->trig != trig)
-		return -EINVAL;
-
-	return 0;
-}
-
 static int kx022a_set_watermark(struct iio_dev *idev, unsigned int val)
 {
 	struct kx022a_data *data = iio_priv(idev);
@@ -725,7 +714,7 @@ static const struct iio_info kx022a_info = {
 	.write_raw = &kx022a_write_raw,
 	.read_avail = &kx022a_read_avail,
 
-	.validate_trigger	= kx022a_validate_trigger,
+	.validate_trigger	= iio_validate_own_trigger,
 	.hwfifo_set_watermark	= kx022a_set_watermark,
 	.hwfifo_flush_to_buffer	= kx022a_fifo_flush,
 };
Signature · 14 lines
2.40.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#5 lore raw

PATCH v4 4/5 iio: light: ROHM BU27008 color sensor

In reply to Matti Vaittinen

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

---
Revision history
v3 => v4:
- use regmap_write_bits() for SWRESET
- reorder Makefile entries
- styling + impoved comments
- drop bu27008_irq_thread_handler() and call iio_trigger_poll() from IRQ top half
- use devm_request_irq() instead of devm_request_threaded_irq()
- Explicitly disable the IRQ from top-half instead of thread + IRQF_ONESHOT (I hope this is now implemented in a way it should)
- drop print severity
- reinit regmap cache after reset
- avoid unnecessary division when waiting measurement to complete
- use kernel-doc for enums

v2 => v3:
- drop bits.h
- drop unnecessary comma after compatible
- Styling / cleaning
- Simplify sleep time computation
- rename bu27008_get_int_time() => bu27008_get_int_time_us()

v1 => v2:
- Fix buffered data demuxing
- Use generic trigger functions instead of rolling own ones
- Drop unnecessary locking
- Use generic iio_validate_own_trigger()
- Some other more trivial fixes for review comments
- use defines for [enable/disable] [measurement/data-ready IRQ] reg values and use regmap_update_bits() directly instead of regamap_[set/clear]_bits()

---
 drivers/iio/light/Kconfig        |  14 +
 drivers/iio/light/Makefile       |   1 +
 drivers/iio/light/rohm-bu27008.c | 993 +++++++++++++++++++++++++++++++
 3 files changed, 1008 insertions(+)
 create mode 100644 drivers/iio/light/rohm-bu27008.c
drivers/iio/light/Kconfig +14 -0
index 6fa31fcd71a1..7888fc439b2f 100644
@@ -289,6 +289,20 @@ config JSA1212
 	  To compile this driver as a module, choose M here:
 	  the module will be called jsa1212.
 
+config ROHM_BU27008
+	tristate "ROHM BU27008 color (RGB+C/IR) sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	help
+	  Enable support for the ROHM BU27008 color sensor.
+	  The ROHM BU27008 is a sensor with 5 photodiodes (red, green,
+	  blue, clear and IR) with four configurable channels. Red and
+	  green being always available and two out of the rest three
+	  (blue, clear, IR) can be selected to be simultaneously measured.
+	  Typical application is adjusting LCD backlight of TVs,
+	  mobile phones and tablet PCs.
+
 config ROHM_BU27034
 	tristate "ROHM BU27034 ambient light sensor"
 	depends on I2C
drivers/iio/light/Makefile +1 -0
index 985f6feaccd4..82dec50a7e19 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_MAX44009)		+= max44009.o
 obj-$(CONFIG_NOA1305)		+= noa1305.o
 obj-$(CONFIG_OPT3001)		+= opt3001.o
 obj-$(CONFIG_PA12203001)	+= pa12203001.o
+obj-$(CONFIG_ROHM_BU27008)	+= rohm-bu27008.o
 obj-$(CONFIG_ROHM_BU27034)	+= rohm-bu27034.o
 obj-$(CONFIG_RPR0521)		+= rpr0521.o
 obj-$(CONFIG_SI1133)		+= si1133.o
drivers/iio/light/rohm-bu27008.c new file +993 -0

993 lines not on this page · raw message

Signature · 14 lines
2.40.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
author maintainer
#14 lore raw

PATCH v4 5/5 MAINTAINERS: Add ROHM BU27008

In reply to Matti Vaittinen

Add myself as a maintainer for ROHM BU27008 color sensor driver.

---
Revision history:
No changes since v1

---
 MAINTAINERS | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +2 -1
index 32772c383ab7..c02e3d2ec348 100644
@@ -18198,10 +18198,11 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/bh1750.yaml
 F:	drivers/iio/light/bh1750.c
 
-ROHM BU27034 AMBIENT LIGHT SENSOR DRIVER
+ROHM BU270xx LIGHT SENSOR DRIVERs
 M:	Matti Vaittinen <[email protected]>
 L:	[email protected]
 S:	Supported
+F:	drivers/iio/light/rohm-bu27008.c
 F:	drivers/iio/light/rohm-bu27034.c
 
 ROHM MULTIFUNCTION BD9571MWV-M PMIC DEVICE DRIVERS
Signature · 14 lines
2.40.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 
#6 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

In reply to Matti Vaittinen
Quoted 9 lines · Matti Vaittinen

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

...

drivers/iio/light/Kconfig config JSA1212 · line 304

Quoted 13 lines
drivers/iio/light/Kconfig +13 -0
+config ROHM_BU27008
+	tristate "ROHM BU27008 color (RGB+C/IR) sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	help
+	  Enable support for the ROHM BU27008 color sensor.
+	  The ROHM BU27008 is a sensor with 5 photodiodes (red, green,
+	  blue, clear and IR) with four configurable channels. Red and
+	  green being always available and two out of the rest three
+	  (blue, clear, IR) can be selected to be simultaneously measured.
+	  Typical application is adjusting LCD backlight of TVs,
+	  mobile phones and tablet PCs.

Module name?

...

drivers/iio/light/rohm-bu27008.c line 255

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+static const struct regmap_range bu27008_read_only_ranges[] = {
+	{
+		.range_min = BU27008_REG_DATA0_LO,
+		.range_max = BU27008_REG_DATA3_HI,
+	}, {
+		.range_min = BU27008_REG_MANUFACTURER_ID,
+		.range_max = BU27008_REG_MANUFACTURER_ID,
 
+	}

+ trailing comma for consistency?

Quoted 1 line

+};

...

drivers/iio/light/rohm-bu27008.c line 269

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+static const struct regmap_config bu27008_regmap = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = BU27008_REG_MAX,
+	.cache_type = REGCACHE_RBTREE,
+	.volatile_table = &bu27008_volatile_regs,
+	.wr_table = &bu27008_ro_regs,

Do you need regmap lock? If so, why (since you have mutex)?

Quoted 1 line

+};

...

Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+static int bu27008_read_one(struct bu27008_data *data, struct iio_dev *idev,
+			    struct iio_chan_spec const *chan, int *val, int *val2)
+{
+	int ret, int_time;
+
+	ret = bu27008_chan_cfg(data, chan);
+	if (ret)
+		return ret;

drivers/iio/light/rohm-bu27008.c line 556

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+
+	ret = bu27008_meas_set(data, BU27008_MEAS_EN);
+	if (ret)
+		return ret;
+
+	int_time = bu27008_get_int_time_us(data);
+	if (int_time < 0)
+		int_time = BU27008_MEAS_TIME_MAX_MS;
+	else
+		int_time /= USEC_PER_MSEC;

The above function returns an error code when negative, so I would rather see

	ret = bu27008_get_int_time_us(data);
	if (ret < 0)
		int_time = BU27008_MEAS_TIME_MAX_MS;
	else
		int_time = ret / USEC_PER_MSEC;

at least this explicitly shows the semantics of the "negative" time.

Quoted 11 lines
drivers/iio/light/rohm-bu27008.c new file +11 -0
+	msleep(int_time);
+
+	ret = bu27008_chan_read_data(data, chan->address, val);
+	if (!ret)
+		ret = IIO_VAL_INT;
+
+	if (bu27008_meas_set(data, BU27008_MEAS_DIS))
+		dev_warn(data->dev, "measurement disabling failed\n");
+
+	return ret;
+}

...

drivers/iio/light/rohm-bu27008.c line 748

Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	ret = regmap_reinit_cache(data->regmap, &bu27008_regmap);
+	if (ret) {
+		dev_err(data->dev, "Failed to reinit reg cache\n");
 
+		return ret;

Dup is not needed.

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

...

drivers/iio/light/rohm-bu27008.c line 927

Quoted 1 line
drivers/iio/light/rohm-bu27008.c new file +1 -0
+	if (i2c->irq) {

Instead of a long body, I would rather see a call to

		ret = ..._setup_irq();
		if (ret)
			return ret;
Quoted 38 lines
drivers/iio/light/rohm-bu27008.c new file +38 -0
+		ret = devm_iio_triggered_buffer_setup(dev, idev,
+						      &iio_pollfunc_store_time,
+						      bu27008_trigger_handler,
+						      &bu27008_buffer_ops);
+		if (ret)
+			return dev_err_probe(dev, ret,
+				     "iio_triggered_buffer_setup_ext FAIL\n");
+
+		itrig = devm_iio_trigger_alloc(dev, "%sdata-rdy-dev%d",
+					       idev->name, iio_device_id(idev));
+		if (!itrig)
+			return -ENOMEM;
+
+		data->trig = itrig;
+
+		itrig->ops = &bu27008_trigger_ops;
+		iio_trigger_set_drvdata(itrig, data);
+
+		name = devm_kasprintf(dev, GFP_KERNEL, "%s-bu27008",
+				      dev_name(dev));
+
+		ret = devm_request_irq(dev, i2c->irq,
+				       &bu27008_data_rdy_poll,
+				       0, name, itrig);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "Could not request IRQ\n");
+
+		ret = devm_iio_trigger_register(dev, itrig);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "Trigger registration failed\n");
+
+		/* set default trigger */
+		idev->trig = iio_trigger_get(itrig);
+	} else {
+		dev_info(dev, "No IRQ, buffered mode disabled\n");
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

Quoted 13 lines · Andy Shevchenko
Quoted 9 lines · Matti Vaittinen

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

...

Quoted 15 lines

drivers/iio/light/Kconfig config JSA1212 · line 304

Quoted 13 lines
drivers/iio/light/Kconfig +13 -0
+config ROHM_BU27008
+	tristate "ROHM BU27008 color (RGB+C/IR) sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	help
+	  Enable support for the ROHM BU27008 color sensor.
+	  The ROHM BU27008 is a sensor with 5 photodiodes (red, green,
+	  blue, clear and IR) with four configurable channels. Red and
+	  green being always available and two out of the rest three
+	  (blue, clear, IR) can be selected to be simultaneously measured.
+	  Typical application is adjusting LCD backlight of TVs,
+	  mobile phones and tablet PCs.

Module name?

We have discussed this several times already.

https://lore.kernel.org/all/[email protected]/

Module name is completely irrelevant when selecting a kernel configuration.

Quoted 11 lines

...

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+static const struct regmap_range bu27008_read_only_ranges[] = {
+	{
+		.range_min = BU27008_REG_DATA0_LO,
+		.range_max = BU27008_REG_DATA3_HI,
+	}, {
+		.range_min = BU27008_REG_MANUFACTURER_ID,
+		.range_max = BU27008_REG_MANUFACTURER_ID,
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	}

+ trailing comma for consistency?

Thanks.

Quoted 4 lines
Quoted 1 line

+};

...

Quoted 9 lines

drivers/iio/light/rohm-bu27008.c line 269

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+static const struct regmap_config bu27008_regmap = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = BU27008_REG_MAX,
+	.cache_type = REGCACHE_RBTREE,
+	.volatile_table = &bu27008_volatile_regs,
+	.wr_table = &bu27008_ro_regs,

Do you need regmap lock? If so, why (since you have mutex)?

I believe you know that regmap uses a default lock when no external lock is given. So, I assume you mean that maybe we could set 'disable_locking' for the regmap here.

It's nice to be occasionally pushed to think "out of the box". And yes, disabling regmap lock is really out of my "normal box" :)

I didn't go through all of the code yet, but I think pretty much all of the sequences which end up to register writes are indeed protected by the mutex. (Well, probe is not but it is expected to only update one bit while rest of the register should stay fixed).

It may be we could live without regmap_lock when driver is in it's current state, but I am not convinced the performance improvement is worth the risk. Having regmap unprotected is not common, and it is also not easy to spot when making changes to the driver. In my opinion it is a bit like asking for a nose-bleed unless there is really heavy reasons to drop the lock... In this case, having the regmap_lock (which is pretty much never locked because we have the mutex as you said) is probably not a penalty that matters.

Quoted 26 lines
Quoted 2 lines

+};

...

Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+static int bu27008_read_one(struct bu27008_data *data, struct iio_dev *idev,
+			    struct iio_chan_spec const *chan, int *val, int *val2)
+{
+	int ret, int_time;
+
+	ret = bu27008_chan_cfg(data, chan);
+	if (ret)
+		return ret;

drivers/iio/light/rohm-bu27008.c line 556

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+
+	ret = bu27008_meas_set(data, BU27008_MEAS_EN);
+	if (ret)
+		return ret;
+
+	int_time = bu27008_get_int_time_us(data);
+	if (int_time < 0)
+		int_time = BU27008_MEAS_TIME_MAX_MS;
+	else
+		int_time /= USEC_PER_MSEC;

The above function returns an error code when negative, so I would rather see

Quoted 7 lines
	ret = bu27008_get_int_time_us(data);
	if (ret < 0)
		int_time = BU27008_MEAS_TIME_MAX_MS;
	else
		int_time = ret / USEC_PER_MSEC;

at least this explicitly shows the semantics of the "negative" time.

Ok.

Quoted 15 lines
Quoted 12 lines
drivers/iio/light/rohm-bu27008.c new file +11 -0
+	msleep(int_time);
+
+	ret = bu27008_chan_read_data(data, chan->address, val);
+	if (!ret)
+		ret = IIO_VAL_INT;
+
+	if (bu27008_meas_set(data, BU27008_MEAS_DIS))
+		dev_warn(data->dev, "measurement disabling failed\n");
+
+	return ret;
+}

...

Quoted 7 lines

drivers/iio/light/rohm-bu27008.c line 748

Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	ret = regmap_reinit_cache(data->regmap, &bu27008_regmap);
+	if (ret) {
+		dev_err(data->dev, "Failed to reinit reg cache\n");
 
+		return ret;

Dup is not needed.

Thanks.

Quoted 10 lines
Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +3 -0
+	}
+
+	return ret;

...

drivers/iio/light/rohm-bu27008.c line 927

Quoted 1 line
drivers/iio/light/rohm-bu27008.c new file +1 -0
+	if (i2c->irq) {

Instead of a long body, I would rather see a call to

Could make sense indeed. I'll see how it would look like, thanks.

Quoted 35 lines
		ret = ..._setup_irq();
		if (ret)
			return ret;
Quoted 30 lines
drivers/iio/light/rohm-bu27008.c new file +30 -0
+		ret = devm_iio_triggered_buffer_setup(dev, idev,
+						      &iio_pollfunc_store_time,
+						      bu27008_trigger_handler,
+						      &bu27008_buffer_ops);
+		if (ret)
+			return dev_err_probe(dev, ret,
+				     "iio_triggered_buffer_setup_ext FAIL\n");
+
+		itrig = devm_iio_trigger_alloc(dev, "%sdata-rdy-dev%d",
+					       idev->name, iio_device_id(idev));
+		if (!itrig)
+			return -ENOMEM;
+
+		data->trig = itrig;
+
+		itrig->ops = &bu27008_trigger_ops;
+		iio_trigger_set_drvdata(itrig, data);
+
+		name = devm_kasprintf(dev, GFP_KERNEL, "%s-bu27008",
+				      dev_name(dev));
+
+		ret = devm_request_irq(dev, i2c->irq,
+				       &bu27008_data_rdy_poll,
+				       0, name, itrig);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "Could not request IRQ\n");
+
+		ret = devm_iio_trigger_register(dev, itrig);
+		if (ret)
Quoted 10 lines

drivers/iio/light/rohm-bu27008.c line 965

Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+			return dev_err_probe(dev, ret,
+					     "Trigger registration failed\n");
+
+		/* set default trigger */
+		idev->trig = iio_trigger_get(itrig);
+	} else {
+		dev_info(dev, "No IRQ, buffered mode disabled\n");
+	}

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#3 lore raw

Re: [PATCH v4 2/5] iio: trigger: Add simple trigger_validation helper

In reply to Matti Vaittinen
Quoted 7 lines · On Wed, 3 May 2023 12:48:57 +0300 Matti Vaittinen <[email protected]>…

Some triggers can only be attached to the IIO device that corresponds to the same physical device. Implement generic helper which can be used as a validate_trigger callback for such devices.

Noticed a trivial capitalisation issue inline.

Jonathan

Quoted 24 lines

---
Revision history
v2: => v3:
- Fix title (space after iio:)
v2: New patch

---
 drivers/iio/industrialio-trigger.c | 22 +++++++++++++++++++++-
 include/linux/iio/trigger.h        |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)
drivers/iio/industrialio-trigger.c +1 -1
index 784dc1e00310..c616297aa754 100644
@@ -322,7 +322,7 @@ int iio_trigger_attach_poll_func(struct iio_trigger *trig,
 	 * this is the case if the IIO device and the trigger device share the
 	 * same parent device.
 	 */
-	if (pf->indio_dev->dev.parent == trig->dev.parent)
+	if (iio_validate_own_trigger(pf->indio_dev, trig))
 		trig->attached_own_device = true;
 
 	return ret;

drivers/iio/industrialio-trigger.c bool iio_trigger_using_own(struct iio_dev *indio_dev) · line 735

Quoted 8 lines
drivers/iio/industrialio-trigger.c +5 -0
@@ -728,6 +728,26 @@ bool iio_trigger_using_own(struct iio_dev *indio_dev)
 }
 EXPORT_SYMBOL(iio_trigger_using_own);
 
+/**
+ * iio_validate_own_trigger - Check if a trigger and IIO device belong to
+ *  the same device
+ * @idev: the IIO device to check
+ * @trig: The IIO trigger to check

The / the consistency needs fixing. I'm not sure on the local / file convention, but it hopefully isn't a random mixture!

Quoted 30 lines
drivers/iio/industrialio-trigger.c +15 -0
+ *
+ * This function can be used as the validate_trigger callback for triggers that
+ * can only be attached to their own device.
+ *
+ * Return: 0 if both the trigger and the IIO device belong to the same
+ * device, -EINVAL otherwise.
+ */
+int iio_validate_own_trigger(struct iio_dev *idev, struct iio_trigger *trig)
+{
+	if (idev->dev.parent != trig->dev.parent)
+		return -EINVAL;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(iio_validate_own_trigger);
+
 /**
  * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
  *  the same device
include/linux/iio/trigger.h +1 -0
index 51f52c5c6092..bce3b1788199 100644
@@ -171,6 +171,7 @@ void iio_trigger_free(struct iio_trigger *trig);
  */
 bool iio_trigger_using_own(struct iio_dev *indio_dev);
 
+int iio_validate_own_trigger(struct iio_dev *idev, struct iio_trigger *trig);
 int iio_trigger_validate_own_device(struct iio_trigger *trig,
 				     struct iio_dev *indio_dev);
 
maintainer
#10 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

In reply to Matti Vaittinen
Quoted 6 lines · On Wed, 3 May 2023 12:50:14 +0300 Matti Vaittinen <[email protected]>…

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Quoted 6 lines

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

Mostly stuff that you asked about in response to earlier version but which I hadn't replied to until today.

Upshot, don't need the manual irq handling in here.

Whilst you aren't setting IRQF_ONESHOT for the pollfunc side of the trigger (the downstream IRQ / IRQ thread) the IIO utility functions are.

Jonathan

Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +0 -0
new file mode 100644
index 000000000000..c04d845062ba
@@ -0,0 +1,993 @@

...

Quoted 13 lines
drivers/iio/light/rohm-bu27008.c new file +13 -0
+
+static int bu27008_read_raw(struct iio_dev *idev,
+			   struct iio_chan_spec const *chan,
+			   int *val, int *val2, long mask)
+{
+	struct bu27008_data *data = iio_priv(idev);
+	int busy, ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+	{
+		busy = iio_device_claim_direct_mode(idev);
+		if (busy)

drivers/iio/light/rohm-bu27008.c line 591

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+			return -EBUSY;
+
+		mutex_lock(&data->mutex);
+		ret = bu27008_read_one(data, idev, chan, val, val2);
+		mutex_unlock(&data->mutex);
+
+		iio_device_release_direct_mode(idev);
+
+		return ret;
+	}

Why {}?

I guess there was a local variable, but isn't any more?

Quoted 37 lines
drivers/iio/light/rohm-bu27008.c new file +35 -0
+	case IIO_CHAN_INFO_SCALE:
+		ret = bu27008_get_scale(data, chan->scan_index == BU27008_IR,
+					val, val2);
+		if (ret)
+			return ret;
+
+		return IIO_VAL_INT_PLUS_NANO;
+
+	case IIO_CHAN_INFO_INT_TIME:
+		ret = bu27008_get_int_time_us(data);
+		if (ret < 0)
+			return ret;
+
+		*val = 0;
+		*val2 = ret;
+
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
 
 
+
+static int bu27008_write_raw(struct iio_dev *idev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct bu27008_data *data = iio_priv(idev);
+	int ret;
+
+	/*
+	 * Do not allow changing scale when measurement is ongoing as doing so
+	 * could make values in the buffer inconsistent.
+	 */
+	ret = iio_device_claim_direct_mode(idev);

drivers/iio/light/rohm-bu27008.c line 689

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+	if (ret)
+		return ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		ret = bu27008_set_scale(data, chan, val, val2);
+		break;
+	case IIO_CHAN_INFO_INT_TIME:
+		if (val)
+			ret = -EINVAL;

Slight preference for

		if (val) {
			ret = -EINVAL;
			break;
		}

		ret = bu27008....
		break;

so that the error path is out of line of the main flow.

Ever so slightly helps people follow this stuff if they are reading 'a lot' of code (every little helps!). I have to look closer at anything other than an obvious direct break / goto / return on error paths to make sure nothing else snuck in that shouldn't be there.

Quoted 34 lines
drivers/iio/light/rohm-bu27008.c new file +32 -0
+		else
+			ret = bu27008_try_set_int_time(data, val2);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	iio_device_release_direct_mode(idev);
+
+	return ret;
+}
 
 
+static irqreturn_t bu27008_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *idev = pf->indio_dev;
+	struct bu27008_data *data = iio_priv(idev);
+	struct {
+		__le16 chan[BU27008_NUM_HW_CHANS];
+		s64 ts __aligned(8);
+	} raw;
+	int ret, dummy;
+
+	memset(&raw, 0, sizeof(raw));
+
+	/*
+	 * After some measurements, it seems reading the
+	 * BU27008_REG_MODE_CONTROL3 debounces the IRQ line
+	 */
+	ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL3, &dummy);
+	if (ret < 0)
+		goto err_read;
+

drivers/iio/light/rohm-bu27008.c line 810

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+	ret = regmap_bulk_read(data->regmap, BU27008_REG_DATA0_LO, &raw.chan,
+			       sizeof(raw.chan));
+	if (ret < 0)
+		goto err_read;
+
+	iio_push_to_buffers_with_timestamp(idev, &raw, pf->timestamp);
+err_read:
+	iio_trigger_notify_done(idev->trig);
+
+	enable_irq(data->irq);

As below. This shouldn't be needed (and if it was it should be in the reenable path that is ultimately a result of that notify_done above and some reference counting fun).

Quoted 15 lines
drivers/iio/light/rohm-bu27008.c new file +15 -0
+
+	return IRQ_HANDLED;
+}
+
+static int bu27008_buffer_preenable(struct iio_dev *idev)
+{
+	struct bu27008_data *data = iio_priv(idev);
+	int chan_sel, ret;
+
+	/* Configure channel selection */
+	if (test_bit(BU27008_BLUE, idev->active_scan_mask)) {
+		if (test_bit(BU27008_CLEAR, idev->active_scan_mask))
+			chan_sel = BU27008_BLUE2_CLEAR3;
+		else
+			chan_sel = BU27008_BLUE2_IR3;

drivers/iio/light/rohm-bu27008.c line 835

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+	} else {
+		chan_sel = BU27008_CLEAR2_IR3;
+	}
+
+	chan_sel = FIELD_PREP(BU27008_MASK_CHAN_SEL, chan_sel);
+
+	ret = regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL3,
+				 BU27008_MASK_CHAN_SEL, chan_sel);
+	if (ret)
+		return ret;

Hmm. I'd missed this before but. This is in the wrong place really (though it probably doesn't make much difference), stuff related to enabling particular channels should be in iio_info->update_scan_mode()

It's arguable that the actual measurement mode setting might come in the postenable callback (after the update_scan_mode() call which in turn follows the preenable callback).

All these callbacks have become a bit blurry over time as we end up with devices that need to do nasty thing in one place. In this particular case it's pretty simple though, so nicer to move the scan mask stuff to the callback that is given the active_scan mask as a parameter.

Quoted 13 lines
drivers/iio/light/rohm-bu27008.c new file +13 -0
+
+	return bu27008_meas_set(data, BU27008_MEAS_EN);
+}
+
+static int bu27008_buffer_postdisable(struct iio_dev *idev)
+{
+	struct bu27008_data *data = iio_priv(idev);
+
+	return bu27008_meas_set(data, BU27008_MEAS_DIS);
+}
+
+static const struct iio_buffer_setup_ops bu27008_buffer_ops = {
+	.preenable = bu27008_buffer_preenable,

drivers/iio/light/rohm-bu27008.c line 858

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+	.postdisable = bu27008_buffer_postdisable,
+};
+
+static irqreturn_t bu27008_data_rdy_poll(int irq, void *private)
+{
+	/*
+	 * The BU27008 keeps IRQ asserted until we read the VALID bit from
+	 * a register. We need to keep the IRQ disabled until this
+	 */
+	disable_irq_nosync(irq);

As per my late reply to your question on this, shouldn't be needed as IRQF_ONESHOT is ultimately set for the interrupts nested below this so they'll get the resulting queuing on the threads which is fine.

Note if this were needed, you might want to provide the reenable callback for the trigger to do the irq reenabling and to implement that correctly against some race conditions, that might need to do another read of the register. That handles weird ordering conditions where another consumer of the trigger is enabled before the device acting as a consumer has it's own buffer enabled.

Anyhow, I don't think it's relevant here and you shouldn't need this unless I've miss understood / remembered how all that interrupt logic is working.

Jonathan

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	iio_trigger_poll(private);
+
+	return IRQ_HANDLED;
+}
author maintainer
#11 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

Hi Jonathan,

Quoted 15 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 3 May 2023 12:50:14 +0300 Matti Vaittinen <[email protected]>…

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Quoted 6 lines

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

Quoted 7 lines

Mostly stuff that you asked about in response to earlier version but which I hadn't replied to until today.

Upshot, don't need the manual irq handling in here.

Whilst you aren't setting IRQF_ONESHOT for the pollfunc side of the trigger (the downstream IRQ / IRQ thread) the IIO utility functions are.

I tried doing:

static int bu27008_setup_trigger(struct bu27008_data *data, struct iio_dev *idev)

{
	struct iio_trigger *itrig;
	char *name;
	int ret;

	ret = devm_iio_triggered_buffer_setup(data->dev, idev,
					      &iio_pollfunc_store_time,
					      bu27008_trigger_handler,
					      &bu27008_buffer_ops);
	if (ret)
		return dev_err_probe(data->dev, ret,
			     "iio_triggered_buffer_setup_ext FAIL\n");

	itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d",
				       idev->name, iio_device_id(idev));
	if (!itrig)
		return -ENOMEM;

	data->trig = itrig;

	itrig->ops = &bu27008_trigger_ops;
	iio_trigger_set_drvdata(itrig, data);

	name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bu27008",
			      dev_name(data->dev));

	ret = devm_request_irq(data->dev, data->irq,
				/* No IRQ disabling */
			       &iio_trigger_generic_data_rdy_poll,
			       0, name, itrig);
	if (ret)
		return dev_err_probe(data->dev, ret, "Could not request IRQ\n");

	ret = devm_iio_trigger_register(data->dev, itrig);
	if (ret)
		return dev_err_probe(data->dev, ret,
				     "Trigger registration failed\n");

	/* set default trigger */
	idev->trig = iio_trigger_get(itrig);

	return 0;
}

It seems to me we get IRQ storm out of it, bu27008_trigger_handler never being called. My assumption is that as soon as the IRQ handling code exits the iio_trigger_generic_data_rdy_poll, it re-enables the IRQ - and because we have level active IRQ and because the bu27008_trigger_handler() has not yet had a chance to read the VALID bit which restores the IRQ-line - we will immediately enter back to the IRQ handling.

This problem does not occur when I use bu27008_data_rdy_poll() (which is the same but disables the IRQ) instead of iio_trigger_generic_data_rdy_poll(), and re-enable the IRQ only after the handler bu27008_trigger_handler() has restored the IRQ line.

Does the sequence above (bu27008_setup_trigger()) look sane?

Quoted 29 lines
Quoted 29 lines
drivers/iio/light/rohm-bu27008.c new file +27 -0
+static irqreturn_t bu27008_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *idev = pf->indio_dev;
+	struct bu27008_data *data = iio_priv(idev);
+	struct {
+		__le16 chan[BU27008_NUM_HW_CHANS];
+		s64 ts __aligned(8);
+	} raw;
+	int ret, dummy;
+
+	memset(&raw, 0, sizeof(raw));
+
+	/*
+	 * After some measurements, it seems reading the
+	 * BU27008_REG_MODE_CONTROL3 debounces the IRQ line
+	 */
+	ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL3, &dummy);
+	if (ret < 0)
+		goto err_read;
+
+	ret = regmap_bulk_read(data->regmap, BU27008_REG_DATA0_LO, &raw.chan,
+			       sizeof(raw.chan));
+	if (ret < 0)
+		goto err_read;
+
+	iio_push_to_buffers_with_timestamp(idev, &raw, pf->timestamp);
Quoted 8 lines

drivers/iio/light/rohm-bu27008.c line 810

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+err_read:
+	iio_trigger_notify_done(idev->trig);
+
+	enable_irq(data->irq);

As below. This shouldn't be needed (and if it was it should be in the reenable path that is ultimately a result of that notify_done above and some reference counting fun).

I will see the reenable callback, thanks!

Quoted 22 lines
Quoted 22 lines
drivers/iio/light/rohm-bu27008.c new file +21 -0
+
+	return IRQ_HANDLED;
+}
+
+static int bu27008_buffer_preenable(struct iio_dev *idev)
+{
+	struct bu27008_data *data = iio_priv(idev);
+	int chan_sel, ret;
+
+	/* Configure channel selection */
+	if (test_bit(BU27008_BLUE, idev->active_scan_mask)) {
+		if (test_bit(BU27008_CLEAR, idev->active_scan_mask))
+			chan_sel = BU27008_BLUE2_CLEAR3;
+		else
+			chan_sel = BU27008_BLUE2_IR3;
+	} else {
+		chan_sel = BU27008_CLEAR2_IR3;
+	}
+
+	chan_sel = FIELD_PREP(BU27008_MASK_CHAN_SEL, chan_sel);
+
Quoted 8 lines

drivers/iio/light/rohm-bu27008.c line 835

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	ret = regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL3,
+				 BU27008_MASK_CHAN_SEL, chan_sel);
+	if (ret)
+		return ret;

Hmm. I'd missed this before but. This is in the wrong place really (though it probably doesn't make much difference), stuff related to enabling particular channels should be in iio_info->update_scan_mode()

Oh. I'll check this out as well.

Quoted 30 lines

It's arguable that the actual measurement mode setting might come in the postenable callback (after the update_scan_mode() call which in turn follows the preenable callback).

All these callbacks have become a bit blurry over time as we end up with devices that need to do nasty thing in one place. In this particular case it's pretty simple though, so nicer to move the scan mask stuff to the callback that is given the active_scan mask as a parameter.

Quoted 19 lines
drivers/iio/light/rohm-bu27008.c new file +19 -0
+
+	return bu27008_meas_set(data, BU27008_MEAS_EN);
+}
+
+static int bu27008_buffer_postdisable(struct iio_dev *idev)
+{
+	struct bu27008_data *data = iio_priv(idev);
+
+	return bu27008_meas_set(data, BU27008_MEAS_DIS);
+}
+
+static const struct iio_buffer_setup_ops bu27008_buffer_ops = {
+	.preenable = bu27008_buffer_preenable,
+	.postdisable = bu27008_buffer_postdisable,
+};
+
+static irqreturn_t bu27008_data_rdy_poll(int irq, void *private)
+{
+	/*
Quoted 8 lines

drivers/iio/light/rohm-bu27008.c line 858

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	 * The BU27008 keeps IRQ asserted until we read the VALID bit from
+	 * a register. We need to keep the IRQ disabled until this
+	 */
+	disable_irq_nosync(irq);

As per my late reply to your question on this, shouldn't be needed as IRQF_ONESHOT is ultimately set for the interrupts nested below this so they'll get the resulting queuing on the threads which is fine.

I see an IRQ storm if I omit this. The threaded trigger handler which 'ACKs' the IRQ gets never ran. I'll see the reenable though! Thanks!

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#8 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

In reply to Vaittinen, Matti
Quoted 2 lines · Matti
Quoted 1 line · Andy Shevchenko

On Wed, May 03, 2023 at 12:50:14PM +0300, Matti Vaittinen wrote:

...

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/light/Kconfig +7 -0
+config ROHM_BU27008
+	tristate "ROHM BU27008 color (RGB+C/IR) sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	help
+	  Enable support for the ROHM BU27008 color sensor.
Quoted 8 lines

drivers/iio/light/Kconfig config JSA1212 · line 304

Quoted 6 lines
drivers/iio/light/Kconfig +6 -0
+	  The ROHM BU27008 is a sensor with 5 photodiodes (red, green,
+	  blue, clear and IR) with four configurable channels. Red and
+	  green being always available and two out of the rest three
+	  (blue, clear, IR) can be selected to be simultaneously measured.
+	  Typical application is adjusting LCD backlight of TVs,
+	  mobile phones and tablet PCs.

Module name?

Quoted 5 lines

We have discussed this several times already.

https://lore.kernel.org/all/[email protected]/

Module name is completely irrelevant when selecting a kernel configuration.

This option is also selectable by user.

...

Quoted 5 lines
Quoted 1 line

Do you need regmap lock? If so, why (since you have mutex)?

I believe you know that regmap uses a default lock when no external lock is given. So, I assume you mean that maybe we could set 'disable_locking' for the regmap here.

Correct.

Quoted 8 lines

It's nice to be occasionally pushed to think "out of the box". And yes, disabling regmap lock is really out of my "normal box" :)

I didn't go through all of the code yet, but I think pretty much all of the sequences which end up to register writes are indeed protected by the mutex. (Well, probe is not but it is expected to only update one bit while rest of the register should stay fixed).

Quoted 8 lines

It may be we could live without regmap_lock when driver is in it's current state, but I am not convinced the performance improvement is worth the risk. Having regmap unprotected is not common, and it is also not easy to spot when making changes to the driver. In my opinion it is a bit like asking for a nose-bleed unless there is really heavy reasons to drop the lock... In this case, having the regmap_lock (which is pretty much never locked because we have the mutex as you said) is probably not a penalty that matters.

Basically you try to justify a hidden mine field in case somebody will think "oh, we are protected by regmap lock, so why to bother call mutex_lock()" and at the end it become a subtle bugs in the code. With disable_locking = true I can see that code author _carefully thought through_ the locking schema and understands the hardware and the code.

P.S. I'm wondering why your lines of text have a single trailing whitespace but the last line.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#9 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

Hi Andy,

Quoted 22 lines · Andy Shevchenko
Quoted 2 lines · Matti
Quoted 1 line · Andy Shevchenko

On Wed, May 03, 2023 at 12:50:14PM +0300, Matti Vaittinen wrote:

...

Quoted 16 lines
Quoted 15 lines

drivers/iio/light/Kconfig config JSA1212 · line 304

Quoted 13 lines
drivers/iio/light/Kconfig +13 -0
+config ROHM_BU27008
+	tristate "ROHM BU27008 color (RGB+C/IR) sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_GTS_HELPER
+	help
+	  Enable support for the ROHM BU27008 color sensor.
+	  The ROHM BU27008 is a sensor with 5 photodiodes (red, green,
+	  blue, clear and IR) with four configurable channels. Red and
+	  green being always available and two out of the rest three
+	  (blue, clear, IR) can be selected to be simultaneously measured.
+	  Typical application is adjusting LCD backlight of TVs,
+	  mobile phones and tablet PCs.

Module name?

Quoted 7 lines
Quoted 5 lines

We have discussed this several times already.

https://lore.kernel.org/all/[email protected]/

Module name is completely irrelevant when selecting a kernel configuration.

This option is also selectable by user.

I don't think the name is selectable. Yes, user selects whether to compile driver as a module or in-kernel - but the module name is completely irrelevant what comes to this decision.

Quoted 27 lines

...

Quoted 5 lines
Quoted 1 line

Do you need regmap lock? If so, why (since you have mutex)?

I believe you know that regmap uses a default lock when no external lock is given. So, I assume you mean that maybe we could set 'disable_locking' for the regmap here.

Correct.

Quoted 8 lines

It's nice to be occasionally pushed to think "out of the box". And yes, disabling regmap lock is really out of my "normal box" :)

I didn't go through all of the code yet, but I think pretty much all of the sequences which end up to register writes are indeed protected by the mutex. (Well, probe is not but it is expected to only update one bit while rest of the register should stay fixed).

Quoted 8 lines

It may be we could live without regmap_lock when driver is in it's current state, but I am not convinced the performance improvement is worth the risk. Having regmap unprotected is not common, and it is also not easy to spot when making changes to the driver. In my opinion it is a bit like asking for a nose-bleed unless there is really heavy reasons to drop the lock... In this case, having the regmap_lock (which is pretty much never locked because we have the mutex as you said) is probably not a penalty that matters.

Quoted 5 lines

Basically you try to justify a hidden mine field in case somebody will think "oh, we are protected by regmap lock, so why to bother call mutex_lock()" and at the end it become a subtle bugs in the code. With disable_locking = true I can see that code author _carefully thought through_ the locking schema and understands the hardware and the code.

I added the disable_locking = true in v5 - but I am not convinced that was a great idea. I am afraid disabling regmap lock is the hidden minefield for average users. I didn't grep the kernel for it but I am afraid the percentage of regmap users who disable locking is very low. Thus, I'd say this is unexpected to many and may lead to bugs although I try to watch out for them. Well, time will tell.

Quoted 2 lines

P.S. I'm wondering why your lines of text have a single trailing whitespace but the last line.

I guess it must be Thunderbird client then. Well, at least it can send out plain-text decently well while working with the exchange servers used by ROHM as well as with the gmail. I am not super happy with Thunderbird, it tends to eat way more resources I wished it did, but it is a working compromise for me. I am interested in hearing if anyone knows a way to configure the Thunderbird to drop these extra spaces.

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#12 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

In reply to Matti Vaittinen
Quoted 74 lines · On Mon, 8 May 2023 09:32:28 +0300 Matti Vaittinen <[email protected]>…

Hi Jonathan,

Quoted 15 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 3 May 2023 12:50:14 +0300 Matti Vaittinen <[email protected]>…

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Quoted 6 lines

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

Quoted 7 lines

Mostly stuff that you asked about in response to earlier version but which I hadn't replied to until today.

Upshot, don't need the manual irq handling in here.

Whilst you aren't setting IRQF_ONESHOT for the pollfunc side of the trigger (the downstream IRQ / IRQ thread) the IIO utility functions are.

I tried doing:

static int bu27008_setup_trigger(struct bu27008_data *data, struct iio_dev *idev)

{
	struct iio_trigger *itrig;
	char *name;
	int ret;

	ret = devm_iio_triggered_buffer_setup(data->dev, idev,
					      &iio_pollfunc_store_time,
					      bu27008_trigger_handler,
					      &bu27008_buffer_ops);
	if (ret)
		return dev_err_probe(data->dev, ret,
			     "iio_triggered_buffer_setup_ext FAIL\n");

	itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d",
				       idev->name, iio_device_id(idev));
	if (!itrig)
		return -ENOMEM;

	data->trig = itrig;

	itrig->ops = &bu27008_trigger_ops;
	iio_trigger_set_drvdata(itrig, data);

	name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bu27008",
			      dev_name(data->dev));

	ret = devm_request_irq(data->dev, data->irq,
				/* No IRQ disabling */
			       &iio_trigger_generic_data_rdy_poll,
			       0, name, itrig);
	if (ret)
		return dev_err_probe(data->dev, ret, "Could not request IRQ\n");

	ret = devm_iio_trigger_register(data->dev, itrig);
	if (ret)
		return dev_err_probe(data->dev, ret,
				     "Trigger registration failed\n");

	/* set default trigger */
	idev->trig = iio_trigger_get(itrig);

	return 0;
}
Quoted 7 lines

It seems to me we get IRQ storm out of it, bu27008_trigger_handler never being called. My assumption is that as soon as the IRQ handling code exits the iio_trigger_generic_data_rdy_poll, it re-enables the IRQ - and because we have level active IRQ and because the bu27008_trigger_handler() has not yet had a chance to read the VALID bit which restores the IRQ-line - we will immediately enter back to the IRQ handling.

Ah. I'd miss understood what was going on here. I thought we were talking race conditions only - not a level interrupt. Sorry for confusion / being half asleep. If it has an Ack like this I'd argue this is really an edge interrupt but that would require a guaranteed drop in the signal. I am assuming the sensor merrily carries on grabbing data, whether or not anyone reads it and so if we treated this as an edge interrupt then the clear to set cycle could be very short (and hence not detected). If it instead doesn't read new data until previous has been read, then things are much simpler.

Hmm. How to make this work cleanly assuming it's case 1. It might be that your current approach is the best though it would be nice to do something in the IIO code (with risk of breaking everyone :() I don't think we can though as we have no way from the trigger implementation side to know if we might get threaded interrupt handling or not on the downstream side.

We have reference counting to reenable a trigger that actually has a hardware mask at the device end when all consumers are done - that should be used for the reenable, not do it in the pollfunc handler. As it's a level interrupt you avoid need to do a bonus read in there I think (sometimes that's necessary because of an edge trigger and a slow read back on a possible unrelated device).

The subtle difference between IRQF_ONESHOT and irq_disable is one uses the irq_mask / unmask callbacks on the irq chip and the other is using the enable / disable ones. That may make no practical difference - I'm not entirely sure. A quick glance at some drivers suggests masking is usually lighter weight as less state is rewrite on reenable.

So in short, move the irq_enable() into the iio_trig->reenable() callback.

Quoted 46 lines

This problem does not occur when I use bu27008_data_rdy_poll() (which is the same but disables the IRQ) instead of iio_trigger_generic_data_rdy_poll(), and re-enable the IRQ only after the handler bu27008_trigger_handler() has restored the IRQ line.

Does the sequence above (bu27008_setup_trigger()) look sane?

Quoted 29 lines
Quoted 29 lines
drivers/iio/light/rohm-bu27008.c new file +27 -0
+static irqreturn_t bu27008_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *idev = pf->indio_dev;
+	struct bu27008_data *data = iio_priv(idev);
+	struct {
+		__le16 chan[BU27008_NUM_HW_CHANS];
+		s64 ts __aligned(8);
+	} raw;
+	int ret, dummy;
+
+	memset(&raw, 0, sizeof(raw));
+
+	/*
+	 * After some measurements, it seems reading the
+	 * BU27008_REG_MODE_CONTROL3 debounces the IRQ line
+	 */
+	ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL3, &dummy);
+	if (ret < 0)
+		goto err_read;
+
+	ret = regmap_bulk_read(data->regmap, BU27008_REG_DATA0_LO, &raw.chan,
+			       sizeof(raw.chan));
+	if (ret < 0)
+		goto err_read;
+
+	iio_push_to_buffers_with_timestamp(idev, &raw, pf->timestamp);
Quoted 8 lines

drivers/iio/light/rohm-bu27008.c line 810

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+err_read:
+	iio_trigger_notify_done(idev->trig);
+
+	enable_irq(data->irq);  

As below. This shouldn't be needed (and if it was it should be in the reenable path that is ultimately a result of that notify_done above and some reference counting fun).

Quoted 1 line

I will see the reenable callback, thanks!

Great

Quoted 73 lines
Quoted 23 lines
Quoted 23 lines
drivers/iio/light/rohm-bu27008.c new file +21 -0
+
+	return IRQ_HANDLED;
+}
+
+static int bu27008_buffer_preenable(struct iio_dev *idev)
+{
+	struct bu27008_data *data = iio_priv(idev);
+	int chan_sel, ret;
+
+	/* Configure channel selection */
+	if (test_bit(BU27008_BLUE, idev->active_scan_mask)) {
+		if (test_bit(BU27008_CLEAR, idev->active_scan_mask))
+			chan_sel = BU27008_BLUE2_CLEAR3;
+		else
+			chan_sel = BU27008_BLUE2_IR3;
+	} else {
+		chan_sel = BU27008_CLEAR2_IR3;
+	}
+
+	chan_sel = FIELD_PREP(BU27008_MASK_CHAN_SEL, chan_sel);
+
Quoted 8 lines

drivers/iio/light/rohm-bu27008.c line 835

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	ret = regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL3,
+				 BU27008_MASK_CHAN_SEL, chan_sel);
+	if (ret)
+		return ret;  

Hmm. I'd missed this before but. This is in the wrong place really (though it probably doesn't make much difference), stuff related to enabling particular channels should be in iio_info->update_scan_mode()

Oh. I'll check this out as well.

Quoted 30 lines

It's arguable that the actual measurement mode setting might come in the postenable callback (after the update_scan_mode() call which in turn follows the preenable callback).

All these callbacks have become a bit blurry over time as we end up with devices that need to do nasty thing in one place. In this particular case it's pretty simple though, so nicer to move the scan mask stuff to the callback that is given the active_scan mask as a parameter.

Quoted 19 lines
drivers/iio/light/rohm-bu27008.c new file +19 -0
+
+	return bu27008_meas_set(data, BU27008_MEAS_EN);
+}
+
+static int bu27008_buffer_postdisable(struct iio_dev *idev)
+{
+	struct bu27008_data *data = iio_priv(idev);
+
+	return bu27008_meas_set(data, BU27008_MEAS_DIS);
+}
+
+static const struct iio_buffer_setup_ops bu27008_buffer_ops = {
+	.preenable = bu27008_buffer_preenable,
+	.postdisable = bu27008_buffer_postdisable,
+};
+
+static irqreturn_t bu27008_data_rdy_poll(int irq, void *private)
+{
+	/*
Quoted 8 lines

drivers/iio/light/rohm-bu27008.c line 858

Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4 -0
+	 * The BU27008 keeps IRQ asserted until we read the VALID bit from
+	 * a register. We need to keep the IRQ disabled until this
+	 */
+	disable_irq_nosync(irq);  

As per my late reply to your question on this, shouldn't be needed as IRQF_ONESHOT is ultimately set for the interrupts nested below this so they'll get the resulting queuing on the threads which is fine.

Quoted 2 lines

I see an IRQ storm if I omit this. The threaded trigger handler which 'ACKs' the IRQ gets never ran. I'll see the reenable though! Thanks!

As you probably figured, I was wrong. Reenable bit stands though!.

Jonathan

Quoted 4 lines

Yours,
-- Matti

author maintainer
#13 lore raw

Re: [PATCH v4 4/5] iio: light: ROHM BU27008 color sensor

Quoted 85 lines · Jonathan Cameron
Quoted 74 lines · On Mon, 8 May 2023 09:32:28 +0300 Matti Vaittinen <[email protected]>…

Hi Jonathan,

Quoted 15 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 3 May 2023 12:50:14 +0300 Matti Vaittinen <[email protected]>…

The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.

Quoted 6 lines

Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt

Quoted 7 lines

Mostly stuff that you asked about in response to earlier version but which I hadn't replied to until today.

Upshot, don't need the manual irq handling in here.

Whilst you aren't setting IRQF_ONESHOT for the pollfunc side of the trigger (the downstream IRQ / IRQ thread) the IIO utility functions are.

I tried doing:

static int bu27008_setup_trigger(struct bu27008_data *data, struct iio_dev *idev)

{
	struct iio_trigger *itrig;
	char *name;
	int ret;

	ret = devm_iio_triggered_buffer_setup(data->dev, idev,
					      &iio_pollfunc_store_time,
					      bu27008_trigger_handler,
					      &bu27008_buffer_ops);
	if (ret)
		return dev_err_probe(data->dev, ret,
			     "iio_triggered_buffer_setup_ext FAIL\n");

	itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d",
				       idev->name, iio_device_id(idev));
	if (!itrig)
		return -ENOMEM;

	data->trig = itrig;

	itrig->ops = &bu27008_trigger_ops;
	iio_trigger_set_drvdata(itrig, data);

	name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bu27008",
			      dev_name(data->dev));

	ret = devm_request_irq(data->dev, data->irq,
				/* No IRQ disabling */
			       &iio_trigger_generic_data_rdy_poll,
			       0, name, itrig);
	if (ret)
		return dev_err_probe(data->dev, ret, "Could not request IRQ\n");

	ret = devm_iio_trigger_register(data->dev, itrig);
	if (ret)
		return dev_err_probe(data->dev, ret,
				     "Trigger registration failed\n");

	/* set default trigger */
	idev->trig = iio_trigger_get(itrig);

	return 0;
}
Quoted 7 lines

It seems to me we get IRQ storm out of it, bu27008_trigger_handler never being called. My assumption is that as soon as the IRQ handling code exits the iio_trigger_generic_data_rdy_poll, it re-enables the IRQ - and because we have level active IRQ and because the bu27008_trigger_handler() has not yet had a chance to read the VALID bit which restores the IRQ-line - we will immediately enter back to the IRQ handling.

Quoted 4 lines

Ah. I'd miss understood what was going on here. I thought we were talking race conditions only - not a level interrupt. Sorry for confusion / being half asleep. If it has an Ack like this I'd argue this is really an edge interrupt but that would require a guaranteed drop in the signal.

Yes. A failure to detect the edge (and skip acking) would leave the IRQ no longer working. I think we have both seen some examples of that in the past ;)

Quoted 2 lines

I am assuming the sensor merrily carries on grabbing data, whether or not anyone reads it

This is also my understanding.

Quoted 4 lines

and so if we treated this as an edge interrupt then
the clear to set cycle could be very short (and hence not detected). If it instead doesn't read new data until previous has been read, then things are much simpler.

I think this is not how BU27008 works.

I think we could probably go on with edge IRQs and cook-up some "re-read the VALID-bit again after the IRQ is for sure enabled to ensure the IRQ does not go unasserted" - mechanism, which would work on 99.99% of the cases. Problem is that some device always handles the 10000th measurement ;) To tell the truth, I never really thought of that.

Quoted 3 lines

Hmm. How to make this work cleanly assuming it's case 1. It might be that your current approach is the best though it would be nice to do something in the IIO code (with risk of breaking everyone :()

I didn't check if this would be doable.

I don't think we can though

Quoted 9 lines

as we have no way from the trigger implementation side to know if we might get threaded interrupt handling or not on the downstream side.

We have reference counting to reenable a trigger that actually has a hardware mask at the device end when all consumers are done - that should be used for the reenable, not do it in the pollfunc handler. As it's a level interrupt you avoid need to do a bonus read in there I think (sometimes that's necessary because of an edge trigger and a slow read back on a possible unrelated device).

Quoted 8 lines

The subtle difference between IRQF_ONESHOT and irq_disable is one uses the irq_mask / unmask callbacks on the irq chip and the other is using the enable / disable ones. That may make no practical difference - I'm not entirely sure. A quick glance at some drivers suggests masking is usually lighter weight as less state is rewrite on reenable.

So in short, move the irq_enable() into the iio_trig->reenable() callback.

This should be what I did at v5 :) Thanks for the help!

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~