blob: 3aee7be3a58a5021161adb4b12e0984a6a54e341 [file] [log] [blame]
Alessandro Zummoa95579c2006-03-27 01:16:42 -08001/*
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 Belloni5b257572018-05-31 23:09:56 +020016#define MAX_RTC_TEST 3
17
Alexandre Belloni4dc24032018-05-31 23:09:57 +020018struct rtc_test_data {
19 struct rtc_device *rtc;
20 time64_t offset;
Alexandre Belloni8be09022018-05-31 23:09:58 +020021 struct timer_list alarm;
22 bool alarm_en;
Alexandre Belloni4dc24032018-05-31 23:09:57 +020023};
24
Alexandre Belloni5b257572018-05-31 23:09:56 +020025struct platform_device *pdev[MAX_RTC_TEST];
Alessandro Zummoa95579c2006-03-27 01:16:42 -080026
Alexandre Belloni8be09022018-05-31 23:09:58 +020027static int test_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
Alessandro Zummoa95579c2006-03-27 01:16:42 -080028{
Alexandre Belloni8be09022018-05-31 23:09:58 +020029 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 Zummoa95579c2006-03-27 01:16:42 -080038 return 0;
39}
40
Alexandre Belloni8be09022018-05-31 23:09:58 +020041static int test_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
Alessandro Zummoa95579c2006-03-27 01:16:42 -080042{
Alexandre Belloni8be09022018-05-31 23:09:58 +020043 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 Zummoa95579c2006-03-27 01:16:42 -080064 return 0;
65}
66
Alexandre Belloni4dc24032018-05-31 23:09:57 +020067static int test_rtc_read_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummoa95579c2006-03-27 01:16:42 -080068{
Alexandre Belloni4dc24032018-05-31 23:09:57 +020069 struct rtc_test_data *rtd = dev_get_drvdata(dev);
70
71 rtc_time64_to_tm(ktime_get_real_seconds() + rtd->offset, tm);
72
Xunlei Pang4d644ab82015-04-01 20:34:28 -070073 return 0;
74}
75
76static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
77{
Alexandre Belloni4dc24032018-05-31 23:09:57 +020078 struct rtc_test_data *rtd = dev_get_drvdata(dev);
79
80 rtd->offset = secs - ktime_get_real_seconds();
81
Alessandro Zummoa95579c2006-03-27 01:16:42 -080082 return 0;
83}
84
John Stultz16380c12011-02-02 17:02:41 -080085static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
Alessandro Zummoa95579c2006-03-27 01:16:42 -080086{
Alexandre Belloni8be09022018-05-31 23:09:58 +020087 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 Stultz16380c12011-02-02 17:02:41 -080095 return 0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -080096}
97
Alexandre Belloni78417682018-05-26 03:57:29 +020098static const struct rtc_class_ops test_rtc_ops = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -080099 .read_time = test_rtc_read_time,
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800100 .read_alarm = test_rtc_read_alarm,
101 .set_alarm = test_rtc_set_alarm,
Alexandre Belloni78417682018-05-26 03:57:29 +0200102 .set_mmss64 = test_rtc_set_mmss64,
John Stultz16380c12011-02-02 17:02:41 -0800103 .alarm_irq_enable = test_rtc_alarm_irq_enable,
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800104};
105
Alexandre Belloni8be09022018-05-31 23:09:58 +0200106static 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 Zummoa95579c2006-03-27 01:16:42 -0800113static ssize_t test_irq_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
115{
116 return sprintf(buf, "%d\n", 42);
117}
118static 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 Sang85368bb2018-04-19 16:06:14 +0200123 struct rtc_device *rtc = dev_get_drvdata(dev);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800124
125 retval = count;
Marcelo Roberto Jimeneza4174932011-02-07 19:16:07 -0200126 if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
David Brownellab6a2d72007-05-08 00:33:30 -0700127 rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
Marcelo Roberto Jimeneza4174932011-02-07 19:16:07 -0200128 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 Brownellab6a2d72007-05-08 00:33:30 -0700136 rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800137 else
138 retval = -EINVAL;
139
140 return retval;
141}
142static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
143
144static int test_probe(struct platform_device *plat_dev)
145{
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200146 struct rtc_test_data *rtd;
Jingoo Handd8d8132013-04-29 16:19:52 -0700147
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200148 rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL);
149 if (!rtd)
150 return -ENOMEM;
Jeff Garzik91046a82006-12-06 20:35:34 -0800151
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200152 platform_set_drvdata(plat_dev, rtd);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800153
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200154 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 Zummoa95579c2006-03-27 01:16:42 -0800158
Alexandre Belloni8be09022018-05-31 23:09:58 +0200159 timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0);
160 rtd->alarm.expires = 0;
161
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800162 return 0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800163}
164
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800165static int test_remove(struct platform_device *plat_dev)
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800166{
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800167 device_remove_file(&plat_dev->dev, &dev_attr_irq);
168
169 return 0;
170}
171
Sam Ravnborgc4646522008-04-28 02:11:55 -0700172static struct platform_driver test_driver = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800173 .probe = test_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800174 .remove = test_remove,
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800175 .driver = {
176 .name = "rtc-test",
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800177 },
178};
179
180static int __init test_init(void)
181{
Alexandre Belloni5b257572018-05-31 23:09:56 +0200182 int i, err;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800183
Sam Ravnborgc4646522008-04-28 02:11:55 -0700184 if ((err = platform_driver_register(&test_driver)))
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800185 return err;
186
Alexandre Belloni5b257572018-05-31 23:09:56 +0200187 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 Zummoa95579c2006-03-27 01:16:42 -0800192 }
193
Alexandre Belloni5b257572018-05-31 23:09:56 +0200194 for (i = 0; i < MAX_RTC_TEST; i++) {
195 err = platform_device_add(pdev[i]);
196 if (err)
197 goto exit_device_del;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800198 }
199
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800200 return 0;
201
Alexandre Belloni5b257572018-05-31 23:09:56 +0200202exit_device_del:
203 for (; i > 0; i--)
204 platform_device_del(pdev[i - 1]);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800205
Alexandre Belloni5b257572018-05-31 23:09:56 +0200206exit_free_mem:
207 for (i = 0; i < MAX_RTC_TEST; i++)
208 platform_device_put(pdev[i]);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800209
Sam Ravnborgc4646522008-04-28 02:11:55 -0700210 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800211 return err;
212}
213
214static void __exit test_exit(void)
215{
Alexandre Belloni5b257572018-05-31 23:09:56 +0200216 int i;
217
218 for (i = 0; i < MAX_RTC_TEST; i++)
219 platform_device_unregister(pdev[i]);
220
Sam Ravnborgc4646522008-04-28 02:11:55 -0700221 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800222}
223
224MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
225MODULE_DESCRIPTION("RTC test driver/device");
226MODULE_LICENSE("GPL");
227
228module_init(test_init);
229module_exit(test_exit);