From: Jonathan Cameron <jic23@kernel.org>
To: Dumitru Ceclan <mitrutzceclan@gmail.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
Rob Herring <robh+dt@kernel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
Conor Dooley <conor+dt@kernel.org>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org,
Ceclan Dumitru <dumitru.ceclan@analog.com>
Subject: Re: [PATCH v4 3/5] iio: amplifiers: hmc425a: move conversion logic
Date: Sun, 21 Jan 2024 17:07:59 +0000 [thread overview]
Message-ID: <20240121170759.7e4488dd@jic23-huawei> (raw)
In-Reply-To: <20240117125124.8326-4-mitrutzceclan@gmail.com>
On Wed, 17 Jan 2024 14:51:12 +0200
Dumitru Ceclan <mitrutzceclan@gmail.com> wrote:
> Move gain-dB<->code conversion logic from read_raw and write_raw to
> hmc425a_gain_dB_to_code() and hmc425a_code_to_gain_dB().
>
> Signed-off-by: Dumitru Ceclan <mitrutzceclan@gmail.com>
Some comments inline
Jonathan
> ---
> drivers/iio/amplifiers/hmc425a.c | 102 ++++++++++++++++++-------------
> 1 file changed, 59 insertions(+), 43 deletions(-)
>
> diff --git a/drivers/iio/amplifiers/hmc425a.c b/drivers/iio/amplifiers/hmc425a.c
> index ed4d72922696..e1162a500daf 100644
> --- a/drivers/iio/amplifiers/hmc425a.c
> +++ b/drivers/iio/amplifiers/hmc425a.c
> @@ -56,35 +56,72 @@ static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
> return 0;
> }
>
> +static int hmc425a_gain_dB_to_code(struct hmc425a_state *st, int val, int val2, int *code)
> +{
> + struct hmc425a_chip_info *inf = st->chip_info;
> + int gain, temp;
> +
> + if (val < 0)
> + gain = (val * 1000) - (val2 / 1000);
> + else
> + gain = (val * 1000) + (val2 / 1000);
> +
> + if (gain > inf->gain_max || gain < inf->gain_min)
> + return -EINVAL;
> +
> + switch (st->type) {
> + case ID_HMC425A:
> + *code = ~((abs(gain) / 500) & 0x3F);
In the next patch I point out that this should be data or callbacks in in
the st->chip_info structure, not encoded in code here based on st->type (which
I want you to get rid of!)
> + return 0;
> + case ID_HMC540S:
> + *code = ~((abs(gain) / 1000) & 0xF);
> + return 0;
> + case ID_ADRF5740:
> + temp = (abs(gain) / 2000) & 0xF;
> + *code = temp & BIT(3) ? temp | BIT(2) : temp;
Given you are moving the code, a comment here might be nice as it's unusual
(bits are 2DB, 4DB, 8DB and another 8DB)
> + return 0;
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static int hmc425a_code_to_gain_dB(struct hmc425a_state *st, int *val, int *val2)
> +{
> + int code, gain;
> +
> + code = st->gain;
> + switch (st->type) {
> + case ID_HMC425A:
> + gain = ~code * -500;
> + break;
> + case ID_HMC540S:
> + gain = ~code * -1000;
> + break;
> + case ID_ADRF5740:
> + code = code & BIT(3) ? code & ~BIT(2) : code;
> + gain = code * -2000;
> + break;
> + }
> +
> + *val = gain / 1000;
> + *val2 = (gain % 1000) * 1000;
> +
> + return 0;
> +}
> +
> static int hmc425a_read_raw(struct iio_dev *indio_dev,
> struct iio_chan_spec const *chan, int *val,
> int *val2, long m)
> {
> struct hmc425a_state *st = iio_priv(indio_dev);
> - int code, gain = 0;
> int ret;
>
> mutex_lock(&st->lock);
> switch (m) {
> case IIO_CHAN_INFO_HARDWAREGAIN:
> - code = st->gain;
> -
> - switch (st->type) {
> - case ID_HMC425A:
> - gain = ~code * -500;
> - break;
> - case ID_HMC540S:
> - gain = ~code * -1000;
> + ret = hmc425a_code_to_gain_dB(st, val, val2);
> + if (ret)
> break;
> - case ID_ADRF5740:
> - code = code & BIT(3) ? code & ~BIT(2) : code;
> - gain = code * -2000;
> - break;
> - }
> -
> - *val = gain / 1000;
> - *val2 = (gain % 1000) * 1000;
> -
> ret = IIO_VAL_INT_PLUS_MICRO_DB;
> break;
> default:
> @@ -100,36 +137,15 @@ static int hmc425a_write_raw(struct iio_dev *indio_dev,
> int val2, long mask)
> {
> struct hmc425a_state *st = iio_priv(indio_dev);
> - struct hmc425a_chip_info *inf = st->chip_info;
> - int code = 0, gain;
> - int ret;
> -
> - if (val < 0)
> - gain = (val * 1000) - (val2 / 1000);
> - else
> - gain = (val * 1000) + (val2 / 1000);
> -
> - if (gain > inf->gain_max || gain < inf->gain_min)
> - return -EINVAL;
> -
> - switch (st->type) {
> - case ID_HMC425A:
> - code = ~((abs(gain) / 500) & 0x3F);
> - break;
> - case ID_HMC540S:
> - code = ~((abs(gain) / 1000) & 0xF);
> - break;
> - case ID_ADRF5740:
> - code = (abs(gain) / 2000) & 0xF;
> - code = code & BIT(3) ? code | BIT(2) : code;
> - break;
> - }
> + int code = 0, ret;
>
> mutex_lock(&st->lock);
> switch (mask) {
> case IIO_CHAN_INFO_HARDWAREGAIN:
> + ret = hmc425a_gain_dB_to_code(st, val, val2, &code);
> + if (ret)
> + break;
> st->gain = code;
> -
> ret = hmc425a_write(indio_dev, st->gain);
> break;
> default:
next prev parent reply other threads:[~2024-01-21 17:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-17 12:51 [PATCH v4 0/5] Add support for LTC6373 Dumitru Ceclan
2024-01-17 12:51 ` [PATCH v4 1/5] dt-bindings: iio: hmc425a: add conditional GPIO array size constraints Dumitru Ceclan
2024-01-17 12:51 ` [PATCH v4 2/5] dt-bindings: iio: hmc425a: add entry for LTC6373 Dumitru Ceclan
2024-01-17 12:51 ` [PATCH v4 3/5] iio: amplifiers: hmc425a: move conversion logic Dumitru Ceclan
2024-01-21 17:07 ` Jonathan Cameron [this message]
2024-01-17 12:51 ` [PATCH v4 4/5] iio: amplifiers: hmc425a: use pointers in match table Dumitru Ceclan
2024-01-21 16:57 ` Jonathan Cameron
2024-01-17 12:51 ` [PATCH v4 5/5] iio: amplifiers: hmc425a: add support for LTC6373 Instrumentation Amplifier Dumitru Ceclan
2024-01-21 17:14 ` Jonathan Cameron
2024-01-26 9:18 ` Petre Rodan
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=20240121170759.7e4488dd@jic23-huawei \
--to=jic23@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dumitru.ceclan@analog.com \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mitrutzceclan@gmail.com \
--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.