Skip to contentSkip to conversation

add support for winsen MHZ19B CO2 sensor

PATCH Superseded Gyeyoung Baek 2 participants 7 messages Apr 22, 2025 – Apr 23, 2025

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Gyeyoung Baek

author maintainer
#0 lore raw

v5 cover add support for winsen MHZ19B CO2 sensor

v5:
- Include the required headers explicitly.
- Fix coding style overall.

v4:
- Ensure buffer is aligned to the cacheline.
- Fix coding style overall.

v3:
- Add vin supply regulator.
- Drop custom ABI.
- Drop unnecessary mutex.

v2:
- Add ABI doc.
- Add complete struct to receive UART transmission successfully.
- Add undersigned as a maintainer for the WINSEN MHZ19B.
- Modify to comply with the IIO subsystem ABI as much as possible.
- Revise the coding style overall.

Gyeyoung Baek (4):
dt-bindings: add winsen to the vendor prefixes
dt-bindings: add device tree support for winsen MHZ19B CO2 sensor
iio: chemical: add support for winsen MHZ19B CO2 sensor
MAINTAINERS: Add WINSEN MHZ19B

 .../bindings/iio/chemical/winsen,mhz19b.yaml  |  33 ++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 MAINTAINERS                                   |   6 +
 drivers/iio/chemical/Kconfig                  |  10 +
 drivers/iio/chemical/Makefile                 |   1 +
 drivers/iio/chemical/mhz19b.c                 | 311 ++++++++++++++++++
 6 files changed, 363 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/winsen,mhz19b.yaml
 create mode 100644 drivers/iio/chemical/mhz19b.c
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v5 1/4 dt-bindings: add winsen to the vendor prefixes

Add winsen to the vendor prefixes.

---
 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index 86f6a19b28ae..6d35549d2e4b 100644
@@ -1689,6 +1689,8 @@ patternProperties:
     description: Wingtech Technology Co., Ltd.
   "^winlink,.*":
     description: WinLink Co., Ltd
+  "^winsen,.*":
+    description: Winsen Corp.
   "^winstar,.*":
     description: Winstar Display Corp.
   "^wirelesstag,.*":
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v5 2/4 dt-bindings: add device tree support for winsen MHZ19B CO2 sensor

In reply to Gyeyoung Baek

Add device tree support for winsen MHZ19B sensor.

---
 .../bindings/iio/chemical/winsen,mhz19b.yaml  | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/winsen,mhz19b.yaml
Documentation/devicetree/bindings/iio/chemical/winsen,mhz19b.yaml new file +33 -0
new file mode 100644
index 000000000000..2a6ddb33f163
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/chemical/winsen,mhz19b.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MHZ19B CO2 sensor
+
+maintainers:
+  - Gyeyoung Baek <[email protected]>
+
+properties:
+  compatible:
+    const: winsen,mhz19b
+
+  vin-supply:
+    description: Regulator that provides power to the sensor
+
+required:
+  - compatible
+  - vin-supply
+
+additionalProperties: false
+
+examples:
+  - |
+    serial {
+      co2-sensor {
+        compatible = "winsen,mhz19b";
+        vin-supply = <&vdd>;
+      };
+    };
+...
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH v5 3/4 iio: chemical: add support for winsen MHZ19B CO2 sensor

In reply to Gyeyoung Baek

Add support for winsen MHZ19B CO2 sensor.

Datasheet: https://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf

---
 drivers/iio/chemical/Kconfig  |  10 ++
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/mhz19b.c | 311 ++++++++++++++++++++++++++++++++++
 3 files changed, 322 insertions(+)
 create mode 100644 drivers/iio/chemical/mhz19b.c
drivers/iio/chemical/Kconfig +10 -0
index 330fe0af946f..7742de3f9cdb 100644
@@ -108,6 +108,16 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config MHZ19B
+	tristate "Winsen MHZ19B CO2 sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build Serdev interface support for the Winsen
+	  MHZ19B CO2 sensor.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called mhz19b.
+
 config PMS7003
 	tristate "Plantower PMS7003 particulate matter sensor"
 	depends on SERIAL_DEV_BUS
drivers/iio/chemical/Makefile +1 -0
index 4866db06bdc9..c63daebf39ac 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_ENS160) += ens160_core.o
 obj-$(CONFIG_ENS160_I2C) += ens160_i2c.o
 obj-$(CONFIG_ENS160_SPI) += ens160_spi.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_MHZ19B) += mhz19b.o
 obj-$(CONFIG_PMS7003) += pms7003.o
 obj-$(CONFIG_SCD30_CORE) += scd30_core.o
 obj-$(CONFIG_SCD30_I2C) += scd30_i2c.o
drivers/iio/chemical/mhz19b.c new file +311 -0

311 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v5 3/4] iio: chemical: add support for winsen MHZ19B CO2 sensor

Quoted 1 line · Gyeyoung Baek

Add support for winsen MHZ19B CO2 sensor.

Winsen (name capitalisation)?

...

Quoted 8 lines
drivers/iio/chemical/mhz19b.c new file +8 -0
+static ssize_t calibration_forced_value_store(struct device *dev,
+					      struct device_attribute *attr,
+					      const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	u16 ppm;
+	int cmd, ret;
+

drivers/iio/chemical/mhz19b.c line 194

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+	ret = kstrtou16(buf, 0, &ppm);
+	if (ret)
+		return ret;
+
+	if (ppm) {
+		if (!in_range(ppm, 1000, 4001)) {
+			dev_dbg(&indio_dev->dev,
+				"span point ppm should be between 1000 and 5000 inclusive.");
+			return -EINVAL;
+		}

I proposed to define the _MIN and _MAX constants for the range and use them in the parameters. Any objection?

drivers/iio/chemical/mhz19b.c line 206

Quoted 11 lines
drivers/iio/chemical/mhz19b.c new file +11 -0
+		cmd = MHZ19B_SPAN_POINT_CMD;
+	} else {
+		cmd = MHZ19B_ZERO_POINT_CMD;
+	}
+
+	ret = mhz19b_serdev_cmd(indio_dev, cmd, ppm);
+	if (ret < 0)
+		return ret;
+
+	return len;
+}

Otherwise LGTM, thanks!

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

Re: [PATCH v5 3/4] iio: chemical: add support for winsen MHZ19B CO2 sensor

Quoted 9 lines

drivers/iio/chemical/mhz19b.c line 194

Quoted 6 lines
drivers/iio/chemical/mhz19b.c new file +6 -0
+	if (ppm) {
+		if (!in_range(ppm, 1000, 4001)) {
+			dev_dbg(&indio_dev->dev,
+				"span point ppm should be between 1000 and 5000 inclusive.");
+			return -EINVAL;
+		}

I proposed to define the _MIN and _MAX constants for the range and use them in the parameters. Any objection?

I'm sorry, my Gmail didn’t show your replies for some reason, so I missed your previous review. I only noticed it after checking iio lore directly.

It seems more readable to use _MIN and _MAX. I'll use those, thanks!

---

Quoted 5 lines

drivers/iio/chemical/mhz19b.c line 168

Quoted 3 lines
drivers/iio/chemical/mhz19b.c new file +3 -0
+
+	return len;
+}

Otherwise LGTM, thanks!

I'll send new patch based on your other review comments. thanks.

--
Regards,
Gyeyoung

author maintainer
#6 lore raw

PATCH v5 4/4 MAINTAINERS: Add WINSEN MHZ19B

In reply to Gyeyoung Baek

Add undersigned as a maintainer for the WINSEN MHZ19B.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 01079a189c93..4a0089db6670 100644
@@ -26015,6 +26015,12 @@ M:	David Härdeman <[email protected]>
 S:	Maintained
 F:	drivers/media/rc/winbond-cir.c
 
+WINSEN MHZ19B
+M:	Gyeyoung Baek <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/chemical/winsen,mhz19b.yaml
+F:	drivers/iio/chemical/mhz19b.c
+
 WINSYSTEMS EBC-C384 WATCHDOG DRIVER
 L:	[email protected]
 S:	Orphan
Signature · 1 line
2.34.1