diff options
author | Alexandre Belloni <alexandre.belloni@bootlin.com> | 2018-05-31 23:09:58 +0200 |
---|---|---|
committer | Alexandre Belloni <alexandre.belloni@bootlin.com> | 2018-05-31 23:13:29 +0200 |
commit | 8be09029940227d30ae310bb3fe25bc8abbfe140 (patch) | |
tree | 82ba4eca323f49b6c672754d69a3356cff3c01e4 /drivers/rtc/rtc-test.c | |
parent | 4dc2403bed0dbc18d90cc637337eebfc1e939c72 (diff) | |
download | talos-obmc-linux-8be09029940227d30ae310bb3fe25bc8abbfe140.tar.gz talos-obmc-linux-8be09029940227d30ae310bb3fe25bc8abbfe140.zip |
rtc: test: emulate alarms using timers
Use timers to emulate alarms. Note that multiple alarms may happen if they
are set more than 15 days after the current RTC time.
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Diffstat (limited to 'drivers/rtc/rtc-test.c')
-rw-r--r-- | drivers/rtc/rtc-test.c | 56 |
1 files changed, 52 insertions, 4 deletions
diff --git a/drivers/rtc/rtc-test.c b/drivers/rtc/rtc-test.c index f50b3321314f..3aee7be3a58a 100644 --- a/drivers/rtc/rtc-test.c +++ b/drivers/rtc/rtc-test.c @@ -18,19 +18,49 @@ struct rtc_test_data { struct rtc_device *rtc; time64_t offset; + struct timer_list alarm; + bool alarm_en; }; struct platform_device *pdev[MAX_RTC_TEST]; -static int test_rtc_read_alarm(struct device *dev, - struct rtc_wkalrm *alrm) +static int test_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) { + struct rtc_test_data *rtd = dev_get_drvdata(dev); + time64_t alarm; + + alarm = (rtd->alarm.expires - jiffies) / HZ; + alarm += ktime_get_real_seconds() + rtd->offset; + + rtc_time64_to_tm(alarm, &alrm->time); + alrm->enabled = rtd->alarm_en; + return 0; } -static int test_rtc_set_alarm(struct device *dev, - struct rtc_wkalrm *alrm) +static int test_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) { + struct rtc_test_data *rtd = dev_get_drvdata(dev); + ktime_t timeout; + u64 expires; + + timeout = rtc_tm_to_time64(&alrm->time) - ktime_get_real_seconds(); + timeout -= rtd->offset; + + del_timer(&rtd->alarm); + + expires = jiffies + timeout * HZ; + if (expires > U32_MAX) + expires = U32_MAX; + + pr_err("ABE: %s +%d %s\n", __FILE__, __LINE__, __func__); + rtd->alarm.expires = expires; + + if (alrm->enabled) + add_timer(&rtd->alarm); + + rtd->alarm_en = alrm->enabled; + return 0; } @@ -54,6 +84,14 @@ static int test_rtc_set_mmss64(struct device *dev, time64_t secs) static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) { + struct rtc_test_data *rtd = dev_get_drvdata(dev); + + rtd->alarm_en = enable; + if (enable) + add_timer(&rtd->alarm); + else + del_timer(&rtd->alarm); + return 0; } @@ -65,6 +103,13 @@ static const struct rtc_class_ops test_rtc_ops = { .alarm_irq_enable = test_rtc_alarm_irq_enable, }; +static void test_rtc_alarm_handler(struct timer_list *t) +{ + struct rtc_test_data *rtd = from_timer(rtd, t, alarm); + + rtc_update_irq(rtd->rtc, 1, RTC_AF | RTC_IRQF); +} + static ssize_t test_irq_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -111,6 +156,9 @@ static int test_probe(struct platform_device *plat_dev) if (IS_ERR(rtd->rtc)) return PTR_ERR(rtd->rtc); + timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0); + rtd->alarm.expires = 0; + return 0; } |