All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Mugilraj Dhavachelvan <dmugil2000@gmail.com>,
	Dragos.Bogdan@analog.com, Darius.Berghe@analog.com,
	Rob Herring <robh+dt@kernel.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	Guenter Roeck <linux@roeck-us.net>,
	Chris Packham <chris.packham@alliedtelesis.co.nz>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Alexandru Ardelean <alexandru.ardelean@analog.com>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH 2/2] iio: potentiometer: Add driver support for AD5110
Date: Sat, 7 Aug 2021 14:11:28 +0200	[thread overview]
Message-ID: <53306463-668e-e291-4539-caca2352ea05@metafoo.de> (raw)
In-Reply-To: <20210807050900.10075-3-dmugil2000@gmail.com>

On 8/7/21 7:08 AM, Mugilraj Dhavachelvan wrote:
> The AD5110/AD5112/AD5114 provide a nonvolatile solution
> for 128-/64-/32-position adjustment applications, offering
> guaranteed low resistor tolerance errors of ±8% and up to
> ±6 mA current density.
>
> Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/AD5110_5112_5114.pdf
> Signed-off-by: Mugilraj Dhavachelvan <dmugil2000@gmail.com>

Thanks for the patch. This looks really good!


> [...]
>
> +static int ad5110_write(struct ad5110_data *data, u8 cmd, u8 val)
> +{
> +	int ret;
> +
> +	mutex_lock(&data->lock);
> +	data->buf[0] = cmd;
> +	data->buf[1] = val;
> +
> +	ret = i2c_master_send_dmasafe(data->client, data->buf, sizeof(data->buf));
> +	mutex_unlock(&data->lock);

This returns the number of bytes written, which can be less then the 
number of bytes requested if there was an error. This should check if 
the right amount of bytes was written and return -EIO otherwise. Same 
for the other places that read/write from I2C.

> +
> +	return ret < 0 ? ret : 0;
> +}
> +
> +static int ad5110_resistor_tol(struct ad5110_data *data, u8 cmd, int val)
> +{
> +	int ret;
> +
> +	ret = ad5110_read(data, cmd, &val);
> +	if (ret)
> +		return ret;
> +
> +	data->tol = FIELD_GET(GENMASK(6, 3), val);
> +	data->tol = ((val & GENMASK(2, 0)) * 1000 / 8) + data->tol * 1000;
> +	data->tol = data->cfg->kohms * data->tol / 100;

This is not wrong, but it can be implemented a bit simpler. The 
tolerance is encoded as a fixed point number, you don't have to treat 
them as two independent fields, but can treat it as one fixed point number.

     data->tol = data->cfg->kohms * (val & GENMASK(6, 0)) * 1000 / 100 / 8;


> +	if (!(val & BIT(7)))
> +		data->tol *= -1;
> +
> +	return 0;
> +}
> +
> +static ssize_t ad5110_eeprom_read(struct device *dev,
> +					  struct device_attribute *attr,
> +					  char *buf)
> +{
> +	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> +	struct ad5110_data *data = iio_priv(indio_dev);
> +	int val = AD5110_WIPER_POS;
> +	int ret;
> +
> +	ret = ad5110_read(data, AD5110_EEPROM_RD, &val);
> +	if (ret)
> +		return ret;
Maybe apply shift to get consistent behavior with `raw`.
> +
> +	return iio_format_value(buf, IIO_VAL_INT, 1, &val);
> +}
> +
> +static ssize_t ad5110_eeprom_write(struct device *dev,
> +					   struct device_attribute *attr,
> +					   const char *buf, size_t len)
> +{
> +	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> +	struct ad5110_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	ret = ad5110_write(data, AD5110_EEPROM_WR, 0);
> +	if (ret) {
> +		dev_err(&data->client->dev, "RDAC to EEPROM write failed\n");
> +		return ret;
> +	}
> +	msleep(20);
> +
> +	return len;
> +}
> +
> +static IIO_DEVICE_ATTR(wiper_pos_eeprom, 0644,
> +		       ad5110_eeprom_read,
> +		       ad5110_eeprom_write, 0);
This is new custom ABI and needs to be documented.
> +static int ad5110_write_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int val, int val2, long mask)
> +{
> +	struct ad5110_data *data = iio_priv(indio_dev);
> +	int ret;
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		if (val >= data->cfg->max_pos || val < 0)
val == data->cfg->max_pos is a valid setting. Writing max_pos puts it in 
top-scale mode which gives maximum resistance.
> +			return -EINVAL;
> +
> +		return ad5110_write(data, AD5110_RDAC_WR, val << data->cfg->shift);
> +	case IIO_CHAN_INFO_ENABLE:
> +		if (val < 0 || val > 1)
> +			return -EINVAL;
> +		if (data->enable == val)
> +			return 0;
> +		ret = ad5110_write(data, AD5110_SHUTDOWN, val);
Doesn't val have to be inverted to get the right behavior?
> +		if (ret)
> +			return ret;
> +		data->enable = val;
> +		return 0;
> +	default:
> +		return -EINVAL;
> +	}
> +}
[...]

  reply	other threads:[~2021-08-07 12:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-07  5:08 [PATCH 0/2] iio: potentiometer: Add driver support for AD5110 Mugilraj Dhavachelvan
2021-08-07  5:08 ` [PATCH 1/2] dt-bindings: iio: potentiometer: Add AD5110 in trivial-devices Mugilraj Dhavachelvan
2021-08-07  5:08 ` [PATCH 2/2] iio: potentiometer: Add driver support for AD5110 Mugilraj Dhavachelvan
2021-08-07 12:11   ` Lars-Peter Clausen [this message]
2021-08-07 17:26     ` Mugilraj Dhavachelvan
2021-08-07 17:34       ` Mugilraj Dhavachelvan
2021-08-08 16:22         ` Jonathan Cameron
2021-08-07 12:17   ` Lars-Peter Clausen
2021-08-07 17:28     ` Mugilraj Dhavachelvan

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=53306463-668e-e291-4539-caca2352ea05@metafoo.de \
    --to=lars@metafoo.de \
    --cc=Darius.Berghe@analog.com \
    --cc=Dragos.Bogdan@analog.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=alexandru.ardelean@analog.com \
    --cc=chris.packham@alliedtelesis.co.nz \
    --cc=devicetree@vger.kernel.org \
    --cc=dmugil2000@gmail.com \
    --cc=jic23@kernel.org \
    --cc=krzk@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=robh+dt@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.