2 * An RTC test device/driver
3 * Copyright (C) 2005 Tower Technologies
4 * Author: Alessandro Zummo <a.zummo@towertech.it>
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.
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/rtc.h>
14 #include <linux/platform_device.h>
16 static int test_mmss64;
17 module_param(test_mmss64, int, 0644);
18 MODULE_PARM_DESC(test_mmss64, "Test struct rtc_class_ops.set_mmss64().");
20 static struct platform_device *test0 = NULL, *test1 = NULL;
22 static int test_rtc_read_alarm(struct device *dev,
23 struct rtc_wkalrm *alrm)
28 static int test_rtc_set_alarm(struct device *dev,
29 struct rtc_wkalrm *alrm)
34 static int test_rtc_read_time(struct device *dev,
37 rtc_time64_to_tm(ktime_get_real_seconds(), tm);
41 static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
43 dev_info(dev, "%s, secs = %lld\n", __func__, (long long)secs);
47 static int test_rtc_set_mmss(struct device *dev, unsigned long secs)
49 dev_info(dev, "%s, secs = %lu\n", __func__, secs);
53 static int test_rtc_proc(struct device *dev, struct seq_file *seq)
55 struct platform_device *plat_dev = to_platform_device(dev);
57 seq_printf(seq, "test\t\t: yes\n");
58 seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
63 static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
68 static struct rtc_class_ops test_rtc_ops = {
69 .proc = test_rtc_proc,
70 .read_time = test_rtc_read_time,
71 .read_alarm = test_rtc_read_alarm,
72 .set_alarm = test_rtc_set_alarm,
73 .set_mmss = test_rtc_set_mmss,
74 .alarm_irq_enable = test_rtc_alarm_irq_enable,
77 static ssize_t test_irq_show(struct device *dev,
78 struct device_attribute *attr, char *buf)
80 return sprintf(buf, "%d\n", 42);
82 static ssize_t test_irq_store(struct device *dev,
83 struct device_attribute *attr,
84 const char *buf, size_t count)
87 struct platform_device *plat_dev = to_platform_device(dev);
88 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
91 if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
92 rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
93 else if (strncmp(buf, "alarm", 5) == 0) {
94 struct rtc_wkalrm alrm;
95 int err = rtc_read_alarm(rtc, &alrm);
97 if (!err && alrm.enabled)
98 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
100 } else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled)
101 rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
107 static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
109 static int test_probe(struct platform_device *plat_dev)
112 struct rtc_device *rtc;
115 test_rtc_ops.set_mmss64 = test_rtc_set_mmss64;
116 test_rtc_ops.set_mmss = NULL;
119 rtc = devm_rtc_device_register(&plat_dev->dev, "test",
120 &test_rtc_ops, THIS_MODULE);
125 err = device_create_file(&plat_dev->dev, &dev_attr_irq);
127 dev_err(&plat_dev->dev, "Unable to create sysfs entry: %s\n",
128 dev_attr_irq.attr.name);
130 platform_set_drvdata(plat_dev, rtc);
135 static int test_remove(struct platform_device *plat_dev)
137 device_remove_file(&plat_dev->dev, &dev_attr_irq);
142 static struct platform_driver test_driver = {
144 .remove = test_remove,
150 static int __init test_init(void)
154 if ((err = platform_driver_register(&test_driver)))
157 if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
159 goto exit_driver_unregister;
162 if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
167 if ((err = platform_device_add(test0)))
170 if ((err = platform_device_add(test1)))
176 platform_device_del(test0);
179 platform_device_put(test1);
182 platform_device_put(test0);
184 exit_driver_unregister:
185 platform_driver_unregister(&test_driver);
189 static void __exit test_exit(void)
191 platform_device_unregister(test0);
192 platform_device_unregister(test1);
193 platform_driver_unregister(&test_driver);
196 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
197 MODULE_DESCRIPTION("RTC test driver/device");
198 MODULE_LICENSE("GPL");
200 module_init(test_init);
201 module_exit(test_exit);