1 // SPDX-License-Identifier: GPL-2.0
3 * RTC subsystem, sysfs interface
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
9 #include <linux/kstrtox.h>
10 #include <linux/module.h>
11 #include <linux/rtc.h>
15 /* device attributes */
18 * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
19 * ideally UTC. However, PCs that also boot to MS-Windows normally use
20 * the local time and change to match daylight savings time. That affects
21 * attributes including date, time, since_epoch, and wakealarm.
25 name_show(struct device *dev, struct device_attribute *attr, char *buf)
27 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
28 dev_name(dev->parent));
30 static DEVICE_ATTR_RO(name);
33 date_show(struct device *dev, struct device_attribute *attr, char *buf)
38 retval = rtc_read_time(to_rtc_device(dev), &tm);
42 return sprintf(buf, "%ptRd\n", &tm);
44 static DEVICE_ATTR_RO(date);
47 time_show(struct device *dev, struct device_attribute *attr, char *buf)
52 retval = rtc_read_time(to_rtc_device(dev), &tm);
56 return sprintf(buf, "%ptRt\n", &tm);
58 static DEVICE_ATTR_RO(time);
61 since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
66 retval = rtc_read_time(to_rtc_device(dev), &tm);
70 time = rtc_tm_to_time64(&tm);
71 retval = sprintf(buf, "%lld\n", time);
76 static DEVICE_ATTR_RO(since_epoch);
79 max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
81 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
85 max_user_freq_store(struct device *dev, struct device_attribute *attr,
86 const char *buf, size_t n)
88 struct rtc_device *rtc = to_rtc_device(dev);
92 err = kstrtoul(buf, 0, &val);
96 if (val >= 4096 || val == 0)
99 rtc->max_user_freq = (int)val;
103 static DEVICE_ATTR_RW(max_user_freq);
106 * hctosys_show - indicate if the given RTC set the system time
107 * @dev: The device that the attribute belongs to.
108 * @attr: The attribute being read.
109 * @buf: The result buffer.
111 * buf is "1" if the system clock was set by this RTC at the last
112 * boot or resume event.
115 hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
117 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
118 if (rtc_hctosys_ret == 0 &&
119 strcmp(dev_name(&to_rtc_device(dev)->dev),
120 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
121 return sprintf(buf, "1\n");
123 return sprintf(buf, "0\n");
125 static DEVICE_ATTR_RO(hctosys);
128 wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
132 struct rtc_wkalrm alm;
134 /* Don't show disabled alarms. For uniformity, RTC alarms are
135 * conceptually one-shot, even though some common RTCs (on PCs)
136 * don't actually work that way.
138 * NOTE: RTC implementations where the alarm doesn't match an
139 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
140 * alarms after they trigger, to ensure one-shot semantics.
142 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
143 if (retval == 0 && alm.enabled) {
144 alarm = rtc_tm_to_time64(&alm.time);
145 retval = sprintf(buf, "%lld\n", alarm);
152 wakealarm_store(struct device *dev, struct device_attribute *attr,
153 const char *buf, size_t n)
158 struct rtc_wkalrm alm;
159 struct rtc_device *rtc = to_rtc_device(dev);
163 /* Only request alarms that trigger in the future. Disable them
164 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
166 retval = rtc_read_time(rtc, &alm.time);
169 now = rtc_tm_to_time64(&alm.time);
172 if (*buf_ptr == '+') {
174 if (*buf_ptr == '=') {
181 retval = kstrtos64(buf_ptr, 0, &alarm);
186 if (alarm > now || push) {
187 /* Avoid accidentally clobbering active alarms; we can't
188 * entirely prevent that here, without even the minimal
189 * locking from the /dev/rtcN api.
191 retval = rtc_read_alarm(rtc, &alm);
196 push = rtc_tm_to_time64(&alm.time);
206 /* Provide a valid future alarm time. Linux isn't EFI,
207 * this time won't be ignored when disabling the alarm.
211 rtc_time64_to_tm(alarm, &alm.time);
213 retval = rtc_set_alarm(rtc, &alm);
214 return (retval < 0) ? retval : n;
216 static DEVICE_ATTR_RW(wakealarm);
219 offset_show(struct device *dev, struct device_attribute *attr, char *buf)
224 retval = rtc_read_offset(to_rtc_device(dev), &offset);
226 retval = sprintf(buf, "%ld\n", offset);
232 offset_store(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t n)
238 retval = kstrtol(buf, 10, &offset);
240 retval = rtc_set_offset(to_rtc_device(dev), offset);
242 return (retval < 0) ? retval : n;
244 static DEVICE_ATTR_RW(offset);
247 range_show(struct device *dev, struct device_attribute *attr, char *buf)
249 return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
250 to_rtc_device(dev)->range_max);
252 static DEVICE_ATTR_RO(range);
254 static struct attribute *rtc_attrs[] = {
258 &dev_attr_since_epoch.attr,
259 &dev_attr_max_user_freq.attr,
260 &dev_attr_hctosys.attr,
261 &dev_attr_wakealarm.attr,
262 &dev_attr_offset.attr,
263 &dev_attr_range.attr,
267 /* The reason to trigger an alarm with no process watching it (via sysfs)
268 * is its side effect: waking from a system state like suspend-to-RAM or
269 * suspend-to-disk. So: no attribute unless that side effect is possible.
270 * (Userspace may disable that mechanism later.)
272 static bool rtc_does_wakealarm(struct rtc_device *rtc)
274 if (!device_can_wakeup(rtc->dev.parent))
277 return !!test_bit(RTC_FEATURE_ALARM, rtc->features);
280 static umode_t rtc_attr_is_visible(struct kobject *kobj,
281 struct attribute *attr, int n)
283 struct device *dev = kobj_to_dev(kobj);
284 struct rtc_device *rtc = to_rtc_device(dev);
285 umode_t mode = attr->mode;
287 if (attr == &dev_attr_wakealarm.attr) {
288 if (!rtc_does_wakealarm(rtc))
290 } else if (attr == &dev_attr_offset.attr) {
291 if (!rtc->ops->set_offset)
293 } else if (attr == &dev_attr_range.attr) {
294 if (!(rtc->range_max - rtc->range_min))
301 static struct attribute_group rtc_attr_group = {
302 .is_visible = rtc_attr_is_visible,
306 static const struct attribute_group *rtc_attr_groups[] = {
311 const struct attribute_group **rtc_get_dev_attribute_groups(void)
313 return rtc_attr_groups;
316 int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
318 size_t old_cnt = 0, add_cnt = 0, new_cnt;
319 const struct attribute_group **groups, **old;
324 groups = rtc->dev.groups;
326 for (; *groups; groups++)
329 for (groups = grps; *groups; groups++)
332 new_cnt = old_cnt + add_cnt + 1;
333 groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
336 memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
337 memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
338 groups[old_cnt + add_cnt] = NULL;
340 old = rtc->dev.groups;
341 rtc->dev.groups = groups;
342 if (old && old != rtc_attr_groups)
343 devm_kfree(&rtc->dev, old);
347 EXPORT_SYMBOL(rtc_add_groups);
349 int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
351 const struct attribute_group *groups[] = { grp, NULL };
353 return rtc_add_groups(rtc, groups);
355 EXPORT_SYMBOL(rtc_add_group);