Alexandre Belloni | c7080e2 | 2018-09-19 03:16:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 2 | /* |
| 3 | * An RTC test device/driver |
| 4 | * Copyright (C) 2005 Tower Technologies |
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/rtc.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 13 | #define MAX_RTC_TEST 3 |
| 14 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 15 | struct rtc_test_data { |
| 16 | struct rtc_device *rtc; |
| 17 | time64_t offset; |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 18 | struct timer_list alarm; |
| 19 | bool alarm_en; |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
Colin Ian King | 2b4f07e | 2018-06-24 11:38:03 +0100 | [diff] [blame] | 22 | static struct platform_device *pdev[MAX_RTC_TEST]; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 23 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 24 | static int test_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 25 | { |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 26 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 27 | time64_t alarm; |
| 28 | |
| 29 | alarm = (rtd->alarm.expires - jiffies) / HZ; |
| 30 | alarm += ktime_get_real_seconds() + rtd->offset; |
| 31 | |
| 32 | rtc_time64_to_tm(alarm, &alrm->time); |
| 33 | alrm->enabled = rtd->alarm_en; |
| 34 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 38 | static int test_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 39 | { |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 40 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 41 | ktime_t timeout; |
| 42 | u64 expires; |
| 43 | |
| 44 | timeout = rtc_tm_to_time64(&alrm->time) - ktime_get_real_seconds(); |
| 45 | timeout -= rtd->offset; |
| 46 | |
| 47 | del_timer(&rtd->alarm); |
| 48 | |
| 49 | expires = jiffies + timeout * HZ; |
| 50 | if (expires > U32_MAX) |
| 51 | expires = U32_MAX; |
| 52 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 53 | rtd->alarm.expires = expires; |
| 54 | |
| 55 | if (alrm->enabled) |
| 56 | add_timer(&rtd->alarm); |
| 57 | |
| 58 | rtd->alarm_en = alrm->enabled; |
| 59 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 63 | static int test_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 64 | { |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 65 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 66 | |
| 67 | rtc_time64_to_tm(ktime_get_real_seconds() + rtd->offset, tm); |
| 68 | |
Xunlei Pang | 4d644ab8 | 2015-04-01 20:34:28 -0700 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Alexandre Belloni | 43dae50 | 2019-04-07 23:30:12 +0200 | [diff] [blame] | 72 | static int test_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Xunlei Pang | 4d644ab8 | 2015-04-01 20:34:28 -0700 | [diff] [blame] | 73 | { |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 74 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 75 | |
Alexandre Belloni | 43dae50 | 2019-04-07 23:30:12 +0200 | [diff] [blame] | 76 | rtd->offset = rtc_tm_to_time64(tm) - ktime_get_real_seconds(); |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 77 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 81 | static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 82 | { |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 83 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 84 | |
| 85 | rtd->alarm_en = enable; |
| 86 | if (enable) |
| 87 | add_timer(&rtd->alarm); |
| 88 | else |
| 89 | del_timer(&rtd->alarm); |
| 90 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 91 | return 0; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Alexandre Belloni | 1928906 | 2018-06-05 23:09:13 +0200 | [diff] [blame] | 94 | static const struct rtc_class_ops test_rtc_ops_noalm = { |
| 95 | .read_time = test_rtc_read_time, |
Alexandre Belloni | 43dae50 | 2019-04-07 23:30:12 +0200 | [diff] [blame] | 96 | .set_time = test_rtc_set_time, |
Alexandre Belloni | 1928906 | 2018-06-05 23:09:13 +0200 | [diff] [blame] | 97 | .alarm_irq_enable = test_rtc_alarm_irq_enable, |
| 98 | }; |
| 99 | |
Alexandre Belloni | 7841768 | 2018-05-26 03:57:29 +0200 | [diff] [blame] | 100 | static const struct rtc_class_ops test_rtc_ops = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 101 | .read_time = test_rtc_read_time, |
Alexandre Belloni | 43dae50 | 2019-04-07 23:30:12 +0200 | [diff] [blame] | 102 | .set_time = test_rtc_set_time, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 103 | .read_alarm = test_rtc_read_alarm, |
| 104 | .set_alarm = test_rtc_set_alarm, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 105 | .alarm_irq_enable = test_rtc_alarm_irq_enable, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 108 | static void test_rtc_alarm_handler(struct timer_list *t) |
| 109 | { |
| 110 | struct rtc_test_data *rtd = from_timer(rtd, t, alarm); |
| 111 | |
| 112 | rtc_update_irq(rtd->rtc, 1, RTC_AF | RTC_IRQF); |
| 113 | } |
| 114 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 115 | static int test_probe(struct platform_device *plat_dev) |
| 116 | { |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 117 | struct rtc_test_data *rtd; |
Jingoo Han | dd8d813 | 2013-04-29 16:19:52 -0700 | [diff] [blame] | 118 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 119 | rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL); |
| 120 | if (!rtd) |
| 121 | return -ENOMEM; |
Jeff Garzik | 91046a8 | 2006-12-06 20:35:34 -0800 | [diff] [blame] | 122 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 123 | platform_set_drvdata(plat_dev, rtd); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 124 | |
Alexandre Belloni | 0b472ad | 2018-06-05 23:09:12 +0200 | [diff] [blame] | 125 | rtd->rtc = devm_rtc_allocate_device(&plat_dev->dev); |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 126 | if (IS_ERR(rtd->rtc)) |
| 127 | return PTR_ERR(rtd->rtc); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 128 | |
Alexandre Belloni | 1928906 | 2018-06-05 23:09:13 +0200 | [diff] [blame] | 129 | switch (plat_dev->id) { |
| 130 | case 0: |
| 131 | rtd->rtc->ops = &test_rtc_ops_noalm; |
| 132 | break; |
| 133 | default: |
| 134 | rtd->rtc->ops = &test_rtc_ops; |
Roman Stratiienko | c19623d | 2019-05-15 17:20:22 +0300 | [diff] [blame] | 135 | device_init_wakeup(&plat_dev->dev, 1); |
Alexandre Belloni | 1928906 | 2018-06-05 23:09:13 +0200 | [diff] [blame] | 136 | } |
Alexandre Belloni | 0b472ad | 2018-06-05 23:09:12 +0200 | [diff] [blame] | 137 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame] | 138 | timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0); |
| 139 | rtd->alarm.expires = 0; |
| 140 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 141 | return devm_rtc_register_device(rtd->rtc); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 144 | static struct platform_driver test_driver = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 145 | .probe = test_probe, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 146 | .driver = { |
| 147 | .name = "rtc-test", |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 148 | }, |
| 149 | }; |
| 150 | |
| 151 | static int __init test_init(void) |
| 152 | { |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 153 | int i, err; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 154 | |
Alexandre Belloni | 540a11d | 2019-03-20 13:03:12 +0100 | [diff] [blame] | 155 | err = platform_driver_register(&test_driver); |
| 156 | if (err) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 157 | return err; |
| 158 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 159 | err = -ENOMEM; |
| 160 | for (i = 0; i < MAX_RTC_TEST; i++) { |
| 161 | pdev[i] = platform_device_alloc("rtc-test", i); |
| 162 | if (!pdev[i]) |
| 163 | goto exit_free_mem; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 166 | for (i = 0; i < MAX_RTC_TEST; i++) { |
| 167 | err = platform_device_add(pdev[i]); |
| 168 | if (err) |
| 169 | goto exit_device_del; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 172 | return 0; |
| 173 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 174 | exit_device_del: |
| 175 | for (; i > 0; i--) |
| 176 | platform_device_del(pdev[i - 1]); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 177 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 178 | exit_free_mem: |
| 179 | for (i = 0; i < MAX_RTC_TEST; i++) |
| 180 | platform_device_put(pdev[i]); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 181 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 182 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 183 | return err; |
| 184 | } |
| 185 | |
| 186 | static void __exit test_exit(void) |
| 187 | { |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 188 | int i; |
| 189 | |
| 190 | for (i = 0; i < MAX_RTC_TEST; i++) |
| 191 | platform_device_unregister(pdev[i]); |
| 192 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 193 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 197 | MODULE_DESCRIPTION("RTC test driver/device"); |
Alexandre Belloni | fd13c93 | 2018-09-19 03:16:31 +0200 | [diff] [blame] | 198 | MODULE_LICENSE("GPL v2"); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 199 | |
| 200 | module_init(test_init); |
| 201 | module_exit(test_exit); |