[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <e9535722-6e16-4927-9aa0-974c4028537f@roeck-us.net>
Date: Wed, 28 Aug 2024 09:35:28 -0700
From: Guenter Roeck <linux@...ck-us.net>
To: Antoni Pokusinski <apokusinski01@...il.com>
Cc: jdelvare@...e.com, linux-hwmon@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3] hwmon: (sht4x): add heater support
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
Powered by blists - more mailing lists