From: Dimitri Fedrau <dima.fedrau@gmail.com>
To: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Jonathan Cameron <jic23@kernel.org>,
Javier Carrasco <javier.carrasco.cruz@gmail.com>,
Li peiyu <579lpy@gmail.com>, Lars-Peter Clausen <lars@metafoo.de>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] iio: humidity: hdc3020: add threshold events support
Date: Mon, 5 Feb 2024 10:53:23 +0100 [thread overview]
Message-ID: <20240205095323.GA2323766@debian> (raw)
In-Reply-To: <20240205093349.00003e10@Huawei.com>
Am Mon, Feb 05, 2024 at 09:33:49AM +0000 schrieb Jonathan Cameron:
> > > > static const u8 HDC3020_S_AUTO_10HZ_MOD0[2] = { 0x27, 0x37 };
> > > >
> > > > +static const u8 HDC3020_S_STATUS[2] = { 0x30, 0x41 };
> > > > +
> > > > static const u8 HDC3020_EXIT_AUTO[2] = { 0x30, 0x93 };
> > > >
> > > > +static const u8 HDC3020_S_T_RH_THRESH_LOW[2] = { 0x61, 0x00 };
> > >
> > > Ah. missed this in original driver, but this use of capitals for
> > > non #defines is really confusing and we should aim to clean that
> > > up.
> > >
> > Could use small letters instead.
>
> That would avoid any confusion.
>
> >
> > > As I mention below, I'm unconvinced that it makes sense to handle
> > > these as pairs.
> > >
> > For the threshold I could convert it as it is for the heater registers:
> >
> > #define HDC3020_S_T_RH_THRESH_MSB 0x61
> > #define HDC3020_S_T_RH_THRESH_LOW 0x00
> > #define HDC3020_S_T_RH_THRESH_LOW_CLR 0x0B
> > #define HDC3020_S_T_RH_THRESH_HIGH_CLR 0x16
> > #define HDC3020_S_T_RH_THRESH_HIGH 0x1D
> >
> > #define HDC3020_R_T_RH_THRESH_MSB 0xE1
> > #define HDC3020_R_T_RH_THRESH_LOW 0x02
> > #define HDC3020_R_T_RH_THRESH_LOW_CLR 0x09
> > #define HDC3020_R_T_RH_THRESH_HIGH_CLR 0x14
> > #define HDC3020_R_T_RH_THRESH_HIGH 0x1F
> >
> > or:
> >
> > #define HDC3020_S_T_RH_THRESH_LOW 0x6100
> > #define HDC3020_S_T_RH_THRESH_LOW_CLR 0x610B
> > #define HDC3020_S_T_RH_THRESH_HIGH_CLR 0x6116
> > #define HDC3020_S_T_RH_THRESH_HIGH 0x611D
> >
> > #define HDC3020_R_T_RH_THRESH_LOW 0x6102
> > #define HDC3020_R_T_RH_THRESH_LOW_CLR 0x6109
> > #define HDC3020_R_T_RH_THRESH_HIGH_CLR 0x6114
> > #define HDC3020_R_T_RH_THRESH_HIGH 0x611F
> >
> > I don't know if it's a good idea, as we would need to make sure it is
> > big endian in the buffer. Probably with a function that handles this.
> I think this is the best plan with a
> put_unaligned_be16() to deal with the endianness.
> The compiler should be able to optimize that heavily.
>
I think that would require some refactoring. I would add patches that
are fixing this. Have there been reasons for using the pairs ? I'm just
curious.
>
> > > > +static int hdc3020_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 hdc3020_data *data = iio_priv(indio_dev);
> > > > + u16 *thresh;
> > > > +
> > > > + /* Select threshold */
> > > > + if (info == IIO_EV_INFO_VALUE) {
> > > > + if (dir == IIO_EV_DIR_RISING)
> > > > + thresh = &data->t_rh_thresh_high;
> > > > + else
> > > > + thresh = &data->t_rh_thresh_low;
> > > > + } else {
> > > > + if (dir == IIO_EV_DIR_RISING)
> > > > + thresh = &data->t_rh_thresh_high_clr;
> > > > + else
> > > > + thresh = &data->t_rh_thresh_low_clr;
> > > > + }
> > > > +
> > > > + guard(mutex)(&data->lock);
> > >
> > > Why take the lock here?
> > >
> > > you are relying on a single value that is already cached.
> > >
> > A single threshold value is used for humidity and temperature values. I
> > didn't see a lock in "iio_ev_value_show", so there might be some
> > concurrent access triggered by "in_temp_thresh_rising_value" and
> > "in_humidityrelative_thresh_rising_value" sysfs files which is not
> > secured by a mutex or similiar.
>
> Unless you going to get value tearing (very unlikely and lots of the
> kernel assumes that won't happen - more of a theoretical possibility
> that we don't want compilers to do!) this just protects against a race
> where you read one and write the other. That doesn't really help us
> as it just moves the race to which one gets the lock first.
>
Yes, it's very unlikely to happen. Anyway, I'm dropping the support for
the caching and with it this function.
Dimitri
next prev parent reply other threads:[~2024-02-05 9:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-04 10:37 [PATCH v2] iio: humidity: hdc3020: add threshold events support Dimitri Fedrau
2024-02-04 11:26 ` Javier Carrasco
2024-02-04 12:38 ` Dimitri Fedrau
2024-02-04 14:43 ` Jonathan Cameron
2024-02-05 7:04 ` Dimitri Fedrau
2024-02-05 9:33 ` Jonathan Cameron
2024-02-05 9:53 ` Dimitri Fedrau [this message]
2024-02-10 16:11 ` Jonathan Cameron
2024-02-12 15:20 ` Dimitri Fedrau
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=20240205095323.GA2323766@debian \
--to=dima.fedrau@gmail.com \
--cc=579lpy@gmail.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=javier.carrasco.cruz@gmail.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.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.