From: Guenter Roeck <linux@roeck-us.net>
To: Antoni Pokusinski <apokusinski01@gmail.com>
Cc: jdelvare@suse.com, linux-hwmon@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] hwmon: (sht4x): add heater support
Date: Wed, 28 Aug 2024 09:35:28 -0700 [thread overview]
Message-ID: <e9535722-6e16-4927-9aa0-974c4028537f@roeck-us.net> (raw)
In-Reply-To: <20240828160511.307768-1-apokusinski01@gmail.com>
On 8/28/24 09:05, Antoni Pokusinski wrote:
> Hello,
>
> I've been thinking on how to approach the problem of NACKs received
> from the sensor while the heater is on but I haven't found
> a perfectly satisfying solution either. This is due to the fact that
> the device does not provide any way to check if the heater is on/off.
>
> 1. I guess that the simplest possible approach would involve sleeping
> in `heater_enable_store()`:
>
> ssize_t heater_enable_store() {
> ...
> mutex_lock(data->lock);
> ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
> msleep(...) /* for >100 or >1000 ms */
> mutex_unlock(data->lock);
> ...
> }
>
> This way, the user would have to wait for the heating to complete in
> order to read RH or temperature measurement. However, I find this
> solution unacceptable since it's completely unnecessary for the user
> to wait for the heating to complete.
>
> 2. A better solution could be possibly to use a wait queue in order
> to defer the job of enabling the heater:
>
> struct sht4x_data {
> ...
> struct work_struct* heater_work; /* This would be initialized
> with the handler described
> below */
> }
>
> The task of sending the "heater_enable" command and sleeping would be
> performed by the worker function:
>
> static void heater_enable_handler(struct work_struct *work) {
> ...
> mutex_lock(data->lock);
> ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
> msleep(...) /* for >100 or >1000 ms */
> mutex_unlock(data->lock);
> ...
> }
>
> And that above mentioned work would be scheduled
> in `heater_enable_store()`:
>
> ssize_t heater_enable_store() {
> ...
> schedule_work(data->heater_work);
> ...
> }
>
> I think that this approach with work queue is better since the user
> doesn't have to wait for the heating to complete and the RH or
> temperature measurements can also be retrieved without the NACK error
> (even though the user still may have to wait for the heater to be
> off), since the `data->lock` mutex is used to guard both measurement
> reads from the sensor and the heating in `heater_enable_handler`.
>
> I'm worried though about the situation where the user writes 1 to
> "heater_enable" while it's already enabled. Since the `work_struct`
> is already on the queue, the `heater_enable_store` would return an
> error and I see no easy solution to this for now.
>
Introducing a worker seems to be overly complicated to me. You could
store the time when heating is expected to be complete, and use that time
in the read function to determine if it needs to wait and/or if it should
trigger another RH read request instead of just reading the RH measurement
from the heater command. Something like
if (still heating)
wait for heater to be turned off and measurement completion
if (heater was active within [select period of time])
read RH directly without additional request
clear heater status
else
if RH was requested recently
report previous RH
else
request RH measurement
read RH
This would also help with the heater_enable problem: It would just do nothing
if the heater is still enabled from the previous command (or maybe return -EBUSY).
If there is some additional requirement by the chip, such as that the heater must
not be enabled for more than X% of the time, that information could also be used
to determine if it can be enabled again.
Thanks,
Guenter
prev parent reply other threads:[~2024-08-28 16:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-26 12:58 Antoni Pokusinski
2024-08-26 18:37 ` Guenter Roeck
2024-08-28 16:05 ` Antoni Pokusinski
2024-08-28 16:35 ` Guenter Roeck [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=e9535722-6e16-4927-9aa0-974c4028537f@roeck-us.net \
--to=linux@roeck-us.net \
--cc=apokusinski01@gmail.com \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®