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 37 static int test_rtc_set_time(struct device *dev, 38 struct rtc_time *tm) 39 { 40 return 0; 41 } 42 43 static int test_rtc_set_mmss(struct device *dev, unsigned long secs) 44 { 45 return 0; 46 } 47 48 static int test_rtc_proc(struct device *dev, struct seq_file *seq) 49 { 50 struct platform_device *plat_dev = to_platform_device(dev); 51 52 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 58 static 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: 74 return -ENOIOCTLCMD; 75 } 76 } 77 78 static struct rtc_class_ops test_rtc_ops = { 79 .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 88 static ssize_t test_irq_show(struct device *dev, 89 struct device_attribute *attr, char *buf) 90 { 91 return sprintf(buf, "%d\n", 42); 92 } 93 static 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; 102 if (strncmp(buf, "tick", 4) == 0) 103 rtc_update_irq(&rtc->class_dev, 1, RTC_PF | RTC_IRQF); 104 else if (strncmp(buf, "alarm", 5) == 0) 105 rtc_update_irq(&rtc->class_dev, 1, RTC_AF | RTC_IRQF); 106 else if (strncmp(buf, "update", 6) == 0) 107 rtc_update_irq(&rtc->class_dev, 1, RTC_UF | RTC_IRQF); 108 else 109 retval = -EINVAL; 110 111 return retval; 112 } 113 static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store); 114 115 static int test_probe(struct platform_device *plat_dev) 116 { 117 int err; 118 struct rtc_device *rtc = rtc_device_register("test", &plat_dev->dev, 119 &test_rtc_ops, THIS_MODULE); 120 if (IS_ERR(rtc)) { 121 err = PTR_ERR(rtc); 122 return err; 123 } 124 device_create_file(&plat_dev->dev, &dev_attr_irq); 125 126 platform_set_drvdata(plat_dev, rtc); 127 128 return 0; 129 } 130 131 static int __devexit test_remove(struct platform_device *plat_dev) 132 { 133 struct rtc_device *rtc = platform_get_drvdata(plat_dev); 134 135 rtc_device_unregister(rtc); 136 device_remove_file(&plat_dev->dev, &dev_attr_irq); 137 138 return 0; 139 } 140 141 static struct platform_driver test_drv = { 142 .probe = test_probe, 143 .remove = __devexit_p(test_remove), 144 .driver = { 145 .name = "rtc-test", 146 .owner = THIS_MODULE, 147 }, 148 }; 149 150 static int __init test_init(void) 151 { 152 int err; 153 154 if ((err = platform_driver_register(&test_drv))) 155 return err; 156 157 if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) { 158 err = -ENOMEM; 159 goto exit_driver_unregister; 160 } 161 162 if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) { 163 err = -ENOMEM; 164 goto exit_free_test0; 165 } 166 167 if ((err = platform_device_add(test0))) 168 goto exit_free_test1; 169 170 if ((err = platform_device_add(test1))) 171 goto exit_device_unregister; 172 173 return 0; 174 175 exit_device_unregister: 176 platform_device_unregister(test0); 177 178 exit_free_test1: 179 platform_device_put(test1); 180 181 exit_free_test0: 182 platform_device_put(test0); 183 184 exit_driver_unregister: 185 platform_driver_unregister(&test_drv); 186 return err; 187 } 188 189 static void __exit test_exit(void) 190 { 191 platform_device_unregister(test0); 192 platform_device_unregister(test1); 193 platform_driver_unregister(&test_drv); 194 } 195 196 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); 197 MODULE_DESCRIPTION("RTC test driver/device"); 198 MODULE_LICENSE("GPL"); 199 200 module_init(test_init); 201 module_exit(test_exit); 202