Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * An RTC test device/driver |
| 3 | * Copyright (C) 2005 Tower Technologies |
| 4 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/rtc.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 16 | #define MAX_RTC_TEST 3 |
| 17 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 18 | struct rtc_test_data { |
| 19 | struct rtc_device *rtc; |
| 20 | time64_t offset; |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 21 | struct timer_list alarm; |
| 22 | bool alarm_en; |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 25 | struct platform_device *pdev[MAX_RTC_TEST]; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 26 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 27 | 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] | 28 | { |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 29 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 30 | time64_t alarm; |
| 31 | |
| 32 | alarm = (rtd->alarm.expires - jiffies) / HZ; |
| 33 | alarm += ktime_get_real_seconds() + rtd->offset; |
| 34 | |
| 35 | rtc_time64_to_tm(alarm, &alrm->time); |
| 36 | alrm->enabled = rtd->alarm_en; |
| 37 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 41 | 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] | 42 | { |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 43 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 44 | ktime_t timeout; |
| 45 | u64 expires; |
| 46 | |
| 47 | timeout = rtc_tm_to_time64(&alrm->time) - ktime_get_real_seconds(); |
| 48 | timeout -= rtd->offset; |
| 49 | |
| 50 | del_timer(&rtd->alarm); |
| 51 | |
| 52 | expires = jiffies + timeout * HZ; |
| 53 | if (expires > U32_MAX) |
| 54 | expires = U32_MAX; |
| 55 | |
| 56 | pr_err("ABE: %s +%d %s\n", __FILE__, __LINE__, __func__); |
| 57 | rtd->alarm.expires = expires; |
| 58 | |
| 59 | if (alrm->enabled) |
| 60 | add_timer(&rtd->alarm); |
| 61 | |
| 62 | rtd->alarm_en = alrm->enabled; |
| 63 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 67 | 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] | 68 | { |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 69 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 70 | |
| 71 | rtc_time64_to_tm(ktime_get_real_seconds() + rtd->offset, tm); |
| 72 | |
Xunlei Pang | 4d644ab8 | 2015-04-01 20:34:28 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int test_rtc_set_mmss64(struct device *dev, time64_t secs) |
| 77 | { |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 78 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 79 | |
| 80 | rtd->offset = secs - ktime_get_real_seconds(); |
| 81 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 85 | 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] | 86 | { |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 87 | struct rtc_test_data *rtd = dev_get_drvdata(dev); |
| 88 | |
| 89 | rtd->alarm_en = enable; |
| 90 | if (enable) |
| 91 | add_timer(&rtd->alarm); |
| 92 | else |
| 93 | del_timer(&rtd->alarm); |
| 94 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 95 | return 0; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Alexandre Belloni | 7841768 | 2018-05-26 03:57:29 +0200 | [diff] [blame] | 98 | static const struct rtc_class_ops test_rtc_ops = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 99 | .read_time = test_rtc_read_time, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 100 | .read_alarm = test_rtc_read_alarm, |
| 101 | .set_alarm = test_rtc_set_alarm, |
Alexandre Belloni | 7841768 | 2018-05-26 03:57:29 +0200 | [diff] [blame] | 102 | .set_mmss64 = test_rtc_set_mmss64, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 103 | .alarm_irq_enable = test_rtc_alarm_irq_enable, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 106 | static void test_rtc_alarm_handler(struct timer_list *t) |
| 107 | { |
| 108 | struct rtc_test_data *rtd = from_timer(rtd, t, alarm); |
| 109 | |
| 110 | rtc_update_irq(rtd->rtc, 1, RTC_AF | RTC_IRQF); |
| 111 | } |
| 112 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 113 | static ssize_t test_irq_show(struct device *dev, |
| 114 | struct device_attribute *attr, char *buf) |
| 115 | { |
| 116 | return sprintf(buf, "%d\n", 42); |
| 117 | } |
| 118 | static ssize_t test_irq_store(struct device *dev, |
| 119 | struct device_attribute *attr, |
| 120 | const char *buf, size_t count) |
| 121 | { |
| 122 | int retval; |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 123 | struct rtc_device *rtc = dev_get_drvdata(dev); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 124 | |
| 125 | retval = count; |
Marcelo Roberto Jimenez | a417493 | 2011-02-07 19:16:07 -0200 | [diff] [blame] | 126 | if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 127 | rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF); |
Marcelo Roberto Jimenez | a417493 | 2011-02-07 19:16:07 -0200 | [diff] [blame] | 128 | else if (strncmp(buf, "alarm", 5) == 0) { |
| 129 | struct rtc_wkalrm alrm; |
| 130 | int err = rtc_read_alarm(rtc, &alrm); |
| 131 | |
| 132 | if (!err && alrm.enabled) |
| 133 | rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); |
| 134 | |
| 135 | } else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 136 | rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 137 | else |
| 138 | retval = -EINVAL; |
| 139 | |
| 140 | return retval; |
| 141 | } |
| 142 | static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store); |
| 143 | |
| 144 | static int test_probe(struct platform_device *plat_dev) |
| 145 | { |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 146 | struct rtc_test_data *rtd; |
Jingoo Han | dd8d813 | 2013-04-29 16:19:52 -0700 | [diff] [blame] | 147 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 148 | rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL); |
| 149 | if (!rtd) |
| 150 | return -ENOMEM; |
Jeff Garzik | 91046a8 | 2006-12-06 20:35:34 -0800 | [diff] [blame] | 151 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 152 | platform_set_drvdata(plat_dev, rtd); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 153 | |
Alexandre Belloni | 4dc2403 | 2018-05-31 23:09:57 +0200 | [diff] [blame] | 154 | rtd->rtc = devm_rtc_device_register(&plat_dev->dev, "test", |
| 155 | &test_rtc_ops, THIS_MODULE); |
| 156 | if (IS_ERR(rtd->rtc)) |
| 157 | return PTR_ERR(rtd->rtc); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 158 | |
Alexandre Belloni | 8be0902 | 2018-05-31 23:09:58 +0200 | [diff] [blame^] | 159 | timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0); |
| 160 | rtd->alarm.expires = 0; |
| 161 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 162 | return 0; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 165 | static int test_remove(struct platform_device *plat_dev) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 166 | { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 167 | device_remove_file(&plat_dev->dev, &dev_attr_irq); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 172 | static struct platform_driver test_driver = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 173 | .probe = test_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 174 | .remove = test_remove, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 175 | .driver = { |
| 176 | .name = "rtc-test", |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 177 | }, |
| 178 | }; |
| 179 | |
| 180 | static int __init test_init(void) |
| 181 | { |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 182 | int i, err; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 183 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 184 | if ((err = platform_driver_register(&test_driver))) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 185 | return err; |
| 186 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 187 | err = -ENOMEM; |
| 188 | for (i = 0; i < MAX_RTC_TEST; i++) { |
| 189 | pdev[i] = platform_device_alloc("rtc-test", i); |
| 190 | if (!pdev[i]) |
| 191 | goto exit_free_mem; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 194 | for (i = 0; i < MAX_RTC_TEST; i++) { |
| 195 | err = platform_device_add(pdev[i]); |
| 196 | if (err) |
| 197 | goto exit_device_del; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 200 | return 0; |
| 201 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 202 | exit_device_del: |
| 203 | for (; i > 0; i--) |
| 204 | platform_device_del(pdev[i - 1]); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 205 | |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 206 | exit_free_mem: |
| 207 | for (i = 0; i < MAX_RTC_TEST; i++) |
| 208 | platform_device_put(pdev[i]); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 209 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 210 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 211 | return err; |
| 212 | } |
| 213 | |
| 214 | static void __exit test_exit(void) |
| 215 | { |
Alexandre Belloni | 5b25757 | 2018-05-31 23:09:56 +0200 | [diff] [blame] | 216 | int i; |
| 217 | |
| 218 | for (i = 0; i < MAX_RTC_TEST; i++) |
| 219 | platform_device_unregister(pdev[i]); |
| 220 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 221 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 225 | MODULE_DESCRIPTION("RTC test driver/device"); |
| 226 | MODULE_LICENSE("GPL"); |
| 227 | |
| 228 | module_init(test_init); |
| 229 | module_exit(test_exit); |