Skip to contentSkip to conversation

iio: adc: add Nuvoton NCT7201 ADC driver

PATCH Superseded Eason Yang 3 participants 7 messages Apr 9, 2025 – Apr 15, 2025

Subsystems (3)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Eason Yang

author maintainer
#0 lore raw

v5 cover iio: adc: add Nuvoton NCT7201 ADC driver

Change since version 5:
- Fix comments
- Add NUVOTON NCT7201 IIO DRIVER section in MAINTAINERS
- Add vdd-supply and vref-supply to the DT example
- Remove mutex since the regmap should already have an internal lock
- Remove redundant assigning values
- Check errors on regmap_write

Change since version 4:
- Fix comments
- Add interrupts and reset-gpios to the DT example
- Use the FIELD_PREP and FIELD_GET
- Add use_single_write in regmap_config
- Use regmap_access_table

Change since version 3:
- Fix comments
- Don't put nct720"x" in the name, just call it nct7201
- Remove differential inputs until conversions are finished
- Add NCT7201_ prefix in all macros and avoid the tables
- Correct event threshold values in raw units
- Add with and without interrupt callback function to have the event config part and one that doesn't
- Remove print an error message if regmap_wirte failed case

Change since version 2:
- Remvoe read-vin-data-size property, default use read word vin data
- Use regmap instead of i2c smbus API
- IIO should be IIO_CHAN_INFO_RAW and _SCALE not _PROCESSED
- Use dev_xxx_probe in probe function and dev_xxx in other functions
- Use devm_iio_device_register replace of iio_device_register
- Use guard(mutex) replace of mutex_lock
- Use get_unaligned_le16 conversion API

Changes since version 1:
- Add new property in iio:adc binding document
- Add new driver for Nuvoton NCT720x driver

Eason Yang (2):
dt-bindings: iio: adc: add NCT7201 ADCs
iio: adc: add support for Nuvoton NCT7201

 .../bindings/iio/adc/nuvoton,nct7201.yaml     |  71 +++
 MAINTAINERS                                   |   7 +
 drivers/iio/adc/Kconfig                       |  11 +
 drivers/iio/adc/Makefile                      |   1 +
 drivers/iio/adc/nct7201.c                     | 488 ++++++++++++++++++
 5 files changed, 578 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml
 create mode 100644 drivers/iio/adc/nct7201.c
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v5 1/2 dt-bindings: iio: adc: add NCT7201 ADCs

Add a binding specification for the Nuvoton NCT7201/NCT7202 up to 12-bit ADCs with I2C interface.

---
 .../bindings/iio/adc/nuvoton,nct7201.yaml     | 71 +++++++++++++++++++
 MAINTAINERS                                   |  6 ++
 2 files changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml
Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml new file +71 -0
new file mode 100644
index 000000000000..773e0cfd9f36
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/nuvoton,nct7201.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton nct7201 and similar ADCs
+
+maintainers:
+  - Eason Yang <[email protected]>
+
+description: |
+  The NCT7201/NCT7202 is a Nuvoton Hardware Monitor IC, contains up to 12
+  voltage monitoring channels, with SMBus interface, and up to 4 sets SMBus
+  address selection by ADDR connection. It also provides ALERT# signal for
+  event notification and reset input RSTIN# to recover it from a fault
+  condition.
+
+  NCT7201 contains 8 voltage monitor inputs (VIN1~VIN8).
+  NCT7202 contains 12 voltage monitor inputs (VIN1~VIN12).
+
+properties:
+  compatible:
+    enum:
+      - nuvoton,nct7201
+      - nuvoton,nct7202
+
+  reg:
+    maxItems: 1
+
+  vdd-supply:
+    description:
+      A 3.3V to supply that powers the chip.
+
+  vref-supply:
+    description:
+      The regulator supply for the ADC reference voltage.
+
+  interrupts:
+    maxItems: 1
+
+  reset-gpios:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@1d {
+            compatible = "nuvoton,nct7202";
+            reg = <0x1d>;
+            vdd-supply = <&vdd>;
+            vref-supply = <&vref>;
+            interrupt-parent = <&gpio3>;
+            interrupts = <30 IRQ_TYPE_LEVEL_LOW>;
+            reset-gpios = <&gpio3 28 GPIO_ACTIVE_LOW>;
+        };
+    };
+...
MAINTAINERS +6 -0
index 96b827049501..f9ec68d4d8b4 100644
@@ -17302,6 +17302,12 @@ F:	drivers/nubus/
 F:	include/linux/nubus.h
 F:	include/uapi/linux/nubus.h
 
+NUVOTON NCT7201 IIO DRIVER
+M:	Eason Yang <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:	Antonino Daplas <[email protected]>
 L:	[email protected]
Signature · 1 line
2.34.1
Reviewed-by on v5 1/2
#2 lore raw

Re: [PATCH v5 1/2] dt-bindings: iio: adc: add NCT7201 ADCs

Quoted 5 lines · Eason Yang
Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml new file +5 -0
+  interrupts:
+    maxItems: 1
+
+  reset-gpios:
+    maxItems: 1

Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml line 53

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml new file +10 -0
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+

If you are going to send a new version, drop blank line here.

Best regards,
Krzysztof

author maintainer
#3 lore raw

PATCH v5 2/2 iio: adc: add support for Nuvoton NCT7201

In reply to Eason Yang

Add Nuvoton NCT7201/NCT7202 system voltage monitor 12-bit ADC driver

NCT7201/NCT7202 supports up to 12 analog voltage monitor inputs and up to 4 SMBus addresses by ADDR pin. Meanwhile, ALERT# hardware event pins for independent alarm signals, and all the threshold values could be set for system protection without any timing delay. It also supports reset input RSTIN# to recover system from a fault condition.

Currently, only single-edge mode conversion and threshold events are supported.

---
 MAINTAINERS               |   1 +
 drivers/iio/adc/Kconfig   |  11 +
 drivers/iio/adc/Makefile  |   1 +
 drivers/iio/adc/nct7201.c | 488 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 501 insertions(+)
 create mode 100644 drivers/iio/adc/nct7201.c
MAINTAINERS +1 -0
index f9ec68d4d8b4..d155780e5ee0 100644
@@ -17307,6 +17307,7 @@ M:	Eason Yang <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/iio/adc/nuvoton,nct7201.yaml
+F:	drivers/iio/adc/nct7201.c
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:	Antonino Daplas <[email protected]>
drivers/iio/adc/Kconfig +11 -0
index 6529df1a498c..6d6af1b51b5e 100644
@@ -1092,6 +1092,17 @@ config NAU7802
 	  To compile this driver as a module, choose M here: the
 	  module will be called nau7802.
 
+config NCT7201
+	tristate "Nuvoton Instruments NCT7201 and NCT7202 Power Monitor"
+	depends on I2C
+	select REGMAP_I2C
+	help
+	  If you say yes here you get support for the Nuvoton NCT7201 and
+	  NCT7202 Voltage Monitor.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nct7201.
+
 config NPCM_ADC
 	tristate "Nuvoton NPCM ADC driver"
 	depends on ARCH_NPCM || COMPILE_TEST
drivers/iio/adc/Makefile +1 -0
index 3e918c3eec69..54e8a7541af6 100644
@@ -97,6 +97,7 @@ obj-$(CONFIG_MESON_SARADC) += meson_saradc.o
 obj-$(CONFIG_MP2629_ADC) += mp2629_adc.o
 obj-$(CONFIG_MXS_LRADC_ADC) += mxs-lradc-adc.o
 obj-$(CONFIG_NAU7802) += nau7802.o
+obj-$(CONFIG_NCT7201) += nct7201.o
 obj-$(CONFIG_NPCM_ADC) += npcm_adc.o
 obj-$(CONFIG_PAC1921) += pac1921.o
 obj-$(CONFIG_PAC1934) += pac1934.o
drivers/iio/adc/nct7201.c new file +488 -0

488 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v5 2/2] iio: adc: add support for Nuvoton NCT7201

Quoted 23 lines · Eason Yang

Add Nuvoton NCT7201/NCT7202 system voltage monitor 12-bit ADC driver

NCT7201/NCT7202 supports up to 12 analog voltage monitor inputs and up to 4 SMBus addresses by ADDR pin. Meanwhile, ALERT# hardware event pins for independent alarm signals, and all the threshold values could be set for system protection without any timing delay. It also supports reset input RSTIN# to recover system from a fault condition.

Currently, only single-edge mode conversion and threshold events are supported.

drivers/iio/adc/nct7201.c new file +12 -0
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>

...

Quoted 1 line

+#define NCT7201_VIN_MAX 12

Is this in volts? Can you add a unit suffix?

...

Quoted 1 line

+#define NCT7201_IN_SCALING 4995

Interesting number, just want to confirm it's indeed 4995 and not 4095.

...

Quoted 18 lines
drivers/iio/adc/nct7201.c new file +17 -0
+static int nct7201_read_event_value(struct iio_dev *indio_dev,
+				    const struct iio_chan_spec *chan,
+				    enum iio_event_type type,
+				    enum iio_event_direction dir,
+				    enum iio_event_info info,
+				    int *val, int *val2)
+{
+	struct nct7201_chip_info *chip = iio_priv(indio_dev);
+	unsigned int value;
+	int err;
+
+	if (chan->type != IIO_VOLTAGE)
+		return -EOPNOTSUPP;
+
+	if (info != IIO_EV_INFO_VALUE)
+		return -EINVAL;
 
+	if (dir == IIO_EV_DIR_FALLING) {

drivers/iio/adc/nct7201.c line 245

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +10 -0
+		err = regmap_read(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
+				  &value);
+		if (err < 0)
+			return err;
+	} else {
+		err = regmap_read(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
+				  &value);
+		if (err < 0)
+			return err;
+	}
	if (dir == IIO_EV_DIR_FALLING) {
		err = regmap_read(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
				  &value);
	} else {
		err = regmap_read(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
				  &value);
	}
	if (err)
		return err;

Here and elsewhere why ' < 0' is used? Do you expect positive return values from those?

Quoted 4 lines
drivers/iio/adc/nct7201.c new file +4 -0
+	*val = FIELD_GET(NCT7201_REG_VIN_MASK, value);
+
+	return IIO_VAL_INT;
+}

...

drivers/iio/adc/nct7201.c line 260

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +9 -0
+static int nct7201_write_event_value(struct iio_dev *indio_dev,
+				     const struct iio_chan_spec *chan,
+				     enum iio_event_type type,
+				     enum iio_event_direction dir,
+				     enum iio_event_info info,
+				     int val, int val2)
+{
+	struct nct7201_chip_info *chip = iio_priv(indio_dev);
 
+	int  err = 0;

Useless assignment.

Quoted 9 lines
drivers/iio/adc/nct7201.c new file +9 -0
+	if (chan->type != IIO_VOLTAGE)
+		return -EOPNOTSUPP;
+
+	if (info != IIO_EV_INFO_VALUE)
+		return -EOPNOTSUPP;
+
+	if (dir == IIO_EV_DIR_FALLING) {
+		err = regmap_write(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
+				   FIELD_PREP(NCT7201_REG_VIN_MASK, val));

drivers/iio/adc/nct7201.c line 280

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +10 -0
+		if (err < 0)
+			return err;
+	} else {
+		err = regmap_write(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
+				   FIELD_PREP(NCT7201_REG_VIN_MASK, val));
+		if (err < 0)
+			return err;
+	}
+
+	return 0;
	if (dir == IIO_EV_DIR_FALLING) {
		err = regmap_write(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
				   FIELD_PREP(NCT7201_REG_VIN_MASK, val));
	} else {
		err = regmap_write(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
				   FIELD_PREP(NCT7201_REG_VIN_MASK, val));
	}
	return err;
Quoted 1 line

+}

...

Quoted 23 lines
drivers/iio/adc/nct7201.c new file +22 -0
+static int nct7201_write_event_config(struct iio_dev *indio_dev,
+				      const struct iio_chan_spec *chan,
+				      enum iio_event_type type,
+				      enum iio_event_direction dir,
+				      bool state)
+{
+	struct nct7201_chip_info *chip = iio_priv(indio_dev);
+	unsigned int mask;
+	int err;
+
+	if (chan->type != IIO_VOLTAGE)
+		return -EOPNOTSUPP;
+
+	mask = BIT(chan->address);
+
+	if (state)
+		chip->vin_mask |= mask;
+	else
+		chip->vin_mask &= ~mask;
 
+	if (chip->num_vin_channels <= 8) {
+		err = regmap_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1,
+				   chip->vin_mask);

drivers/iio/adc/nct7201.c line 328

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +10 -0
+		if (err < 0)
+			return err;
+	} else {
+		err = regmap_bulk_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1,
+					&chip->vin_mask, sizeof(chip->vin_mask));
+		if (err < 0)
+			return err;
+	}
+
+	return 0;

Same as above.

Quoted 1 line

+}

...

drivers/iio/adc/nct7201.c line 359

Quoted 3 lines
drivers/iio/adc/nct7201.c new file +3 -0
+static int nct7201_init_chip(struct nct7201_chip_info *chip)
+{
+	u8 data[2] = {0};

'0' is not needed. Shouldn't this be __le16 or __be16 instead?

drivers/iio/adc/nct7201.c line 366

Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+	unsigned int value;
+	int err;
+
+	err = regmap_write(chip->regmap, NCT7201_REG_CONFIGURATION,
+			   NCT7201_BIT_CONFIGURATION_RESET);
+	if (err < 0)
+		return dev_err_probe(&chip->client->dev, -EIO,
	struct device *dev = &chip->client->dev;

at the top of the function will help a lot in tiding up the below code.

Shadowed error code, why?

drivers/iio/adc/nct7201.c line 373

Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+				     "Failed to write NCT7201_REG_CONFIGURATION\n");
+
+	/*
+	 * After about 25 msecs, the device should be ready and then the Power
+	 * Up bit will be set to 1. If not, wait for it.
+	 */
+	mdelay(25);

No sleep? Why? Can't you use fsleep()?

drivers/iio/adc/nct7201.c line 378

Quoted 5 lines
drivers/iio/adc/nct7201.c new file +5 -0
+	err = regmap_read(chip->regmap, NCT7201_REG_BUSY_STATUS, &value);
+	if (err < 0)
+		return err;
+	if (!(value & NCT7201_BIT_PWR_UP))
+		return dev_err_probe(&chip->client->dev, -EIO,

Shadowed error code, why?

drivers/iio/adc/nct7201.c line 386

Quoted 8 lines
drivers/iio/adc/nct7201.c new file +8 -0
+				     "Failed to power up after reset\n");
+
+	/* Enable Channel */
+	if (chip->num_vin_channels <= 8) {
+		data[0] = NCT7201_REG_CHANNEL_ENABLE_1_MASK;
+		err = regmap_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1, data[0]);
+		if (err < 0)
+			return dev_err_probe(&chip->client->dev, -EIO,

Why error code is shadowed?

drivers/iio/adc/nct7201.c line 394

Quoted 8 lines
drivers/iio/adc/nct7201.c new file +8 -0
+					     "Failed to write NCT7201_REG_CHANNEL_ENABLE_1\n");
+	} else {
+		data[0] = NCT7201_REG_CHANNEL_ENABLE_1_MASK;
+		data[1] = NCT7201_REG_CHANNEL_ENABLE_2_MASK;
+		err = regmap_bulk_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1,
+					data, ARRAY_SIZE(data));
+		if (err < 0)
+			return dev_err_probe(&chip->client->dev, -EIO,

Ditto.

Review

Quoted 2 lines
(quoted diff) +2 -0
+					     "Failed to write NCT7201_REG_CHANNEL_ENABLE_1 and NCT7201_REG_CHANNEL_ENABLE_2\n");
+	}

Just make it 16-bit type, define one value and use just simple English in the error message: "Failed to write channel enable mask\n");

Same to all your error messages.

drivers/iio/adc/nct7201.c line 407

Quoted 11 lines
drivers/iio/adc/nct7201.c new file +9 -0
+	chip->vin_mask = get_unaligned_le16(data);
+
+	/* Start monitoring if needed */
+	err = regmap_read(chip->regmap, NCT7201_REG_CONFIGURATION, &value);
+	if (err < 0)
+		return dev_err_probe(&chip->client->dev, -EIO,
+				     "Failed to read NCT7201_REG_CONFIGURATION\n");
 
+	regmap_set_bits(chip->regmap, NCT7201_REG_CONFIGURATION, NCT7201_BIT_CONFIGURATION_START);
 
+	return 0;

No error check? Why?

Quoted 1 line

+}

...

drivers/iio/adc/nct7201.c line 412

Quoted 3 lines
drivers/iio/adc/nct7201.c new file +3 -0
+static int nct7201_probe(struct i2c_client *client)
+{
+	const struct nct7201_adc_model_data *model_data;
	struct device *dev = &client->dev;

drivers/iio/adc/nct7201.c line 419

Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+	struct nct7201_chip_info *chip;
+	struct iio_dev *indio_dev;
+	int ret;
+
+	model_data = i2c_get_match_data(client);
+	if (!model_data)
+		return -EINVAL;

ENODEV is more suitable here.

Quoted 9 lines
drivers/iio/adc/nct7201.c new file +9 -0
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
+	if (!indio_dev)
+		return -ENOMEM;
+	chip = iio_priv(indio_dev);
+
+	chip->regmap = devm_regmap_init_i2c(client, &nct7201_regmap8_config);
+	if (IS_ERR(chip->regmap))
+		return dev_err_probe(&client->dev, PTR_ERR(chip->regmap),

drivers/iio/adc/nct7201.c line 438

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +9 -0
+				     "Failed to init regmap\n");
+
+	chip->regmap16 = devm_regmap_init_i2c(client, &nct7201_regmap16_config);
+	if (IS_ERR(chip->regmap16))
+		return dev_err_probe(&client->dev, PTR_ERR(chip->regmap16),
+				     "Failed to init regmap16\n");
+
+	chip->num_vin_channels = model_data->num_vin_channels;
 
+	chip->client = client;

How exactly is _client_ used elsewhere? Shouldn't it be just a struct device pointer?

drivers/iio/adc/nct7201.c line 441

Quoted 2 lines
drivers/iio/adc/nct7201.c new file +2 -0
+	ret = nct7201_init_chip(chip);
+	if (ret < 0)

Do you expect positive returned values? What is their meaning? Why do you skip them?

Quoted 13 lines
drivers/iio/adc/nct7201.c new file +13 -0
+		return ret;
+
+	indio_dev->name = model_data->model_name;
+	indio_dev->channels = model_data->channels;
+	indio_dev->num_channels = model_data->num_channels;
+	if (client->irq)
+		indio_dev->info = &nct7201_info;
+	else
+		indio_dev->info = &nct7201_info_no_irq;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#5 lore raw

Re: [PATCH v5 2/2] iio: adc: add support for Nuvoton NCT7201

Dear Andy,

Thanks for the review and the comments.
Will fix all.

Andy Shevchenko <[email protected]> 於 2025年4月10日 週四 上午12:19寫道:

Quoted 28 lines · Eason Yang
Quoted 23 lines

Add Nuvoton NCT7201/NCT7202 system voltage monitor 12-bit ADC driver

NCT7201/NCT7202 supports up to 12 analog voltage monitor inputs and up to 4 SMBus addresses by ADDR pin. Meanwhile, ALERT# hardware event pins for independent alarm signals, and all the threshold values could be set for system protection without any timing delay. It also supports reset input RSTIN# to recover system from a fault condition.

Currently, only single-edge mode conversion and threshold events are supported.

drivers/iio/adc/nct7201.c new file +12 -0
+#include <linux/array_size.h>
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>

...

Quoted 3 lines
Quoted 1 line

+#define NCT7201_VIN_MAX 12

Is this in volts? Can you add a unit suffix?

Rename it as
#define NCT7201_MAX_CHANNEL 12

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+#define NCT7201_IN_SCALING                           4995
+#define NCT720X_IN_SCALING_FACTOR         10000

Interesting number, just want to confirm it's indeed 4995 and not 4095.

These definitions are for real voltage calculations, the formula is Voltage(V) = 13bitCountValue * 0.0004995 However, the definitions are not used anymore. So remove them.

Quoted 41 lines

...

Quoted 18 lines
drivers/iio/adc/nct7201.c new file +17 -0
+static int nct7201_read_event_value(struct iio_dev *indio_dev,
+                                 const struct iio_chan_spec *chan,
+                                 enum iio_event_type type,
+                                 enum iio_event_direction dir,
+                                 enum iio_event_info info,
+                                 int *val, int *val2)
+{
+     struct nct7201_chip_info *chip = iio_priv(indio_dev);
+     unsigned int value;
+     int err;
+
+     if (chan->type != IIO_VOLTAGE)
+             return -EOPNOTSUPP;
+
+     if (info != IIO_EV_INFO_VALUE)
+             return -EINVAL;
 
+     if (dir == IIO_EV_DIR_FALLING) {

drivers/iio/adc/nct7201.c line 245

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +10 -0
+             err = regmap_read(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
+                               &value);
+             if (err < 0)
+                     return err;
+     } else {
+             err = regmap_read(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
+                               &value);
+             if (err < 0)
+                     return err;
+     }
        if (dir == IIO_EV_DIR_FALLING) {
                err = regmap_read(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
                                  &value);
        } else {
                err = regmap_read(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
                                  &value);
        }
        if (err)
                return err;
Quoted 3 lines

Here and elsewhere why ' < 0' is used? Do you expect positive return values from those?

In regmap_read function, A value of zero will be returned on success, a negative errno will be returned in error cases.
We don't have a positive return case.

Quoted 93 lines
Quoted 4 lines
drivers/iio/adc/nct7201.c new file +4 -0
+     *val = FIELD_GET(NCT7201_REG_VIN_MASK, value);
+
+     return IIO_VAL_INT;
+}

...

drivers/iio/adc/nct7201.c line 260

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +9 -0
+static int nct7201_write_event_value(struct iio_dev *indio_dev,
+                                  const struct iio_chan_spec *chan,
+                                  enum iio_event_type type,
+                                  enum iio_event_direction dir,
+                                  enum iio_event_info info,
+                                  int val, int val2)
+{
+     struct nct7201_chip_info *chip = iio_priv(indio_dev);
 
+     int  err = 0;

Useless assignment.

Quoted 9 lines
drivers/iio/adc/nct7201.c new file +9 -0
+     if (chan->type != IIO_VOLTAGE)
+             return -EOPNOTSUPP;
+
+     if (info != IIO_EV_INFO_VALUE)
+             return -EOPNOTSUPP;
+
+     if (dir == IIO_EV_DIR_FALLING) {
+             err = regmap_write(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
+                                FIELD_PREP(NCT7201_REG_VIN_MASK, val));

drivers/iio/adc/nct7201.c line 280

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +10 -0
+             if (err < 0)
+                     return err;
+     } else {
+             err = regmap_write(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
+                                FIELD_PREP(NCT7201_REG_VIN_MASK, val));
+             if (err < 0)
+                     return err;
+     }
+
+     return 0;
        if (dir == IIO_EV_DIR_FALLING) {
                err = regmap_write(chip->regmap16, NCT7201_REG_VIN_LOW_LIMIT(chan->address),
                                   FIELD_PREP(NCT7201_REG_VIN_MASK, val));
        } else {
                err = regmap_write(chip->regmap16, NCT7201_REG_VIN_HIGH_LIMIT(chan->address),
                                   FIELD_PREP(NCT7201_REG_VIN_MASK, val));
        }
        return err;
Quoted 1 line

+}

...

Quoted 23 lines
drivers/iio/adc/nct7201.c new file +22 -0
+static int nct7201_write_event_config(struct iio_dev *indio_dev,
+                                   const struct iio_chan_spec *chan,
+                                   enum iio_event_type type,
+                                   enum iio_event_direction dir,
+                                   bool state)
+{
+     struct nct7201_chip_info *chip = iio_priv(indio_dev);
+     unsigned int mask;
+     int err;
+
+     if (chan->type != IIO_VOLTAGE)
+             return -EOPNOTSUPP;
+
+     mask = BIT(chan->address);
+
+     if (state)
+             chip->vin_mask |= mask;
+     else
+             chip->vin_mask &= ~mask;
 
+     if (chip->num_vin_channels <= 8) {
+             err = regmap_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1,
+                                chip->vin_mask);

drivers/iio/adc/nct7201.c line 328

Quoted 10 lines
drivers/iio/adc/nct7201.c new file +10 -0
+             if (err < 0)
+                     return err;
+     } else {
+             err = regmap_bulk_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1,
+                                     &chip->vin_mask, sizeof(chip->vin_mask));
+             if (err < 0)
+                     return err;
+     }
+
+     return 0;

Same as above.

Quoted 1 line

+}

Quoted 8 lines

...

drivers/iio/adc/nct7201.c line 359

Quoted 3 lines
drivers/iio/adc/nct7201.c new file +3 -0
+static int nct7201_init_chip(struct nct7201_chip_info *chip)
+{
+     u8 data[2] = {0};

'0' is not needed. Shouldn't this be __le16 or __be16 instead?

+ __be16 data = NCT7201_REG_CHANNEL_ENABLE_MASK;

Quoted 8 lines

drivers/iio/adc/nct7201.c line 366

Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+     unsigned int value;
+     int err;
+
+     err = regmap_write(chip->regmap, NCT7201_REG_CONFIGURATION,
+                        NCT7201_BIT_CONFIGURATION_RESET);
+     if (err < 0)
+             return dev_err_probe(&chip->client->dev, -EIO,
Quoted 6 lines
        struct device *dev = &chip->client->dev;

at the top of the function will help a lot in tiding up the below code.

Shadowed error code, why?

Do not shadow the return code by -EIO and let the regmap API caller decide.

Quoted 10 lines

drivers/iio/adc/nct7201.c line 373

Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+                                  "Failed to write NCT7201_REG_CONFIGURATION\n");
+
+     /*
+      * After about 25 msecs, the device should be ready and then the Power
+      * Up bit will be set to 1. If not, wait for it.
+      */
+     mdelay(25);

No sleep? Why? Can't you use fsleep()?

+ msleep(25);

Quoted 7 lines

drivers/iio/adc/nct7201.c line 378

Quoted 5 lines
drivers/iio/adc/nct7201.c new file +5 -0
+     err = regmap_read(chip->regmap, NCT7201_REG_BUSY_STATUS, &value);
+     if (err < 0)
+             return err;
+     if (!(value & NCT7201_BIT_PWR_UP))
+             return dev_err_probe(&chip->client->dev, -EIO,

Shadowed error code, why?

Do not shadow the return code by -EIO and let the regmap API caller decide.

Quoted 10 lines

drivers/iio/adc/nct7201.c line 386

Quoted 9 lines
drivers/iio/adc/nct7201.c new file +8 -0
+                                  "Failed to power up after reset\n");
+
+     /* Enable Channel */
+     if (chip->num_vin_channels <= 8) {
+             data[0] = NCT7201_REG_CHANNEL_ENABLE_1_MASK;
+             err = regmap_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1, data[0]);
+             if (err < 0)
+                     return dev_err_probe(&chip->client->dev, -EIO,
Quoted 2 lines

Why error code is shadowed?

Do not shadow the return code by -EIO and let the regmap API caller decide.

Quoted 11 lines

drivers/iio/adc/nct7201.c line 394

Quoted 8 lines
drivers/iio/adc/nct7201.c new file +8 -0
+                                          "Failed to write NCT7201_REG_CHANNEL_ENABLE_1\n");
+     } else {
+             data[0] = NCT7201_REG_CHANNEL_ENABLE_1_MASK;
+             data[1] = NCT7201_REG_CHANNEL_ENABLE_2_MASK;
+             err = regmap_bulk_write(chip->regmap, NCT7201_REG_CHANNEL_ENABLE_1,
+                                     data, ARRAY_SIZE(data));
+             if (err < 0)
+                     return dev_err_probe(&chip->client->dev, -EIO,

Ditto.

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                                          "Failed to write NCT7201_REG_CHANNEL_ENABLE_1 and NCT7201_REG_CHANNEL_ENABLE_2\n");
+     }

Just make it 16-bit type, define one value and use just simple English in the error message: "Failed to write channel enable mask\n");

Same to all your error messages.

We would examine all the error messages in simple English.

Quoted 8 lines
Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+     chip->vin_mask = get_unaligned_le16(data);
+
+     /* Start monitoring if needed */
+     err = regmap_read(chip->regmap, NCT7201_REG_CONFIGURATION, &value);
+     if (err < 0)
+             return dev_err_probe(&chip->client->dev, -EIO,
+                                  "Failed to read NCT7201_REG_CONFIGURATION\n");
Quoted 6 lines

drivers/iio/adc/nct7201.c line 407

Quoted 3 lines
drivers/iio/adc/nct7201.c new file +2 -0
+     regmap_set_bits(chip->regmap, NCT7201_REG_CONFIGURATION, NCT7201_BIT_CONFIGURATION_START);
 
+     return 0;

No error check? Why?

+ err = regmap_set_bits(chip->regmap, NCT7201_REG_CONFIGURATION,
+ NCT7201_BIT_CONFIGURATION_START);
+ if (err)
+ return dev_err_probe(dev, err, "Failed to start monitoring\n");

Quoted 18 lines
Quoted 1 line

+}

...

drivers/iio/adc/nct7201.c line 412

Quoted 3 lines
drivers/iio/adc/nct7201.c new file +3 -0
+static int nct7201_probe(struct i2c_client *client)
+{
+     const struct nct7201_adc_model_data *model_data;
        struct device *dev = &client->dev;

drivers/iio/adc/nct7201.c line 419

Quoted 7 lines
drivers/iio/adc/nct7201.c new file +7 -0
+     struct nct7201_chip_info *chip;
+     struct iio_dev *indio_dev;
+     int ret;
+
+     model_data = i2c_get_match_data(client);
+     if (!model_data)
+             return -EINVAL;
Quoted 2 lines

ENODEV is more suitable here.

+ return -ENODEV

Quoted 18 lines
Quoted 17 lines
drivers/iio/adc/nct7201.c new file +17 -0
+
+     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
+     if (!indio_dev)
+             return -ENOMEM;
+     chip = iio_priv(indio_dev);
+
+     chip->regmap = devm_regmap_init_i2c(client, &nct7201_regmap8_config);
+     if (IS_ERR(chip->regmap))
+             return dev_err_probe(&client->dev, PTR_ERR(chip->regmap),
+                                  "Failed to init regmap\n");
+
+     chip->regmap16 = devm_regmap_init_i2c(client, &nct7201_regmap16_config);
+     if (IS_ERR(chip->regmap16))
+             return dev_err_probe(&client->dev, PTR_ERR(chip->regmap16),
+                                  "Failed to init regmap16\n");
+
+     chip->num_vin_channels = model_data->num_vin_channels;
Quoted 4 lines
Quoted 1 line

+ chip->client = client;

How exactly is _client_ used elsewhere? Shouldn't it be just a struct device pointer?

Yes, it is just a struct device pointer.
In nct7201_init_chip(chip), we would use the chip->client->dev as dev_err_probe() parameter

Quoted 7 lines

drivers/iio/adc/nct7201.c line 441

Quoted 3 lines
drivers/iio/adc/nct7201.c new file +2 -0
+     ret = nct7201_init_chip(chip);
+     if (ret < 0)

Do you expect positive returned values? What is their meaning? Why do you skip them?

No, we don't expect positive return values.

Quoted 19 lines

drivers/iio/adc/nct7201.c line 454

Quoted 13 lines
drivers/iio/adc/nct7201.c new file +13 -0
+             return ret;
+
+     indio_dev->name = model_data->model_name;
+     indio_dev->channels = model_data->channels;
+     indio_dev->num_channels = model_data->num_channels;
+     if (client->irq)
+             indio_dev->info = &nct7201_info;
+     else
+             indio_dev->info = &nct7201_info_no_irq;
+     indio_dev->modes = INDIO_DIRECT_MODE;
+
+     return devm_iio_device_register(&client->dev, indio_dev);
+}

--
With Best Regards,
Andy Shevchenko

#6 lore raw

Re: [PATCH v5 2/2] iio: adc: add support for Nuvoton NCT7201

Quoted 2 lines · Yu-Hsian Yang

Andy Shevchenko <[email protected]> 於 2025年4月10日 週四 上午12:19寫道:

Quoted 1 line · Eason Yang

...

Quoted 6 lines
Quoted 2 lines

Here and elsewhere why ' < 0' is used? Do you expect positive return values from those?

In regmap_read function, A value of zero will be returned on success, a negative errno will be returned in error cases.
We don't have a positive return case.

So, can we remove ' < 0' parts where they are not required, please?

...

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ chip->client = client;

How exactly is _client_ used elsewhere? Shouldn't it be just a struct device pointer?

Yes, it is just a struct device pointer.
In nct7201_init_chip(chip), we would use the chip->client->dev as dev_err_probe() parameter

Just save there struct device *dev and use chip->dev instead.

Signature · 2 lines
With Best Regards,
Andy Shevchenko