blob: bc930022004a58fc2dcb344c86bf9da180abd317 [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
37static int test_rtc_set_time(struct device *dev,
38 struct rtc_time *tm)
39{
40 return 0;
41}
42
43static int test_rtc_set_mmss(struct device *dev, unsigned long secs)
44{
45 return 0;
46}
47
48static int test_rtc_proc(struct device *dev, struct seq_file *seq)
49{
50 struct platform_device *plat_dev = to_platform_device(dev);
51
Alessandro Zummoa95579c2006-03-27 01:16:42 -080052 seq_printf(seq, "test\t\t: yes\n");
53 seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
54
55 return 0;
56}
57
58static int test_rtc_ioctl(struct device *dev, unsigned int cmd,
59 unsigned long arg)
60{
61 /* We do support interrupts, they're generated
62 * using the sysfs interface.
63 */
64 switch (cmd) {
65 case RTC_PIE_ON:
66 case RTC_PIE_OFF:
67 case RTC_UIE_ON:
68 case RTC_UIE_OFF:
69 case RTC_AIE_ON:
70 case RTC_AIE_OFF:
71 return 0;
72
73 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -070074 return -ENOIOCTLCMD;
Alessandro Zummoa95579c2006-03-27 01:16:42 -080075 }
76}
77
David Brownellff8371a2006-09-30 23:28:17 -070078static const struct rtc_class_ops test_rtc_ops = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -080079 .proc = test_rtc_proc,
80 .read_time = test_rtc_read_time,
81 .set_time = test_rtc_set_time,
82 .read_alarm = test_rtc_read_alarm,
83 .set_alarm = test_rtc_set_alarm,
84 .set_mmss = test_rtc_set_mmss,
85 .ioctl = test_rtc_ioctl,
86};
87
88static ssize_t test_irq_show(struct device *dev,
89 struct device_attribute *attr, char *buf)
90{
91 return sprintf(buf, "%d\n", 42);
92}
93static ssize_t test_irq_store(struct device *dev,
94 struct device_attribute *attr,
95 const char *buf, size_t count)
96{
97 int retval;
98 struct platform_device *plat_dev = to_platform_device(dev);
99 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
100
101 retval = count;
David Brownelld728b1e2006-11-25 11:09:28 -0800102 local_irq_disable();
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800103 if (strncmp(buf, "tick", 4) == 0)
David Brownellab6a2d72007-05-08 00:33:30 -0700104 rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800105 else if (strncmp(buf, "alarm", 5) == 0)
David Brownellab6a2d72007-05-08 00:33:30 -0700106 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800107 else if (strncmp(buf, "update", 6) == 0)
David Brownellab6a2d72007-05-08 00:33:30 -0700108 rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800109 else
110 retval = -EINVAL;
David Brownelld728b1e2006-11-25 11:09:28 -0800111 local_irq_enable();
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800112
113 return retval;
114}
115static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
116
117static int test_probe(struct platform_device *plat_dev)
118{
119 int err;
120 struct rtc_device *rtc = rtc_device_register("test", &plat_dev->dev,
121 &test_rtc_ops, THIS_MODULE);
122 if (IS_ERR(rtc)) {
123 err = PTR_ERR(rtc);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800124 return err;
125 }
Jeff Garzik91046a82006-12-06 20:35:34 -0800126
127 err = device_create_file(&plat_dev->dev, &dev_attr_irq);
128 if (err)
129 goto err;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800130
131 platform_set_drvdata(plat_dev, rtc);
132
133 return 0;
Jeff Garzik91046a82006-12-06 20:35:34 -0800134
135err:
136 rtc_device_unregister(rtc);
137 return err;
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800138}
139
140static int __devexit test_remove(struct platform_device *plat_dev)
141{
142 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
143
144 rtc_device_unregister(rtc);
145 device_remove_file(&plat_dev->dev, &dev_attr_irq);
146
147 return 0;
148}
149
Sam Ravnborgc4646522008-04-28 02:11:55 -0700150static struct platform_driver test_driver = {
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800151 .probe = test_probe,
152 .remove = __devexit_p(test_remove),
153 .driver = {
154 .name = "rtc-test",
155 .owner = THIS_MODULE,
156 },
157};
158
159static int __init test_init(void)
160{
161 int err;
162
Sam Ravnborgc4646522008-04-28 02:11:55 -0700163 if ((err = platform_driver_register(&test_driver)))
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800164 return err;
165
166 if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
167 err = -ENOMEM;
168 goto exit_driver_unregister;
169 }
170
171 if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
172 err = -ENOMEM;
173 goto exit_free_test0;
174 }
175
176 if ((err = platform_device_add(test0)))
177 goto exit_free_test1;
178
179 if ((err = platform_device_add(test1)))
180 goto exit_device_unregister;
181
182 return 0;
183
184exit_device_unregister:
185 platform_device_unregister(test0);
186
187exit_free_test1:
188 platform_device_put(test1);
189
190exit_free_test0:
191 platform_device_put(test0);
192
193exit_driver_unregister:
Sam Ravnborgc4646522008-04-28 02:11:55 -0700194 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800195 return err;
196}
197
198static void __exit test_exit(void)
199{
200 platform_device_unregister(test0);
201 platform_device_unregister(test1);
Sam Ravnborgc4646522008-04-28 02:11:55 -0700202 platform_driver_unregister(&test_driver);
Alessandro Zummoa95579c2006-03-27 01:16:42 -0800203}
204
205MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
206MODULE_DESCRIPTION("RTC test driver/device");
207MODULE_LICENSE("GPL");
208
209module_init(test_init);
210module_exit(test_exit);