Skip to contentSkip to conversation

dt-bindings: iio: light: ROHM BH1745

PATCH In review Mudit Sharma 4 participants 8 messages Jul 18, 2024 – Aug 6, 2024

Subsystems (3)
lore
Share & tools
lore

Mudit Sharma

author maintainer
#0 lore raw

PATCH v8 1/2 dt-bindings: iio: light: ROHM BH1745

Add ROHM BH1745 - 4 channel I2C colour sensor's dt-bindings.

---
v7->v8:
- no change
v6->v7:
- no change
v5->v6:
- no change
v4->v5:
- add power supply info
v3->v4:
- no changes
v2->v3:
- move 'additionalproperties' after 'required' block
- remove block style indicator '|' from description v1->v2:
- fix yaml issue: make maintainers a list

 .../bindings/iio/light/rohm,bh1745.yaml       | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/light/rohm,bh1745.yaml
Documentation/devicetree/bindings/iio/light/rohm,bh1745.yaml new file +53 -0
new file mode 100644
index 000000000000..44896795c67e
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/light/rohm,bh1745.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ROHM BH1745 colour sensor
+
+maintainers:
+  - Mudit Sharma <[email protected]>
+
+description:
+  BH1745 is an I2C colour sensor with red, green, blue and clear
+  channels. It has a programmable active low interrupt pin.
+  Interrupt occurs when the signal from the selected interrupt
+  source channel crosses set interrupt threshold high/low level.
+
+properties:
+  compatible:
+    const: rohm,bh1745
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  vdd-supply: true
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        colour-sensor@38 {
+            compatible = "rohm,bh1745";
+            reg = <0x38>;
+            interrupt-parent = <&gpio>;
+            interrupts = <19 IRQ_TYPE_LEVEL_LOW>;
+            vdd-supply = <&vdd>;
+        };
+    };
+
+...
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v8 2/2 iio: light: ROHM BH1745 colour sensor

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Add myself as the maintainer for this driver in MAINTAINERS.

---
v7->v8:
- Code format fixes:
- Make register fields easily associable with register name
- Avoid reassigning variables used for reading variable value to bit field.
- Move 'devm_add_action_or_reset()' just after 'power_on()'
- Drop val2 parameter from 'bh1745_set_scale()' as value of scale is going to be of type IIO_VAL_INT
v6->v7:
- Remove trigger registration
- Code format fixes
- Organise register information
- Simply return 'dev_err_probe()' in probe functions
- Use '/**/' for comments
- Clearly label 'readable' and 'writeable' for readable and writeable regmap ranges
- Drop gain and integration time LU tables and use functions from GTS helper
- Fix logic in 'bh1745_read_event_config()' and 'bh1745_write_event_config()'
- Better utilise regmap API
v5->v6:
- Fix typo
- Fix Indentation
- Drop read in bh1745_set_trigger_state() as configuring all the value v4->v5:
- Provide scale instead of HW gain
- Use GTS helpers
- Code style fixes
- Add check for part ID during probe
- Always enable latch when enabling interrupt
- Use devm_add_action_or_reset() and drop bh1745_remove() function
- Drop custom DEVICE_ATTR and provide related read_avail and setup interrupt source with event_config
- Make buffer support independent of IRQ
- Add power regulator handing with devm_regulator_get_enable()
- Drop read and write wrappers and use regmap functions directly
- Add MODULE_DEVICE_TABLE for of_device_id
v3->v4:
- Fix formatting:
- Remove redundant new line
- Use '0x' rather than '0X'
v2->v3:
- Squash commit for addition to MAINTAINERS
- Fix code style for consistency:
- New line before last 'return'
- Use variable name 'value' instead of 'val' in

    'bh1745_set_trigger_state()'

- Align function parameters to match parenthesis
- Avoid use of magic number
- Use named enum instead of anonymous enum
- Use 'guard(mutex)(&data->lock)' instead of 'mutex_lock()' 'mutex_unlock()'
- Only initialize 'ret' and 'value' when necessary
- Fix and optimize logic for in_interrupt_latch_store()
- Fix error handling in irq , trigger handlers and dev attribute for latch
v1->v2:
- No changes

 MAINTAINERS                |   6 +
 drivers/iio/light/Kconfig  |  13 +
 drivers/iio/light/Makefile |   1 +
 drivers/iio/light/bh1745.c | 883 +++++++++++++++++++++++++++++++++++++
 4 files changed, 903 insertions(+)
 create mode 100644 drivers/iio/light/bh1745.c
MAINTAINERS +6 -0
index 2ca8f35dfe03..e9ff6f465e7f 100644
@@ -19404,6 +19404,12 @@ S:	Supported
 F:	drivers/power/supply/bd99954-charger.c
 F:	drivers/power/supply/bd99954-charger.h
 
+ROHM BH1745 COLOUR SENSOR
+M:	Mudit Sharma <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/iio/light/bh1745.c
+
 ROHM BH1750 AMBIENT LIGHT SENSOR DRIVER
 M:	Tomasz Duszynski <[email protected]>
 S:	Maintained
drivers/iio/light/Kconfig +13 -0
index 9a587d403118..6cde702fa78d 100644
@@ -114,6 +114,19 @@ config AS73211
 	 This driver can also be built as a module.  If so, the module
 	 will be called as73211.
 
+config BH1745
+	tristate "ROHM BH1745 colour sensor"
+	depends on I2C
+	select REGMAP_I2C
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	select IIO_GTS_HELPER
+	help
+	  Say Y here to build support for the ROHM bh1745 colour sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called bh1745.
+
 config BH1750
 	tristate "ROHM BH1750 ambient light sensor"
 	depends on I2C
drivers/iio/light/Makefile +1 -0
index a30f906e91ba..939a701a06ac 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_APDS9300)		+= apds9300.o
 obj-$(CONFIG_APDS9306)		+= apds9306.o
 obj-$(CONFIG_APDS9960)		+= apds9960.o
 obj-$(CONFIG_AS73211)		+= as73211.o
+obj-$(CONFIG_BH1745)		+= bh1745.o
 obj-$(CONFIG_BH1750)		+= bh1750.o
 obj-$(CONFIG_BH1780)		+= bh1780.o
 obj-$(CONFIG_CM32181)		+= cm32181.o
drivers/iio/light/bh1745.c new file +883 -0

883 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v8 2/2] iio: light: ROHM BH1745 colour sensor

Quoted 9 lines · Mudit Sharma

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Quoted 5 lines

Add myself as the maintainer for this driver in MAINTAINERS.

Hi Mudit,

a couple of nitpicks inline.

...

Quoted 22 lines
drivers/iio/light/bh1745.c new file +22 -0
+static int bh1745_set_int_time(struct bh1745_data *data, int val, int val2)
+{
+	struct device *dev = data->dev;
+	int ret;
+	int value;
+	int current_int_time, current_hwgain_sel, current_hwgain;
+	int new_hwgain, new_hwgain_sel, new_int_time_sel;
+	int req_int_time = (1000000 * val) + val2;
+
+	if (!iio_gts_valid_time(&data->gts, req_int_time)) {
+		dev_dbg(dev, "Unsupported integration time requested: %d\n",
+			req_int_time);
+		return -EINVAL;
+	}
+
+	ret = bh1745_get_int_time(data, &current_int_time);
+	if (ret)
+		return ret;
+
+	if (current_int_time == req_int_time)
+		return 0;
+

drivers/iio/light/bh1745.c line 369

Quoted 10 lines
drivers/iio/light/bh1745.c new file +10 -0
+	ret = regmap_read(data->regmap, BH1745_MODE_CTRL2, &value);
+	if (ret)
+		return ret;
+
+	current_hwgain_sel = FIELD_GET(BH1745_CTRL2_ADC_GAIN_MASK, value);
+	current_hwgain = iio_gts_find_gain_by_sel(&data->gts, current_hwgain_sel);
+	ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, current_hwgain,
+						     current_int_time, req_int_time,
+						     &new_hwgain);
+	if (new_hwgain < 0) {

Typo in debug message: corresponding. I would recommend you to pass codespell to checkpatch. It will often catch such things.

Quoted 4 lines
drivers/iio/light/bh1745.c new file +4 -0
+		dev_dbg(dev, "No corrosponding gain for requested integration time\n");
+		return ret;
+	}
+

...

Quoted 1 line

+static int bh1745_write_raw_get_fmt(struct iio_dev *indio_dev,

Nit: the alignment seems to be a bit off here.

Quoted 15 lines
drivers/iio/light/bh1745.c new file +15 -0
+				      struct iio_chan_spec const *chan,
+				      long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_INT_TIME:
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+

...

drivers/iio/light/bh1745.c line 632

Quoted 12 lines
drivers/iio/light/bh1745.c new file +12 -0
+static int bh1745_write_event_config(struct iio_dev *indio_dev,
+				     const struct iio_chan_spec *chan,
+				     enum iio_event_type type,
+				     enum iio_event_direction dir, int state)
+{
+	struct bh1745_data *data = iio_priv(indio_dev);
+	int value;
+
+	if (state == 0)
+		return regmap_clear_bits(data->regmap, BH1745_INTR, BH1745_INTR_ENABLE);
+
+	if (state == 1) {

Nit: empty line at the beginning of the scope.

Quoted 24 lines
drivers/iio/light/bh1745.c new file +23 -0
+
+		/* Latch is always enabled when enabling interrupt */
+		value = BH1745_INTR_ENABLE;
+
+		switch (chan->channel2) {
+		case IIO_MOD_LIGHT_RED:
+			return regmap_write(data->regmap, BH1745_INTR,
+					    value | FIELD_PREP(BH1745_INTR_SOURCE_MASK,
+							       BH1745_INTR_SOURCE_RED));
+
+		case IIO_MOD_LIGHT_GREEN:
+			return regmap_write(data->regmap, BH1745_INTR,
+					    value | FIELD_PREP(BH1745_INTR_SOURCE_MASK,
+							       BH1745_INTR_SOURCE_GREEN));
+
+		case IIO_MOD_LIGHT_BLUE:
+			return regmap_write(data->regmap, BH1745_INTR,
+					    value | FIELD_PREP(BH1745_INTR_SOURCE_MASK,
+							       BH1745_INTR_SOURCE_BLUE));
+
+		case IIO_MOD_LIGHT_CLEAR:
+			return regmap_write(data->regmap, BH1745_INTR,
+					    value | FIELD_PREP(BH1745_INTR_SOURCE_MASK,

drivers/iio/light/bh1745.c line 665

Quoted 10 lines
drivers/iio/light/bh1745.c new file +10 -0
+							       BH1745_INTR_SOURCE_CLEAR));
+
+		default:
+			return -EINVAL;
+		}
+	}
+
+	return -EINVAL;
+}
+

Best regards,
Javier Carrasco

author maintainer
#3 lore raw

Re: [PATCH v8 2/2] iio: light: ROHM BH1745 colour sensor

Quoted 18 lines · Javier Carrasco
Quoted 9 lines · Mudit Sharma

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Quoted 5 lines

Add myself as the maintainer for this driver in MAINTAINERS.

Hi Mudit,

Quoted 8 lines

a couple of nitpicks inline.

...

Typo in debug message: corresponding. I would recommend you to pass codespell to checkpatch. It will often catch such things.

Hi Javier,

Thank you for reviewing and the recommendation. Will keep in mind for future patches.

Best regards,
Mudit Sharma

Quoted 4 lines
Quoted 4 lines
drivers/iio/light/bh1745.c new file +4 -0
+		dev_dbg(dev, "No corrosponding gain for requested integration time\n");
+		return ret;
+	}
+
maintainer
#4 lore raw

Re: [PATCH v8 2/2] iio: light: ROHM BH1745 colour sensor

In reply to Mudit Sharma
Quoted 9 lines · On Thu, 18 Jul 2024 23:02:06 +0100 Mudit Sharma <[email protected]>…

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Quoted 5 lines

Add myself as the maintainer for this driver in MAINTAINERS.

Hi Mudit.

Rather than go around again, I've applied a few things I noticed and changes Javier suggests whilst picking this up.

I also tweaked a few long lines.

Anyhow, applied to the testing branch of iio.git which will be rebased on rc1 once available and pushed out as togreg for linux-next to pick up.

Thanks,

Jonathan

Quoted 6 lines
drivers/iio/light/bh1745.c new file +6 -0
+
+static irqreturn_t bh1745_trigger_handler(int interrupt, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct bh1745_data *data = iio_priv(indio_dev);

drivers/iio/light/bh1745.c line 736

Quoted 10 lines
drivers/iio/light/bh1745.c new file +10 -0
+	struct {
+		u16 chans[4];
+		s64 timestamp __aligned(8);
+	} scan;
+	u16 value;
+	int ret;
+	int i;
+	int j = 0;
+
+	for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) {

Nuno's new helper iio_for_each_active_channel()
cleans this up. I'll switch to that whilst applying rather than adding another driver for him to convert over.

Quoted 20 lines
drivers/iio/light/bh1745.c new file +19 -0
+		ret = regmap_bulk_read(data->regmap, BH1745_RED_LSB + 2 * i, &value, 2);
+		if (ret)
+			goto err;
+
+		scan.chans[j++] = value;
+	}
+
+	iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
+
+err:
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
 
+
+static int bh1745_probe(struct i2c_client *client)
+{
+	int ret;
+	int value;

drivers/iio/light/bh1745.c line 818

Quoted 10 lines
drivers/iio/light/bh1745.c new file +10 -0
+	int part_id;
+	struct bh1745_data *data;
+	struct iio_dev *indio_dev;
+	struct device *dev = &client->dev;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	i2c_set_clientdata(client, indio_dev);

Trivial, but is this ever used? I couldn't figure out where it if is. So I've dropped it. Shout if it needs to be here.

Jonathan

author maintainer
#5 lore raw

Re: [PATCH v8 2/2] iio: light: ROHM BH1745 colour sensor

Quoted 14 lines · Jonathan Cameron
Quoted 11 lines · On Thu, 18 Jul 2024 23:02:06 +0100 Mudit Sharma <[email protected]>…

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Add myself as the maintainer for this driver in MAINTAINERS.

Quoted 8 lines
Quoted 3 lines

Hi Mudit.

Rather than go around again, I've applied a few things I noticed and changes Javier suggests whilst picking this up.

Hi Jonathan,

Thank you for reviewing and applying the changes.

Quoted 9 lines

I also tweaked a few long lines.

Anyhow, applied to the testing branch of iio.git which will be rebased on rc1 once available and pushed out as togreg for linux-next to pick up.

Thanks,

Quoted 8 lines

Jonathan

drivers/iio/light/bh1745.c line 818

Quoted 2 lines
drivers/iio/light/bh1745.c new file +2 -0
+
+	i2c_set_clientdata(client, indio_dev);

Trivial, but is this ever used? I couldn't figure out where it if is. So I've dropped it. Shout if it needs to be here.

This isn't used and can be dropped.

Thank you.

Best regards,
Mudit Sharma

#6 lore raw

Re: [PATCH v8 2/2] iio: light: ROHM BH1745 colour sensor

In reply to Mudit Sharma
Quoted 9 lines · Mudit Sharma

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Quoted 5 lines

Add myself as the maintainer for this driver in MAINTAINERS.

Hi Mudit & All :)

I know I am late. The series has already been applied (thanks Jonathan!). I've mostly been offline for the last 1.5 months or so - "all work and no play makes Jack a dull boy", you know ;)

Anyways, as Jonathan asked me to take a look at the GTS stuff (at v7), I tried to quickly glance at this. It looks good to me!

Well, the real test will be the users of the sensor driver - so please let us know if GTS stuff brings problems to users. I am mostly interested in knowing if gain changes caused by integration time changes are handled gracefully by the users. :) Well, seeing there is no per-channel gain or integration time setting, you should be safe from the worst side-effects :)

Nice driver!

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

Re: [PATCH v8 2/2] iio: light: ROHM BH1745 colour sensor

Quoted 18 lines · Matti Vaittinen
Quoted 9 lines · Mudit Sharma

Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.

Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.

Quoted 5 lines

Add myself as the maintainer for this driver in MAINTAINERS.

Hi Mudit & All :)

Quoted 7 lines

I know I am late. The series has already been applied (thanks Jonathan!). I've mostly been offline for the last 1.5 months or so - "all work and no play makes Jack a dull boy", you know ;)

Anyways, as Jonathan asked me to take a look at the GTS stuff (at v7), I tried to quickly glance at this. It looks good to me!

Well, the real test will be the users of the sensor driver - so please let us know if GTS stuff brings problems to users. I am mostly interested in knowing if gain changes caused by integration time changes are handled gracefully by the users. :) Well, seeing there is no per- channel gain or integration time setting, you should be safe from the worst side-effects :)

Nice driver!

Hi Matti,

Thank you for your review on this :)

Best regards,
Mudit Sharma

Quoted 4 lines

Yours,
    -- Matti