2 * RTC subsystem, sysfs interface
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/rtc.h>
18 /* device attributes */
20 static ssize_t rtc_sysfs_show_name(struct class_device *dev, char *buf)
22 return sprintf(buf, "%s\n", to_rtc_device(dev)->name);
24 static CLASS_DEVICE_ATTR(name, S_IRUGO, rtc_sysfs_show_name, NULL);
26 static ssize_t rtc_sysfs_show_date(struct class_device *dev, char *buf)
31 retval = rtc_read_time(to_rtc_device(dev), &tm);
33 retval = sprintf(buf, "%04d-%02d-%02d\n",
34 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
39 static CLASS_DEVICE_ATTR(date, S_IRUGO, rtc_sysfs_show_date, NULL);
41 static ssize_t rtc_sysfs_show_time(struct class_device *dev, char *buf)
46 retval = rtc_read_time(to_rtc_device(dev), &tm);
48 retval = sprintf(buf, "%02d:%02d:%02d\n",
49 tm.tm_hour, tm.tm_min, tm.tm_sec);
54 static CLASS_DEVICE_ATTR(time, S_IRUGO, rtc_sysfs_show_time, NULL);
56 static ssize_t rtc_sysfs_show_since_epoch(struct class_device *dev, char *buf)
61 retval = rtc_read_time(to_rtc_device(dev), &tm);
64 rtc_tm_to_time(&tm, &time);
65 retval = sprintf(buf, "%lu\n", time);
70 static CLASS_DEVICE_ATTR(since_epoch, S_IRUGO, rtc_sysfs_show_since_epoch, NULL);
72 static struct attribute *rtc_attrs[] = {
73 &class_device_attr_name.attr,
74 &class_device_attr_date.attr,
75 &class_device_attr_time.attr,
76 &class_device_attr_since_epoch.attr,
80 static struct attribute_group rtc_attr_group = {
86 rtc_sysfs_show_wakealarm(struct class_device *dev, char *buf)
90 struct rtc_wkalrm alm;
92 /* Don't show disabled alarms; but the RTC could leave the
93 * alarm enabled after it's already triggered. Alarms are
94 * conceptually one-shot, even though some common hardware
95 * (PCs) doesn't actually work that way.
97 * REVISIT maybe we should require RTC implementations to
98 * disable the RTC alarm after it triggers, for uniformity.
100 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
101 if (retval == 0 && alm.enabled) {
102 rtc_tm_to_time(&alm.time, &alarm);
103 retval = sprintf(buf, "%lu\n", alarm);
110 rtc_sysfs_set_wakealarm(struct class_device *dev, const char *buf, size_t n)
113 unsigned long now, alarm;
114 struct rtc_wkalrm alm;
115 struct rtc_device *rtc = to_rtc_device(dev);
117 /* Only request alarms that trigger in the future. Disable them
118 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
120 retval = rtc_read_time(rtc, &alm.time);
123 rtc_tm_to_time(&alm.time, &now);
125 alarm = simple_strtoul(buf, NULL, 0);
127 /* Avoid accidentally clobbering active alarms; we can't
128 * entirely prevent that here, without even the minimal
129 * locking from the /dev/rtcN api.
131 retval = rtc_read_alarm(rtc, &alm);
141 /* Provide a valid future alarm time. Linux isn't EFI,
142 * this time won't be ignored when disabling the alarm.
146 rtc_time_to_tm(alarm, &alm.time);
148 retval = rtc_set_alarm(rtc, &alm);
149 return (retval < 0) ? retval : n;
151 static const CLASS_DEVICE_ATTR(wakealarm, S_IRUGO | S_IWUSR,
152 rtc_sysfs_show_wakealarm, rtc_sysfs_set_wakealarm);
155 /* The reason to trigger an alarm with no process watching it (via sysfs)
156 * is its side effect: waking from a system state like suspend-to-RAM or
157 * suspend-to-disk. So: no attribute unless that side effect is possible.
158 * (Userspace may disable that mechanism later.)
160 static inline int rtc_does_wakealarm(struct class_device *class_dev)
162 struct rtc_device *rtc;
164 if (!device_can_wakeup(class_dev->dev))
166 rtc = to_rtc_device(class_dev);
167 return rtc->ops->set_alarm != NULL;
171 static int rtc_sysfs_add_device(struct class_device *class_dev,
172 struct class_interface *class_intf)
176 dev_dbg(class_dev->dev, "rtc intf: sysfs\n");
178 err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group);
180 dev_err(class_dev->dev, "failed to create %s\n",
182 else if (rtc_does_wakealarm(class_dev)) {
183 /* not all RTCs support both alarms and wakeup */
184 err = class_device_create_file(class_dev,
185 &class_device_attr_wakealarm);
187 dev_err(class_dev->dev, "failed to create %s\n",
189 sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
196 static void rtc_sysfs_remove_device(struct class_device *class_dev,
197 struct class_interface *class_intf)
199 if (rtc_does_wakealarm(class_dev))
200 class_device_remove_file(class_dev,
201 &class_device_attr_wakealarm);
202 sysfs_remove_group(&class_dev->kobj, &rtc_attr_group);
205 /* interface registration */
207 static struct class_interface rtc_sysfs_interface = {
208 .add = &rtc_sysfs_add_device,
209 .remove = &rtc_sysfs_remove_device,
212 static int __init rtc_sysfs_init(void)
214 return rtc_interface_register(&rtc_sysfs_interface);
217 static void __exit rtc_sysfs_exit(void)
219 class_interface_unregister(&rtc_sysfs_interface);
222 subsys_initcall(rtc_sysfs_init);
223 module_exit(rtc_sysfs_exit);
225 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
226 MODULE_DESCRIPTION("RTC class sysfs interface");
227 MODULE_LICENSE("GPL");