Skip to contentSkip to conversation

Support ROHM BU27008 RGB sensor

PATCH Superseded Matti Vaittinen 4 participants 17 messages Apr 26, 2023 – May 13, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Matti Vaittinen

author maintainer
#0 lore raw

v3 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
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              | 963 ++++++++++++++++++
 include/linux/iio/trigger.h                   |   1 +
 8 files changed, 1052 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: 52cc189b4fc6af6accc45fe7b7053d76d8724059

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 v3 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 v3 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
#3 lore raw

PATCH v3 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
#4 lore raw

PATCH v3 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
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 | 963 +++++++++++++++++++++++++++++++
 3 files changed, 978 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..881173952301 100644
@@ -39,6 +39,7 @@ obj-$(CONFIG_NOA1305)		+= noa1305.o
 obj-$(CONFIG_OPT3001)		+= opt3001.o
 obj-$(CONFIG_PA12203001)	+= pa12203001.o
 obj-$(CONFIG_ROHM_BU27034)	+= rohm-bu27034.o
+obj-$(CONFIG_ROHM_BU27008)	+= rohm-bu27008.o
 obj-$(CONFIG_RPR0521)		+= rpr0521.o
 obj-$(CONFIG_SI1133)		+= si1133.o
 obj-$(CONFIG_SI1145)		+= si1145.o
drivers/iio/light/rohm-bu27008.c new file +963 -0

963 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
#16 lore raw

PATCH v3 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 ac81383129a6..6c0775aabade 100644
@@ -18099,10 +18099,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 =] 
maintainer
#5 lore raw

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

In reply to Matti Vaittinen
Quoted 11 lines · On Wed, 26 Apr 2023 11:08:17 +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.

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

Hi Matti,

Mostly trivial stuff, but some confusion has occurred with respect to the two interrupts involve in an IIO trigger. Specifically the pollfunc stuff occurs on the downward side of the irqchip that hides on the consumer side of an IIO trigger) and is set up by devm_iio_trigered_buffer_setup() not of the interrupt that calls iio_trigger_poll[_nested]()

Jonathan

Quoted 55 lines

---
Revision history
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 | 963 +++++++++++++++++++++++++++++++
 3 files changed, 978 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 +0 -0
index 985f6feaccd4..881173952301 100644

drivers/iio/light/Makefile obj-$(CONFIG_NOA1305) += noa1305.o · line 42

Quoted 4 lines
drivers/iio/light/Makefile +1 -0
@@ -39,6 +39,7 @@ obj-$(CONFIG_NOA1305)		+= noa1305.o
 obj-$(CONFIG_OPT3001)		+= opt3001.o
 obj-$(CONFIG_PA12203001)	+= pa12203001.o
 obj-$(CONFIG_ROHM_BU27034)	+= rohm-bu27034.o
+obj-$(CONFIG_ROHM_BU27008)	+= rohm-bu27008.o

Numeric order in Makefile as well as Kconfig.

Quoted 63 lines
drivers/iio/light/Makefile +0 -0
 obj-$(CONFIG_RPR0521)		+= rpr0521.o
 obj-$(CONFIG_SI1133)		+= si1133.o
 obj-$(CONFIG_SI1145)		+= si1145.o
drivers/iio/light/rohm-bu27008.c new file +52 -0
new file mode 100644
index 000000000000..2728e6d5a321
 
+
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};
+
+/* We can always measure red and green at same time */
+#define ALWAYS_SCANNABLE (BIT(BU27008_RED) | BIT(BU27008_GREEN))
+
+/* We use these data channel configs. Ensure scan_masks below follow them too */
+#define BU27008_BLUE2_CLEAR3		0x0 /* buffer is R, G, B, C */
+#define BU27008_CLEAR2_IR3		0x1 /* buffer is R, G, C, IR */
+#define BU27008_BLUE2_IR3		0x2 /* buffer is R, G, B, IR */
+
+static const unsigned long bu27008_scan_masks[] = {
+	/* buffer is R, G, B, C */
+	ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_CLEAR),
+	/* buffer is R, G, C, IR */
+	ALWAYS_SCANNABLE | BIT(BU27008_CLEAR) | BIT(BU27008_IR),
+	/* buffer is R, G, B, IR */
+	ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_IR),
+	0
+};
 
 
+
+static int bu27008_get_int_time_sel(struct bu27008_data *data, int *sel)
+{
+	int ret, val;
+
+	ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL1, &val);
+	*sel = FIELD_GET(BU27008_MASK_MEAS_MODE, val);
+
+	return ret;
+}
+
+static int bu27008_set_int_time_sel(struct bu27008_data *data, int sel)
+{
+	return regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL1,
+				  BU27008_MASK_MEAS_MODE, sel);
+}
+

drivers/iio/light/rohm-bu27008.c

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+static int bu27008_get_int_time_us(struct bu27008_data *data)
+{
+	int ret, sel;
+
+	ret = bu27008_get_int_time_sel(data, &sel);
+	if (ret)
+		return ret;
+
+	return iio_gts_find_int_time_by_sel(&data->gts,
+					    sel & BU27008_MASK_MEAS_MODE);

sel already masked in bu27008_get_int_time_sel(). No point in doing it again (was going to say use FIELD_GET() but then noticed you did above!)

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

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+}
 
+
+static int bu27008_try_find_new_time_gain(struct bu27008_data *data, int val,
+					  int val2, int *gain_sel)
+{
+	/* Could not support new scale with existing int-time */

I'd move that above the function and change it to

	/* Called if the new scale could not be supported with existing int-time */

Down here it is not clear that this applies to the whole funciton.

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

Quoted 12 lines
drivers/iio/light/rohm-bu27008.c new file +12 -0
+	int i, ret, new_time_sel;
+
+	for (i = 0; i < data->gts.num_itime; i++) {
+		new_time_sel = data->gts.itime_table[i].sel;
+		ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts,
+					new_time_sel, val, val2 * 1000, gain_sel);
+		if (!ret)
+			break;
+	}
+	if (i == data->gts.num_itime) {
+		dev_err(data->dev, "Can't support scale %u %u\n", val,
+			val2);

Line wrapping inconsistent. I like short lines with appropriate flexibility where longer ones are more readable. However, I am fairly sure this one fits under 80 chars as a single line.

Quoted 16 lines
drivers/iio/light/rohm-bu27008.c new file +16 -0
+
+		return -EINVAL;
+	}
+
+	return bu27008_set_int_time_sel(data, new_time_sel);
+}
+
+static int bu27008_set_scale(struct bu27008_data *data,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2)
+{
+	int ret, gain_sel, time_sel;
+
+	if (chan->scan_index == BU27008_IR)
+		return -EINVAL;
+

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

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+	mutex_lock(&data->mutex);
+
+	ret = bu27008_get_int_time_sel(data, &time_sel);
+	if (ret < 0)
+		goto unlock_out;
+
+	ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel,
+						val, val2 * 1000, &gain_sel);
+	if (ret)
+		ret = bu27008_try_find_new_time_gain(data, val, val2, &gain_sel);

Obviously it is code that doesn't make any functional difference, but I'd prefer to see

	if (ret) {
		ret = bu27....
		if (ret)
			goto unlock_out;
	}
	ret = bu27008_write_gain_sel();

so that each error path is out of line, but the good path is the linear flow.

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+
+	if (!ret)
+		ret = bu27008_write_gain_sel(data, gain_sel);
+
+unlock_out:
+	mutex_unlock(&data->mutex);
+
+	return ret;
+}

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

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+
+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;
+
+	/*
+	 * We should not allow changing scale when measurement is ongoing.

"We should" is a statement of a desire, whereas here you want to say it is prevented.

* Do not allow changing scale when measurement is ongoing as
* doing so could make values in the buffer inconsistent. */

Quoted 23 lines
drivers/iio/light/rohm-bu27008.c new file +23 -0
+	 * This could make values in buffer inconsistent.
+	 */
+	ret = iio_device_claim_direct_mode(idev);
+	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;
+		else
+			ret = bu27008_try_set_int_time(data, val2);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	iio_device_release_direct_mode(idev);
+
+	return ret;

drivers/iio/light/rohm-bu27008.c

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+}
 
 
+static irqreturn_t bu27008_irq_thread_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);
+
+	iio_trigger_poll_nested(data->trig);

See below but this is what alerted me to something unusual. It never makes sense to have iio_trigger_poll_nested() called unless there is a check on whether it should be called! If there isn't iio_trigger_poll() in the top half is the right thing to do.

Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+
+	return IRQ_HANDLED;
+}
+
 
 
+static int bu27008_probe(struct i2c_client *i2c)
+{

...

Quoted 15 lines
drivers/iio/light/rohm-bu27008.c new file +15 -0
+
+	if (i2c->irq) {
+		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;
+

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

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+		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_threaded_irq(dev, i2c->irq,
+						iio_pollfunc_store_time,

This is on the wrong irq. iio_pollfunc_store_time is used with the trigger not here. Basically what happens is the caller of iio_poll_trigger() fires the input to a software irq chip that then signals all the of the downstream irqs (which are the individual consumers of the triggers). If that's triggered from the top half / non threaded bit of the interrupt the iio_pollfunc_store_time() will be called in that non threaded context before the individual threads for the trigger consumer are started.

If there is nothing to do in the actual interrupt as it's a data ready only signal, then you should just call iio_trigger_poll() in the top half and use devm_request_irq() only as there is no thread in this interrupt (though there is one for the interrupt below the software interrupt chip).

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

Quoted 13 lines
drivers/iio/light/rohm-bu27008.c new file +13 -0
+						&bu27008_irq_thread_handler,
+						IRQF_ONESHOT, name, idev->pollfunc);
+		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");
+	} else {
+		dev_warn(dev, "No IRQ configured\n");

Why is it a warning? Either driver works without an IRQ, or it doesn't. dev_dbg() or dev_info() at most.

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+	}
+
+	ret = devm_iio_device_register(dev, idev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "Unable to register iio device\n");
+
+	return ret;
+}
#6 lore raw

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

Quoted 36 lines · Jonathan Cameron
Quoted 11 lines · On Wed, 26 Apr 2023 11:08:17 +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.

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

Hi Matti,

Mostly trivial stuff, but some confusion has occurred with respect to the two interrupts involve in an IIO trigger. Specifically the pollfunc stuff occurs on the downward side of the irqchip that hides on the consumer side of an IIO trigger) and is set up by devm_iio_trigered_buffer_setup() not of the interrupt that calls iio_trigger_poll[_nested]()

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

Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5 -0
+
+static int bu27008_try_find_new_time_gain(struct bu27008_data *data, int val,
+					  int val2, int *gain_sel)
+{
+	/* Could not support new scale with existing int-time */

I'd move that above the function and change it to

	/* Called if the new scale could not be supported with existing int-time */

Down here it is not clear that this applies to the whole funciton.

Quoted 16 lines

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

Quoted 12 lines
drivers/iio/light/rohm-bu27008.c new file +12 -0
+	int i, ret, new_time_sel;
+
+	for (i = 0; i < data->gts.num_itime; i++) {
+		new_time_sel = data->gts.itime_table[i].sel;
+		ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts,
+					new_time_sel, val, val2 * 1000, gain_sel);
+		if (!ret)
+			break;
+	}
+	if (i == data->gts.num_itime) {
+		dev_err(data->dev, "Can't support scale %u %u\n", val,
+			val2);

Line wrapping inconsistent. I like short lines with appropriate flexibility where longer ones are more readable. However, I am fairly sure this one fits under 80 chars as a single line.

Thanks! Both the comment and the line-wrapping were just left like this when I pulled this part of a functionality into this new function. Well spotted!

Quoted 29 lines
Quoted 17 lines
drivers/iio/light/rohm-bu27008.c new file +16 -0
+
+		return -EINVAL;
+	}
+
+	return bu27008_set_int_time_sel(data, new_time_sel);
+}
+
+static int bu27008_set_scale(struct bu27008_data *data,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2)
+{
+	int ret, gain_sel, time_sel;
+
+	if (chan->scan_index == BU27008_IR)
+		return -EINVAL;
+

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

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+	mutex_lock(&data->mutex);
+
+	ret = bu27008_get_int_time_sel(data, &time_sel);
+	if (ret < 0)
+		goto unlock_out;
+
+	ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel,
+						val, val2 * 1000, &gain_sel);
+	if (ret)
+		ret = bu27008_try_find_new_time_gain(data, val, val2, &gain_sel);

Obviously it is code that doesn't make any functional difference, but I'd prefer to see

Quoted 8 lines
	if (ret) {
		ret = bu27....
		if (ret)
			goto unlock_out;
	}
	ret = bu27008_write_gain_sel();

so that each error path is out of line, but the good path is the linear flow.

... Yuck! The difference of tastes ... ;)
Well, not worth fighting I guess.

Quoted 54 lines
Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+
+	if (!ret)
+		ret = bu27008_write_gain_sel(data, gain_sel);
+
+unlock_out:
+	mutex_unlock(&data->mutex);
+
+	return ret;

drivers/iio/light/rohm-bu27008.c

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +8 -0
+}
 
 
+static irqreturn_t bu27008_irq_thread_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);
+
+	iio_trigger_poll_nested(data->trig);

See below but this is what alerted me to something unusual. It never makes sense to have iio_trigger_poll_nested() called unless there is a check on whether it should be called! If there isn't iio_trigger_poll() in the top half is the right thing to do. >

Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+
+	return IRQ_HANDLED;
+}
+
 
 
+static int bu27008_probe(struct i2c_client *i2c)
+{

...

Quoted 19 lines
drivers/iio/light/rohm-bu27008.c new file +19 -0
+
+	if (i2c->irq) {
+		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);
Quoted 8 lines

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

Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+
+		name = devm_kasprintf(dev, GFP_KERNEL, "%s-bu27008",
+				      dev_name(dev));
+
+		ret = devm_request_threaded_irq(dev, i2c->irq,
+						iio_pollfunc_store_time,

This is on the wrong irq.

Seems like I have some homework to do :)

Right. I now see I pass the iio_pollfunc_store_time() as top half for both the "real IRQ" generated by the device (here), as well as a top-half for the devm_iio_triggered_buffer_setup(). Ideally I like the idea of taking the timestamp in the top half for the device-generated IRQ as it is closer the moment HW did acquire the sample - but it really would make no difference here (even if I did it correctly).

iio_pollfunc_store_time is used with the trigger not

Quoted 6 lines

here. Basically what happens is the caller of iio_poll_trigger() fires the input to a software irq chip that then signals all the of the downstream irqs (which are the individual consumers of the triggers). If that's triggered from the top half / non threaded bit of the interrupt the iio_pollfunc_store_time() will be called in that non threaded context before the individual threads for the trigger consumer are started.

Oh. So, you mean the iio_pollfunc_store_time() is automatically called already before kicking the SW-IRQ? So we don't need it in devm_iio_triggered_buffer_setup() anymore?

Quoted 4 lines

If there is nothing to do in the actual interrupt as it's a data ready only signal, then you should just call iio_trigger_poll() in the top half and use devm_request_irq() only as there is no thread in this interrupt (though there is one for the interrupt below the software interrupt chip).

I haven't tested this yet so please ignore me if I am writing nonsense - but... The BU27008 will keep the IRQ line asserted until a register is read. We can't read the register form HW-IRQ so we need to keep the IRQ disabled until the threaded trigger handler is ran. With the setup we have here, the IRQF_ONESHOT, took care of this. I assume that changing to call the iio_poll_trigger() from top-half means I need to explicitly disable the IRQ and re-enable it at the end of the trigger thread after reading the register which debounces the IRQ line?

Quoted 18 lines
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +3 -0
+						&bu27008_irq_thread_handler,
+						IRQF_ONESHOT, name, idev->pollfunc);
+		if (ret)

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

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+			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");
+	} else {
+		dev_warn(dev, "No IRQ configured\n");

Why is it a warning? Either driver works without an IRQ, or it doesn't. dev_dbg() or dev_info() at most.

Some of it works. Well, maybe I'll change it to tell that device works in raw_read only mode.

Thanks again 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! ~~
#10 lore raw

Re: [PATCH v3 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/rohm-bu27008.c line 61

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS

Why not converting comments to a kernel-doc?

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

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};

Ditto.

...

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

Quoted 2 lines
drivers/iio/light/rohm-bu27008.c new file +2 -0
+	if (int_time < 0)
+		int_time = 400000;

Adding 3 0:s to drop them below with a heavy division operation? Well done! Or did I miss anything?

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

Quoted 1 line
drivers/iio/light/rohm-bu27008.c new file +1 -0
+	msleep(int_time / 1000);

USEC_PER_MSEC ?

...

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

Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+	ret = devm_iio_device_register(dev, idev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "Unable to register iio device\n");
+
+	return ret;

return 0 will suffice.

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

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

Hi Andy

Thanks for the review.

Quoted 23 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

...

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

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS

Why not converting comments to a kernel-doc?

Quoted 11 lines

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

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};

Ditto.

I see no value having entities which are not intended to be used outside this file documented in any "global" documentation. One who is ever going to use these or wonder what these are - will most likely be watching this file. My personal view is that the generated docs should be kept lean. In my opinion the problem of the day is the time we spend looking for a needle hidden in a haystack. In my opinion adding this to kernel-doc just adds hay :)

I still can do this if no-one else objects. I almost never look at the generated docs myself. Usually I just look the docs from code files - and kernel-doc format is not any worse for me to read. Still, I can imagine including this type of stuff to generic doc just bloats them and my not serve well those who use them.

Quoted 8 lines

...

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

Quoted 2 lines
drivers/iio/light/rohm-bu27008.c new file +2 -0
+	if (int_time < 0)
+		int_time = 400000;

Adding 3 0:s to drop them below with a heavy division operation? Well done! Or did I miss anything?

No. You did not miss anything. This can be improved.

Quoted 4 lines

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

Quoted 2 lines
drivers/iio/light/rohm-bu27008.c new file +1 -0
+	msleep(int_time / 1000);

USEC_PER_MSEC ?

Ok.

Quoted 10 lines

...

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

Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+	ret = devm_iio_device_register(dev, idev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "Unable to register iio device\n");
+
+	return ret;

return 0 will suffice.

Ok.

Thanks,
-- 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
#7 lore raw

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

In reply to Vaittinen, Matti
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
drivers/iio/light/rohm-bu27008.c new file +19 -0
+
+	if (i2c->irq) {
+		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);
Quoted 8 lines

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

Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+
+		name = devm_kasprintf(dev, GFP_KERNEL, "%s-bu27008",
+				      dev_name(dev));
+
+		ret = devm_request_threaded_irq(dev, i2c->irq,
+						iio_pollfunc_store_time,  

This is on the wrong irq.

Quoted 8 lines

Seems like I have some homework to do :)

Right. I now see I pass the iio_pollfunc_store_time() as top half for both the "real IRQ" generated by the device (here), as well as a top-half for the devm_iio_triggered_buffer_setup(). Ideally I like the idea of taking the timestamp in the top half for the device-generated IRQ as it is closer the moment HW did acquire the sample - but it really would make no difference here (even if I did it correctly).

It's the matter of few function calls later. So trivial timing wise.

Quoted 9 lines

iio_pollfunc_store_time is used with the trigger not

Quoted 6 lines

here. Basically what happens is the caller of iio_poll_trigger() fires the input to a software irq chip that then signals all the of the downstream irqs (which are the individual consumers of the triggers). If that's triggered from the top half / non threaded bit of the interrupt the iio_pollfunc_store_time() will be called in that non threaded context before the individual threads for the trigger consumer are started.

Quoted 3 lines

Oh. So, you mean the iio_pollfunc_store_time() is automatically called already before kicking the SW-IRQ? So we don't need it in devm_iio_triggered_buffer_setup() anymore?

No. Whatever you register as the top half of the poll_func in the call to devm_iio_triggered_buffer_setup() will be called as part of the SW irq chip handling - not this isn't a SW-IRQ at all, it's just some demultiplexing code. The top half of an IIO poll func runs in the the trigger interrupt handler (under iio_trigger_poll) before those in turn start their own interrupt threads to handle whatever you registered as the threads in devm_iio_trigger_buffer_setup()

Quoted 6 lines
Quoted 5 lines

If there is nothing to do in the actual interrupt as it's a data ready only signal, then you should just call iio_trigger_poll() in the top half and use devm_request_irq() only as there is no thread in this interrupt (though there is one for the interrupt below the software interrupt chip).

Quoted 8 lines

I haven't tested this yet so please ignore me if I am writing nonsense - but... The BU27008 will keep the IRQ line asserted until a register is read. We can't read the register form HW-IRQ so we need to keep the IRQ disabled until the threaded trigger handler is ran. With the setup we have here, the IRQF_ONESHOT, took care of this. I assume that changing to call the iio_poll_trigger() from top-half means I need to explicitly disable the IRQ and re-enable it at the end of the trigger thread after reading the register which debounces the IRQ line?

Hmm. I'm trying to remember how this works (wrote this a very long time ago). I'm fairly sure it's not an issue because we use IRQF_ONESHOT down one level so exercise the same prevention of the threads triggering multiple times etc.
https://elixir.bootlin.com/linux/latest/source/drivers/iio/buffer/industrialio-triggered-buffer.c#L57

It doesn't matter if the device interrupt fires again as it will still be masked at our software irqchip level and will then get queued up and the thread will run again.

Quoted 28 lines
Quoted 19 lines
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +3 -0
+						&bu27008_irq_thread_handler,
+						IRQF_ONESHOT, name, idev->pollfunc);
+		if (ret)

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

Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10 -0
+			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");
+	} else {
+		dev_warn(dev, "No IRQ configured\n");  

Why is it a warning? Either driver works without an IRQ, or it doesn't. dev_dbg() or dev_info() at most.

Some of it works. Well, maybe I'll change it to tell that device works in raw_read only mode.

Thanks again for the help!

Yours,
-- Matti

maintainer
#12 lore raw

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

In reply to Vaittinen, Matti
Quoted 49 lines · Matti

Hi Andy

Thanks for the review.

Quoted 23 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

...

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

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS  

Why not converting comments to a kernel-doc?

Quoted 11 lines

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

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};  

Ditto.

I see no value having entities which are not intended to be used outside this file documented in any "global" documentation. One who is ever going to use these or wonder what these are - will most likely be watching this file. My personal view is that the generated docs should be kept lean. In my opinion the problem of the day is the time we spend looking for a needle hidden in a haystack. In my opinion adding this to kernel-doc just adds hay :)

Quoted 5 lines

I still can do this if no-one else objects. I almost never look at the generated docs myself. Usually I just look the docs from code files - and kernel-doc format is not any worse for me to read. Still, I can imagine including this type of stuff to generic doc just bloats them and my not serve well those who use them.

Unless someone specifically adds this doc to the main docs build, the kernel-doc won't end up in the docs anyway. It just provides a nice bit of consistent formatting. Even if they do add this for some reason, there are controls on internal vs external (exported stuff) being added to the docs.

Quoted 35 lines
Quoted 9 lines

...

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

Quoted 2 lines
drivers/iio/light/rohm-bu27008.c new file +2 -0
+	if (int_time < 0)
+		int_time = 400000;  

Adding 3 0:s to drop them below with a heavy division operation? Well done! Or did I miss anything?

No. You did not miss anything. This can be improved.

Quoted 4 lines

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

Quoted 2 lines
drivers/iio/light/rohm-bu27008.c new file +1 -0
+	msleep(int_time / 1000);  

USEC_PER_MSEC ?

Ok.

Quoted 10 lines

...

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

Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6 -0
+	ret = devm_iio_device_register(dev, idev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "Unable to register iio device\n");
+
+	return ret;  

return 0 will suffice.

Ok.

Thanks,
-- Matti

author maintainer
#13 lore raw

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

Quoted 59 lines · Jonathan Cameron
Quoted 49 lines · Matti

Hi Andy

Thanks for the review.

Quoted 23 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

...

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

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS

Why not converting comments to a kernel-doc?

Quoted 11 lines

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

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};

Ditto.

I see no value having entities which are not intended to be used outside this file documented in any "global" documentation. One who is ever going to use these or wonder what these are - will most likely be watching this file. My personal view is that the generated docs should be kept lean. In my opinion the problem of the day is the time we spend looking for a needle hidden in a haystack. In my opinion adding this to kernel-doc just adds hay :)

Quoted 5 lines

I still can do this if no-one else objects. I almost never look at the generated docs myself. Usually I just look the docs from code files - and kernel-doc format is not any worse for me to read. Still, I can imagine including this type of stuff to generic doc just bloats them and my not serve well those who use them.

Quoted 2 lines

Unless someone specifically adds this doc to the main docs build, the kernel-doc won't end up in the docs anyway.

Ah! This makes sense. Thanks for correcting me!

Quoted 4 lines

It just provides a nice
bit of consistent formatting. Even if they do add this for some reason, there are controls on internal vs external (exported stuff) being added to the docs.

I'll use kernel-doc for this then.

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! ~~
author maintainer
#8 lore raw

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

In reply to Jonathan Cameron
Quoted 14 lines · Jonathan Cameron
Quoted 5 lines
Quoted 4 lines

If there is nothing to do in the actual interrupt as it's a data ready only signal, then you should just call iio_trigger_poll() in the top half and use devm_request_irq() only as there is no thread in this interrupt (though there is one for the interrupt below the software interrupt chip).

Quoted 8 lines

I haven't tested this yet so please ignore me if I am writing nonsense - but... The BU27008 will keep the IRQ line asserted until a register is read. We can't read the register form HW-IRQ so we need to keep the IRQ disabled until the threaded trigger handler is ran. With the setup we have here, the IRQF_ONESHOT, took care of this. I assume that changing to call the iio_poll_trigger() from top-half means I need to explicitly disable the IRQ and re-enable it at the end of the trigger thread after reading the register which debounces the IRQ line?

Quoted 3 lines

Hmm. I'm trying to remember how this works (wrote this a very long time ago). I'm fairly sure it's not an issue because we use IRQF_ONESHOT down one level so exercise the same prevention of the threads triggering multiple times etc >

https://elixir.bootlin.com/linux/latest/source/drivers/iio/buffer/industrialio-triggered-buffer.c#L57

Quoted 4 lines

It doesn't matter if the device interrupt fires again as it will still be masked at our software irqchip level and will then get queued up and the thread will run again.

After reading this I am not at all sure I am using the trigger correctly. I see the iio_trigger_attach_poll_func() registering threaded handler with the IRQF_ONESHOT which is stored in the iio_alloc_pollfunc() as you pointed above.

The iio_trigger_attach_poll_func() is called from sysfs callback when trigger is enabled. So, if this is supposed to be the device IRQ, then I am not at all sure the driver should be requesting the IRQ at the probe(). If it is not the device's IRQ, then I guess the IRQF_ONESHOT passed in here won't help. I need to try seeing some examples how other drivers are using the triggers. Getting back to this tomorrow...

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! ~~
#14 lore raw

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

In reply to Jonathan Cameron
Quoted 4 lines · Jonathan Cameron
Quoted 2 lines · Matti
Quoted 1 line · Andy Shevchenko

On Wed, Apr 26, 2023 at 11:08:17AM +0300, Matti Vaittinen wrote:

...

Quoted 38 lines
Quoted 31 lines
Quoted 10 lines

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

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS  

Why not converting comments to a kernel-doc?

Quoted 11 lines

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

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};  

Ditto.

I see no value having entities which are not intended to be used outside this file documented in any "global" documentation. One who is ever going to use these or wonder what these are - will most likely be watching this file. My personal view is that the generated docs should be kept lean. In my opinion the problem of the day is the time we spend looking for a needle hidden in a haystack. In my opinion adding this to kernel-doc just adds hay :)

Quoted 5 lines

I still can do this if no-one else objects. I almost never look at the generated docs myself. Usually I just look the docs from code files - and kernel-doc format is not any worse for me to read. Still, I can imagine including this type of stuff to generic doc just bloats them and my not serve well those who use them.

Quoted 5 lines

Unless someone specifically adds this doc to the main docs build, the kernel-doc won't end up in the docs anyway. It just provides a nice bit of consistent formatting. Even if they do add this for some reason, there are controls on internal vs external (exported stuff) being added to the docs.

I can run it manually and see in a nice form instead of browsing file for that, so there is still a usefulness in my opinion. Esp. taking into account that comments are already there. It's just different and helpful form of representation. No?

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

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

Quoted 52 lines · Andy Shevchenko
Quoted 4 lines · Jonathan Cameron
Quoted 2 lines · Matti
Quoted 1 line · Andy Shevchenko

On Wed, Apr 26, 2023 at 11:08:17AM +0300, Matti Vaittinen wrote:

...

Quoted 38 lines
Quoted 31 lines
Quoted 10 lines

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

Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7 -0
+enum {
+	BU27008_RED,	/* Always data0 */
+	BU27008_GREEN,	/* Always data1 */
+	BU27008_BLUE,	/* data2, configurable (blue / clear) */
+	BU27008_CLEAR,	/* data2 or data3 */
+	BU27008_IR,	/* data3 */
+	BU27008_NUM_CHANS

Why not converting comments to a kernel-doc?

Quoted 11 lines

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

Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9 -0
+};
+
+enum {
+	BU27008_DATA0, /* Always RED */
+	BU27008_DATA1, /* Always GREEN */
+	BU27008_DATA2, /* Blue or Clear */
+	BU27008_DATA3, /* IR or Clear */
+	BU27008_NUM_HW_CHANS
+};

Ditto.

I see no value having entities which are not intended to be used outside this file documented in any "global" documentation. One who is ever going to use these or wonder what these are - will most likely be watching this file. My personal view is that the generated docs should be kept lean. In my opinion the problem of the day is the time we spend looking for a needle hidden in a haystack. In my opinion adding this to kernel-doc just adds hay :)

Quoted 5 lines

I still can do this if no-one else objects. I almost never look at the generated docs myself. Usually I just look the docs from code files - and kernel-doc format is not any worse for me to read. Still, I can imagine including this type of stuff to generic doc just bloats them and my not serve well those who use them.

Quoted 5 lines

Unless someone specifically adds this doc to the main docs build, the kernel-doc won't end up in the docs anyway. It just provides a nice bit of consistent formatting. Even if they do add this for some reason, there are controls on internal vs external (exported stuff) being added to the docs.

Quoted 4 lines

I can run it manually and see in a nice form instead of browsing file for that, so there is still a usefulness in my opinion. Esp. taking into account that comments are already there. It's just different and helpful form of representation. No?

My main objection was a _misunderstanding_ that the kernel-doc formatted comments would automatically end up in generated docs. As I wrote, I rarely (never) generate the docs. I use the docs from sources, hence it is not easy for me to see this value. Nevertheless, I also wrote

Quoted 1 line
Quoted 1 line
Quoted 1 line

and kernel-doc format is not any worse for me to read.

Hence, I did format these comments as kernel-doc in v5. The only slight disadvantage (from my perspective) in using the kernel-doc is increased amount of lines with pretty much no additional information. I can live with that though.

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
#9 lore raw

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

In reply to Matti Vaittinen
Quoted 30 lines · On Sun, 7 May 2023 19:13:38 +0300 Matti Vaittinen <[email protected]>…
Quoted 14 lines · Jonathan Cameron
Quoted 5 lines
Quoted 4 lines

If there is nothing to do in the actual interrupt as it's a data ready only signal, then you should just call iio_trigger_poll() in the top half and use devm_request_irq() only as there is no thread in this interrupt (though there is one for the interrupt below the software interrupt chip).

Quoted 8 lines

I haven't tested this yet so please ignore me if I am writing nonsense - but... The BU27008 will keep the IRQ line asserted until a register is read. We can't read the register form HW-IRQ so we need to keep the IRQ disabled until the threaded trigger handler is ran. With the setup we have here, the IRQF_ONESHOT, took care of this. I assume that changing to call the iio_poll_trigger() from top-half means I need to explicitly disable the IRQ and re-enable it at the end of the trigger thread after reading the register which debounces the IRQ line?

Quoted 3 lines

Hmm. I'm trying to remember how this works (wrote this a very long time ago). I'm fairly sure it's not an issue because we use IRQF_ONESHOT down one level so exercise the same prevention of the threads triggering multiple times etc >

https://elixir.bootlin.com/linux/latest/source/drivers/iio/buffer/industrialio-triggered-buffer.c#L57

Quoted 4 lines

It doesn't matter if the device interrupt fires again as it will still be masked at our software irqchip level and will then get queued up and the thread will run again.

After reading this I am not at all sure I am using the trigger correctly. I see the iio_trigger_attach_poll_func() registering threaded handler with the IRQF_ONESHOT which is stored in the iio_alloc_pollfunc() as you pointed above.

Quoted 6 lines

The iio_trigger_attach_poll_func() is called from sysfs callback when trigger is enabled. So, if this is supposed to be the device IRQ, then I am not at all sure the driver should be requesting the IRQ at the probe(). If it is not the device's IRQ, then I guess the IRQF_ONESHOT passed in here won't help. I need to try seeing some examples how other drivers are using the triggers. Getting back to this tomorrow...

I think you are confusing two different irqs here / I managed to confuse you even more.

The one in iio_trigger_attach_pollfunc() is a child of the trigger - it's called when the trigger 'fires' along with any other children attached (if multiple IIO devices have current_trigger set tot his one). The one you are registering for is the parent of the trigger and calls iio_poll_trigger() in it's handler which in turn will result in interrupts for any children of the trigger (that is the pollfunc's which are interrupt handlers underneath).

Not sure this horrible diagram will help

Data Ready Interrupt
|
iio_trigger_poll() after checking it's right int etc. Easy here.

            |
    ________|__________________________________________________________________
    |                             |                                  |         |
 Pollfunc 1 Int              Pollfunc 2 Int
    |                             |
 Top half of PF 1           Top half of PF2 return IRQ_THREAD etc
    |                             |
.....................................
    |                             |
 Thread PF 1                 Thread PF 2
    |                             |
iio_push_to_buffers..()    iio_push_to_bufffers..()
For trigger consumer       For trigger consumer
device 1                   device 2.

So in the pollfunc interrupts have IRQF_ONESHOT set so the masking happens in the internal irq_chip that IIO uses to handle this demux of the trigger signal to multiple different sensor drivers to make them grab data to push into buffers. This whole thing is built to enable sync capture of multiple signals where possible (hardware restrictions mean this only works for some combinations).
Years ago, we had our own registration scheme that didn't use interrupts internally and it was pointed out (Arnd Bergmann I think...) that we were just replicating what goes on in an interrupt chip so should use that instead.

Jonathan

Quoted 4 lines

Yours,

	-- Matti.