mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] iio: chemical: Add driver and bindings for acd10
@ 2026-11-22  6:30 Akhilesh Patil
  2026-11-22  6:31 ` [PATCH 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Akhilesh Patil @ 2026-11-22  6:30 UTC (permalink / raw)
  To: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
	joshua.crofts1, maxwell, u.kleine-koenig
  Cc: linux-iio, devicetree, linux-kernel, akhileshpatilvnit, akhilesh

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

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.

Driver supports reading co2, getting sw version and instrument number.
Single point calibration and preheat implementation is left for
future contributions.

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                 |   1 +
 drivers/iio/chemical/acd10.c                  | 365 ++++++++++++++++++
 5 files changed, 417 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
 create mode 100644 drivers/iio/chemical/acd10.c

-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] dt-bindings: iio: chemical: Add aosong acd10
  2026-11-22  6:30 [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
@ 2026-11-22  6:31 ` Akhilesh Patil
  2026-11-22  6:32 ` [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
  2026-11-23  9:28 ` [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Andy Shevchenko
  2 siblings, 0 replies; 7+ messages in thread
From: Akhilesh Patil @ 2026-11-22  6:31 UTC (permalink / raw)
  To: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
	joshua.crofts1, maxwell, u.kleine-koenig
  Cc: linux-iio, devicetree, linux-kernel, akhileshpatilvnit, akhilesh

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.

Signed-off-by: Akhilesh Patil <akhilesh.patil@linux.dev>
---
 .../bindings/iio/chemical/aosong,acd10.yaml   | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml

diff --git a/Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml b/Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
new file mode 100644
index 000000000000..bd6bad01550f
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/chemical/aosong,acd10.yaml
@@ -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 <akhilesh.patil@linux.dev>
+
+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";
+        };
+    };
+...
-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor
  2026-11-22  6:30 [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
  2026-11-22  6:31 ` [PATCH 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
@ 2026-11-22  6:32 ` Akhilesh Patil
  2026-11-22 19:31   ` Jonathan Cameron
  2026-11-23  9:28 ` [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Andy Shevchenko
  2 siblings, 1 reply; 7+ messages in thread
From: Akhilesh Patil @ 2026-11-22  6:32 UTC (permalink / raw)
  To: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
	joshua.crofts1, maxwell, u.kleine-koenig
  Cc: linux-iio, devicetree, linux-kernel, akhileshpatilvnit, akhilesh

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.

Signed-off-by: Akhilesh Patil <akhilesh.patil@linux.dev>
---
 MAINTAINERS                   |   7 +
 drivers/iio/chemical/Kconfig  |  10 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/acd10.c  | 365 ++++++++++++++++++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 drivers/iio/chemical/acd10.c

diff --git a/MAINTAINERS b/MAINTAINERS
index e8f06145fb54..a5d70cd11dda 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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 <akhilesh.patil@linux.dev>
+L:	linux-iio@vger.kernel.org
+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 <anshulusr@gmail.com>
 L:	linux-iio@vger.kernel.org
diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig
index b22afa1f6d59..2bbf49135997 100644
--- a/drivers/iio/chemical/Kconfig
+++ b/drivers/iio/chemical/Kconfig
@@ -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
diff --git a/drivers/iio/chemical/Makefile b/drivers/iio/chemical/Makefile
index 2287a00a6b75..29937988cbfe 100644
--- a/drivers/iio/chemical/Makefile
+++ b/drivers/iio/chemical/Makefile
@@ -4,6 +4,7 @@
 #
 
 # 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
diff --git a/drivers/iio/chemical/acd10.c b/drivers/iio/chemical/acd10.c
new file mode 100644
index 000000000000..b92f01e7bf4a
--- /dev/null
+++ b/drivers/iio/chemical/acd10.c
@@ -0,0 +1,365 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ACD10 CO2 gas sensor driver
+ *
+ * Copyright (c) 2026 Akhilesh Patil <akhilesh.patil@linux.dev>
+ *
+ * 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>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/completion.h>
+#include <linux/dev_printk.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/types.h>
+#include <linux/jiffies.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/serdev.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+
+#define ACD10_SERIAL_BAUDRATE		1200
+#define ACD10_SERDEV_TIMEOUT		msecs_to_jiffies(500)
+#define ACD10_FRAME_START		0xFE
+#define ACD10_FRAME_FIXED_CODE		0xA6
+#define ACD10_FRAME_DATA_START		4U
+#define ACD10_FRAME_DATA_LEN_IDX	2U
+#define ACD10_FRAME_DATA_LEN_MAX	11U
+#define ACD10_FRAME_CMD_CODE_IDX	3U
+
+struct acd10_state {
+	struct serdev_device *serdev;
+
+	/* Lock to acquire during CMD/RESP sequence */
+	struct mutex lock;
+	struct completion buf_ready;
+
+	/* Single buffer to hold tx and rx data */
+	u8 buf[16];
+	u8 buf_idx;
+	u8 rx_expected_len;
+
+	u8 sw_rev[16];
+	u8 inst_num[16];
+};
+
+enum acd10_cmds {
+	ACD10_CMD_READ_CO2 = 0,
+	ACD10_CMD_CAL,
+	ACD10_CMD_READ_SWREV,
+	ACD10_CMD_READ_INS_NUM,
+};
+
+struct acd10_cmd_info {
+	u8 cmd_code;
+	u8 tx_frame_len;
+	u8 tx_data_len;
+	u8 rx_frame_len;
+};
+
+static const struct acd10_cmd_info cmd_info[] = {
+	[ACD10_CMD_READ_CO2] = {
+		.cmd_code = 0x01,
+		.tx_frame_len = 5,
+		.tx_data_len = 0,
+		.rx_frame_len = 9,
+	},
+
+	[ACD10_CMD_CAL] = {
+		.cmd_code = 0x03,
+		.tx_frame_len = 7,
+		.tx_data_len = 2,
+		.rx_frame_len = 5,
+	},
+
+	[ACD10_CMD_READ_SWREV] = {
+		.cmd_code = 0x1E,
+		.tx_frame_len = 5,
+		.tx_data_len = 0,
+		/*
+		 * Note: datasheet says sw rev response is D1-D11 with D11 reserved.
+		 * However, sensor actually sends only D1-D10 (10 bytes).
+		 * Hence, rx_frame_len is 15 and not 16.
+		 */
+		.rx_frame_len = 15,
+	},
+
+	[ACD10_CMD_READ_INS_NUM] = {
+		.cmd_code = 0x1F,
+		.tx_frame_len = 5,
+		.tx_data_len = 0,
+		.rx_frame_len = 15,
+	}
+};
+
+static u8 acd10_get_checksum(u8 *buf, u8 len)
+{
+	u8 checksum = 0;
+
+	for (u8 i = 1; i < len - 1; i += 1)
+		checksum += buf[i];
+
+	return checksum;
+}
+
+/*
+ * Do transfer based on command and fill drv_state->buf with received data.
+ * Caller of this function should use mutex drv_state->lock.
+ */
+static int acd10_serdev_transfer(struct iio_dev *indio_dev, enum acd10_cmds cmd)
+{
+	struct acd10_state *drv_state = iio_priv(indio_dev);
+	struct serdev_device *serdev = drv_state->serdev;
+	struct device *dev = &indio_dev->dev;
+	int ret;
+
+	/* Prepare transmit buffer along with checksum */
+	drv_state->buf[0] = ACD10_FRAME_START;
+	drv_state->buf[1] = 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);
+
+	/* Transmit the buffer over serdev */
+	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;
+
+	/* Read the received bytes in buffer */
+	ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
+							ACD10_SERDEV_TIMEOUT);
+	if (ret < 0)
+		return ret;
+	if (!ret)
+		return -ETIMEDOUT;
+
+	/* validate frame header */
+	if (drv_state->buf[0] != ACD10_FRAME_START ||
+	    drv_state->buf[1] != ACD10_FRAME_FIXED_CODE) {
+		dev_err(dev, "RX frame invalid\n");
+		return -EIO;
+	}
+
+	/* Maximum data len can be max 11 */
+	if (drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] > ACD10_FRAME_DATA_LEN_MAX)
+		return -EIO;
+
+	/* Validate command echo */
+	if (drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] != cmd_info[cmd].cmd_code)
+		return -EIO;
+
+	/* Validate checksum */
+	if (drv_state->buf[cmd_info[cmd].rx_frame_len - 1] !=
+	    acd10_get_checksum(drv_state->buf, cmd_info[cmd].rx_frame_len)) {
+		dev_err(dev, "Checksum error\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int acd10_get_sw_rev(struct iio_dev *indio_dev)
+{
+	struct acd10_state *drv_state = iio_priv(indio_dev);
+	struct device *dev = &indio_dev->dev;
+	int ret;
+
+	ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_SWREV);
+	if (ret)
+		return ret;
+
+	memcpy(drv_state->sw_rev, &drv_state->buf[ACD10_FRAME_DATA_START], 10);
+	drv_state->sw_rev[10] = '\0';
+
+	dev_info(dev, "Software revision: %s\n", drv_state->sw_rev);
+
+	return 0;
+}
+
+static int acd10_get_instrument_num(struct iio_dev *indio_dev)
+{
+	struct acd10_state *drv_state = iio_priv(indio_dev);
+	struct device *dev = &indio_dev->dev;
+	int ret;
+
+	ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_INS_NUM);
+	if (ret)
+		return ret;
+
+	memcpy(drv_state->inst_num, &drv_state->buf[ACD10_FRAME_DATA_START], 10);
+	drv_state->inst_num[10] = '\0';
+
+	dev_info(dev, "Instrument number: %s\n", drv_state->inst_num);
+
+	return 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[drv_state->buf_idx++] = data[i++];
+		consume -= 1;
+		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;
+}
+
+static int acd10_read_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan,
+			  int *val, int *val2, long mask)
+{
+	struct acd10_state *drv_state = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+
+		/* Read CO2 sensor value*/
+		scoped_guard(mutex, &drv_state->lock) {
+			ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_CO2);
+			if (ret)
+				return ret;
+
+			*val = get_unaligned_be16(&drv_state->buf[ACD10_FRAME_DATA_START]);
+		}
+
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		*val = 0;
+		*val2 = 100;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info acd10_info = {
+	.read_raw = &acd10_read_raw,
+};
+
+static const struct iio_chan_spec acd10_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE),
+	},
+};
+
+static const struct serdev_device_ops acd10_ser_ops = {
+	.receive_buf = &acd10_receive_buf,
+	.write_wakeup = serdev_device_write_wakeup,
+};
+
+static int acd10_probe(struct serdev_device *serdev)
+{
+	int ret;
+	struct device *dev = &serdev->dev;
+	struct iio_dev *indio_dev;
+	struct acd10_state *drv_state;
+	unsigned int baudrate;
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(struct acd10_state));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	indio_dev->name = "acd10";
+	indio_dev->channels = acd10_channels;
+	indio_dev->num_channels = ARRAY_SIZE(acd10_channels);
+	indio_dev->info = &acd10_info;
+
+	serdev_device_set_drvdata(serdev, indio_dev);
+
+	drv_state = iio_priv(indio_dev);
+	drv_state->serdev = serdev;
+
+	init_completion(&drv_state->buf_ready);
+	ret = devm_mutex_init(dev, &drv_state->lock);
+	if (ret)
+		return ret;
+
+	serdev_device_set_client_ops(serdev, &acd10_ser_ops);
+	ret = devm_serdev_device_open(dev, serdev);
+	if (ret) {
+		dev_err(dev, "ACD10: serial device open failed\n");
+		return ret;
+	}
+
+	baudrate = serdev_device_set_baudrate(serdev, ACD10_SERIAL_BAUDRATE);
+	dev_info(dev, "baud rate set to: %u\n", baudrate);
+	if (baudrate != ACD10_SERIAL_BAUDRATE) {
+		dev_err(dev, "baud rate expected: %u, got: %u\n",
+			ACD10_SERIAL_BAUDRATE, baudrate);
+		return -EIO;
+	}
+	serdev_device_set_flow_control(serdev, false);
+	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
+	if (ret)
+		return ret;
+
+	ret = acd10_get_sw_rev(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = acd10_get_instrument_num(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_device_register(dev, indio_dev);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static const struct of_device_id acd10_of_match_table[] = {
+	{ .compatible = "aosong,acd10", },
+	{ }
+};
+
+static struct serdev_device_driver acd10_driver = {
+	.driver = {
+		.name = "acd10",
+		.of_match_table = acd10_of_match_table,
+	},
+	.probe = acd10_probe,
+};
+module_serdev_device_driver(acd10_driver);
+
+MODULE_AUTHOR("Akhilesh Patil");
+MODULE_DESCRIPTION("ACD10 CO2 gas sensor driver over serdev interface");
+MODULE_LICENSE("GPL");
-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor
  2026-11-22  6:32 ` [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
@ 2026-11-22 19:31   ` Jonathan Cameron
  2026-12-02 12:17     ` Akhilesh Patil
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Cameron @ 2026-11-22 19:31 UTC (permalink / raw)
  To: Akhilesh Patil
  Cc: andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt, joshua.crofts1,
	maxwell, u.kleine-koenig, linux-iio, devicetree, linux-kernel,
	akhileshpatilvnit, akhilesh

On Sun, 22 Nov 2026 06:32:46 +0000
Akhilesh Patil <akhilesh.patil@linux.dev> wrote:

> 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.
> 
> Signed-off-by: Akhilesh Patil <akhilesh.patil@linux.dev>

A few things from sashiko. I didn't look at whether they were correct or not
https://sashiko.dev/#/patchset/cover.1795326744.git.akhilesh.patil%40linux.dev

Some other comments below.  Given this is a new driver, give it about a week
before sending v2 to allow others time to review.

Thanks,

Jonathan

> diff --git a/drivers/iio/chemical/acd10.c b/drivers/iio/chemical/acd10.c
> new file mode 100644
> index 000000000000..b92f01e7bf4a
> --- /dev/null
> +++ b/drivers/iio/chemical/acd10.c
> @@ -0,0 +1,365 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * ACD10 CO2 gas sensor driver
> + *
> + * Copyright (c) 2026 Akhilesh Patil <akhilesh.patil@linux.dev>
> + *
> + * 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>
> +#include <linux/bits.h>
> +#include <linux/cleanup.h>
> +#include <linux/completion.h>
> +#include <linux/dev_printk.h>
> +#include <linux/device.h>

I haven't checked for things actually in device.h but make
sure this shouldn't be replaced with more specific headers.

> +#include <linux/errno.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/types.h>
> +#include <linux/jiffies.h>
> +#include <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.

> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/serdev.h>
> +#include <linux/string.h>
> +#include <linux/types.h>
> +#include <linux/unaligned.h>

> +
> +enum acd10_cmds {
> +	ACD10_CMD_READ_CO2 = 0,

Where they line up with values on the datasheet, I would always
specify them all. Doesn't matter so much when there are only 4
but for large lists it saves on a lot of counting!

> +	ACD10_CMD_CAL,
> +	ACD10_CMD_READ_SWREV,
> +	ACD10_CMD_READ_INS_NUM,
> +};
> +
> +struct acd10_cmd_info {
> +	u8 cmd_code;
> +	u8 tx_frame_len;
> +	u8 tx_data_len;
> +	u8 rx_frame_len;
> +};


> +
> +/*
> + * Do transfer based on command and fill drv_state->buf with received data.
> + * Caller of this function should use mutex drv_state->lock.

__must_hold() markings can change that documentation to something enforced
but the static analysis tools.

> + */
> +static int acd10_serdev_transfer(struct iio_dev *indio_dev, enum acd10_cmds cmd)
> +{
> +	struct acd10_state *drv_state = iio_priv(indio_dev);
> +	struct serdev_device *serdev = drv_state->serdev;
> +	struct device *dev = &indio_dev->dev;
> +	int ret;
> +
> +	/* Prepare transmit buffer along with checksum */
> +	drv_state->buf[0] = ACD10_FRAME_START;
> +	drv_state->buf[1] = ACD10_FRAME_FIXED_CODE;
> +	drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] = cmd_info[cmd].tx_data_len;

Why do these two get IDX defines whils previous do not?

> +	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);

Are there even any entries between this and the previous?  I would kind of
expect to see some zeroing of those prior to CRC given the code here.
If there aren't what is tx_frame_len for?


> +	drv_state->buf_idx = 0;
> +	drv_state->rx_expected_len = cmd_info[cmd].rx_frame_len;
> +	reinit_completion(&drv_state->buf_ready);
> +
> +	/* Transmit the buffer over serdev */
> +	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;
> +
> +	/* Read the received bytes in buffer */
> +	ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
> +							ACD10_SERDEV_TIMEOUT);
> +	if (ret < 0)
> +		return ret;
> +	if (!ret)
> +		return -ETIMEDOUT;
> +
> +	/* validate frame header */
> +	if (drv_state->buf[0] != ACD10_FRAME_START ||
> +	    drv_state->buf[1] != ACD10_FRAME_FIXED_CODE) {
> +		dev_err(dev, "RX frame invalid\n");
> +		return -EIO;
> +	}
> +
> +	/* Maximum data len can be max 11 */
> +	if (drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] > ACD10_FRAME_DATA_LEN_MAX)
> +		return -EIO;
> +
> +	/* Validate command echo */
> +	if (drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] != cmd_info[cmd].cmd_code)
> +		return -EIO;
> +
> +	/* Validate checksum */

That's fairly obvious from the code.  Keep comment for where they add
value over what can be seen without.  Same applies to at least some of the ones above.

> +	if (drv_state->buf[cmd_info[cmd].rx_frame_len - 1] !=
> +	    acd10_get_checksum(drv_state->buf, cmd_info[cmd].rx_frame_len)) {
> +		dev_err(dev, "Checksum error\n");
> +		return -EIO;
> +	}
> +
> +	return 0;
> +}
> +
> +static int acd10_get_sw_rev(struct iio_dev *indio_dev)
> +{
> +	struct acd10_state *drv_state = iio_priv(indio_dev);
> +	struct device *dev = &indio_dev->dev;
> +	int ret;
> +
> +	ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_SWREV);
> +	if (ret)
> +		return ret;
> +
> +	memcpy(drv_state->sw_rev, &drv_state->buf[ACD10_FRAME_DATA_START], 10);
> +	drv_state->sw_rev[10] = '\0';
> +
> +	dev_info(dev, "Software revision: %s\n", drv_state->sw_rev);

dev_dbg() probably.  

We tend to not print this stuff by default as it leads to ever more bloated
kernel logs.

> +
> +	return 0;
> +}
> +
> +static int acd10_get_instrument_num(struct iio_dev *indio_dev)
> +{
> +	struct acd10_state *drv_state = iio_priv(indio_dev);
> +	struct device *dev = &indio_dev->dev;
> +	int ret;
> +
> +	ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_INS_NUM);
> +	if (ret)
> +		return ret;
> +
> +	memcpy(drv_state->inst_num, &drv_state->buf[ACD10_FRAME_DATA_START], 10);
> +	drv_state->inst_num[10] = '\0';
> +
> +	dev_info(dev, "Instrument number: %s\n", drv_state->inst_num);

Noisy.  I'm not sure what this is. Can it map to serial_number which is
standard ABI?

> +
> +	return 0;
> +}

> +static int acd10_read_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan,
> +			  int *val, int *val2, long mask)
> +{
> +	struct acd10_state *drv_state = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +
> +		/* Read CO2 sensor value*/
> +		scoped_guard(mutex, &drv_state->lock) {
Preference where it is applicable is to use scope definition for case blocks that
need a guard.
	case IIO_CHAN_INFO_RAW: {
		int ret;

		guard(mutex)(&drv_state->lock);

		ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_CO2);
		if (ret)
			return ret;

		*val = get_unaligned_be16(&drv_state->buf[ACD10_FRAME_DATA_START]);

		return IIO_VAL_INT;
	}

Mostly because it remove unnecessary indent.
 
> +			ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_CO2);
> +			if (ret)
> +				return ret;
> +
> +			*val = get_unaligned_be16(&drv_state->buf[ACD10_FRAME_DATA_START]);
> +		}
> +
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		*val = 0;
> +		*val2 = 100;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}

> +
> +static int acd10_probe(struct serdev_device *serdev)
> +{
> +	int ret;

Where ordering isn't required due to dependencies mild preference for
reverse xmas tree.

> +	struct device *dev = &serdev->dev;
> +	struct iio_dev *indio_dev;
> +	struct acd10_state *drv_state;
> +	unsigned int baudrate;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(struct acd10_state));

sizeof(*drv_state)

> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	indio_dev->name = "acd10";
> +	indio_dev->channels = acd10_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(acd10_channels);
> +	indio_dev->info = &acd10_info;
> +
> +	serdev_device_set_drvdata(serdev, indio_dev);
> +
> +	drv_state = iio_priv(indio_dev);
> +	drv_state->serdev = serdev;
> +
> +	init_completion(&drv_state->buf_ready);
> +	ret = devm_mutex_init(dev, &drv_state->lock);
> +	if (ret)
> +		return ret;
> +
> +	serdev_device_set_client_ops(serdev, &acd10_ser_ops);
> +	ret = devm_serdev_device_open(dev, serdev);
> +	if (ret) {
> +		dev_err(dev, "ACD10: serial device open failed\n");
> +		return ret;
		return dev_err_probe(dev, ret, ...

It's both shorter and saves any effort by reviewer to figure
out if deferral is possible on a given call.  Use this for every
error print in probe + any in functions only called from probe.

> +	}
> +
> +	baudrate = serdev_device_set_baudrate(serdev, ACD10_SERIAL_BAUDRATE);
> +	dev_info(dev, "baud rate set to: %u\n", baudrate);

Too noisy for a production driver.  dev_dbg() or drop it entirely.

> +	if (baudrate != ACD10_SERIAL_BAUDRATE) {
> +		dev_err(dev, "baud rate expected: %u, got: %u\n",
> +			ACD10_SERIAL_BAUDRATE, baudrate);
> +		return -EIO;
> +	}
> +	serdev_device_set_flow_control(serdev, false);
> +	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
> +	if (ret)
> +		return ret;
> +
> +	ret = acd10_get_sw_rev(indio_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = acd10_get_instrument_num(indio_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = devm_iio_device_register(dev, indio_dev);
> +	if (ret)
> +		return ret;
> +
> +	return 0;

	return devm_iio_device_register();

> +}



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] iio: chemical: Add driver and bindings for acd10
  2026-11-22  6:30 [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
  2026-11-22  6:31 ` [PATCH 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
  2026-11-22  6:32 ` [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
@ 2026-11-23  9:28 ` Andy Shevchenko
  2026-12-02 12:05   ` Akhilesh Patil
  2 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-23  9:28 UTC (permalink / raw)
  To: Akhilesh Patil
  Cc: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
	joshua.crofts1, maxwell, u.kleine-koenig, linux-iio, devicetree,
	linux-kernel, akhileshpatilvnit, akhilesh

On Sun, Nov 22, 2026 at 06:30:43AM +0000, Akhilesh Patil wrote:
> This patch series adds support for Aosong ASAIR acd10 CO2 (Carbon
> dioxide) sensor driver in IIO subsystem. 

There is no answer to the question "Why a brand new driver?"

> Patch 1: Adds bindings for this hardware
> Patch 2: Adds driver code for acd10 under drivers/iio/chemical
> 
> 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.
> 
> Driver supports reading co2, getting sw version and instrument number.
> Single point calibration and preheat implementation is left for
> future contributions.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] iio: chemical: Add driver and bindings for acd10
  2026-11-23  9:28 ` [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Andy Shevchenko
@ 2026-12-02 12:05   ` Akhilesh Patil
  0 siblings, 0 replies; 7+ messages in thread
From: Akhilesh Patil @ 2026-12-02 12:05 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: jic23, andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt,
	joshua.crofts1, maxwell, u.kleine-koenig, linux-iio, devicetree,
	linux-kernel, akhileshpatilvnit, akhilesh

On Mon, Nov 23, 2026 at 11:28:42AM +0200, Andy Shevchenko wrote:
> On Sun, Nov 22, 2026 at 06:30:43AM +0000, Akhilesh Patil wrote:
> > This patch series adds support for Aosong ASAIR acd10 CO2 (Carbon
> > dioxide) sensor driver in IIO subsystem. 
> 
> There is no answer to the question "Why a brand new driver?"

Sure. Will add a section describing it in v2 patch series.

> 
> > Patch 1: Adds bindings for this hardware
> > Patch 2: Adds driver code for acd10 under drivers/iio/chemical
> > 
> > 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.
> > 
> > Driver supports reading co2, getting sw version and instrument number.
> > Single point calibration and preheat implementation is left for
> > future contributions.
> 
> -- 
> With Best Regards,
> Andy Shevchenko
> 
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor
  2026-11-22 19:31   ` Jonathan Cameron
@ 2026-12-02 12:17     ` Akhilesh Patil
  0 siblings, 0 replies; 7+ messages in thread
From: Akhilesh Patil @ 2026-12-02 12:17 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: andy, krzk+dt, robh, dlechner, nuno.sa, conor+dt, joshua.crofts1,
	maxwell, u.kleine-koenig, linux-iio, devicetree, linux-kernel,
	akhileshpatilvnit, akhilesh

On Sun, Nov 22, 2026 at 07:31:55PM +0000, Jonathan Cameron wrote:
> On Sun, 22 Nov 2026 06:32:46 +0000
> Akhilesh Patil <akhilesh.patil@linux.dev> wrote:
> 
> > 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.
> > 
> > Signed-off-by: Akhilesh Patil <akhilesh.patil@linux.dev>
> 
> A few things from sashiko. I didn't look at whether they were correct or not
> https://sashiko.dev/#/patchset/cover.1795326744.git.akhilesh.patil%40linux.dev

My judgement on AI review: There are 3 issues flagged. 

1) Buffer overflow and race -->  overstated and very less likely

Sashiko: Can a late UART response after a signal-interrupted or 
timed-out read race with the next transfer and overflow 
drv_state->buf[16]?

It is possible but very less likely according to me.

I have intentionally kept Rx design less complex.
Sensor does not send data on its own, every response is sent upon 
receiving command. stray bytes are less likely.

Aborted transfers, or stray bytes, even if occurs in rare cases, will 
cleanly reset during next read.

I have added bound check on buf_idx in v2, just to be memory safe if 
this by any chance happens in rarest of the rare case.

2) Missing regulator support in driver --> applicable

I use fixed 5v supply for sensor on my board, so regulator support is 
not needed, I have kept that property not mandatory in DT bindings.
But there is no harm adding devm_regulator_get_enabled() to support 
driver for boards with power controlled over regulator. Added this in v2 
patch.


3) Missing MODULE_DEVICE_TABLE() --> applicable

MODULE_DEVICE_TABLE(of, acd10_of_match_table) was indeed missing in the
driver. Without this, this breaks loadable module. Hence, Added that in 
v2.  I usually test with statically built module, hence missed this in 
v1.

> 
> Some other comments below.  Given this is a new driver, give it about a week
> before sending v2 to allow others time to review.

ACK.

Thanks for the review, I am publishing v2 series addressing the review 
comments.

Regards,

Akhilesh

> 
> Thanks,
> 
> Jonathan
> 
> > diff --git a/drivers/iio/chemical/acd10.c b/drivers/iio/chemical/acd10.c
> > new file mode 100644
> > index 000000000000..b92f01e7bf4a
> > --- /dev/null
> > +++ b/drivers/iio/chemical/acd10.c
> > @@ -0,0 +1,365 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * ACD10 CO2 gas sensor driver
> > + *
> > + * Copyright (c) 2026 Akhilesh Patil <akhilesh.patil@linux.dev>
> > + *
> > + * 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>
> > +#include <linux/bits.h>
> > +#include <linux/cleanup.h>
> > +#include <linux/completion.h>
> > +#include <linux/dev_printk.h>
> > +#include <linux/device.h>
> 
> I haven't checked for things actually in device.h but make
> sure this shouldn't be replaced with more specific headers.

Dropped device.h. This driver does not need anything extra from it.

> 
> > +#include <linux/errno.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/iio/types.h>
> > +#include <linux/jiffies.h>
> > +#include <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.

bus header linux/serdev.h does not include this.
I think we need to keep mod_devicetable.h here, otherwise build breaks.

> 
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/serdev.h>
> > +#include <linux/string.h>
> > +#include <linux/types.h>
> > +#include <linux/unaligned.h>
> 
> > +
> > +enum acd10_cmds {
> > +	ACD10_CMD_READ_CO2 = 0,
> 
> Where they line up with values on the datasheet, I would always
> specify them all. Doesn't matter so much when there are only 4
> but for large lists it saves on a lot of counting!

This enum is used for indexing into cmd_info[] array to get metadata
for commands such as command_code, frame lengths which actually come
from datasheet. I used enum here for better readability while calling
acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_CO2), also debugger can
recognize them as compared to using macros.

Sure. I will specify all of them explicitly here as list is small.

> 
> > +	ACD10_CMD_CAL,
> > +	ACD10_CMD_READ_SWREV,
> > +	ACD10_CMD_READ_INS_NUM,
> > +};
> > +
> > +struct acd10_cmd_info {
> > +	u8 cmd_code;
> > +	u8 tx_frame_len;
> > +	u8 tx_data_len;
> > +	u8 rx_frame_len;
> > +};
> 
> 
> > +
> > +/*
> > + * Do transfer based on command and fill drv_state->buf with received data.
> > + * Caller of this function should use mutex drv_state->lock.
> 
> __must_hold() markings can change that documentation to something enforced
> but the static analysis tools.

Okay. I use GCC, but I got to know with LLVM it can help fix locking at 
compile time. So, added __must_hold() and did quick compile check with 
LLVM, this will also need taking lock in get sw rev and instrument num 
functions. Locks are not needed there as they are only called during 
probe, but no harm adding it, hence added lock there as well. Added 
entry in Makefile also.

drivers/iio/chemical/acd10.c:184:8: warning: calling function 
'acd10_serdev_transfer' requires holding mutex
      'iio_priv(indio_dev).lock' exclusively [-Wthread-safety-analysis]
        184 |         ret = acd10_serdev_transfer(indio_dev, drv_state, 
        ACD10_CMD_READ_SWREV);
              |               ^

> 
> > + */
> > +static int acd10_serdev_transfer(struct iio_dev *indio_dev, enum acd10_cmds cmd)
> > +{
> > +	struct acd10_state *drv_state = iio_priv(indio_dev);
> > +	struct serdev_device *serdev = drv_state->serdev;
> > +	struct device *dev = &indio_dev->dev;
> > +	int ret;
> > +
> > +	/* Prepare transmit buffer along with checksum */
> > +	drv_state->buf[0] = ACD10_FRAME_START;
> > +	drv_state->buf[1] = ACD10_FRAME_FIXED_CODE;
> > +	drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] = cmd_info[cmd].tx_data_len;
> 
> Why do these two get IDX defines whils previous do not?

Fixed.
Defined and used ACD10_FRAME_START_IDX and ACD10_FRAME_FIXED_CODE_IDX 
wherever applicable.

> 
> > +	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);
> 
> Are there even any entries between this and the previous?  I would kind of
> expect to see some zeroing of those prior to CRC given the code here.
> If there aren't what is tx_frame_len for?

For 3 commands which are implemented in this series (tx_frame_len = 5), 
there are no data entries. CRC is followed right after CMD_CODE.

Only for calibration command (tx_frame_len = 7), there should be 2 data 
bytes before CRC. This will be filled by the caller of 
acd10_serdev_transfer() OR can be handled specially for calibration 
command here itself. For now, I have kept CMD_CAL out of scope of this 
series.

tx_frame_len is total # of bytes in tx command which is 5 for 3 commands 
and 7 only for calibration command.

> 
> 
> > +	drv_state->buf_idx = 0;
> > +	drv_state->rx_expected_len = cmd_info[cmd].rx_frame_len;
> > +	reinit_completion(&drv_state->buf_ready);
> > +
> > +	/* Transmit the buffer over serdev */
> > +	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;
> > +
> > +	/* Read the received bytes in buffer */
> > +	ret = wait_for_completion_interruptible_timeout(&drv_state->buf_ready,
> > +							ACD10_SERDEV_TIMEOUT);
> > +	if (ret < 0)
> > +		return ret;
> > +	if (!ret)
> > +		return -ETIMEDOUT;
> > +
> > +	/* validate frame header */
> > +	if (drv_state->buf[0] != ACD10_FRAME_START ||
> > +	    drv_state->buf[1] != ACD10_FRAME_FIXED_CODE) {
> > +		dev_err(dev, "RX frame invalid\n");
> > +		return -EIO;
> > +	}
> > +
> > +	/* Maximum data len can be max 11 */
> > +	if (drv_state->buf[ACD10_FRAME_DATA_LEN_IDX] > ACD10_FRAME_DATA_LEN_MAX)
> > +		return -EIO;
> > +
> > +	/* Validate command echo */
> > +	if (drv_state->buf[ACD10_FRAME_CMD_CODE_IDX] != cmd_info[cmd].cmd_code)
> > +		return -EIO;
> > +
> > +	/* Validate checksum */
> 
> That's fairly obvious from the code.  Keep comment for where they add
> value over what can be seen without.  Same applies to at least some of the ones above.

ACK. Removed obvious comments. 

> 
> > +	if (drv_state->buf[cmd_info[cmd].rx_frame_len - 1] !=
> > +	    acd10_get_checksum(drv_state->buf, cmd_info[cmd].rx_frame_len)) {
> > +		dev_err(dev, "Checksum error\n");
> > +		return -EIO;
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> > +static int acd10_get_sw_rev(struct iio_dev *indio_dev)
> > +{
> > +	struct acd10_state *drv_state = iio_priv(indio_dev);
> > +	struct device *dev = &indio_dev->dev;
> > +	int ret;
> > +
> > +	ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_SWREV);
> > +	if (ret)
> > +		return ret;
> > +
> > +	memcpy(drv_state->sw_rev, &drv_state->buf[ACD10_FRAME_DATA_START], 10);
> > +	drv_state->sw_rev[10] = '\0';
> > +
> > +	dev_info(dev, "Software revision: %s\n", drv_state->sw_rev);
> 
> dev_dbg() probably.  
> 
> We tend to not print this stuff by default as it leads to ever more bloated
> kernel logs.
> 

Agree. Used dev_dbg()

> > +
> > +	return 0;
> > +}
> > +
> > +static int acd10_get_instrument_num(struct iio_dev *indio_dev)
> > +{
> > +	struct acd10_state *drv_state = iio_priv(indio_dev);
> > +	struct device *dev = &indio_dev->dev;
> > +	int ret;
> > +
> > +	ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_INS_NUM);
> > +	if (ret)
> > +		return ret;
> > +
> > +	memcpy(drv_state->inst_num, &drv_state->buf[ACD10_FRAME_DATA_START], 10);
> > +	drv_state->inst_num[10] = '\0';
> > +
> > +	dev_info(dev, "Instrument number: %s\n", drv_state->inst_num);
> 
> Noisy.  I'm not sure what this is. Can it map to serial_number which is
> standard ABI?
> 

Used dev_dbg() here.

This is 10 bytes ASCII code per device. Yes, this can be mapped to IIO 
standard ABI serialnumber. Added this in v2.

My main intent of getting and printing software version and instrument 
number here is to sanity check communication with the device before 
probe is marked success. As device supports commands to get these 
values, I thought to implement it along with core raw read
function.

On my device it comes: 

# cat /sys/bus/iio/devices/iio:device0/serialnumber
0188152356

> > +
> > +	return 0;
> > +}
> 
> > +static int acd10_read_raw(struct iio_dev *indio_dev,
> > +			  struct iio_chan_spec const *chan,
> > +			  int *val, int *val2, long mask)
> > +{
> > +	struct acd10_state *drv_state = iio_priv(indio_dev);
> > +	int ret;
> > +
> > +	switch (mask) {
> > +	case IIO_CHAN_INFO_RAW:
> > +
> > +		/* Read CO2 sensor value*/
> > +		scoped_guard(mutex, &drv_state->lock) {
> Preference where it is applicable is to use scope definition for case blocks that
> need a guard.
> 	case IIO_CHAN_INFO_RAW: {
> 		int ret;
> 
> 		guard(mutex)(&drv_state->lock);
> 
> 		ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_CO2);
> 		if (ret)
> 			return ret;
> 
> 		*val = get_unaligned_be16(&drv_state->buf[ACD10_FRAME_DATA_START]);
> 
> 		return IIO_VAL_INT;
> 	}
> 
> Mostly because it remove unnecessary indent.

Sure. Used guard() instead of scoped_guard() for mutex here to improve 
on indentation. No functional impact.

>  
> > +			ret = acd10_serdev_transfer(indio_dev, ACD10_CMD_READ_CO2);
> > +			if (ret)
> > +				return ret;
> > +
> > +			*val = get_unaligned_be16(&drv_state->buf[ACD10_FRAME_DATA_START]);
> > +		}
> > +
> > +		return IIO_VAL_INT;
> > +
> > +	case IIO_CHAN_INFO_SCALE:
> > +		*val = 0;
> > +		*val2 = 100;
> > +		return IIO_VAL_INT_PLUS_MICRO;
> > +
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> 
> > +
> > +static int acd10_probe(struct serdev_device *serdev)
> > +{
> > +	int ret;
> 
> Where ordering isn't required due to dependencies mild preference for
> reverse xmas tree.

Fixed.
Ordered as per reverse xmas tree for probe function.
Checked other functions in the driver - others already in order.

> 
> > +	struct device *dev = &serdev->dev;
> > +	struct iio_dev *indio_dev;
> > +	struct acd10_state *drv_state;
> > +	unsigned int baudrate;
> > +
> > +	indio_dev = devm_iio_device_alloc(dev, sizeof(struct acd10_state));
> 
> sizeof(*drv_state)

Fixed.

> 
> > +	if (!indio_dev)
> > +		return -ENOMEM;
> > +
> > +	indio_dev->name = "acd10";
> > +	indio_dev->channels = acd10_channels;
> > +	indio_dev->num_channels = ARRAY_SIZE(acd10_channels);
> > +	indio_dev->info = &acd10_info;
> > +
> > +	serdev_device_set_drvdata(serdev, indio_dev);
> > +
> > +	drv_state = iio_priv(indio_dev);
> > +	drv_state->serdev = serdev;
> > +
> > +	init_completion(&drv_state->buf_ready);
> > +	ret = devm_mutex_init(dev, &drv_state->lock);
> > +	if (ret)
> > +		return ret;
> > +
> > +	serdev_device_set_client_ops(serdev, &acd10_ser_ops);
> > +	ret = devm_serdev_device_open(dev, serdev);
> > +	if (ret) {
> > +		dev_err(dev, "ACD10: serial device open failed\n");
> > +		return ret;
> 		return dev_err_probe(dev, ret, ...
> 
> It's both shorter and saves any effort by reviewer to figure
> out if deferral is possible on a given call.  Use this for every
> error print in probe + any in functions only called from probe.

ACK. Used dev_err_probe() for devm_serdev_device_open() and 
_set_baudrate() during probe. In our case although at both places 
deferral is not possible, but yes, it improves readability and compacts 
code.

> 
> > +	}
> > +
> > +	baudrate = serdev_device_set_baudrate(serdev, ACD10_SERIAL_BAUDRATE);
> > +	dev_info(dev, "baud rate set to: %u\n", baudrate);
> 
> Too noisy for a production driver.  dev_dbg() or drop it entirely.

ACK. Used dev_dbg()

> 
> > +	if (baudrate != ACD10_SERIAL_BAUDRATE) {
> > +		dev_err(dev, "baud rate expected: %u, got: %u\n",
> > +			ACD10_SERIAL_BAUDRATE, baudrate);
> > +		return -EIO;
> > +	}
> > +	serdev_device_set_flow_control(serdev, false);
> > +	ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = acd10_get_sw_rev(indio_dev);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = acd10_get_instrument_num(indio_dev);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = devm_iio_device_register(dev, indio_dev);
> > +	if (ret)
> > +		return ret;
> > +
> > +	return 0;
> 
> 	return devm_iio_device_register();

Fixed.

> 
> > +}
> 
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-02 12:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22  6:30 [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Akhilesh Patil
2026-11-22  6:31 ` [PATCH 1/2] dt-bindings: iio: chemical: Add aosong acd10 Akhilesh Patil
2026-11-22  6:32 ` [PATCH 2/2] iio: chemical: acd10: Add driver for ACD10 CO2 sensor Akhilesh Patil
2026-11-22 19:31   ` Jonathan Cameron
2026-12-02 12:17     ` Akhilesh Patil
2026-11-23  9:28 ` [PATCH 0/2] iio: chemical: Add driver and bindings for acd10 Andy Shevchenko
2026-12-02 12:05   ` Akhilesh Patil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®