Skip to contentSkip to conversation

iio: chemical: Add driver and bindings for acd10

PATCH In review Akhilesh Patil 3 participants 5 messages Dec 2 – Dec 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover iio: chemical: Add driver and bindings for acd10

This patch series adds support for Aosong ASAIR acd10 CO2 (Carbon dioxide) sensor driver in IIO subsystem.

Patch 1: Adds bindings for this hardware
Patch 2: Adds driver code for acd10 under drivers/iio/chemical

Why new driver under iio/chemical ?
I have checked existing chemical drivers from iio/chemical.
Many of the drivers are for other vendors and are I2C based with different register layout. They do not match with serial interface of acd10 co2 sensor. ags02ma is the same vendor, but it is an I2C based with a different protocol. There are few drivers with serial interface - scd30, pms7003, sps30, and mhz19b. however their framing, command positions, padding, checksum calculation, supported commands, reply etc, all differ from acd10. Given the above differences from existing drivers, it is my judgement to add a new driver for it.

Quick overview of acd10:
- This is CO2 sensor based on NDIR technology.
- Can measure CO2 concentration up to 5000ppm
- Supports serial interface over UART with 1200 baud rate.

Datasheet: https://www.es.co.th/Schemetic/PDF/ACD10.PDF

Protocol:
acd10 defines 4 types of UART frames - read co2, get software version, get instrument number and set single point calibration.
Frame format: fixed header, length of data, command code, Actual data, checksum. Response from the sensor follows same format.
Total length of each tx and rx frame for 4 commands is fixed and defined in the datasheet. Driver uses this information to write and read from sensor for each command over serdev interface.

Testing:
Driver is tested on Texas Instruments am62x sk board with sensor connected on UART5 on 40pin IO expander. Additional 3.3 to 5v bidirectional logic translator is used to interface with 3.3v IOs of am62x. Values are read using iio sysfs interface. Also I have blown into the sensor to observe sensor output increase due to CO2 in my exhale. Waveforms for read co2, get sw rev and get ins num commands are validated on logic analyzer.

Slight deviation from datasheet:
For command get software version, datasheet mentions frame length of 16 bytes including D11 reserved, however it is observed that sensor sends 15 bytes with length field 0xb (11 bytes of data), but actual data is 10 bytes. Hence, this is handled in the driver.

Scope of this series:
Driver supports reading co2, getting sw version and instrument number(serialnumber). As sensor serial protocol is fairly simple, I have intentionally kept receiver implementation simple. Single point calibration and preheat implementation is left for future contributions and are maked as TODO items in the driver file.

Changes in v2:
- Fixed possibility of buffer overflow in acd10_receive_buf() by doing bound checks.
- Added regulator support in driver.
- Added missing MODULE_DEVICE_TABLE() for loadable kernel modules.
- Dropped device.h from include files.
- Initialized all enum acd10_cmds members explicitly.
- Used __must_hold markings for acd10_serdev_transfer() mutex usage.
- Protected functions acd10_get_sw_rev() and acd10_get_instrument_num() with mutex lock.
- Defined and used ACD10_FRAME_START_IDX and ACD10_FRAME_FIXED_CODE_IDX wherever applicable.
- Removed obvious comments from acd10_serdev_transfer().
- Used dev_dbg() instead of dev_info() wherever applicable to avoid more bloated kernel logs.
- Mapped instrument number to standard IIO ABI serialnumber and added sysfs attribute code to export it.
- Used guard() instead of scoped_guard() inside acd10_read_raw()
- Arranged _probe function's local variables as per reverse xmas tree convention.
- Used sizeof(*drv_state) instead of sizeof(struct acd10_state) inside _probe function.
- Used dev_err_probe() to handle err during probe wherever applicable.
- Clean up of devm_iio_device_register() call from _probe function.
- Added section "Why new driver" in cover letter of the patch series.
- Link to v1: https://lore.kernel.org/linux-iio/[email protected]/

Akhilesh Patil (2):
dt-bindings: iio: chemical: Add aosong acd10
iio: chemical: acd10: Add driver for ACD10 CO2 sensor

 .../bindings/iio/chemical/aosong,acd10.yaml   |  34 ++
 MAINTAINERS                                   |   7 +
 drivers/iio/chemical/Kconfig                  |  10 +
 drivers/iio/chemical/Makefile                 |   3 +
 drivers/iio/chemical/acd10.c                  | 389 ++++++++++++++++++
 5 files changed, 443 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
 create mode 100644 drivers/iio/chemical/acd10.c
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/2 dt-bindings: iio: chemical: Add aosong acd10

Add bindings for acd10 CO2 sensor. This sensor has UART serial interface with 1200 baudrate and can measure CO2 concentrations in the range of 400 to 5000 ppm.

---
 .../bindings/iio/chemical/aosong,acd10.yaml   | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml new file +34 -0
new file mode 100644
index 000000000000..bd6bad01550f
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/chemical/aosong,acd10.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: aosong acd10 carbon dioxide sensor
+
+maintainers:
+  - Akhilesh Patil <[email protected]>
+
+description:
+  aosong acd10 is NDIR CO2 sensor with measurement range of 400 to 5000 ppm.
+  It provides serial interface over UART communication with fixed baud rate of 1200.
+
+properties:
+  compatible:
+    const: aosong,acd10
+
+  vcc-supply: true
+
+required:
+  - compatible
+
+additionalProperties: false
+
+examples:
+  - |
+    serial {
+        co2-sensor {
+            compatible = "aosong,acd10";
+        };
+    };
+...
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/2 iio: chemical: acd10: Add driver for ACD10 CO2 sensor

In reply to Akhilesh Patil

Add driver for Aosong ASAIR ACD10 carbon dioxide sensor. Use serdev interface to communicate with sensor over UART. Implement read co2, get software revision, get instrument number commands.

Tested on TI am62x sk board on UART5 with 1200 baud rate.

---
 MAINTAINERS                   |   7 +
 drivers/iio/chemical/Kconfig  |  10 +
 drivers/iio/chemical/Makefile |   3 +
 drivers/iio/chemical/acd10.c  | 389 ++++++++++++++++++++++++++++++++++
 4 files changed, 409 insertions(+)
 create mode 100644 drivers/iio/chemical/acd10.c
MAINTAINERS +7 -0
index e8f06145fb54..a5d70cd11dda 100644
@@ -3743,6 +3743,13 @@ S:	Supported
 W:	http://www.akm.com/
 F:	drivers/iio/magnetometer/ak8974.c
 
+AOSONG ACD10 CARBON DIOXIDE SENSOR DRIVER
+M:	Akhilesh Patil <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
+F:	drivers/iio/chemical/acd10.c
+
 AOSONG AGS02MA TVOC SENSOR DRIVER
 M:	Anshul Dalal <[email protected]>
 L:	[email protected]
drivers/iio/chemical/Kconfig +10 -0
index b22afa1f6d59..2bbf49135997 100644
@@ -5,6 +5,16 @@
 
 menu "Chemical Sensors"
 
+config ACD10
+	tristate "Aosong ACD10 CO2 gas sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build Aosong ASAIR ACD10 CO2 (carbon dioxide)
+	  sensor driver.
+
+	  To compile this ACD10 driver as a loadable kernel module,
+	  choose M here: the module will be called acd10
+
 config AOSONG_AGS02MA
 	tristate "Aosong AGS02MA TVOC sensor driver"
 	depends on I2C
drivers/iio/chemical/Makefile +3 -0
index 2287a00a6b75..2187f596728c 100644
@@ -3,7 +3,10 @@
 # Makefile for IIO chemical sensors
 #
 
+CONTEXT_ANALYSIS_acd10.o := y
+
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ACD10)	+= acd10.o
 obj-$(CONFIG_AOSONG_AGS02MA)	+= ags02ma.o
 obj-$(CONFIG_ATLAS_PH_SENSOR)	+= atlas-sensor.o
 obj-$(CONFIG_ATLAS_EZO_SENSOR)	+= atlas-ezo-sensor.o
drivers/iio/chemical/acd10.c new file +389 -0

389 lines not on this page · raw message

Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] iio: chemical: acd10: shared TX/RX buffer race in acd10_serdev_transfer() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] iio: chemical: acd10: shared TX/RX buffer race in acd10_serdev_transfer()
--

commit 0d5d850b993452ce98f539b196f7b69e4ad1b6e8
Author: Akhilesh Patil <[email protected]>

iio: chemical: acd10: Add driver for ACD10 CO2 sensor

This commit adds an IIO driver for the Aosong ASAIR ACD10 CO2 sensor using the serdev UART interface.

Quoted 5 lines
drivers/iio/chemical/acd10.c new file +0 -0
new file mode 100644
index 0000000000000..a77dde636ea74

[ ... ]

Quoted 32 lines
drivers/iio/chemical/acd10.c new file +32 -0
+static int acd10_serdev_transfer(struct acd10_state *drv_state, enum acd10_cmds cmd)
+				 __must_hold(&drv_state->lock)
+{
+	struct serdev_device *serdev = drv_state->serdev;
+	struct device *dev = &serdev->dev;
+	int ret;
+
+	/* Prepare transmit buffer along with checksum and send over serial */
+	drv_state->buf[ACD10_FRAME_START_IDX] = ACD10_FRAME_START;
+	drv_state->buf[ACD10_FRAME_FIXED_CODE_IDX] = ACD10_FRAME_FIXED_CODE;
+	drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] = cmd_info[cmd].tx_data_len;
+	drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] = cmd_info[cmd].cmd_code;
+	drv_state->buf[cmd_info[cmd].tx_frame_len - 1] =
+		acd10_get_checksum(drv_state->buf, cmd_info[cmd].tx_frame_len);
+	drv_state->buf_idx = 0;
+	drv_state->rx_expected_len = cmd_info[cmd].rx_frame_len;
+	reinit_completion(&drv_state->buf_ready);
+
+	ret = serdev_device_write(serdev, drv_state->buf, cmd_info[cmd].tx_frame_len,
+				  ACD10_SERDEV_TIMEOUT);
+	if (ret < 0)
+		return ret;
+	if (ret != cmd_info[cmd].tx_frame_len)
+		return -EIO;
+
+	/* Wait till we receive expected number of bytes for sent cmd */
+	ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
+							ACD10_SERDEV_TIMEOUT);
+	if (ret < 0)
+		return ret;
+	if (!ret)
+		return -ETIMEDOUT;

[ ... ]

Quoted 12 lines
drivers/iio/chemical/acd10.c new file +12 -0
+static size_t acd10_receive_buf(struct serdev_device *serdev,
+				const u8 *data, size_t len)
+{
+	struct iio_dev *indio_dev = serdev_device_get_drvdata(serdev);
+	struct acd10_state *drv_state = iio_priv(indio_dev);
+	size_t consume = len;
+	int i = 0;
+
+	while (drv_state->rx_expected_len && consume > 0 &&
+	       drv_state->buf_idx < sizeof(drv_state->buf)) {
+		drv_state->buf[drv_state->buf_idx++] = data[i++];
+		consume -= 1;

drivers/iio/chemical/acd10.c line 241

Quoted 10 lines
drivers/iio/chemical/acd10.c new file +10 -0
+		drv_state->rx_expected_len -= 1;
+	}
+
+	if (drv_state->rx_expected_len == 0) {
+		drv_state->buf_idx = 0;
+		complete(&drv_state->buf_ready);
+	}
+
+	return len;
+}

[Severity: Medium]
Can an interrupted or timed-out transfer leave drv_state->rx_expected_len non-zero and allow acd10_receive_buf() to race on the shared drv_state->buf during the next transfer?

At 1200 baud, a 9-byte response takes around 75 ms to arrive. If wait_for_completion_interruptible_timeout() is interrupted by a signal (or times out) while the response is in flight, acd10_serdev_transfer() returns without resetting drv_state->rx_expected_len:

CPU0 kworker (tty flip buffer)
acd10_serdev_transfer()
rx_expected_len = 9
serdev_device_write(...)
wait_for_completion...()

    -> -ERESTARTSYS

acd10_serdev_transfer()
buf[0] = ACD10_FRAME_START
...

                                  acd10_receive_buf()
                                    buf[buf_idx++] = data[i++]

buf[4] = acd10_get_checksum(buf)
serdev_device_write(buf) <-- sends corrupted TX frame

Because acd10_receive_buf() does not take drv_state->lock and shares drv_state->buf with the transmit path, late bytes from the previous response overwrite the outgoing frame or shift the receive buffer of subsequent transfers out of alignment.

In addition, in acd10_receive_buf(), if an unexpected byte arrives when drv_state->rx_expected_len is already 0, the while loop does not run, but the if (drv_state->rx_expected_len == 0) check still calls complete(&drv_state->buf_ready). And in acd10_serdev_transfer(), drv_state->rx_expected_len is armed before reinit_completion() and serdev_device_write(), so a completion that fires before reinit_completion() is lost.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH v2 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor

In reply to Akhilesh Patil

Hello,

Quoted 91 lines · Akhilesh Patil

Add driver for Aosong ASAIR ACD10 carbon dioxide sensor. Use serdev interface to communicate with sensor over UART. Implement read co2, get software revision, get instrument number commands.

Tested on TI am62x sk board on UART5 with 1200 baud rate.

---
 MAINTAINERS                   |   7 +
 drivers/iio/chemical/Kconfig  |  10 +
 drivers/iio/chemical/Makefile |   3 +
 drivers/iio/chemical/acd10.c  | 389 ++++++++++++++++++++++++++++++++++
 4 files changed, 409 insertions(+)
 create mode 100644 drivers/iio/chemical/acd10.c
MAINTAINERS +7 -0
index e8f06145fb54..a5d70cd11dda 100644
@@ -3743,6 +3743,13 @@ S:	Supported
 W:	http://www.akm.com/
 F:	drivers/iio/magnetometer/ak8974.c
 
+AOSONG ACD10 CARBON DIOXIDE SENSOR DRIVER
+M:	Akhilesh Patil <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
+F:	drivers/iio/chemical/acd10.c
+
 AOSONG AGS02MA TVOC SENSOR DRIVER
 M:	Anshul Dalal <[email protected]>
 L:	[email protected]
drivers/iio/chemical/Kconfig +10 -0
index b22afa1f6d59..2bbf49135997 100644
@@ -5,6 +5,16 @@
 
 menu "Chemical Sensors"
 
+config ACD10
+	tristate "Aosong ACD10 CO2 gas sensor"
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here to build Aosong ASAIR ACD10 CO2 (carbon dioxide)
+	  sensor driver.
+
+	  To compile this ACD10 driver as a loadable kernel module,
+	  choose M here: the module will be called acd10
+
 config AOSONG_AGS02MA
 	tristate "Aosong AGS02MA TVOC sensor driver"
 	depends on I2C
drivers/iio/chemical/Makefile +3 -0
index 2287a00a6b75..2187f596728c 100644
@@ -3,7 +3,10 @@
 # Makefile for IIO chemical sensors
 #
 
+CONTEXT_ANALYSIS_acd10.o := y
+
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_ACD10)	+= acd10.o
 obj-$(CONFIG_AOSONG_AGS02MA)	+= ags02ma.o
 obj-$(CONFIG_ATLAS_PH_SENSOR)	+= atlas-sensor.o
 obj-$(CONFIG_ATLAS_EZO_SENSOR)	+= atlas-ezo-sensor.o
drivers/iio/chemical/acd10.c new file +15 -0
new file mode 100644
index 000000000000..a77dde636ea7
@@ -0,0 +1,389 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ACD10 CO2 gas sensor driver
+ *
+ * Copyright (c) 2026 Akhilesh Patil <[email protected]>
+ *
+ * Datasheet:
+ * https://www.es.co.th/Schemetic/PDF/ACD10.PDF
+ *
+ * TODO additional features:
+ *	1) single-point calibration feature.
+ *	2) Sensor preheat wait feature.
+ */
+
+#include <linux/array_size.h>

drivers/iio/chemical/acd10.c line 25

Quoted 10 lines
drivers/iio/chemical/acd10.c new file +10 -0
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/completion.h>
+#include <linux/dev_printk.h>
+#include <linux/errno.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/types.h>
+#include <linux/jiffies.h>
+#include <linux/mod_devicetable.h>

In his review of v1
(https://lore.kernel.org/linux-iio/20261122192823.6a3430e6@jic23-hlaptop/)
Jonathan wrote about <linux/mod_devicetable.h>:

Not needed any more unless something is wrong in the serdev header. These are now assumed to always come from the bus header.

With "These" refering to *_device_id
I confirm that <linux/serdev.h> indeed provides of_device_id (which is the only one used in your driver). As I'm working on removing that header, please drop it.

drivers/iio/chemical/acd10.c line 376

Quoted 5 lines
drivers/iio/chemical/acd10.c new file +5 -0
+static const struct of_device_id acd10_of_match_table[] = {
+	{ .compatible = "aosong,acd10", },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, acd10_of_match_table);

Please also drop the , just before the closing }.

Best regards
Uwe