2 * RTC subsystem, sysfs interface
4 * Copyright (C) 2005 Tower Technologies
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 */
21 * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
22 * ideally UTC. However, PCs that also boot to MS-Windows normally use
23 * the local time and change to match daylight savings time. That affects
24 * attributes including date, time, since_epoch, and wakealarm.
28 name_show(struct device *dev, struct device_attribute *attr, char *buf)
30 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
31 dev_name(dev->parent));
33 static DEVICE_ATTR_RO(name);
36 date_show(struct device *dev, struct device_attribute *attr, char *buf)
41 retval = rtc_read_time(to_rtc_device(dev), &tm);
43 retval = sprintf(buf, "%04d-%02d-%02d\n",
44 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
49 static DEVICE_ATTR_RO(date);
52 time_show(struct device *dev, struct device_attribute *attr, char *buf)
57 retval = rtc_read_time(to_rtc_device(dev), &tm);
59 retval = sprintf(buf, "%02d:%02d:%02d\n",
60 tm.tm_hour, tm.tm_min, tm.tm_sec);
65 static DEVICE_ATTR_RO(time);
68 since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
73 retval = rtc_read_time(to_rtc_device(dev), &tm);
76 rtc_tm_to_time(&tm, &time);
77 retval = sprintf(buf, "%lu\n", time);
82 static DEVICE_ATTR_RO(since_epoch);
85 max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
87 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
91 max_user_freq_store(struct device *dev, struct device_attribute *attr,
92 const char *buf, size_t n)
94 struct rtc_device *rtc = to_rtc_device(dev);
98 err = kstrtoul(buf, 0, &val);
102 if (val >= 4096 || val == 0)
105 rtc->max_user_freq = (int)val;
109 static DEVICE_ATTR_RW(max_user_freq);
112 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
114 * Returns 1 if the system clock was set by this RTC at the last
115 * boot or resume event.
118 hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
120 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
121 if (rtc_hctosys_ret == 0 &&
122 strcmp(dev_name(&to_rtc_device(dev)->dev),
123 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
124 return sprintf(buf, "1\n");
127 return sprintf(buf, "0\n");
129 static DEVICE_ATTR_RO(hctosys);
132 wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
136 struct rtc_wkalrm alm;
138 /* Don't show disabled alarms. For uniformity, RTC alarms are
139 * conceptually one-shot, even though some common RTCs (on PCs)
140 * don't actually work that way.
142 * NOTE: RTC implementations where the alarm doesn't match an
143 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
144 * alarms after they trigger, to ensure one-shot semantics.
146 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
147 if (retval == 0 && alm.enabled) {
148 rtc_tm_to_time(&alm.time, &alarm);
149 retval = sprintf(buf, "%lu\n", alarm);
156 wakealarm_store(struct device *dev, struct device_attribute *attr,
157 const char *buf, size_t n)
160 unsigned long now, alarm;
161 unsigned long push = 0;
162 struct rtc_wkalrm alm;
163 struct rtc_device *rtc = to_rtc_device(dev);
167 /* Only request alarms that trigger in the future. Disable them
168 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
170 retval = rtc_read_time(rtc, &alm.time);
173 rtc_tm_to_time(&alm.time, &now);
176 if (*buf_ptr == '+') {
178 if (*buf_ptr == '=') {
184 retval = kstrtoul(buf_ptr, 0, &alarm);
190 if (alarm > now || push) {
191 /* Avoid accidentally clobbering active alarms; we can't
192 * entirely prevent that here, without even the minimal
193 * locking from the /dev/rtcN api.
195 retval = rtc_read_alarm(rtc, &alm);
200 rtc_tm_to_time(&alm.time, &push);
210 /* Provide a valid future alarm time. Linux isn't EFI,
211 * this time won't be ignored when disabling the alarm.
215 rtc_time_to_tm(alarm, &alm.time);
217 retval = rtc_set_alarm(rtc, &alm);
218 return (retval < 0) ? retval : n;
220 static DEVICE_ATTR_RW(wakealarm);
223 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
228 retval = rtc_read_offset(to_rtc_device(dev), &offset);
230 retval = sprintf(buf, "%ld\n", offset);
236 offset_store(struct device *dev, struct device_attribute *attr,
237 const char *buf, size_t n)
242 retval = kstrtol(buf, 10, &offset);
244 retval = rtc_set_offset(to_rtc_device(dev), offset);
246 return (retval < 0) ? retval : n;
248 static DEVICE_ATTR_RW(offset);
250 static struct attribute *rtc_attrs[] = {
254 &dev_attr_since_epoch.attr,
255 &dev_attr_max_user_freq.attr,
256 &dev_attr_hctosys.attr,
257 &dev_attr_wakealarm.attr,
258 &dev_attr_offset.attr,
262 /* The reason to trigger an alarm with no process watching it (via sysfs)
263 * is its side effect: waking from a system state like suspend-to-RAM or
264 * suspend-to-disk. So: no attribute unless that side effect is possible.
265 * (Userspace may disable that mechanism later.)
267 static bool rtc_does_wakealarm(struct rtc_device *rtc)
269 if (!device_can_wakeup(rtc->dev.parent))
272 return rtc->ops->set_alarm != NULL;
275 static umode_t rtc_attr_is_visible(struct kobject *kobj,
276 struct attribute *attr, int n)
278 struct device *dev = container_of(kobj, struct device, kobj);
279 struct rtc_device *rtc = to_rtc_device(dev);
280 umode_t mode = attr->mode;
282 if (attr == &dev_attr_wakealarm.attr) {
283 if (!rtc_does_wakealarm(rtc))
285 } else if (attr == &dev_attr_offset.attr) {
286 if (!rtc->ops->set_offset)
293 static struct attribute_group rtc_attr_group = {
294 .is_visible = rtc_attr_is_visible,
298 static const struct attribute_group *rtc_attr_groups[] = {
303 const struct attribute_group **rtc_get_dev_attribute_groups(void)
305 return rtc_attr_groups;