blob: 7746e65b93f290aab1db8c422d997d38962ac5ff [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
16static struct platform_device *test0 = NULL, *test1 = NULL;
17
18static int test_rtc_read_alarm(struct device *dev,
19 struct rtc_wkalrm *alrm)
20{
21 return 0;
22}
23
24static int test_rtc_set_alarm(struct device *dev,
25 struct rtc_wkalrm *alrm)
26{
27 return 0;
28}
29
30static int test_rtc_read_time(struct device *dev,
31 struct rtc_time *tm)
32{
33 rtc_time_to_tm(get_seconds(), tm);
34 return 0;
35}
36
Alessandro Zummoa95579c2006-03-27 01:16:42 -080037static int test_rtc_set_mmss(struct device *dev, unsigned long secs)
38{
Alessandro Zummobbccf832009-01-06 14:42:21 -080039 dev_info(dev, "%s, secs = %lu\n", __func__, secs);
Alessandro Zummoa95579c2006-03-27 01:16:42 -080040 return 0;
41}
42
43static int test_rtc_proc(struct device *dev, struct seq_file *seq)
44{
45 struct platform_device *plat_dev = to_platform_device(dev);
46
Alessandro Zummoa95579c2006-03-27 01:16:42 -080047 seq_printf(seq, "test\t\t: yes\n");
48 seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
49
50 return 0;
51}
52
John Stultz16380c12011-02-02 17:02:41 -080053static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
Alessandro Zummoa95579c2006-03-27 01:16:42 -080054{
John Stultz16380c12011-02-02 17:02:41 -080055 return 0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -080056}
57
David Brownellff8371a2006-09-30 23:28:17 -070058static const struct rtc_class_ops test_rtc_ops = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -080059 .proc = test_rtc_proc,
60 .read_time = test_rtc_read_time,
Alessandro Zummoa95579c2006-03-27 01:16:42 -080061 .read_alarm = test_rtc_read_alarm,
62 .set_alarm = test_rtc_set_alarm,
63 .set_mmss = test_rtc_set_mmss,
John Stultz16380c12011-02-02 17:02:41 -080064 .alarm_irq_enable = test_rtc_alarm_irq_enable,
Alessandro Zummoa95579c2006-03-27 01:16:42 -080065};
66
67static ssize_t test_irq_show(struct device *dev,
68 struct device_attribute *attr, char *buf)
69{
70 return sprintf(buf, "%d\n", 42);
71}
72static ssize_t test_irq_store(struct device *dev,
73 struct device_attribute *attr,
74 const char *buf, size_t count)
75{
76 int retval;
77 struct platform_device *plat_dev = to_platform_device(dev);
78 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
79
80 retval = count;
Marcelo Roberto Jimeneza4174932011-02-07 19:16:07 -020081 if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
David Brownellab6a2d72007-05-08 00:33:30 -070082 rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
Marcelo Roberto Jimeneza4174932011-02-07 19:16:07 -020083 else if (strncmp(buf, "alarm", 5) == 0) {
84 struct rtc_wkalrm alrm;
85 int err = rtc_read_alarm(rtc, &alrm);
86
87 if (!err && alrm.enabled)
88 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
89
90 } else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled)
David Brownellab6a2d72007-05-08 00:33:30 -070091 rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
Alessandro Zummoa95579c2006-03-27 01:16:42 -080092 else
93 retval = -EINVAL;
94
95 return retval;
96}
97static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
98
99static int test_probe(struct platform_device *plat_dev)
100{
101 int err;
Jingoo Handd8d8132013-04-29 16:19:52 -0700102 struct rtc_device *rtc;
103
104 rtc = devm_rtc_device_register(&plat_dev->dev, "test",
105 &test_rtc_ops, THIS_MODULE);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800106 if (IS_ERR(rtc)) {
107 err = PTR_ERR(rtc);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800108 return err;
109 }
Jeff Garzik91046a82006-12-06 20:35:34 -0800110
111 err = device_create_file(&plat_dev->dev, &dev_attr_irq);
112 if (err)
113 goto err;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800114
115 platform_set_drvdata(plat_dev, rtc);
116
117 return 0;
Jeff Garzik91046a82006-12-06 20:35:34 -0800118
119err:
Jeff Garzik91046a82006-12-06 20:35:34 -0800120 return err;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800121}
122
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800123static int test_remove(struct platform_device *plat_dev)
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800124{
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800125 device_remove_file(&plat_dev->dev, &dev_attr_irq);
126
127 return 0;
128}
129
Sam Ravnborgc4646522008-04-28 02:11:55 -0700130static struct platform_driver test_driver = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800131 .probe = test_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800132 .remove = test_remove,
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800133 .driver = {
134 .name = "rtc-test",
135 .owner = THIS_MODULE,
136 },
137};
138
139static int __init test_init(void)
140{
141 int err;
142
Sam Ravnborgc4646522008-04-28 02:11:55 -0700143 if ((err = platform_driver_register(&test_driver)))
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800144 return err;
145
146 if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
147 err = -ENOMEM;
148 goto exit_driver_unregister;
149 }
150
151 if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
152 err = -ENOMEM;
Wei Yongjun942bfb32012-12-17 16:02:27 -0800153 goto exit_put_test0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800154 }
155
156 if ((err = platform_device_add(test0)))
Wei Yongjun942bfb32012-12-17 16:02:27 -0800157 goto exit_put_test1;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800158
159 if ((err = platform_device_add(test1)))
Wei Yongjun942bfb32012-12-17 16:02:27 -0800160 goto exit_del_test0;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800161
162 return 0;
163
Wei Yongjun942bfb32012-12-17 16:02:27 -0800164exit_del_test0:
165 platform_device_del(test0);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800166
Wei Yongjun942bfb32012-12-17 16:02:27 -0800167exit_put_test1:
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800168 platform_device_put(test1);
169
Wei Yongjun942bfb32012-12-17 16:02:27 -0800170exit_put_test0:
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800171 platform_device_put(test0);
172
173exit_driver_unregister:
Sam Ravnborgc4646522008-04-28 02:11:55 -0700174 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800175 return err;
176}
177
178static void __exit test_exit(void)
179{
180 platform_device_unregister(test0);
181 platform_device_unregister(test1);
Sam Ravnborgc4646522008-04-28 02:11:55 -0700182 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800183}
184
185MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
186MODULE_DESCRIPTION("RTC test driver/device");
187MODULE_LICENSE("GPL");
188
189module_init(test_init);
190module_exit(test_exit);