blob: 390f928fd6fcd99b8489d8d499305805fdfc0451 [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
Xunlei Pang4d644ab82015-04-01 20:34:28 -070016static int test_mmss64;
17module_param(test_mmss64, int, 0644);
18MODULE_PARM_DESC(test_mmss64, "Test struct rtc_class_ops.set_mmss64().");
19
Alessandro Zummoa95579c2006-03-27 01:16:42 -080020static struct platform_device *test0 = NULL, *test1 = NULL;
21
22static int test_rtc_read_alarm(struct device *dev,
23 struct rtc_wkalrm *alrm)
24{
25 return 0;
26}
27
28static int test_rtc_set_alarm(struct device *dev,
29 struct rtc_wkalrm *alrm)
30{
31 return 0;
32}
33
34static int test_rtc_read_time(struct device *dev,
35 struct rtc_time *tm)
36{
Xunlei Pang4d644ab82015-04-01 20:34:28 -070037 rtc_time64_to_tm(ktime_get_real_seconds(), tm);
38 return 0;
39}
40
41static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
42{
43 dev_info(dev, "%s, secs = %lld\n", __func__, (long long)secs);
Alessandro Zummoa95579c2006-03-27 01:16:42 -080044 return 0;
45}
46
Alessandro Zummoa95579c2006-03-27 01:16:42 -080047static int test_rtc_set_mmss(struct device *dev, unsigned long secs)
48{
Alessandro Zummobbccf832009-01-06 14:42:21 -080049 dev_info(dev, "%s, secs = %lu\n", __func__, secs);
Alessandro Zummoa95579c2006-03-27 01:16:42 -080050 return 0;
51}
52
53static int test_rtc_proc(struct device *dev, struct seq_file *seq)
54{
55 struct platform_device *plat_dev = to_platform_device(dev);
56
Alessandro Zummoa95579c2006-03-27 01:16:42 -080057 seq_printf(seq, "test\t\t: yes\n");
58 seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
59
60 return 0;
61}
62
John Stultz16380c12011-02-02 17:02:41 -080063static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
Alessandro Zummoa95579c2006-03-27 01:16:42 -080064{
John Stultz16380c12011-02-02 17:02:41 -080065 return 0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -080066}
67
Xunlei Pang4d644ab82015-04-01 20:34:28 -070068static struct rtc_class_ops test_rtc_ops = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -080069 .proc = test_rtc_proc,
70 .read_time = test_rtc_read_time,
Alessandro Zummoa95579c2006-03-27 01:16:42 -080071 .read_alarm = test_rtc_read_alarm,
72 .set_alarm = test_rtc_set_alarm,
73 .set_mmss = test_rtc_set_mmss,
John Stultz16380c12011-02-02 17:02:41 -080074 .alarm_irq_enable = test_rtc_alarm_irq_enable,
Alessandro Zummoa95579c2006-03-27 01:16:42 -080075};
76
77static ssize_t test_irq_show(struct device *dev,
78 struct device_attribute *attr, char *buf)
79{
80 return sprintf(buf, "%d\n", 42);
81}
82static ssize_t test_irq_store(struct device *dev,
83 struct device_attribute *attr,
84 const char *buf, size_t count)
85{
86 int retval;
Wolfram Sang85368bb2018-04-19 16:06:14 +020087 struct rtc_device *rtc = dev_get_drvdata(dev);
Alessandro Zummoa95579c2006-03-27 01:16:42 -080088
89 retval = count;
Marcelo Roberto Jimeneza4174932011-02-07 19:16:07 -020090 if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
David Brownellab6a2d72007-05-08 00:33:30 -070091 rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
Marcelo Roberto Jimeneza4174932011-02-07 19:16:07 -020092 else if (strncmp(buf, "alarm", 5) == 0) {
93 struct rtc_wkalrm alrm;
94 int err = rtc_read_alarm(rtc, &alrm);
95
96 if (!err && alrm.enabled)
97 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
98
99 } else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled)
David Brownellab6a2d72007-05-08 00:33:30 -0700100 rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800101 else
102 retval = -EINVAL;
103
104 return retval;
105}
106static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
107
108static int test_probe(struct platform_device *plat_dev)
109{
110 int err;
Jingoo Handd8d8132013-04-29 16:19:52 -0700111 struct rtc_device *rtc;
112
Xunlei Pang4d644ab82015-04-01 20:34:28 -0700113 if (test_mmss64) {
114 test_rtc_ops.set_mmss64 = test_rtc_set_mmss64;
115 test_rtc_ops.set_mmss = NULL;
116 }
117
Jingoo Handd8d8132013-04-29 16:19:52 -0700118 rtc = devm_rtc_device_register(&plat_dev->dev, "test",
119 &test_rtc_ops, THIS_MODULE);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800120 if (IS_ERR(rtc)) {
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700121 return PTR_ERR(rtc);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800122 }
Jeff Garzik91046a82006-12-06 20:35:34 -0800123
124 err = device_create_file(&plat_dev->dev, &dev_attr_irq);
125 if (err)
Alessandro Zummo4071ea22014-04-03 14:49:36 -0700126 dev_err(&plat_dev->dev, "Unable to create sysfs entry: %s\n",
127 dev_attr_irq.attr.name);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800128
129 platform_set_drvdata(plat_dev, rtc);
130
131 return 0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800132}
133
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800134static int test_remove(struct platform_device *plat_dev)
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800135{
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800136 device_remove_file(&plat_dev->dev, &dev_attr_irq);
137
138 return 0;
139}
140
Sam Ravnborgc4646522008-04-28 02:11:55 -0700141static struct platform_driver test_driver = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800142 .probe = test_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800143 .remove = test_remove,
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800144 .driver = {
145 .name = "rtc-test",
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800146 },
147};
148
149static int __init test_init(void)
150{
151 int err;
152
Sam Ravnborgc4646522008-04-28 02:11:55 -0700153 if ((err = platform_driver_register(&test_driver)))
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800154 return err;
155
156 if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
157 err = -ENOMEM;
158 goto exit_driver_unregister;
159 }
160
161 if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
162 err = -ENOMEM;
Wei Yongjun942bfb32012-12-17 16:02:27 -0800163 goto exit_put_test0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800164 }
165
166 if ((err = platform_device_add(test0)))
Wei Yongjun942bfb32012-12-17 16:02:27 -0800167 goto exit_put_test1;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800168
169 if ((err = platform_device_add(test1)))
Wei Yongjun942bfb32012-12-17 16:02:27 -0800170 goto exit_del_test0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800171
172 return 0;
173
Wei Yongjun942bfb32012-12-17 16:02:27 -0800174exit_del_test0:
175 platform_device_del(test0);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800176
Wei Yongjun942bfb32012-12-17 16:02:27 -0800177exit_put_test1:
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800178 platform_device_put(test1);
179
Wei Yongjun942bfb32012-12-17 16:02:27 -0800180exit_put_test0:
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800181 platform_device_put(test0);
182
183exit_driver_unregister:
Sam Ravnborgc4646522008-04-28 02:11:55 -0700184 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800185 return err;
186}
187
188static void __exit test_exit(void)
189{
190 platform_device_unregister(test0);
191 platform_device_unregister(test1);
Sam Ravnborgc4646522008-04-28 02:11:55 -0700192 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800193}
194
195MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
196MODULE_DESCRIPTION("RTC test driver/device");
197MODULE_LICENSE("GPL");
198
199module_init(test_init);
200module_exit(test_exit);