From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Eason Yang <j2anfernee@gmail.com>
Cc: jic23@kernel.org, lars@metafoo.de, robh@kernel.org,
krzk+dt@kernel.org, conor+dt@kernel.org, dlechner@baylibre.com,
nuno.sa@analog.com, javier.carrasco.cruz@gmail.com,
gstols@baylibre.com, tgamblin@baylibre.com,
alisadariana@gmail.com, antoniu.miclaus@analog.com,
eblanc@baylibre.com, jstephan@baylibre.com,
matteomartelli3@gmail.com,
angelogioacchino.delregno@collabora.com,
herve.codina@bootlin.com, marcelo.schmitt@analog.com,
chanh@os.amperecomputing.com, KWLIU@nuvoton.com,
yhyang2@nuvoton.com, linux-iio@vger.kernel.org,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/2] iio: adc: add support for Nuvoton NCT7201
Date: Wed, 9 Apr 2025 19:19:30 +0300 [thread overview]
Message-ID: <Z_aeEuIk9brES6dM@smile.fi.intel.com> (raw)
In-Reply-To: <20250409012351.2543450-3-j2anfernee@gmail.com>
On Wed, Apr 09, 2025 at 09:23:51AM +0800, Eason Yang wrote:
> 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.
> +#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>
...
> +#define NCT7201_VIN_MAX 12
Is this in volts? Can you add a unit suffix?
...
> +#define NCT7201_IN_SCALING 4995
Interesting number, just want to confirm it's indeed 4995 and not 4095.
...
> +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) {
> + 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?
> + *val = FIELD_GET(NCT7201_REG_VIN_MASK, value);
> +
> + return IIO_VAL_INT;
> +}
...
> +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.
> + 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));
> + 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;
> +}
...
> +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);
> + 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.
> +}
...
> +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?
> + 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?
> + "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()?
> + 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?
> + "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?
> + "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.
> + "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.
> + 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?
> +}
...
> +static int nct7201_probe(struct i2c_client *client)
> +{
> + const struct nct7201_adc_model_data *model_data;
struct device *dev = &client->dev;
> + 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.
> +
> + 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;
> + chip->client = client;
How exactly is _client_ used elsewhere? Shouldn't it be just a struct device
pointer?
> + ret = nct7201_init_chip(chip);
> + if (ret < 0)
Do you expect positive returned values? What is their meaning?
Why do you skip them?
> + 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
next prev parent reply other threads:[~2025-04-09 16:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-09 1:23 [PATCH v5 0/2] iio: adc: add Nuvoton NCT7201 ADC driver Eason Yang
2025-04-09 1:23 ` [PATCH v5 1/2] dt-bindings: iio: adc: add NCT7201 ADCs Eason Yang
2025-04-09 5:55 ` Krzysztof Kozlowski
2025-04-09 1:23 ` [PATCH v5 2/2] iio: adc: add support for Nuvoton NCT7201 Eason Yang
2025-04-09 16:19 ` Andy Shevchenko [this message]
2025-04-14 13:40 ` Yu-Hsian Yang
2025-04-15 8:26 ` Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z_aeEuIk9brES6dM@smile.fi.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=KWLIU@nuvoton.com \
--cc=alisadariana@gmail.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=antoniu.miclaus@analog.com \
--cc=chanh@os.amperecomputing.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=eblanc@baylibre.com \
--cc=gstols@baylibre.com \
--cc=herve.codina@bootlin.com \
--cc=j2anfernee@gmail.com \
--cc=javier.carrasco.cruz@gmail.com \
--cc=jic23@kernel.org \
--cc=jstephan@baylibre.com \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcelo.schmitt@analog.com \
--cc=matteomartelli3@gmail.com \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=tgamblin@baylibre.com \
--cc=yhyang2@nuvoton.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox