From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: ARC-Seal: i=1; a=rsa-sha256; t=1524333875; cv=none; d=google.com; s=arc-20160816; b=g+70M+DcsearwQ8QRBzSOD4fC6SHRipF+G/rTv47GaF6NmIDHwvmYceKzVmqoLlM7D IGfpz3hZMoh8Mus0r2IG1hdj/pcm9jQOnN67raO+Kp9gFBaJeU9ZBTVx6jVKcvApUm3E TzW7rbKZ4X7kzmLTKgb9llQP/M+T7XcPrLqoS9ODX8kr5rmKrJHSoWsVAhC9f+Bd9HRv 1+4W0mPjLNazKqFAF33HYvBri1Cj0ReFbqnW33s/4dehDGi7irNMZxn1WLkk9jvnSwlC uGogs518sKYIBvbQgSFxjDaOGDl+lCt1dhzWF9z2U7qkMYdOTq9wBIknLvd45/ueW3nK 7hMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:dkim-signature:arc-authentication-results; bh=QM4oF3Ls1g8m7r3CMibkDPy1LldhwF6dEQTRqH84msU=; b=YMUdvLR8Jc834noqneBFBuDNZ+cgs1cdJB7ziDnCxmjTJqgNyGut7oS5luw76QUa0k M6FsPCQ3pHqCNhyOWIlAD9fBMosuN/y9MewZyHSdsoTbyNgM51h0GEUHMyexzpa9I9Jz L/VlaOwvMa050KQU5Zv8wGsN9FoXVr1Md9oV2AqMwYBfuD7fzS+tsT5EtYP/pY/U+7Ca la7jLJ3LqmNjFizEGMdCtr6j9J0SmLDglRfIdRrQBv5W0ZIOalKqMXPim9JIs0/kXyGz Zpx7ZD+gY7rGUIzQ63SAH21Bhq8UW+1Q2z7e/DpZqHkUjVgnAeyGIjXL3jXXXER7T7Qw IqNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=slryGsD9; spf=pass (google.com: domain of john3909@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=john3909@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=slryGsD9; spf=pass (google.com: domain of john3909@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=john3909@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Google-Smtp-Source: AIpwx49iOBRTmgd9B+Wp0iNCJJlPQCmVuZI8aX8hxbvXHVPv8K94nKzDMQS72AKkjI6P14ezNWbBSw== From: John Syne Message-Id: <7D2111AF-E3C0-479E-9AC2-F91F9BE9316B@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_A431DB94-1476-4891-AAB2-9C29A07C0133" Mime-Version: 1.0 (Mac OS X Mail 11.3 \(3445.6.18\)) Subject: Re: [PATCH 3/3] stagging:iio:meter: Add ade7854_write_raw function Date: Sat, 21 Apr 2018 11:04:26 -0700 In-Reply-To: <20180421182608.3371a50b@archlinux> Cc: Rodrigo Siqueira , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Greg Kroah-Hartman , linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org To: Jonathan Cameron References: <82a36a0f4ee84d8ff53be2055bad22be4b75634a.1524311298.git.rodrigosiqueiramelo@gmail.com> <20180421182608.3371a50b@archlinux> X-Mailer: Apple Mail (2.3445.6.18) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1598356754160899697?= X-GMAIL-MSGID: =?utf-8?q?1598379917710792752?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: --Apple-Mail=_A431DB94-1476-4891-AAB2-9C29A07C0133 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Regards, John > On Apr 21, 2018, at 10:26 AM, Jonathan Cameron = wrote: >=20 > On Sat, 21 Apr 2018 08:56:19 -0300 > Rodrigo Siqueira > wrote: >=20 >> This patch adds the ade7854_write_raw() function which is responsible >> for handling the write operation for registers: AIGAIN, BIGAIN, = CIGAIN, >> NIGAIN, AVGAIN, BVGAIN, and CVGAIN. Finally, this patch completely >> removes the old ABI used for handling the registers mentioned above. >>=20 >> Signed-off-by: Rodrigo Siqueira > > The baby steps approach here is good, but with all 3 patches as one it > would have been a lot easier to follow. >=20 > This is almost fine except for the issue I had with how the channels > were defined in the first place. >=20 > Also a question about scales inline... >=20 >> --- >> drivers/staging/iio/meter/ade7854.c | 60 = ++++++++++++----------------- >> 1 file changed, 25 insertions(+), 35 deletions(-) >>=20 >> diff --git a/drivers/staging/iio/meter/ade7854.c = b/drivers/staging/iio/meter/ade7854.c >> index 242ecde75900..df19c8b4b5d7 100644 >> --- a/drivers/staging/iio/meter/ade7854.c >> +++ b/drivers/staging/iio/meter/ade7854.c >> @@ -228,34 +228,6 @@ static int ade7854_reset(struct device *dev) >> return st->write_reg(dev, ADE7854_CONFIG, val, 16); >> } >>=20 >> -static IIO_DEV_ATTR_AIGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_AIGAIN); >> -static IIO_DEV_ATTR_BIGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_BIGAIN); >> -static IIO_DEV_ATTR_CIGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_CIGAIN); >> -static IIO_DEV_ATTR_NIGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_NIGAIN); >> -static IIO_DEV_ATTR_AVGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_AVGAIN); >> -static IIO_DEV_ATTR_BVGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_BVGAIN); >> -static IIO_DEV_ATTR_CVGAIN(0644, >> - NULL, >> - ade7854_write_24bit, >> - ADE7854_CVGAIN); >> static IIO_DEV_ATTR_APPARENT_POWER_A_GAIN(0644, >> ade7854_read_24bit, >> ade7854_write_24bit, >> @@ -497,6 +469,30 @@ static int ade7854_read_raw(struct iio_dev = *indio_dev, >> return -EINVAL; >> } >>=20 >> +static int ade7854_write_raw(struct iio_dev *indio_dev, >> + struct iio_chan_spec const *chan, >> + int val, int val2, long mask) >> +{ >> + struct ade7854_state *st =3D iio_priv(indio_dev); >> + int ret; >> + >> + if (mask !=3D IIO_CHAN_INFO_SCALE) >> + return -EINVAL; >> + >> + switch (chan->type) { >> + case IIO_CURRENT: >> + case IIO_VOLTAGE: > Probably need some range checking to make sure we have a sane value. >=20 > Also, I'm curious about units here. >=20 > you are using CHAN_INFO_SCALE which would mean this is linked to > the _RAW value (the actual reading of the channel) by _RAW*_SCALE =3D = value in > defined base units for the channel type. So I'd expect to see some > code here doing a conversion to the internal format for the device. These should be IIO_CHAN_INFO_HARDWAREGAIN as they are used to correct=20= any imperfections of the sensors. A user space application calculates = the error and based on a formula determines the correct register value.=20 Meter values such as Voltage, Current, Power, Energy use a value/LSB = method,=20 which is determined while applying a known voltage and current. After = that, any parameter is multiplied by the respective value/LSB to get a real world = value.=20 Regards, John >=20 > I wouldn't expect to see a value from userspace written unconverted > into the register. >=20 > (I missed this in the previous patch - sorry). >=20 > Jonathan >=20 >> + ret =3D st->write_reg(&indio_dev->dev, chan->address, = val, 24); >> + if (ret < 0) >> + return ret; >> + return 0; >> + default: >> + break; >> + } >> + >> + return -EINVAL; >> +} >> + >> static int ade7854_initial_setup(struct iio_dev *indio_dev) >> { >> int ret; >> @@ -521,13 +517,6 @@ static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("8000"); >> static IIO_CONST_ATTR(name, "ade7854"); >>=20 >> static struct attribute *ade7854_attributes[] =3D { >> - &iio_dev_attr_aigain.dev_attr.attr, >> - &iio_dev_attr_bigain.dev_attr.attr, >> - &iio_dev_attr_cigain.dev_attr.attr, >> - &iio_dev_attr_nigain.dev_attr.attr, >> - &iio_dev_attr_avgain.dev_attr.attr, >> - &iio_dev_attr_bvgain.dev_attr.attr, >> - &iio_dev_attr_cvgain.dev_attr.attr, >> &iio_dev_attr_linecyc.dev_attr.attr, >> &iio_dev_attr_sagcyc.dev_attr.attr, >> &iio_dev_attr_cfcyc.dev_attr.attr, >> @@ -599,6 +588,7 @@ static const struct attribute_group = ade7854_attribute_group =3D { >> static const struct iio_info ade7854_info =3D { >> .attrs =3D &ade7854_attribute_group, >> .read_raw =3D &ade7854_read_raw, >> + .write_raw =3D &ade7854_write_raw, >> }; >>=20 >> int ade7854_probe(struct iio_dev *indio_dev, struct device *dev) --Apple-Mail=_A431DB94-1476-4891-AAB2-9C29A07C0133 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

Regards,
John





On Apr 21, 2018, at 10:26 AM, Jonathan Cameron <jic23@kernel.org> = wrote:

On Sat, 21 Apr 2018 08:56:19 = -0300
Rodrigo = Siqueira <rodrigosiqueiramelo@gmail.com> wrote:

This patch adds the = ade7854_write_raw() function which is responsible
for = handling the write operation for registers: AIGAIN, BIGAIN, CIGAIN,
NIGAIN, AVGAIN, BVGAIN, and CVGAIN. Finally, this patch = completely
removes the old ABI used for handling the = registers mentioned above.

Signed-off-by: = Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
The baby steps approach here is good, but with all 3 patches = as one it
would have = been a lot easier to follow.

This is almost fine except for the issue I had with how the = channels
were defined = in the first place.

Also a question about scales inline...

---
drivers/staging/iio/meter/ade7854.c | 60 = ++++++++++++-----------------
1 file changed, 25 = insertions(+), 35 deletions(-)

diff --git = a/drivers/staging/iio/meter/ade7854.c = b/drivers/staging/iio/meter/ade7854.c
index = 242ecde75900..df19c8b4b5d7 100644
--- = a/drivers/staging/iio/meter/ade7854.c
+++ = b/drivers/staging/iio/meter/ade7854.c
@@ -228,34 +228,6 @@ = static int ade7854_reset(struct device *dev)
return = st->write_reg(dev, ADE7854_CONFIG, val, 16);
}

-static IIO_DEV_ATTR_AIGAIN(0644,
- = = NULL,
- ade7854_write_24bit,
- = = ADE7854_AIGAIN);
-static = IIO_DEV_ATTR_BIGAIN(0644,
- NULL,
- = ade7854_write_24bit,
- ADE7854_BIGAIN);
-static IIO_DEV_ATTR_CIGAIN(0644,
- NULL,
- = = ade7854_write_24bit,
- ADE7854_CIGAIN);
-static IIO_DEV_ATTR_NIGAIN(0644,
- NULL,
- = = ade7854_write_24bit,
- ADE7854_NIGAIN);
-static IIO_DEV_ATTR_AVGAIN(0644,
- NULL,
- = = ade7854_write_24bit,
- ADE7854_AVGAIN);
-static IIO_DEV_ATTR_BVGAIN(0644,
- NULL,
- = = ade7854_write_24bit,
- ADE7854_BVGAIN);
-static IIO_DEV_ATTR_CVGAIN(0644,
- NULL,
- = = ade7854_write_24bit,
- ADE7854_CVGAIN);
static IIO_DEV_ATTR_APPARENT_POWER_A_GAIN(0644,
= = ade7854_read_24bit,
ade7854_write_24bit,
@@ -497,6 +469,30 @@ static int ade7854_read_raw(struct = iio_dev *indio_dev,
return -EINVAL;
}

+static int ade7854_write_raw(struct iio_dev = *indio_dev,
+      struc= t iio_chan_spec const *chan,
+      int = val, int val2, long mask)
+{
+ struct = ade7854_state *st =3D iio_priv(indio_dev);
+ int = ret;
+
+ if (mask !=3D = IIO_CHAN_INFO_SCALE)
+ return -EINVAL;
+
+ = switch (chan->type) {
+ case = IIO_CURRENT:
+ case IIO_VOLTAGE:
Probably need some range checking to make sure we have a sane = value.

Also, I'm = curious about units here.

you are using CHAN_INFO_SCALE which would mean this is linked = to
the _RAW = value (the actual reading of the channel) by _RAW*_SCALE =3D value = in
defined base = units for the channel type.  So I'd expect to see some
code here doing a conversion to = the internal format for the device.
These should be = IIO_CHAN_INFO_HARDWAREGAIN as they are used to = correct 
any imperfections of the sensors. A user space = application calculates the error and
based on a formula = determines the correct register value. 

Meter values such as Voltage, Current, Power, = Energy use a value/LSB method, 
which is determined while = applying a known voltage and current. After that, = any
parameter is multiplied by the respective value/LSB to get = a real world value. 

Regards,
John

I wouldn't expect to see a value from userspace written = unconverted
into the = register.

(I missed = this in the previous patch - sorry).

Jonathan

+ ret =3D = st->write_reg(&indio_dev->dev, chan->address, val, 24);
+ = = if (ret < 0)
+ return ret;
+ return = 0;
+ default:
+ break;
+ }
+
+ return -EINVAL;
+}+
static int ade7854_initial_setup(struct = iio_dev *indio_dev)
{
int = ret;
@@ -521,13 +517,6 @@ static = IIO_CONST_ATTR_SAMP_FREQ_AVAIL("8000");
static = IIO_CONST_ATTR(name, "ade7854");

static = struct attribute *ade7854_attributes[] =3D {
- = &iio_dev_attr_aigain.dev_attr.attr,
- = &iio_dev_attr_bigain.dev_attr.attr,
- = &iio_dev_attr_cigain.dev_attr.attr,
- = &iio_dev_attr_nigain.dev_attr.attr,
- = &iio_dev_attr_avgain.dev_attr.attr,
- = &iio_dev_attr_bvgain.dev_attr.attr,
- = &iio_dev_attr_cvgain.dev_attr.attr,
= &iio_dev_attr_linecyc.dev_attr.attr,
= &iio_dev_attr_sagcyc.dev_attr.attr,
= &iio_dev_attr_cfcyc.dev_attr.attr,
@@ -599,6 = +588,7 @@ static const struct attribute_group ade7854_attribute_group =3D = {
static const struct iio_info ade7854_info =3D {
= .attrs =3D &ade7854_attribute_group,
.read_raw = =3D &ade7854_read_raw,
+ .write_raw =3D = &ade7854_write_raw,
};

int = ade7854_probe(struct iio_dev *indio_dev, struct device = *dev)

= --Apple-Mail=_A431DB94-1476-4891-AAB2-9C29A07C0133--