From: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
To: Antoni Pokusinski <apokusinski01@gmail.com>
Cc: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
andy@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] iio: mpl3115: add threshold events support
Date: Wed, 29 Oct 2025 14:13:43 -0300 [thread overview]
Message-ID: <aQJLR-DrdVRQ3dc1@debian-BULLSEYE-live-builder-AMD64> (raw)
In-Reply-To: <20251028213351.77368-3-apokusinski01@gmail.com>
...
> +static int mpl3115_read_event_config(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir)
> +{
> + struct mpl3115_data *data = iio_priv(indio_dev);
> + u8 int_en_mask;
> +
> + switch (chan->type) {
> + case IIO_PRESSURE:
> + int_en_mask = MPL3115_CTRL4_INT_EN_PTH;
> + break;
Usual convention in IIO drivers is to return early whenever possible
return !!(data->ctrl_reg4 & MPL3115_CTRL4_INT_EN_PTH);
> + case IIO_TEMP:
> + int_en_mask = MPL3115_CTRL4_INT_EN_TTH;
> + break;
same here
return !!(data->ctrl_reg4 & MPL3115_CTRL4_INT_EN_TTH);
> + default:
> + return -EINVAL;
> + }
> +
> + return !!(data->ctrl_reg4 & int_en_mask);
> +}
> +
...
> +static int mpl3115_read_thresh(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 mpl3115_data *data = iio_priv(indio_dev);
> + int ret, press_pa;
> + __be16 tmp;
> +
> + if (info != IIO_EV_INFO_VALUE)
> + return -EINVAL;
> +
> + switch (chan->type) {
> + case IIO_PRESSURE:
> + ret = i2c_smbus_read_i2c_block_data(data->client,
> + MPL3115_PRESS_TGT, 2,
> + (u8 *) &tmp);
> + if (ret < 0)
> + return ret;
> +
> + /**
> + * Target value for the pressure is
> + * 16-bit unsigned value in 2 Pa units
> + */
> + press_pa = be16_to_cpu(tmp) << 1;
> + *val = press_pa / KILO;
> + *val2 = (press_pa % KILO) * MILLI;
> +
> + return IIO_VAL_INT_PLUS_MICRO;
Looks like this is intended to provide the value in kilopascal. Though, as
specified by pressure_raw ABI [1], we only get to kilopascal after applying
channel _offset and _scale. So, this would have to use _input ABI [2], or
provide a value that can be scaled to kilopascal.
If I'm not missing anything,
*val = be16_to_cpu(tmp) << 1;
return IIO_VAL_INT;
would comply with the _raw ABI.
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/tree/Documentation/ABI/testing/sysfs-bus-iio?h=testing#n397
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/tree/Documentation/ABI/testing/sysfs-bus-iio?h=testing#n1061
> + case IIO_TEMP:
> + ret = i2c_smbus_read_byte_data(data->client, MPL3115_TEMP_TGT);
> + if (ret < 0)
> + return ret;
> +
> + /* Target value for the temperature is 8-bit 2's complement */
> + *val = sign_extend32(ret, 7);
> +
> + return IIO_VAL_INT;
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static int mpl3115_write_thresh(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 mpl3115_data *data = iio_priv(indio_dev);
> + u8 tmp[2];
> +
> + if (info != IIO_EV_INFO_VALUE)
> + return -EINVAL;
> +
> + switch (chan->type) {
> + case IIO_PRESSURE:
> + val = (val * KILO + val2 / MILLI) >> 1;
same here. You seem to want to use the _input ABI.
Or, if you chose to use the _raw ABI, take the raw threshold value
val = val >> 1;
if (val < 0 || val > 0xffff)
return -EINVAL;
...
Might also use a local variable to hold the adjusted val >> 1 pressure threshold.
You may also add docs for those. e.g.
What: /sys/.../iio:deviceX/events/in_pressure_thresh_rising_en
and
What: /sys/.../events/in_pressure_raw_thresh_rising_value
to ABI documentation.
The ABI doc would probably be best appreciated in a separate patch.
> +
> + if (val < 0 || val > 0xffff)
> + return -EINVAL;
> +
> + tmp[0] = FIELD_GET(GENMASK(15, 8), val);
> + tmp[1] = FIELD_GET(GENMASK(7, 0), val);
> +
> + return i2c_smbus_write_i2c_block_data(data->client,
> + MPL3115_PRESS_TGT, 2, tmp);
With best regards,
Marcelo
prev parent reply other threads:[~2025-10-29 17:12 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-28 21:33 [PATCH 0/2] iio: mpl3115: support for events Antoni Pokusinski
2025-10-28 21:33 ` [PATCH 1/2] iio: mpl3115: add ctrl_reg4 to mpl3115_data Antoni Pokusinski
2025-10-29 17:20 ` Marcelo Schmitt
2025-10-28 21:33 ` [PATCH 2/2] iio: mpl3115: add threshold events support Antoni Pokusinski
2025-10-29 8:24 ` Andy Shevchenko
2025-10-29 22:46 ` Antoni Pokusinski
2025-10-30 9:18 ` Andy Shevchenko
2025-10-29 17:13 ` Marcelo Schmitt [this message]
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=aQJLR-DrdVRQ3dc1@debian-BULLSEYE-live-builder-AMD64 \
--to=marcelo.schmitt1@gmail.com \
--cc=andy@kernel.org \
--cc=apokusinski01@gmail.com \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.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