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/module.h>
10 #include <linux/rtc.h>
14 /* device attributes */
17 * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
18 * ideally UTC. However, PCs that also boot to MS-Windows normally use
19 * the local time and change to match daylight savings time. That affects
20 * attributes including date, time, since_epoch, and wakealarm.
24 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
26 return sprintf(buf
, "%s %s\n", dev_driver_string(dev
->parent
),
27 dev_name(dev
->parent
));
29 static DEVICE_ATTR_RO(name
);
32 date_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
37 retval
= rtc_read_time(to_rtc_device(dev
), &tm
);
41 return sprintf(buf
, "%ptRd\n", &tm
);
43 static DEVICE_ATTR_RO(date
);
46 time_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
51 retval
= rtc_read_time(to_rtc_device(dev
), &tm
);
55 return sprintf(buf
, "%ptRt\n", &tm
);
57 static DEVICE_ATTR_RO(time
);
60 since_epoch_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
65 retval
= rtc_read_time(to_rtc_device(dev
), &tm
);
69 time
= rtc_tm_to_time64(&tm
);
70 retval
= sprintf(buf
, "%lld\n", time
);
75 static DEVICE_ATTR_RO(since_epoch
);
78 max_user_freq_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
80 return sprintf(buf
, "%d\n", to_rtc_device(dev
)->max_user_freq
);
84 max_user_freq_store(struct device
*dev
, struct device_attribute
*attr
,
85 const char *buf
, size_t n
)
87 struct rtc_device
*rtc
= to_rtc_device(dev
);
91 err
= kstrtoul(buf
, 0, &val
);
95 if (val
>= 4096 || val
== 0)
98 rtc
->max_user_freq
= (int)val
;
102 static DEVICE_ATTR_RW(max_user_freq
);
105 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
106 * @dev: The device that the attribute belongs to.
107 * @attr: The attribute being read.
108 * @buf: The result buffer.
110 * buf is "1" if the system clock was set by this RTC at the last
111 * boot or resume event.
114 hctosys_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
116 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
117 if (rtc_hctosys_ret
== 0 &&
118 strcmp(dev_name(&to_rtc_device(dev
)->dev
),
119 CONFIG_RTC_HCTOSYS_DEVICE
) == 0)
120 return sprintf(buf
, "1\n");
122 return sprintf(buf
, "0\n");
124 static DEVICE_ATTR_RO(hctosys
);
127 wakealarm_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
131 struct rtc_wkalrm alm
;
133 /* Don't show disabled alarms. For uniformity, RTC alarms are
134 * conceptually one-shot, even though some common RTCs (on PCs)
135 * don't actually work that way.
137 * NOTE: RTC implementations where the alarm doesn't match an
138 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
139 * alarms after they trigger, to ensure one-shot semantics.
141 retval
= rtc_read_alarm(to_rtc_device(dev
), &alm
);
142 if (retval
== 0 && alm
.enabled
) {
143 alarm
= rtc_tm_to_time64(&alm
.time
);
144 retval
= sprintf(buf
, "%lld\n", alarm
);
151 wakealarm_store(struct device
*dev
, struct device_attribute
*attr
,
152 const char *buf
, size_t n
)
157 struct rtc_wkalrm alm
;
158 struct rtc_device
*rtc
= to_rtc_device(dev
);
162 /* Only request alarms that trigger in the future. Disable them
163 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
165 retval
= rtc_read_time(rtc
, &alm
.time
);
168 now
= rtc_tm_to_time64(&alm
.time
);
171 if (*buf_ptr
== '+') {
173 if (*buf_ptr
== '=') {
180 retval
= kstrtos64(buf_ptr
, 0, &alarm
);
185 if (alarm
> now
|| push
) {
186 /* Avoid accidentally clobbering active alarms; we can't
187 * entirely prevent that here, without even the minimal
188 * locking from the /dev/rtcN api.
190 retval
= rtc_read_alarm(rtc
, &alm
);
195 push
= rtc_tm_to_time64(&alm
.time
);
205 /* Provide a valid future alarm time. Linux isn't EFI,
206 * this time won't be ignored when disabling the alarm.
210 rtc_time64_to_tm(alarm
, &alm
.time
);
212 retval
= rtc_set_alarm(rtc
, &alm
);
213 return (retval
< 0) ? retval
: n
;
215 static DEVICE_ATTR_RW(wakealarm
);
218 offset_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
223 retval
= rtc_read_offset(to_rtc_device(dev
), &offset
);
225 retval
= sprintf(buf
, "%ld\n", offset
);
231 offset_store(struct device
*dev
, struct device_attribute
*attr
,
232 const char *buf
, size_t n
)
237 retval
= kstrtol(buf
, 10, &offset
);
239 retval
= rtc_set_offset(to_rtc_device(dev
), offset
);
241 return (retval
< 0) ? retval
: n
;
243 static DEVICE_ATTR_RW(offset
);
246 range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
248 return sprintf(buf
, "[%lld,%llu]\n", to_rtc_device(dev
)->range_min
,
249 to_rtc_device(dev
)->range_max
);
251 static DEVICE_ATTR_RO(range
);
253 static struct attribute
*rtc_attrs
[] = {
257 &dev_attr_since_epoch
.attr
,
258 &dev_attr_max_user_freq
.attr
,
259 &dev_attr_hctosys
.attr
,
260 &dev_attr_wakealarm
.attr
,
261 &dev_attr_offset
.attr
,
262 &dev_attr_range
.attr
,
266 /* The reason to trigger an alarm with no process watching it (via sysfs)
267 * is its side effect: waking from a system state like suspend-to-RAM or
268 * suspend-to-disk. So: no attribute unless that side effect is possible.
269 * (Userspace may disable that mechanism later.)
271 static bool rtc_does_wakealarm(struct rtc_device
*rtc
)
273 if (!device_can_wakeup(rtc
->dev
.parent
))
276 return rtc
->ops
->set_alarm
!= NULL
;
279 static umode_t
rtc_attr_is_visible(struct kobject
*kobj
,
280 struct attribute
*attr
, int n
)
282 struct device
*dev
= kobj_to_dev(kobj
);
283 struct rtc_device
*rtc
= to_rtc_device(dev
);
284 umode_t mode
= attr
->mode
;
286 if (attr
== &dev_attr_wakealarm
.attr
) {
287 if (!rtc_does_wakealarm(rtc
))
289 } else if (attr
== &dev_attr_offset
.attr
) {
290 if (!rtc
->ops
->set_offset
)
292 } else if (attr
== &dev_attr_range
.attr
) {
293 if (!(rtc
->range_max
- rtc
->range_min
))
300 static struct attribute_group rtc_attr_group
= {
301 .is_visible
= rtc_attr_is_visible
,
305 static const struct attribute_group
*rtc_attr_groups
[] = {
310 const struct attribute_group
**rtc_get_dev_attribute_groups(void)
312 return rtc_attr_groups
;
315 int rtc_add_groups(struct rtc_device
*rtc
, const struct attribute_group
**grps
)
317 size_t old_cnt
= 0, add_cnt
= 0, new_cnt
;
318 const struct attribute_group
**groups
, **old
;
323 groups
= rtc
->dev
.groups
;
325 for (; *groups
; groups
++)
328 for (groups
= grps
; *groups
; groups
++)
331 new_cnt
= old_cnt
+ add_cnt
+ 1;
332 groups
= devm_kcalloc(&rtc
->dev
, new_cnt
, sizeof(*groups
), GFP_KERNEL
);
335 memcpy(groups
, rtc
->dev
.groups
, old_cnt
* sizeof(*groups
));
336 memcpy(groups
+ old_cnt
, grps
, add_cnt
* sizeof(*groups
));
337 groups
[old_cnt
+ add_cnt
] = NULL
;
339 old
= rtc
->dev
.groups
;
340 rtc
->dev
.groups
= groups
;
341 if (old
&& old
!= rtc_attr_groups
)
342 devm_kfree(&rtc
->dev
, old
);
346 EXPORT_SYMBOL(rtc_add_groups
);
348 int rtc_add_group(struct rtc_device
*rtc
, const struct attribute_group
*grp
)
350 const struct attribute_group
*groups
[] = { grp
, NULL
};
352 return rtc_add_groups(rtc
, groups
);
354 EXPORT_SYMBOL(rtc_add_group
);