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 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 53 | static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 54 | { |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 55 | return 0; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 56 | } |
| 57 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 58 | static const struct rtc_class_ops test_rtc_ops = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 59 | .proc = test_rtc_proc, |
| 60 | .read_time = test_rtc_read_time, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 61 | .read_alarm = test_rtc_read_alarm, |
| 62 | .set_alarm = test_rtc_set_alarm, |
| 63 | .set_mmss = test_rtc_set_mmss, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 64 | .alarm_irq_enable = test_rtc_alarm_irq_enable, |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static ssize_t test_irq_show(struct device *dev, |
| 68 | struct device_attribute *attr, char *buf) |
| 69 | { |
| 70 | return sprintf(buf, "%d\n", 42); |
| 71 | } |
| 72 | static 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 Jimenez | a417493 | 2011-02-07 19:16:07 -0200 | [diff] [blame] | 81 | if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 82 | rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF); |
Marcelo Roberto Jimenez | a417493 | 2011-02-07 19:16:07 -0200 | [diff] [blame] | 83 | 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 Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 91 | rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 92 | else |
| 93 | retval = -EINVAL; |
| 94 | |
| 95 | return retval; |
| 96 | } |
| 97 | static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store); |
| 98 | |
| 99 | static int test_probe(struct platform_device *plat_dev) |
| 100 | { |
| 101 | int err; |
| 102 | struct rtc_device *rtc = rtc_device_register("test", &plat_dev->dev, |
| 103 | &test_rtc_ops, THIS_MODULE); |
| 104 | if (IS_ERR(rtc)) { |
| 105 | err = PTR_ERR(rtc); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 106 | return err; |
| 107 | } |
Jeff Garzik | 91046a8 | 2006-12-06 20:35:34 -0800 | [diff] [blame] | 108 | |
| 109 | err = device_create_file(&plat_dev->dev, &dev_attr_irq); |
| 110 | if (err) |
| 111 | goto err; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 112 | |
| 113 | platform_set_drvdata(plat_dev, rtc); |
| 114 | |
| 115 | return 0; |
Jeff Garzik | 91046a8 | 2006-12-06 20:35:34 -0800 | [diff] [blame] | 116 | |
| 117 | err: |
| 118 | rtc_device_unregister(rtc); |
| 119 | return err; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static int __devexit test_remove(struct platform_device *plat_dev) |
| 123 | { |
| 124 | struct rtc_device *rtc = platform_get_drvdata(plat_dev); |
| 125 | |
| 126 | rtc_device_unregister(rtc); |
| 127 | device_remove_file(&plat_dev->dev, &dev_attr_irq); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 132 | static struct platform_driver test_driver = { |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 133 | .probe = test_probe, |
| 134 | .remove = __devexit_p(test_remove), |
| 135 | .driver = { |
| 136 | .name = "rtc-test", |
| 137 | .owner = THIS_MODULE, |
| 138 | }, |
| 139 | }; |
| 140 | |
| 141 | static int __init test_init(void) |
| 142 | { |
| 143 | int err; |
| 144 | |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 145 | if ((err = platform_driver_register(&test_driver))) |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 146 | return err; |
| 147 | |
| 148 | if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) { |
| 149 | err = -ENOMEM; |
| 150 | goto exit_driver_unregister; |
| 151 | } |
| 152 | |
| 153 | if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) { |
| 154 | err = -ENOMEM; |
Wei Yongjun | 942bfb3 | 2012-12-17 16:02:27 -0800 | [diff] [blame^] | 155 | goto exit_put_test0; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | if ((err = platform_device_add(test0))) |
Wei Yongjun | 942bfb3 | 2012-12-17 16:02:27 -0800 | [diff] [blame^] | 159 | goto exit_put_test1; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 160 | |
| 161 | if ((err = platform_device_add(test1))) |
Wei Yongjun | 942bfb3 | 2012-12-17 16:02:27 -0800 | [diff] [blame^] | 162 | goto exit_del_test0; |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | |
Wei Yongjun | 942bfb3 | 2012-12-17 16:02:27 -0800 | [diff] [blame^] | 166 | exit_del_test0: |
| 167 | platform_device_del(test0); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 168 | |
Wei Yongjun | 942bfb3 | 2012-12-17 16:02:27 -0800 | [diff] [blame^] | 169 | exit_put_test1: |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 170 | platform_device_put(test1); |
| 171 | |
Wei Yongjun | 942bfb3 | 2012-12-17 16:02:27 -0800 | [diff] [blame^] | 172 | exit_put_test0: |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 173 | platform_device_put(test0); |
| 174 | |
| 175 | exit_driver_unregister: |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 176 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 177 | return err; |
| 178 | } |
| 179 | |
| 180 | static void __exit test_exit(void) |
| 181 | { |
| 182 | platform_device_unregister(test0); |
| 183 | platform_device_unregister(test1); |
Sam Ravnborg | c464652 | 2008-04-28 02:11:55 -0700 | [diff] [blame] | 184 | platform_driver_unregister(&test_driver); |
Alessandro Zummo | a95579c | 2006-03-27 01:16:42 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 188 | MODULE_DESCRIPTION("RTC test driver/device"); |
| 189 | MODULE_LICENSE("GPL"); |
| 190 | |
| 191 | module_init(test_init); |
| 192 | module_exit(test_exit); |