rtc: cros-ec: Detect and report supported alarm window size

The RTC on some older Chromebooks can only handle alarms less than
24 hours in the future. The only way to find out is to try to set
an alarm further in the future. If that fails, assume that the RTC
connected to the EC can only handle less than 24 hours of alarm
window, and report that value to the RTC core.

After that change, it is no longer necessary to limit the alarm time
when setting it. Report any excessive alarms to the caller instead.

Cc: Brian Norris <briannorris@chromium.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20230817225537.4053865-4-linux@roeck-us.net
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
This commit is contained in:
Guenter Roeck 2023-08-17 15:55:33 -07:00 committed by Alexandre Belloni
parent 781589e40a
commit 00c3092d88

View File

@ -182,21 +182,15 @@ 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) {
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;
}
dev_err(dev, "error setting alarm in %u seconds: %d\n",
alarm_offset, ret);
/*
* The EC code returns -EINVAL if the alarm time is too
* far in the future. Convert it to the expected error code.
*/
if (ret == -EINVAL)
ret = -ERANGE;
return ret;
}
return 0;
@ -355,6 +349,20 @@ static int cros_ec_rtc_probe(struct platform_device *pdev)
cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
cros_ec_rtc->rtc->range_max = U32_MAX;
/*
* The RTC on some older Chromebooks can only handle alarms less than
* 24 hours in the future. The only way to find out is to try to set an
* alarm further in the future. If that fails, assume that the RTC
* connected to the EC can only handle less than 24 hours of alarm
* window.
*/
ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, SECS_PER_DAY * 2);
if (ret == -EINVAL)
cros_ec_rtc->rtc->alarm_offset_max = SECS_PER_DAY - 1;
(void)cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
EC_RTC_ALARM_CLEAR);
ret = devm_rtc_register_device(cros_ec_rtc->rtc);
if (ret)
return ret;