All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Alessandro Zummo <a.zummo@towertech.it>,
	Benson Leung <bleung@chromium.org>,
	linux-rtc@vger.kernel.org, chrome-platform@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	Brian Norris <briannorris@chromium.org>
Subject: Re: [PATCH] rtc: cros-ec: Limit RTC alarm range if needed
Date: Mon, 31 Oct 2022 18:10:53 +0100	[thread overview]
Message-ID: <Y2ABnbBGSJGM3gSS@mail.local> (raw)
In-Reply-To: <20221029005400.2712577-1-linux@roeck-us.net>

Hello,

On 28/10/2022 17:54:00-0700, Guenter Roeck wrote:
> RTC chips on some older Chromebooks can only handle alarms less than 24
> hours in the future. Attempts to set an alarm beyond that range fails.
> The most severe impact of this limitation is that suspend requests fail
> if alarmtimer_suspend() tries to set an alarm for more than 24 hours
> in the future.
> 
> Try to set the real-time alarm to just below 24 hours if setting it to
> a larger value fails to work around the problem. While not perfect, it
> is better than just failing the call. A similar workaround is already
> implemented in the rtc-tps6586x driver.

I'm not super convinced this is actually better than failing the call
because your are implementing policy in the driver which is bad from a
user point of view. It would be way better to return -ERANGE and let
userspace select a better alarm time.
Do you have to know in advance which are the "older" chromebooks that
are affected?

> 
> Drop error messages in cros_ec_rtc_get() and cros_ec_rtc_set() since the
> calling code also logs an error and to avoid spurious error messages if
> setting the alarm ultimately succeeds.
> 
> Cc: Brian Norris <briannorris@chromium.org>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>  drivers/rtc/rtc-cros-ec.c | 35 ++++++++++++++++++++---------------
>  1 file changed, 20 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/rtc/rtc-cros-ec.c b/drivers/rtc/rtc-cros-ec.c
> index 887f5193e253..a3ec066d8066 100644
> --- a/drivers/rtc/rtc-cros-ec.c
> +++ b/drivers/rtc/rtc-cros-ec.c
> @@ -14,6 +14,8 @@
>  
>  #define DRV_NAME	"cros-ec-rtc"
>  
> +#define SECS_PER_DAY	(24 * 60 * 60)
> +
>  /**
>   * struct cros_ec_rtc - Driver data for EC RTC
>   *
> @@ -43,13 +45,8 @@ static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
>  	msg.msg.insize = sizeof(msg.data);
>  
>  	ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
> -	if (ret < 0) {
> -		dev_err(cros_ec->dev,
> -			"error getting %s from EC: %d\n",
> -			command == EC_CMD_RTC_GET_VALUE ? "time" : "alarm",
> -			ret);
> +	if (ret < 0)
>  		return ret;
> -	}
>  
>  	*response = msg.data.time;
>  
> @@ -59,7 +56,7 @@ static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
>  static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
>  			   u32 param)
>  {
> -	int ret = 0;
> +	int ret;
>  	struct {
>  		struct cros_ec_command msg;
>  		struct ec_response_rtc data;
> @@ -71,13 +68,8 @@ static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
>  	msg.data.time = param;
>  
>  	ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
> -	if (ret < 0) {
> -		dev_err(cros_ec->dev, "error setting %s on EC: %d\n",
> -			command == EC_CMD_RTC_SET_VALUE ? "time" : "alarm",
> -			ret);
> +	if (ret < 0)
>  		return ret;
> -	}
> -
>  	return 0;
>  }
>  
> @@ -190,8 +182,21 @@ static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>  
>  	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
>  	if (ret < 0) {
> -		dev_err(dev, "error setting alarm: %d\n", ret);
> -		return ret;
> +		if (ret == -EINVAL && alarm_offset >= SECS_PER_DAY) {
> +			/*
> +			 * RTC chips on some older Chromebooks can only handle
> +			 * alarms up to 24h in the future. Try to set an alarm
> +			 * below that limit to avoid suspend failures.
> +			 */
> +			ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
> +					      SECS_PER_DAY - 1);
> +		}
> +
> +		if (ret < 0) {
> +			dev_err(dev, "error setting alarm in %u seconds: %d\n",
> +				alarm_offset, ret);
> +			return ret;
> +		}
>  	}
>  
>  	return 0;
> -- 
> 2.36.2
> 

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

  parent reply	other threads:[~2022-10-31 17:11 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-29  0:54 [PATCH] rtc: cros-ec: Limit RTC alarm range if needed Guenter Roeck
2022-10-29  1:50 ` Brian Norris
2022-10-31  3:26 ` Tzung-Bi Shih
2022-10-31 16:36   ` Brian Norris
2022-10-31 17:10 ` Alexandre Belloni [this message]
2022-10-31 17:56   ` Brian Norris
2022-10-31 21:55     ` Alexandre Belloni
2022-10-31 22:47       ` Guenter Roeck
2022-10-31 18:19   ` Guenter Roeck
2022-10-31 22:14     ` Alexandre Belloni
2022-10-31 23:07       ` Guenter Roeck
2022-11-02 18:48         ` Guenter Roeck
2022-11-07 22:52           ` Alexandre Belloni
2022-11-08 16:59             ` Guenter Roeck
2022-11-14 18:08 ` Alexandre Belloni

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=Y2ABnbBGSJGM3gSS@mail.local \
    --to=alexandre.belloni@bootlin.com \
    --cc=a.zummo@towertech.it \
    --cc=bleung@chromium.org \
    --cc=briannorris@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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.