xref: /openbmc/linux/drivers/rtc/rtc-test.c (revision 2a598d0b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * An RTC test device/driver
4  * Copyright (C) 2005 Tower Technologies
5  * Author: Alessandro Zummo <a.zummo@towertech.it>
6  */
7 
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/rtc.h>
11 #include <linux/platform_device.h>
12 
13 #define MAX_RTC_TEST 3
14 
15 struct rtc_test_data {
16 	struct rtc_device *rtc;
17 	time64_t offset;
18 	struct timer_list alarm;
19 	bool alarm_en;
20 };
21 
22 static struct platform_device *pdev[MAX_RTC_TEST];
23 
24 static int test_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
25 {
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 
35 	return 0;
36 }
37 
38 static int test_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
39 {
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 
53 	rtd->alarm.expires = expires;
54 
55 	if (alrm->enabled)
56 		add_timer(&rtd->alarm);
57 
58 	rtd->alarm_en = alrm->enabled;
59 
60 	return 0;
61 }
62 
63 static int test_rtc_read_time(struct device *dev, struct rtc_time *tm)
64 {
65 	struct rtc_test_data *rtd = dev_get_drvdata(dev);
66 
67 	rtc_time64_to_tm(ktime_get_real_seconds() + rtd->offset, tm);
68 
69 	return 0;
70 }
71 
72 static int test_rtc_set_time(struct device *dev, struct rtc_time *tm)
73 {
74 	struct rtc_test_data *rtd = dev_get_drvdata(dev);
75 
76 	rtd->offset = rtc_tm_to_time64(tm) - ktime_get_real_seconds();
77 
78 	return 0;
79 }
80 
81 static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
82 {
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 
91 	return 0;
92 }
93 
94 static const struct rtc_class_ops test_rtc_ops_noalm = {
95 	.read_time = test_rtc_read_time,
96 	.set_time = test_rtc_set_time,
97 	.alarm_irq_enable = test_rtc_alarm_irq_enable,
98 };
99 
100 static const struct rtc_class_ops test_rtc_ops = {
101 	.read_time = test_rtc_read_time,
102 	.set_time = test_rtc_set_time,
103 	.read_alarm = test_rtc_read_alarm,
104 	.set_alarm = test_rtc_set_alarm,
105 	.alarm_irq_enable = test_rtc_alarm_irq_enable,
106 };
107 
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 
115 static int test_probe(struct platform_device *plat_dev)
116 {
117 	struct rtc_test_data *rtd;
118 
119 	rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL);
120 	if (!rtd)
121 		return -ENOMEM;
122 
123 	platform_set_drvdata(plat_dev, rtd);
124 
125 	rtd->rtc = devm_rtc_allocate_device(&plat_dev->dev);
126 	if (IS_ERR(rtd->rtc))
127 		return PTR_ERR(rtd->rtc);
128 
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;
135 		device_init_wakeup(&plat_dev->dev, 1);
136 	}
137 
138 	timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0);
139 	rtd->alarm.expires = 0;
140 
141 	return devm_rtc_register_device(rtd->rtc);
142 }
143 
144 static struct platform_driver test_driver = {
145 	.probe	= test_probe,
146 	.driver = {
147 		.name = "rtc-test",
148 	},
149 };
150 
151 static int __init test_init(void)
152 {
153 	int i, err;
154 
155 	err = platform_driver_register(&test_driver);
156 	if (err)
157 		return err;
158 
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;
164 	}
165 
166 	for (i = 0; i < MAX_RTC_TEST; i++) {
167 		err = platform_device_add(pdev[i]);
168 		if (err)
169 			goto exit_device_del;
170 	}
171 
172 	return 0;
173 
174 exit_device_del:
175 	for (; i > 0; i--)
176 		platform_device_del(pdev[i - 1]);
177 
178 exit_free_mem:
179 	for (i = 0; i < MAX_RTC_TEST; i++)
180 		platform_device_put(pdev[i]);
181 
182 	platform_driver_unregister(&test_driver);
183 	return err;
184 }
185 
186 static void __exit test_exit(void)
187 {
188 	int i;
189 
190 	for (i = 0; i < MAX_RTC_TEST; i++)
191 		platform_device_unregister(pdev[i]);
192 
193 	platform_driver_unregister(&test_driver);
194 }
195 
196 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
197 MODULE_DESCRIPTION("RTC test driver/device");
198 MODULE_LICENSE("GPL v2");
199 
200 module_init(test_init);
201 module_exit(test_exit);
202