Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | static struct platform_device *test0 = NULL, *test1 = NULL; |
| 17 | |
| 18 | static int test_rtc_read_alarm(struct device *dev, |
| 19 | struct rtc_wkalrm *alrm) |
| 20 | { |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | static int test_rtc_set_alarm(struct device *dev, |
| 25 | struct rtc_wkalrm *alrm) |
| 26 | { |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | static 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 Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 37 | static int test_rtc_set_mmss(struct device *dev, unsigned long secs) |
| 38 | { |
Alessandro Zummo | bbccf83 | 2009-01-06 14:42:21 -0800 | [diff] [blame] | 39 | dev_info(dev, "%s, secs = %lu\n", __func__, secs); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int test_rtc_proc(struct device *dev, struct seq_file *seq) |
| 44 | { |
| 45 | struct platform_device *plat_dev = to_platform_device(dev); |
| 46 | |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 47 | 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 | |
| 53 | static int test_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 54 | unsigned long arg) |
| 55 | { |
| 56 | /* We do support interrupts, they're generated |
| 57 | * using the sysfs interface. |
| 58 | */ |
| 59 | switch (cmd) { |
| 60 | case RTC_PIE_ON: |
| 61 | case RTC_PIE_OFF: |
| 62 | case RTC_UIE_ON: |
| 63 | case RTC_UIE_OFF: |
| 64 | case RTC_AIE_ON: |
| 65 | case RTC_AIE_OFF: |
| 66 | return 0; |
| 67 | |
| 68 | default: |
Alessandro Zummo | b3969e5 | 2006-05-20 15:00:29 -0700 | [diff] [blame] | 69 | return -ENOIOCTLCMD; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 73 | static const struct rtc_class_ops test_rtc_ops = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 74 | .proc = test_rtc_proc, |
| 75 | .read_time = test_rtc_read_time, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 76 | .read_alarm = test_rtc_read_alarm, |
| 77 | .set_alarm = test_rtc_set_alarm, |
| 78 | .set_mmss = test_rtc_set_mmss, |
| 79 | .ioctl = test_rtc_ioctl, |
| 80 | }; |
| 81 | |
| 82 | static ssize_t test_irq_show(struct device *dev, |
| 83 | struct device_attribute *attr, char *buf) |
| 84 | { |
| 85 | return sprintf(buf, "%d\n", 42); |
| 86 | } |
| 87 | static ssize_t test_irq_store(struct device *dev, |
| 88 | struct device_attribute *attr, |
| 89 | const char *buf, size_t count) |
| 90 | { |
| 91 | int retval; |
| 92 | struct platform_device *plat_dev = to_platform_device(dev); |
| 93 | struct rtc_device *rtc = platform_get_drvdata(plat_dev); |
| 94 | |
| 95 | retval = count; |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 96 | local_irq_disable(); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 97 | if (strncmp(buf, "tick", 4) == 0) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 98 | rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 99 | else if (strncmp(buf, "alarm", 5) == 0) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 100 | rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 101 | else if (strncmp(buf, "update", 6) == 0) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 102 | rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 103 | else |
| 104 | retval = -EINVAL; |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 105 | local_irq_enable(); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 106 | |
| 107 | return retval; |
| 108 | } |
| 109 | static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store); |
| 110 | |
| 111 | static int test_probe(struct platform_device *plat_dev) |
| 112 | { |
| 113 | int err; |
| 114 | struct rtc_device *rtc = rtc_device_register("test", &plat_dev->dev, |
| 115 | &test_rtc_ops, THIS_MODULE); |
| 116 | if (IS_ERR(rtc)) { |
| 117 | err = PTR_ERR(rtc); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 118 | return err; |
| 119 | } |
Jeff Garzik | 91046a8 | 2006-12-06 20:35:34 -0800 | [diff] [blame] | 120 | |
| 121 | err = device_create_file(&plat_dev->dev, &dev_attr_irq); |
| 122 | if (err) |
| 123 | goto err; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 124 | |
| 125 | platform_set_drvdata(plat_dev, rtc); |
| 126 | |
| 127 | return 0; |
Jeff Garzik | 91046a8 | 2006-12-06 20:35:34 -0800 | [diff] [blame] | 128 | |
| 129 | err: |
| 130 | rtc_device_unregister(rtc); |
| 131 | return err; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int __devexit test_remove(struct platform_device *plat_dev) |
| 135 | { |
| 136 | struct rtc_device *rtc = platform_get_drvdata(plat_dev); |
| 137 | |
| 138 | rtc_device_unregister(rtc); |
| 139 | device_remove_file(&plat_dev->dev, &dev_attr_irq); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 144 | static struct platform_driver test_driver = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 145 | .probe = test_probe, |
| 146 | .remove = __devexit_p(test_remove), |
| 147 | .driver = { |
| 148 | .name = "rtc-test", |
| 149 | .owner = THIS_MODULE, |
| 150 | }, |
| 151 | }; |
| 152 | |
| 153 | static int __init test_init(void) |
| 154 | { |
| 155 | int err; |
| 156 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 157 | if ((err = platform_driver_register(&test_driver))) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 158 | return err; |
| 159 | |
| 160 | if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) { |
| 161 | err = -ENOMEM; |
| 162 | goto exit_driver_unregister; |
| 163 | } |
| 164 | |
| 165 | if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) { |
| 166 | err = -ENOMEM; |
| 167 | goto exit_free_test0; |
| 168 | } |
| 169 | |
| 170 | if ((err = platform_device_add(test0))) |
| 171 | goto exit_free_test1; |
| 172 | |
| 173 | if ((err = platform_device_add(test1))) |
| 174 | goto exit_device_unregister; |
| 175 | |
| 176 | return 0; |
| 177 | |
| 178 | exit_device_unregister: |
| 179 | platform_device_unregister(test0); |
| 180 | |
| 181 | exit_free_test1: |
| 182 | platform_device_put(test1); |
| 183 | |
| 184 | exit_free_test0: |
| 185 | platform_device_put(test0); |
| 186 | |
| 187 | exit_driver_unregister: |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 188 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static void __exit test_exit(void) |
| 193 | { |
| 194 | platform_device_unregister(test0); |
| 195 | platform_device_unregister(test1); |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 196 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 200 | MODULE_DESCRIPTION("RTC test driver/device"); |
| 201 | MODULE_LICENSE("GPL"); |
| 202 | |
| 203 | module_init(test_init); |
| 204 | module_exit(test_exit); |