Skip to contentSkip to conversation

add support for winsen MHZ19B CO2 sensor

PATCH Superseded Gyeyoung Baek 4 participants 24 messages Apr 3, 2025 – Apr 7, 2025

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Gyeyoung Baek

author maintainer
#0 lore raw

cover add support for winsen MHZ19B CO2 sensor

This patch series adds support for winsen MHZ19B CO2 sensor.

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

v1:
- 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 (5):
dt-bindings: add winsen to the vendor prefixes
dt-bindings: add device tree support for winsen MHZ19B CO2 sensor
ABI: iio: add new ABI doc for mhz19b
iio: chemical: add support for winsen MHZ19B CO2 sensor
MAINTAINERS: Add WINSEN MHZ19B

 .../ABI/testing/sysfs-bus-iio-chemical-mhz19b |   7 +
 .../bindings/iio/chemical/winsen,mhz19b.yaml  |  33 ++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 MAINTAINERS                                   |   7 +
 drivers/iio/chemical/Kconfig                  |  10 +
 drivers/iio/chemical/Makefile                 |   1 +
 drivers/iio/chemical/mhz19b.c                 | 386 ++++++++++++++++++
 7 files changed, 446 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/winsen,mhz19b.yaml
 create mode 100644 drivers/iio/chemical/mhz19b.c

--
2.34.1

author maintainer
#1 lore raw

PATCH 1/5 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 5079ca6ce1d1..ee7f6100c432 100644
@@ -1679,6 +1679,8 @@ patternProperties:
     description: Wingtech Technology Co., Ltd.
   "^winlink,.*":
     description: WinLink Co., Ltd
+  "^winsen,.*":
+    description: Winsen Corp.
   "^winstar,.*":
     description: Winstar Display Corp.
   "^wirelesstag,.*":

--
2.34.1

Acked-by on v1 1/5
#2 lore raw

Re: [PATCH v1 1/5] dt-bindings: add winsen to the vendor prefixes

Quoted 3 lines · Gyeyoung Baek

Add winsen to the vendor prefixes.

I am pretty sure you sent v1, so this is v2. And if this is anyhow confusing, then just use standard tools - b4 - which would do this right.

---

<form letter>
This is an automated instruction, just in case, because many review tags are being ignored. If you know the process, you can skip it (please do not feel offended by me posting it here - no bad intentions intended). If you do not know the process, here is a short explanation:

Please add Acked-by/Reviewed-by/Tested-by tags when posting new versions of patchset, under or above your Signed-off-by tag, unless patch changed significantly (e.g. new properties added to the DT bindings). Tag is "received", when provided in a message replied to you on the mailing list. Tools like b4 can help here. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for tags received on the version they apply.

Full context and explanation:
https://elixir.bootlin.com/linux/v6.12-rc3/source/Documentation/process/submitting-patches.rst#L577
</form letter>

Best regards,
Krzysztof

author maintainer
#3 lore raw

Re: [PATCH v1 1/5] dt-bindings: add winsen to the vendor prefixes

Hello Krzysztof, thank you for the review.

Quoted 9 lines · Krzysztof Kozlowski
Quoted 3 lines · Gyeyoung Baek

Add winsen to the vendor prefixes.

I am pretty sure you sent v1, so this is v2. And if this is anyhow confusing, then just use standard tools - b4 - which would do this right.

Yes, actually this is v2. I did it manually, so there was a mistake. Thank you for the tips.

Thanks,
Gyeyoung Baek

author maintainer
#4 lore raw

PATCH 2/5 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..16ec902c906e
@@ -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>;
+      };
+    };
+...

--
2.34.1

Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH v1 2/5] dt-bindings: add device tree support for winsen MHZ19B CO2 sensor

Quoted 4 lines · Gyeyoung Baek

Add device tree support for winsen MHZ19B sensor.

---

---

<form letter>
This is an automated instruction, just in case, because many review tags are being ignored. If you know the process, you can skip it (please do not feel offended by me posting it here - no bad intentions intended). If you do not know the process, here is a short explanation:

Please add Acked-by/Reviewed-by/Tested-by tags when posting new versions of patchset, under or above your Signed-off-by tag, unless patch changed significantly (e.g. new properties added to the DT bindings). Tag is "received", when provided in a message replied to you on the mailing list. Tools like b4 can help here. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for tags received on the version they apply.

Full context and explanation:
https://elixir.bootlin.com/linux/v6.12-rc3/source/Documentation/process/submitting-patches.rst#L577
</form letter>

Best regards,
Krzysztof

author maintainer
#6 lore raw

PATCH 3/5 ABI: iio: add new ABI doc for mhz19b

In reply to Gyeyoung Baek

Add support for winsen MHZ19B CO2 sensor.

---
 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +7 -0
new file mode 100644
index 000000000000..6cdfd34be016
@@ -0,0 +1,7 @@
+What:		/sys/bus/iio/devices/co2_range
+Date:		April 2025
+KernelVersion:	6.14
+Contact:	Gyeyoung Baek <[email protected]>
+Description:
+		Writing a value adjust maximum measurable PPM.
+		should be 2000 or 5000.

--
2.34.1

#7 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Quoted 14 lines · Gyeyoung Baek

Add support for winsen MHZ19B CO2 sensor.

---
 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +0 -0
new file mode 100644
index 000000000000..6cdfd34be016

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 1

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
@@ -0,0 +1,7 @@
+What:		/sys/bus/iio/devices/co2_range

Incomplete path

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 2

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
+Date:		April 2025

Not possible. This will be probably June.

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 3

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
+KernelVersion:	6.14

No way you can put ABI into version already released. It's impossible. That's v6.16.

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 4

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
+

Best regards,
Krzysztof

author maintainer
#8 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Quoted 20 lines · Krzysztof Kozlowski
Quoted 14 lines · Gyeyoung Baek

Add support for winsen MHZ19B CO2 sensor.

---
 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +0 -0
new file mode 100644
index 000000000000..6cdfd34be016

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 1

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
@@ -0,0 +1,7 @@
+What:                /sys/bus/iio/devices/co2_range

Incomplete path

Quoted 8 lines

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 2

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
+Date:                April 2025

Not possible. This will be probably June.

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 3

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +1 -0
+KernelVersion:       6.14

No way you can put ABI into version already released. It's impossible. That's v6.16.

sorry, I'll read the kernel doc more carefully.

Thanks,
Gyeyoung

#9 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

In reply to Gyeyoung Baek
Quoted 14 lines · On Thu, 3 Apr 2025 14:32:23 +0900 Gyeyoung Baek <[email protected]> wrote:

Add support for winsen MHZ19B CO2 sensor.

---
 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +0 -0
new file mode 100644
index 000000000000..6cdfd34be016

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 7

Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +7 -0
@@ -0,0 +1,7 @@
+What:		/sys/bus/iio/devices/co2_range
+Date:		April 2025
+KernelVersion:	6.14
+Contact:	Gyeyoung Baek <[email protected]>
+Description:
+		Writing a value adjust maximum measurable PPM.
+		should be 2000 or 5000.

I haven't checked but assume this also results in a scaling of the measure _raw values? If so the control should be via the standard ABI scale. If you need to be able to establish the range, provide the _available for the _raw via the read_avail() callback and setting appropriate bit in info_mask_separate_available

General rule is don't introduce new ABI unless it is impossible to provide the same information via existing interfaces. The decision to use scale rather than range info to control channel scaling was made a very long time ago and having a mixture of the two would make for very complex userspace code.

Jonathan

Quoted 4 lines

--
2.34.1

author maintainer
#10 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Hello Jonathan, thank you for the review.

Quoted 11 lines
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +5 -0
new file mode 100644
index 000000000000..6cdfd34be016
@@ -0,0 +1,7 @@
+What:                /sys/bus/iio/devices/co2_range
+Date:                April 2025
+KernelVersion:       6.14
+Contact:     Gyeyoung Baek <[email protected]>
+Description:
Quoted 9 lines

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 7

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +2 -0
+             Writing a value adjust maximum measurable PPM.
+             should be 2000 or 5000.

I haven't checked but assume this also results in a scaling of the measure _raw values? If so the control should be via the standard ABI scale. If you need to be able to establish the range, provide the _available for the _raw via the read_avail() callback and setting appropriate bit in info_mask_separate_available

In this device, changing the measurement range does not affect the unit or scaling.
As far as I know, increasing the range just leads to a decrease in accuracy.

Quoted 5 lines

General rule is don't introduce new ABI unless it is impossible to provide the same information via existing interfaces. The decision to use scale rather than range info to control channel scaling was made a very long time ago and having a mixture of the two would make for very complex userspace code.

I’ve reviewed the sysfs-bus-iio documentation, I think there is no suitable interface for this case. So I'll drop this option.

Thanks,
Gyeyoung

maintainer
#11 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Quoted 23 lines · On Sat, 5 Apr 2025 22:47:45 +0900 gyeyoung <[email protected]> wrote:

Hello Jonathan, thank you for the review.

Quoted 11 lines
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +5 -0
new file mode 100644
index 000000000000..6cdfd34be016
@@ -0,0 +1,7 @@
+What:                /sys/bus/iio/devices/co2_range
+Date:                April 2025
+KernelVersion:       6.14
+Contact:     Gyeyoung Baek <[email protected]>
+Description:
Quoted 9 lines

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 7

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +2 -0
+             Writing a value adjust maximum measurable PPM.
+             should be 2000 or 5000.  

I haven't checked but assume this also results in a scaling of the measure _raw values? If so the control should be via the standard ABI scale. If you need to be able to establish the range, provide the _available for the _raw via the read_avail() callback and setting appropriate bit in info_mask_separate_available

Quoted 3 lines

In this device, changing the measurement range does not affect the unit or scaling.
As far as I know, increasing the range just leads to a decrease in accuracy.

That's unusual but fair enough. hardwaregain is perhaps appropriate as this doesn't really map to calibscale which is the other thing close to this.

Quoted 12 lines
Quoted 6 lines

General rule is don't introduce new ABI unless it is impossible to provide the same information via existing interfaces. The decision to use scale rather than range info to control channel scaling was made a very long time ago and having a mixture of the two would make for very complex userspace code.

I’ve reviewed the sysfs-bus-iio documentation, I think there is no suitable interface for this case. So I'll drop this option.

Thanks,
Gyeyoung

maintainer
#12 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Quoted 26 lines · On Sun, 6 Apr 2025 12:20:48 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 23 lines · On Sat, 5 Apr 2025 22:47:45 +0900 gyeyoung <[email protected]> wrote:

Hello Jonathan, thank you for the review.

Quoted 20 lines

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 7

Quoted 13 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +7 -0
new file mode 100644
index 000000000000..6cdfd34be016
@@ -0,0 +1,7 @@
+What:                /sys/bus/iio/devices/co2_range
+Date:                April 2025
+KernelVersion:       6.14
+Contact:     Gyeyoung Baek <[email protected]>
+Description:
+             Writing a value adjust maximum measurable PPM.
+             should be 2000 or 5000.    

I haven't checked but assume this also results in a scaling of the measure _raw values? If so the control should be via the standard ABI scale. If you need to be able to establish the range, provide the _available for the _raw via the read_avail() callback and setting appropriate bit in info_mask_separate_available

Quoted 8 lines
Quoted 3 lines

In this device, changing the measurement range does not affect the unit or scaling.
As far as I know, increasing the range just leads to a decrease in accuracy.

That's unusual but fair enough. hardwaregain is perhaps appropriate as this doesn't really map to calibscale which is the other thing close to this.

Actually - any idea what the gain is doing? Is it adjusting a analog amplfier, or messing with the integration time (I have little idea how these sensors work!)

Jonathan

Quoted 13 lines
Quoted 12 lines
Quoted 6 lines

General rule is don't introduce new ABI unless it is impossible to provide the same information via existing interfaces. The decision to use scale rather than range info to control channel scaling was made a very long time ago and having a mixture of the two would make for very complex userspace code.

I’ve reviewed the sysfs-bus-iio documentation, I think there is no suitable interface for this case. So I'll drop this option.

Thanks,
Gyeyoung

author maintainer
#13 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Hello Jonathan,

Quoted 34 lines · Jonathan Cameron
Quoted 30 lines · On Sun, 6 Apr 2025 12:20:48 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 26 lines · On Sat, 5 Apr 2025 22:47:45 +0900 gyeyoung <[email protected]> wrote:

Hello Jonathan, thank you for the review.

Quoted 11 lines
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +5 -0
new file mode 100644
index 000000000000..6cdfd34be016
@@ -0,0 +1,7 @@
+What:                /sys/bus/iio/devices/co2_range
+Date:                April 2025
+KernelVersion:       6.14
+Contact:     Gyeyoung Baek <[email protected]>
+Description:
Quoted 9 lines

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 7

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +2 -0
+             Writing a value adjust maximum measurable PPM.
+             should be 2000 or 5000.

I haven't checked but assume this also results in a scaling of the measure _raw values? If so the control should be via the standard ABI scale. If you need to be able to establish the range, provide the _available for the _raw via the read_avail() callback and setting appropriate bit in info_mask_separate_available

In this device, changing the measurement range does not affect the unit or scaling.
As far as I know, increasing the range just leads to a decrease in accuracy.

Quoted 8 lines
Quoted 4 lines

That's unusual but fair enough. hardwaregain is perhaps appropriate as this doesn't really map to calibscale which is the other thing close to this.

Actually - any idea what the gain is doing? Is it adjusting a analog amplfier, or messing with the integration time (I have little idea how these sensors work!)

sorry, I'm not affiliated with the vendor,
so I do not have any information other than the datasheet.
And the datasheet doesn't specify any trade-offs related to adjusting the range. (only "± (50ppm+3% reading value)" is here)

I think this range setting would be better implemented in some other way (like module parameter) rather than IIO subsystem. For now I'll just drop this range setting code..

Regards,
Gyeyoung

maintainer
#14 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Quoted 45 lines · On Sun, 6 Apr 2025 22:31:43 +0900 gyeyoung <[email protected]> wrote:

Hello Jonathan,

Quoted 34 lines · Jonathan Cameron
Quoted 30 lines · On Sun, 6 Apr 2025 12:20:48 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 26 lines · On Sat, 5 Apr 2025 22:47:45 +0900 gyeyoung <[email protected]> wrote:

Hello Jonathan, thank you for the review.

Quoted 11 lines
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +5 -0
new file mode 100644
index 000000000000..6cdfd34be016
@@ -0,0 +1,7 @@
+What:                /sys/bus/iio/devices/co2_range
+Date:                April 2025
+KernelVersion:       6.14
+Contact:     Gyeyoung Baek <[email protected]>
+Description:
Quoted 9 lines

Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b line 7

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-iio-chemical-mhz19b new file +2 -0
+             Writing a value adjust maximum measurable PPM.
+             should be 2000 or 5000.  

I haven't checked but assume this also results in a scaling of the measure _raw values? If so the control should be via the standard ABI scale. If you need to be able to establish the range, provide the _available for the _raw via the read_avail() callback and setting appropriate bit in info_mask_separate_available

In this device, changing the measurement range does not affect the unit or scaling.
As far as I know, increasing the range just leads to a decrease in accuracy.

Quoted 8 lines
Quoted 4 lines

That's unusual but fair enough. hardwaregain is perhaps appropriate as this doesn't really map to calibscale which is the other thing close to this.

Actually - any idea what the gain is doing? Is it adjusting a analog amplfier, or messing with the integration time (I have little idea how these sensors work!)

Quoted 7 lines

sorry, I'm not affiliated with the vendor,
so I do not have any information other than the datasheet.
And the datasheet doesn't specify any trade-offs related to adjusting the range. (only "± (50ppm+3% reading value)" is here)

I think this range setting would be better implemented in some other way (like module parameter) rather than IIO subsystem.

I'm not in general keen on module parameters effecting policy (and noise vs range is definitely a policy thing) so I think we would want to figure out a suitable sysfs ABI.

Quoted 1 line

For now I'll just drop this range setting code..

Makes sense - can revisit when needed.

Quoted 3 lines

Regards,
Gyeyoung

author maintainer
#15 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Hello Jonathan,

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

Actually - any idea what the gain is doing? Is it adjusting a analog amplfier, or messing with the integration time (I have little idea how these sensors work!)

sorry, I'm not affiliated with the vendor,
so I do not have any information other than the datasheet.
And the datasheet doesn't specify any trade-offs related to adjusting the range. (only "± (50ppm+3% reading value)" is here)

Quoted 6 lines
Quoted 2 lines

I think this range setting would be better implemented in some other way (like module parameter) rather than IIO subsystem.

I'm not in general keen on module parameters effecting policy (and noise vs range is definitely a policy thing) so I think we would want to figure out a suitable sysfs ABI.

Or, how about adding IIO_CHAN_INFO_RANGE to the iio_chan_info_postfix[] array for range setting?
I think there might be examples of devices that expand the range without affecting scaling,
like this device (though I haven’t found a case yet...)

If this is a valid approach, would it be okay if I submit a patch for it?

Regards,
Gyeyoung

maintainer
#16 lore raw

Re: [PATCH v1 3/5] ABI: iio: add new ABI doc for mhz19b

Quoted 19 lines · On Mon, 7 Apr 2025 11:10:21 +0900 gyeyoung <[email protected]> wrote:

Hello Jonathan,

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

Actually - any idea what the gain is doing? Is it adjusting a analog amplfier, or messing with the integration time (I have little idea how these sensors work!)

sorry, I'm not affiliated with the vendor,
so I do not have any information other than the datasheet.
And the datasheet doesn't specify any trade-offs related to adjusting the range. (only "± (50ppm+3% reading value)" is here)

Quoted 6 lines
Quoted 2 lines

I think this range setting would be better implemented in some other way (like module parameter) rather than IIO subsystem.

I'm not in general keen on module parameters effecting policy (and noise vs range is definitely a policy thing) so I think we would want to figure out a suitable sysfs ABI.

Quoted 7 lines

Or, how about adding IIO_CHAN_INFO_RANGE to the iio_chan_info_postfix[] array for range setting?
I think there might be examples of devices that expand the range without affecting scaling,
like this device (though I haven’t found a case yet...)

If this is a valid approach, would it be okay if I submit a patch for it?

No. The reason isn't technical, but rather that if we provide that we'll spend years explaining to people that _scale is the right choice if the _scale is 1/_range (or similar).

Even without making it easy a lot of drivers try to do that!

I think leave it as an open question for the future for now.

Jonathan

Quoted 4 lines

Regards,
Gyeyoung

author maintainer
#17 lore raw

PATCH 4/5 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 | 386 ++++++++++++++++++++++++++++++++++
 3 files changed, 397 insertions(+)
 create mode 100644 drivers/iio/chemical/mhz19b.c
drivers/iio/chemical/Kconfig +10 -0
index 330fe0af946f..bb4dfe3986f6 100644
@@ -108,6 +108,16 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config MHZ19B
+	tristate "MHZ19B CO2 sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build Serdev binterface support for the 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 +385 -0

385 lines not on this page · raw message

--
2.34.1

#18 lore raw

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

Quoted 18 lines · 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 | 386 ++++++++++++++++++++++++++++++++++
 3 files changed, 397 insertions(+)
 create mode 100644 drivers/iio/chemical/mhz19b.c
drivers/iio/chemical/Kconfig +0 -0
index 330fe0af946f..bb4dfe3986f6 100644

drivers/iio/chemical/Kconfig config IAQCORE · line 111

Quoted 4 lines
drivers/iio/chemical/Kconfig +1 -0
@@ -108,6 +108,16 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config MHZ19B

probably WINSEN_MHZ19B

drivers/iio/chemical/Kconfig config IAQCORE · line 112

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+	tristate "MHZ19B CO2 sensor"

Similarly here

drivers/iio/chemical/Kconfig config IAQCORE · line 115

Quoted 3 lines
drivers/iio/chemical/Kconfig +3 -0
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build Serdev binterface support for the MHZ19B

Typo, interface

Quoted 48 lines
drivers/iio/chemical/Kconfig +5 -0
+	  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 +22 -0
new file mode 100644
index 000000000000..f8f06c01623f
@@ -0,0 +1,386 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * mh-z19b co2 sensor driver
+ *
+ * Copyright (c) 2025 Gyeyoung Baek <[email protected]>
+ *
+ * Datasheet:
+ * https://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf
+ *
+ * TODO:
+ *  - vin supply regulator
+ */
+
+#include <linux/cleanup.h>
+#include <linux/completion.h>
+#include <linux/device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>

drivers/iio/chemical/mhz19b.c line 32

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+#include <linux/mutex.h>
+#include <linux/regulator/consumer.h>
+#include <linux/serdev.h>
+#include <linux/unaligned.h>
+
+struct mhz19b_state {
+	struct serdev_device *serdev;
+
+	/* TO DO, nothing for now.*/
+	struct regulator *vin_supply;

Drop. Don't add dead code.

drivers/iio/chemical/mhz19b.c line 45

Quoted 13 lines
drivers/iio/chemical/mhz19b.c new file +13 -0
+
+	/* serdev receive buffer.
+	 * When data is received from the MH-Z19B,
+	 * the 'mhz19b_receive_buf' callback function is called and fills this buffer.
+	 */
+	char buf[9];
+	int buf_idx;
+
+	/* must wait the 'buf' is filled with 9 bytes.*/
+	struct completion buf_ready;
+
+	/* protect access to mhz19b_state */
+	struct mutex lock;

mhz19b_receive_buf() does not need any locking?

Best regards,
Krzysztof

author maintainer
#19 lore raw

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

Hello Krzysztof, Thank you for the review.

Quoted 6 lines
Quoted 6 lines
drivers/iio/chemical/mhz19b.c new file +6 -0
+     /* serdev receive buffer.
+      * When data is received from the MH-Z19B,
+      * the 'mhz19b_receive_buf' callback function is called and fills this buffer.
+      */
+     char buf[9];
+     int buf_idx;
Quoted 9 lines

drivers/iio/chemical/mhz19b.c line 45

Quoted 6 lines
drivers/iio/chemical/mhz19b.c new file +6 -0
+
+     /* must wait the 'buf' is filled with 9 bytes.*/
+     struct completion buf_ready;
+
+     /* protect access to mhz19b_state */
+     struct mutex lock;

mhz19b_receive_buf() does not need any locking?

As far as I know, receive_buf() starts from flush_to_ldisc() first. But this already has locking, so I think it's fine.

Thanks,
Gyeyoung

#20 lore raw

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

In reply to Gyeyoung Baek
Quoted 6 lines · On Thu, 3 Apr 2025 14:32:24 +0900 Gyeyoung Baek <[email protected]> wrote:

Add support for winsen MHZ19B CO2 sensor.

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

Hi.

Various comments inline,

Thanks,

Jonathan

drivers/iio/chemical/mhz19b.c line 32

Quoted 12 lines
drivers/iio/chemical/mhz19b.c new file +5 -0
new file mode 100644
index 000000000000..f8f06c01623f
@@ -0,0 +1,386 @@
 
+struct mhz19b_state {
+	struct serdev_device *serdev;
+
+	/* TO DO, nothing for now.*/
+	struct regulator *vin_supply;

Unlikely you need this here. Look at the
devm regulator calls.

drivers/iio/chemical/mhz19b.c

Quoted 2 lines
drivers/iio/chemical/mhz19b.c new file +2 -0
+
+	/* serdev receive buffer.

Fix comment syntax as mentioned below.

drivers/iio/chemical/mhz19b.c line 44

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+	 * When data is received from the MH-Z19B,
+	 * the 'mhz19b_receive_buf' callback function is called and fills this buffer.
+	 */
+	char buf[9];
+	int buf_idx;
+
+	/* must wait the 'buf' is filled with 9 bytes.*/
+	struct completion buf_ready;
+
+	/* protect access to mhz19b_state */

Be more specific. What and why? It's not protecting the regulator.

Quoted 6 lines
drivers/iio/chemical/mhz19b.c new file +6 -0
+	struct mutex lock;
+};
+
+/*
+ * commands have following format:
+ *

drivers/iio/chemical/mhz19b.c line 60

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+ * +------+------+-----+------+------+------+------+------+-------+
+ * | 0xFF | 0x01 | cmd | arg0 | arg1 | 0x00 | 0x00 | 0x00 | cksum |
+ * +------+------+-----+------+------+------+------+------+-------+
+ *
+ * The following commands are defined in the datasheet.
+ * https://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf
+ */
+#define MHZ19B_CMD_SIZE 9
+
+/* ABC logic in MHZ19B means auto calibration.

One line comment so move the */ up to end of this line.

drivers/iio/chemical/mhz19b.c line 86

Quoted 7 lines

+ */

drivers/iio/chemical/mhz19b.c new file +5 -0
+
+static int mhz19b_serdev_cmd(struct iio_dev *indio_dev,
+	int cmd, void *arg)
+{
+	int ret = 0;

Move declaraton to the scope where it is used.
Then it will be obcious that it is alaway set.

Quoted 44 lines
drivers/iio/chemical/mhz19b.c new file +44 -0
+	struct mhz19b_state *st = iio_priv(indio_dev);
+	struct serdev_device *serdev = st->serdev;
+	struct device *dev = &indio_dev->dev;
+
+	/* commands format is described above. */
+	uint8_t cmd_buf[MHZ19B_CMD_SIZE] = {
+		0xFF, 0x01, cmd,
+	};
+
+	switch (cmd) {
+	case MHZ19B_ABC_LOGIC_CMD: {
+		bool enable = *((bool *)arg);
+
+		cmd_buf[3] = enable ? 0xA0 : 0x00;
+		break;
+	} case MHZ19B_SPAN_POINT_CMD: {
+		uint16_t ppm = *((uint16_t *)arg);
+
+		put_unaligned_be16(ppm, &cmd_buf[3]);
+		break;
+	} case MHZ19B_DETECTION_RANGE_CMD: {
+		uint16_t range = *((uint16_t *)arg);
+
+		put_unaligned_be16(range, &cmd_buf[3]);
+		break;
+	} default:
+		break;
+	}
+	cmd_buf[MHZ19B_CMD_SIZE - 1] = mhz19b_get_checksum(cmd_buf);
+
+	scoped_guard(mutex, &st->lock) {
+		/* write buf to uart ctrl syncronously */
+		ret = serdev_device_write(serdev, cmd_buf, MHZ19B_CMD_SIZE, 0);
+		if (ret != MHZ19B_CMD_SIZE) {
+			dev_err(dev, "write err, %d bytes written", ret);
+			return -EINVAL;
+		}
+
+		switch (cmd) {
+		case MHZ19B_READ_CO2_CMD:
+			ret = wait_for_completion_interruptible_timeout(&st->buf_ready,
+				MHZ19B_SERDEV_TIMEOUT);
+			if (ret < 0)
+				return ret;

drivers/iio/chemical/mhz19b.c line 140

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+			if (!ret)
+				return -ETIMEDOUT;
+
+			ret = mhz19b_get_checksum(st->buf);
+			if (st->buf[MHZ19B_CMD_SIZE - 1] != mhz19b_get_checksum(st->buf)) {
+				dev_err(dev, "checksum err");
+				return -EINVAL;
+			}
+
+			ret = get_unaligned_be16(&st->buf[2]);

return get_unaligned_be16()

drivers/iio/chemical/mhz19b.c line 147

Quoted 7 lines
drivers/iio/chemical/mhz19b.c new file +7 -0
+			return ret;
+		default:
+			/* no response commands. */
+			return 0;
+		}
+	}
+}

Cache the ones that aren't a one time thing then (such as this one).

drivers/iio/chemical/mhz19b.c line 171

Quoted 9 lines
drivers/iio/chemical/mhz19b.c new file +9 -0
+
+static ssize_t calibration_auto_enable_store(struct device *dev,
+	struct device_attribute *attr,
+	const char *buf, size_t len)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	bool enable;
+
+	int ret = kstrtobool(buf, &enable);

As below. Either have this in the declaration block or not. This sort of half and half with extra blank lines is unusual.

Quoted 15 lines
drivers/iio/chemical/mhz19b.c new file +15 -0
+
+	if (ret)
+		return ret;
+
+	ret = mhz19b_serdev_cmd(indio_dev, MHZ19B_ABC_LOGIC_CMD, &enable);
+	if (ret < 0)
+		return ret;
+
+	return len;
+}
+static IIO_DEVICE_ATTR_WO(calibration_auto_enable, 0);
+
+/* write 0		: zero point calibration_auto_enable
+ *	(make sure the sensor had been worked under 400ppm for over 20 minutes.)
+ *

drivers/iio/chemical/mhz19b.c line 196

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+ * write 1000-5000	: span point calibration:
+ *	(make sure the sensor had been worked under a certain level co2 for over 20 minutes.)
+ */
+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);
+	uint16_t ppm;
+	int cmd;

The line break here is odd. I'd define

	int ret, cmd;

	ret = kstrou16()

drivers/iio/chemical/mhz19b.c line 199

Quoted 3 lines
drivers/iio/chemical/mhz19b.c new file +3 -0
+
+	int ret = kstrtou16(buf, 10, &ppm);
+

This blank line is also then unnecessary.

Quoted 14 lines
drivers/iio/chemical/mhz19b.c new file +14 -0
+	if (ret)
+		return ret;
+
+	/* at least 1000ppm */
+	if (ppm) {
+		if (ppm < 1000 || ppm > 5000) {
+			dev_dbg(&indio_dev->dev,
+				"span point ppm should be 1000~5000");
+			return -EINVAL;
+		}
+
+		cmd = MHZ19B_SPAN_POINT_CMD;
+	} else {
+		cmd = MHZ19B_ZERO_POINT_CMD;

drivers/iio/chemical/mhz19b.c line 223

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+	}
+
+	ret = mhz19b_serdev_cmd(indio_dev, cmd, &ppm);
+	if (ret < 0)
+		return ret;
+
+	return len;
+}
+static IIO_CONST_ATTR(calibration_forced_value_available,
+	"0 1000-5000");

Check the syntax for available attributes. We don't have a good way to describe this particular set of ranges. So best option may unfortunately be to not describe it at all.
Anyone calibrating this device is going to be reading the datasheet anyway so should know what is possible.

drivers/iio/chemical/mhz19b.c line 226

Quoted 3 lines
drivers/iio/chemical/mhz19b.c new file +3 -0
+static IIO_DEVICE_ATTR_WO(calibration_forced_value, 0);
+
+/* MH-Z19B supports a measurement range adjustment feature.

/*
* MH-Z...

Quoted 3 lines

+ * It can measure up to 2000 ppm or up to 5000 ppm.
+ */
+static ssize_t co2_range_store(struct device *dev,

As per comments on ABI docs patch, I don't think we need custom ABI for this.

drivers/iio/chemical/mhz19b.c

Quoted 2 lines
drivers/iio/chemical/mhz19b.c new file +2 -0
+	struct device_attribute *attr,
+	const char *buf, size_t len)

Except on very long lines, align parameters with just after ( rather.

Quoted 5 lines
drivers/iio/chemical/mhz19b.c new file +5 -0
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	int ret;
+	uint16_t range;
+

drivers/iio/chemical/mhz19b.c line 246

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+	ret = kstrtou16(buf, 10, &range);
+	if (ret)
+		return ret;
+
+	/* Detection Range should be 2000 or 5000 */
+	if (!(range == 2000 || range == 5000)) {
+		dev_dbg(&indio_dev->dev, "detection range should be 2000 or 5000");
+		return -EINVAL;
+	}
+

No need for more than one blank line.

Quoted 17 lines
drivers/iio/chemical/mhz19b.c new file +17 -0
+
+	ret = mhz19b_serdev_cmd(indio_dev, MHZ19B_DETECTION_RANGE_CMD, &range);
+	if (ret < 0)
+		return ret;
+
+	return len;
+}
+static IIO_CONST_ATTR(co2_range_available,
+	"2000 5000");
+static IIO_DEVICE_ATTR_WO(co2_range, 0);
+
+static struct attribute *mhz19b_attrs[] = {
+	&iio_const_attr_calibration_forced_value_available.dev_attr.attr,
+	&iio_const_attr_co2_range_available.dev_attr.attr,
+	&iio_dev_attr_calibration_auto_enable.dev_attr.attr,
+	&iio_dev_attr_calibration_forced_value.dev_attr.attr,
+	&iio_dev_attr_co2_range.dev_attr.attr,

drivers/iio/chemical/mhz19b.c line 281

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +9 -0
+	NULL
+};
 
+
+static const struct iio_chan_spec mhz19b_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),

I'm curious. We have a range control in your custom ABI, but no scale. So what units is this in?

drivers/iio/chemical/mhz19b.c line 302

Quoted 6 lines
drivers/iio/chemical/mhz19b.c new file +5 -0
+	},
+};
 
+
+/* The 'serdev_device_write' function returns -EINVAL if the 'write_wakeup' member is NULL,
+ * so it must be mandatory.

Answering that in review was fine. No need for comment (which is formatted wrong for IIO that uses /*
* The ...

drivers/iio/chemical/mhz19b.c line 306

Quoted 4 lines

+ */

drivers/iio/chemical/mhz19b.c new file +3 -0
+static void mhz19b_write_wakeup(struct serdev_device *serdev)
+{
+	struct iio_dev *indio_dev = dev_get_drvdata(&serdev->dev);

There was still an open question on that where I asked
if a standard existing callback was the correct minimal thing to do.

Quoted 17 lines
drivers/iio/chemical/mhz19b.c new file +17 -0
+
+	dev_dbg(&indio_dev->dev, "mhz19b_write_wakeup");
+}
+
+static const struct serdev_device_ops mhz19b_ops = {
+	.receive_buf = mhz19b_receive_buf,
+	.write_wakeup = mhz19b_write_wakeup,
+};
+
+static int mhz19b_probe(struct serdev_device *serdev)
+{
+	int ret;
+	struct device *dev = &serdev->dev;
+	struct iio_dev *indio_dev;
+	struct mhz19b_state *st;
+
+	serdev_device_set_client_ops(serdev, &mhz19b_ops);

drivers/iio/chemical/mhz19b.c line 333

Quoted 10 lines
drivers/iio/chemical/mhz19b.c new file +10 -0
+
+	ret = devm_serdev_device_open(dev, serdev);
+	if (ret)
+		return ret;
+
+	ret = serdev_device_set_baudrate(serdev, 9600);
+	if (ret < 0)
+		return ret;
+
+	/* void type func, no return */

No need for comment then! I was wrong in previous review.

drivers/iio/chemical/mhz19b.c line 341

Quoted 8 lines
drivers/iio/chemical/mhz19b.c new file +8 -0
+	serdev_device_set_flow_control(serdev, false);
+
+	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
+	if (ret < 0)
+		return ret;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(struct mhz19b_state));
+	if (indio_dev == NULL)

For simple pointer validity checks.

if (!indio_dev) is probably more common than checking if NUL.

drivers/iio/chemical/mhz19b.c line 354

Quoted 13 lines
drivers/iio/chemical/mhz19b.c new file +13 -0
+		return ret;
+	dev_set_drvdata(dev, indio_dev);
+
+	st = iio_priv(indio_dev);
+	st->serdev = serdev;
+
+	init_completion(&st->buf_ready);
+	ret = devm_mutex_init(dev, &st->lock);
+	if (ret)
+		return ret;
+
+	/* TO DO:
+	 *  - vin supply

The code is very simple to just turn on the power supply
at driver load and off at unload. Simpler to do that from the start than end up with it being added later.

drivers/iio/chemical/mhz19b.c line 361

Quoted 7 lines
drivers/iio/chemical/mhz19b.c new file +7 -0
+	 */
+
+	indio_dev->name = "mh-z19b";
+	indio_dev->channels = mhz19b_channels;
+	indio_dev->num_channels = ARRAY_SIZE(mhz19b_channels);
+	indio_dev->info = &mhz19b_info;
+	ret = devm_iio_device_register(dev, indio_dev);

return devm_iio_device_register();

Quoted 7 lines
drivers/iio/chemical/mhz19b.c new file +5 -0
+	if (ret)
+		return ret;
+
+	return 0;
+}
author maintainer
#21 lore raw

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

Hello Jonathan, thank you for the review.

Quoted 4 lines
Quoted 4 lines
drivers/iio/chemical/mhz19b.c new file +4 -0
+      * When data is received from the MH-Z19B,
+      * the 'mhz19b_receive_buf' callback function is called and fills this buffer.
+      */
+     char buf[9];
Quoted 8 lines

drivers/iio/chemical/mhz19b.c line 44

Quoted 6 lines
drivers/iio/chemical/mhz19b.c new file +6 -0
+     int buf_idx;
+
+     /* must wait the 'buf' is filled with 9 bytes.*/
+     struct completion buf_ready;
+
+     /* protect access to mhz19b_state */

Be more specific. What and why? It's not protecting the regulator.

I added it at first since the buffer is shared, but there shouldn’t be any locking issues in this case. I’ll drop it.

Quoted 6 lines
Quoted 3 lines
(quoted diff) +2 -0
+     struct mutex lock;
+};
Quoted 8 lines

drivers/iio/chemical/mhz19b.c line 223

Quoted 2 lines
drivers/iio/chemical/mhz19b.c new file +2 -0
+static IIO_CONST_ATTR(calibration_forced_value_available,
+     "0 1000-5000");

Check the syntax for available attributes. We don't have a good way to describe this particular set of ranges. So best option may unfortunately be to not describe it at all.
Anyone calibrating this device is going to be reading the datasheet anyway so should know what is possible.

Yes, it seems better to keep this as a comment only.

Quoted 14 lines
Quoted 13 lines
drivers/iio/chemical/mhz19b.c new file +13 -0
+}
+static IIO_CONST_ATTR(co2_range_available,
+     "2000 5000");
+static IIO_DEVICE_ATTR_WO(co2_range, 0);
+
+static struct attribute *mhz19b_attrs[] = {
+     &iio_const_attr_calibration_forced_value_available.dev_attr.attr,
+     &iio_const_attr_co2_range_available.dev_attr.attr,
+     &iio_dev_attr_calibration_auto_enable.dev_attr.attr,
+     &iio_dev_attr_calibration_forced_value.dev_attr.attr,
+     &iio_dev_attr_co2_range.dev_attr.attr,
+     NULL
+};
Quoted 11 lines

drivers/iio/chemical/mhz19b.c line 281

Quoted 7 lines
drivers/iio/chemical/mhz19b.c new file +7 -0
+
+static const struct iio_chan_spec mhz19b_channels[] = {
+     {
+             .type = IIO_CONCENTRATION,
+             .channel2 = IIO_MOD_CO2,
+             .modified = 1,
+             .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),

I'm curious. We have a range control in your custom ABI, but no scale. So what units is this in?

The unit is ppm. and as far as I understand, co2 input(processed) unit
is percentage concentration.
so In this device, the scale is a constant value (0.0001), regardless of the range.
Is my understanding correct?

Quoted 8 lines

drivers/iio/chemical/mhz19b.c line 306

Quoted 4 lines

+ */

drivers/iio/chemical/mhz19b.c new file +3 -0
+static void mhz19b_write_wakeup(struct serdev_device *serdev)
+{
+     struct iio_dev *indio_dev = dev_get_drvdata(&serdev->dev);

There was still an open question on that where I asked
if a standard existing callback was the correct minimal thing to do.

ah, serdev_device_write_wakeup() fits perfectly in this case. I miss that, sorry.

Quoted 4 lines
Quoted 4 lines
drivers/iio/chemical/mhz19b.c new file +4 -0
+
+     init_completion(&st->buf_ready);
+     ret = devm_mutex_init(dev, &st->lock);
+     if (ret)
Quoted 8 lines

drivers/iio/chemical/mhz19b.c line 354

Quoted 4 lines
drivers/iio/chemical/mhz19b.c new file +4 -0
+             return ret;
+
+     /* TO DO:
+      *  - vin supply

The code is very simple to just turn on the power supply
at driver load and off at unload. Simpler to do that from the start than end up with it being added later.

Yes, I agree with you. I will add some code for it.

Thanks,
Gyeyoung

maintainer
#22 lore raw

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

Quoted 11 lines
Quoted 11 lines

drivers/iio/chemical/mhz19b.c line 281

Quoted 7 lines
drivers/iio/chemical/mhz19b.c new file +7 -0
+
+static const struct iio_chan_spec mhz19b_channels[] = {
+     {
+             .type = IIO_CONCENTRATION,
+             .channel2 = IIO_MOD_CO2,
+             .modified = 1,
+             .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),  

I'm curious. We have a range control in your custom ABI, but no scale. So what units is this in?

Quoted 5 lines

The unit is ppm. and as far as I understand, co2 input(processed) unit
is percentage concentration.
so In this device, the scale is a constant value (0.0001), regardless of the range.
Is my understanding correct?

Yes.

author maintainer
#23 lore raw

PATCH 5/5 MAINTAINERS: Add WINSEN MHZ19B

In reply to Gyeyoung Baek

Add undersigned as a maintainer for the WINSEN MHZ19B.

---
 MAINTAINERS | 7 +++++++
 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
index 98e9e318178b..c78c758cd069 100644
@@ -25615,6 +25615,13 @@ 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/ABI/testing/sysfs-bus-iio-chemical-mhz19b
+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

--
2.34.1