qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@kaod.org>
To: Peter Maydell <peter.maydell@linaro.org>, <qemu-arm@nongnu.org>,
	<qemu-devel@nongnu.org>
Subject: Re: [PATCH 2/2] tmp105: Correct handling of temperature limit checks
Date: Mon, 16 Nov 2020 18:00:31 +0100	[thread overview]
Message-ID: <24d24e3a-b594-c45e-309e-a1bb51586d6b@kaod.org> (raw)
In-Reply-To: <20201110150023.25533-3-peter.maydell@linaro.org>

On 11/10/20 4:00 PM, Peter Maydell wrote:
> The TMP105 datasheet says that in Interrupt Mode (when TM==1) the device
> signals an alert when the temperature equals or exceeds the T_high value and
> then remains high until a device register is read or the device responds to
> the SMBUS Alert Response address, or the device is put into Shutdown Mode.
> Thereafter the Alert pin will only be re-signalled when temperature falls
> below T_low; alert can then be cleared in the same set of ways, and the
> device returns to its initial "alert when temperature goes above T_high"
> mode. (If this textual description is confusing, see figure 3 in the
> TI datasheet at https://www.ti.com/lit/gpn/tmp105 .)
> 
> We were misimplementing this as a simple "always alert if temperature is
> above T_high or below T_low" condition, which gives a spurious alert on
> startup if using the "T_high = 80 degrees C, T_low = 75 degrees C" reset
> limit values.
> 
> Implement the correct (hysteresis) behaviour by tracking whether we
> are currently looking for the temperature to rise over T_high or
> for it to fall below T_low. Our implementation of the comparator
> mode (TM==0) wasn't wrong, but rephrase it to match the way that
> interrupt mode is now handled for clarity.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

LGTM,

Reviewed-by: Cédric Le Goater <clg@kaod.org>

Thanks,

C.


> ---
>  hw/misc/tmp105.c | 70 +++++++++++++++++++++++++++++++++++++++++-------
>  hw/misc/tmp105.h |  7 +++++
>  2 files changed, 68 insertions(+), 9 deletions(-)
> 
> diff --git a/hw/misc/tmp105.c b/hw/misc/tmp105.c
> index 0a4aad4854..d299d9b21b 100644
> --- a/hw/misc/tmp105.c
> +++ b/hw/misc/tmp105.c
> @@ -41,16 +41,40 @@ static void tmp105_alarm_update(TMP105State *s)
>              return;
>      }
>  
> -    if ((s->config >> 1) & 1) {					/* TM */
> -        if (s->temperature >= s->limit[1])
> -            s->alarm = 1;
> -        else if (s->temperature < s->limit[0])
> -            s->alarm = 1;
> +    if (s->config >> 1 & 1) {
> +        /*
> +         * TM == 1 : Interrupt mode. We signal Alert when the
> +         * temperature rises above T_high, and expect the guest to clear
> +         * it (eg by reading a device register).
> +         */
> +        if (s->detect_falling) {
> +            if (s->temperature < s->limit[0]) {
> +                s->alarm = 1;
> +                s->detect_falling = false;
> +            }
> +        } else {
> +            if (s->temperature >= s->limit[1]) {
> +                s->alarm = 1;
> +                s->detect_falling = true;
> +            }
> +        }
>      } else {
> -        if (s->temperature >= s->limit[1])
> -            s->alarm = 1;
> -        else if (s->temperature < s->limit[0])
> -            s->alarm = 0;
> +        /*
> +         * TM == 0 : Comparator mode. We signal Alert when the temperature
> +         * rises above T_high, and stop signalling it when the temperature
> +         * falls below T_low.
> +         */
> +        if (s->detect_falling) {
> +            if (s->temperature < s->limit[0]) {
> +                s->alarm = 0;
> +                s->detect_falling = false;
> +            }
> +        } else {
> +            if (s->temperature >= s->limit[1]) {
> +                s->alarm = 1;
> +                s->detect_falling = true;
> +            }
> +        }
>      }
>  
>      tmp105_interrupt_update(s);
> @@ -197,6 +221,29 @@ static int tmp105_post_load(void *opaque, int version_id)
>      return 0;
>  }
>  
> +static bool detect_falling_needed(void *opaque)
> +{
> +    TMP105State *s = opaque;
> +
> +    /*
> +     * We only need to migrate the detect_falling bool if it's set;
> +     * for migration from older machines we assume that it is false
> +     * (ie temperature is not out of range).
> +     */
> +    return s->detect_falling;
> +}
> +
> +static const VMStateDescription vmstate_tmp105_detect_falling = {
> +    .name = "TMP105/detect-falling",
> +    .version_id = 1,
> +    .minimum_version_id = 1,
> +    .needed = detect_falling_needed,
> +    .fields = (VMStateField[]) {
> +        VMSTATE_BOOL(detect_falling, TMP105State),
> +        VMSTATE_END_OF_LIST()
> +    }
> +};
> +
>  static const VMStateDescription vmstate_tmp105 = {
>      .name = "TMP105",
>      .version_id = 0,
> @@ -212,6 +259,10 @@ static const VMStateDescription vmstate_tmp105 = {
>          VMSTATE_UINT8(alarm, TMP105State),
>          VMSTATE_I2C_SLAVE(i2c, TMP105State),
>          VMSTATE_END_OF_LIST()
> +    },
> +    .subsections = (const VMStateDescription*[]) {
> +        &vmstate_tmp105_detect_falling,
> +        NULL
>      }
>  };
>  
> @@ -224,6 +275,7 @@ static void tmp105_reset(I2CSlave *i2c)
>      s->config = 0;
>      s->faults = tmp105_faultq[(s->config >> 3) & 3];
>      s->alarm = 0;
> +    s->detect_falling = false;
>  
>      s->limit[0] = 0x4b00; /* T_LOW, 75 degrees C */
>      s->limit[1] = 0x5000; /* T_HIGH, 80 degrees C */
> diff --git a/hw/misc/tmp105.h b/hw/misc/tmp105.h
> index e5198fce80..7c97071ad7 100644
> --- a/hw/misc/tmp105.h
> +++ b/hw/misc/tmp105.h
> @@ -43,6 +43,13 @@ struct TMP105State {
>      int16_t limit[2];
>      int faults;
>      uint8_t alarm;
> +    /*
> +     * The TMP105 initially looks for a temperature rising above T_high;
> +     * once this is detected, the condition it looks for next is the
> +     * temperature falling below T_low. This flag is false when initially
> +     * looking for T_high, true when looking for T_low.
> +     */
> +    bool detect_falling;
>  };
>  
>  #endif
> 



  reply	other threads:[~2020-11-16 17:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-10 15:00 [PATCH 0/2] hw/misc/tmp105: Correct temperature limit check logic Peter Maydell
2020-11-10 15:00 ` [PATCH 1/2] hw/misc/tmp105: reset the T_low and T_High registers Peter Maydell
2020-11-16 16:31   ` Cédric Le Goater
2020-11-10 15:00 ` [PATCH 2/2] tmp105: Correct handling of temperature limit checks Peter Maydell
2020-11-16 17:00   ` Cédric Le Goater [this message]
2020-11-10 15:09 ` [PATCH 0/2] hw/misc/tmp105: Correct temperature limit check logic no-reply
2020-11-16 16:14 ` Peter Maydell

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=24d24e3a-b594-c45e-309e-a1bb51586d6b@kaod.org \
    --to=clg@kaod.org \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).