blob: 0c30b04326c5e2faa27f7f2d07630d6701ee6625 [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 int test_probe(struct platform_device *plat_dev)
114{
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200115 struct rtc_test_data *rtd;
Jingoo Handd8d8132013-04-29 16:19:52 -0700116
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200117 rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL);
118 if (!rtd)
119 return -ENOMEM;
Jeff Garzik91046a82006-12-06 20:35:34 -0800120
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200121 platform_set_drvdata(plat_dev, rtd);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800122
Alexandre Belloni0b472ad2018-06-05 23:09:12 +0200123 rtd->rtc = devm_rtc_allocate_device(&plat_dev->dev);
Alexandre Belloni4dc24032018-05-31 23:09:57 +0200124 if (IS_ERR(rtd->rtc))
125 return PTR_ERR(rtd->rtc);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800126
Alexandre Belloni0b472ad2018-06-05 23:09:12 +0200127 rtd->rtc->ops = &test_rtc_ops;
128
Alexandre Belloni8be09022018-05-31 23:09:58 +0200129 timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0);
130 rtd->alarm.expires = 0;
131
Alexandre Belloni0b472ad2018-06-05 23:09:12 +0200132 return rtc_register_device(rtd->rtc);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800133}
134
Sam Ravnborgc4646522008-04-28 02:11:55 -0700135static struct platform_driver test_driver = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800136 .probe = test_probe,
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800137 .driver = {
138 .name = "rtc-test",
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800139 },
140};
141
142static int __init test_init(void)
143{
Alexandre Belloni5b257572018-05-31 23:09:56 +0200144 int i, err;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800145
Sam Ravnborgc4646522008-04-28 02:11:55 -0700146 if ((err = platform_driver_register(&test_driver)))
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800147 return err;
148
Alexandre Belloni5b257572018-05-31 23:09:56 +0200149 err = -ENOMEM;
150 for (i = 0; i < MAX_RTC_TEST; i++) {
151 pdev[i] = platform_device_alloc("rtc-test", i);
152 if (!pdev[i])
153 goto exit_free_mem;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800154 }
155
Alexandre Belloni5b257572018-05-31 23:09:56 +0200156 for (i = 0; i < MAX_RTC_TEST; i++) {
157 err = platform_device_add(pdev[i]);
158 if (err)
159 goto exit_device_del;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800160 }
161
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800162 return 0;
163
Alexandre Belloni5b257572018-05-31 23:09:56 +0200164exit_device_del:
165 for (; i > 0; i--)
166 platform_device_del(pdev[i - 1]);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800167
Alexandre Belloni5b257572018-05-31 23:09:56 +0200168exit_free_mem:
169 for (i = 0; i < MAX_RTC_TEST; i++)
170 platform_device_put(pdev[i]);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800171
Sam Ravnborgc4646522008-04-28 02:11:55 -0700172 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800173 return err;
174}
175
176static void __exit test_exit(void)
177{
Alexandre Belloni5b257572018-05-31 23:09:56 +0200178 int i;
179
180 for (i = 0; i < MAX_RTC_TEST; i++)
181 platform_device_unregister(pdev[i]);
182
Sam Ravnborgc4646522008-04-28 02:11:55 -0700183 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800184}
185
186MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
187MODULE_DESCRIPTION("RTC test driver/device");
188MODULE_LICENSE("GPL");
189
190module_init(test_init);
191module_exit(test_exit);