]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/rtc/sysfs.c
rtc: x1205: Add DT probing support
[mirror_ubuntu-jammy-kernel.git] / drivers / rtc / sysfs.c
CommitLineData
cdf7545a 1// SPDX-License-Identifier: GPL-2.0
c5c3e192
AZ
2/*
3 * RTC subsystem, sysfs interface
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
cdf7545a 7 */
c5c3e192
AZ
8
9#include <linux/module.h>
10#include <linux/rtc.h>
11
ab6a2d70
DB
12#include "rtc-core.h"
13
14
c5c3e192
AZ
15/* device attributes */
16
8a0bdfd7
DB
17/*
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.
22 */
23
cd966209 24static ssize_t
f21e6835 25name_show(struct device *dev, struct device_attribute *attr, char *buf)
c5c3e192 26{
77a73f3c
AB
27 return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
28 dev_name(dev->parent));
c5c3e192 29}
f21e6835 30static DEVICE_ATTR_RO(name);
c5c3e192 31
cd966209 32static ssize_t
f21e6835 33date_show(struct device *dev, struct device_attribute *attr, char *buf)
c5c3e192
AZ
34{
35 ssize_t retval;
36 struct rtc_time tm;
37
ab6a2d70 38 retval = rtc_read_time(to_rtc_device(dev), &tm);
5548cbf7
AS
39 if (retval)
40 return retval;
c5c3e192 41
5548cbf7 42 return sprintf(buf, "%ptRd\n", &tm);
c5c3e192 43}
f21e6835 44static DEVICE_ATTR_RO(date);
c5c3e192 45
cd966209 46static ssize_t
f21e6835 47time_show(struct device *dev, struct device_attribute *attr, char *buf)
c5c3e192
AZ
48{
49 ssize_t retval;
50 struct rtc_time tm;
51
ab6a2d70 52 retval = rtc_read_time(to_rtc_device(dev), &tm);
5548cbf7
AS
53 if (retval)
54 return retval;
c5c3e192 55
5548cbf7 56 return sprintf(buf, "%ptRt\n", &tm);
c5c3e192 57}
f21e6835 58static DEVICE_ATTR_RO(time);
c5c3e192 59
cd966209 60static ssize_t
f21e6835 61since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
c5c3e192
AZ
62{
63 ssize_t retval;
64 struct rtc_time tm;
65
ab6a2d70 66 retval = rtc_read_time(to_rtc_device(dev), &tm);
c5c3e192 67 if (retval == 0) {
9a06da2e
BW
68 time64_t time;
69
70 time = rtc_tm_to_time64(&tm);
71 retval = sprintf(buf, "%lld\n", time);
c5c3e192
AZ
72 }
73
74 return retval;
75}
f21e6835 76static DEVICE_ATTR_RO(since_epoch);
c5c3e192 77
06c65eb4 78static ssize_t
f21e6835 79max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
06c65eb4
BK
80{
81 return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
82}
83
84static ssize_t
f21e6835 85max_user_freq_store(struct device *dev, struct device_attribute *attr,
06c65eb4
BK
86 const char *buf, size_t n)
87{
88 struct rtc_device *rtc = to_rtc_device(dev);
f571287b
LC
89 unsigned long val;
90 int err;
91
92 err = kstrtoul(buf, 0, &val);
93 if (err)
94 return err;
06c65eb4
BK
95
96 if (val >= 4096 || val == 0)
97 return -EINVAL;
98
99 rtc->max_user_freq = (int)val;
100
101 return n;
102}
f21e6835 103static DEVICE_ATTR_RW(max_user_freq);
06c65eb4 104
4c24e29e
DF
105/**
106 * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
107 *
108 * Returns 1 if the system clock was set by this RTC at the last
109 * boot or resume event.
110 */
d8c1acb1 111static ssize_t
f21e6835 112hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
d8c1acb1
MG
113{
114#ifdef CONFIG_RTC_HCTOSYS_DEVICE
d0ab4a4d
UKK
115 if (rtc_hctosys_ret == 0 &&
116 strcmp(dev_name(&to_rtc_device(dev)->dev),
117 CONFIG_RTC_HCTOSYS_DEVICE) == 0)
d8c1acb1
MG
118 return sprintf(buf, "1\n");
119 else
120#endif
121 return sprintf(buf, "0\n");
122}
f21e6835
GKH
123static DEVICE_ATTR_RO(hctosys);
124
3925a5ce 125static ssize_t
a17ccd1c 126wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
3925a5ce
DB
127{
128 ssize_t retval;
9a06da2e 129 time64_t alarm;
3925a5ce
DB
130 struct rtc_wkalrm alm;
131
8a0bdfd7
DB
132 /* Don't show disabled alarms. For uniformity, RTC alarms are
133 * conceptually one-shot, even though some common RTCs (on PCs)
134 * don't actually work that way.
3925a5ce 135 *
8a0bdfd7
DB
136 * NOTE: RTC implementations where the alarm doesn't match an
137 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
138 * alarms after they trigger, to ensure one-shot semantics.
3925a5ce 139 */
ab6a2d70 140 retval = rtc_read_alarm(to_rtc_device(dev), &alm);
3925a5ce 141 if (retval == 0 && alm.enabled) {
9a06da2e
BW
142 alarm = rtc_tm_to_time64(&alm.time);
143 retval = sprintf(buf, "%lld\n", alarm);
3925a5ce
DB
144 }
145
146 return retval;
147}
148
149static ssize_t
a17ccd1c 150wakealarm_store(struct device *dev, struct device_attribute *attr,
cd966209 151 const char *buf, size_t n)
3925a5ce
DB
152{
153 ssize_t retval;
9a06da2e
BW
154 time64_t now, alarm;
155 time64_t push = 0;
3925a5ce 156 struct rtc_wkalrm alm;
ab6a2d70 157 struct rtc_device *rtc = to_rtc_device(dev);
84281c2d 158 const char *buf_ptr;
c116bc2a 159 int adjust = 0;
3925a5ce
DB
160
161 /* Only request alarms that trigger in the future. Disable them
162 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
163 */
ab6a2d70 164 retval = rtc_read_time(rtc, &alm.time);
3925a5ce
DB
165 if (retval < 0)
166 return retval;
9a06da2e 167 now = rtc_tm_to_time64(&alm.time);
3925a5ce 168
84281c2d 169 buf_ptr = buf;
c116bc2a
ZY
170 if (*buf_ptr == '+') {
171 buf_ptr++;
1df0a471
BT
172 if (*buf_ptr == '=') {
173 buf_ptr++;
174 push = 1;
175 } else
176 adjust = 1;
c116bc2a 177 }
9a06da2e 178 retval = kstrtos64(buf_ptr, 0, &alarm);
f571287b
LC
179 if (retval)
180 return retval;
c116bc2a
ZY
181 if (adjust) {
182 alarm += now;
183 }
1df0a471 184 if (alarm > now || push) {
3925a5ce
DB
185 /* Avoid accidentally clobbering active alarms; we can't
186 * entirely prevent that here, without even the minimal
187 * locking from the /dev/rtcN api.
188 */
ab6a2d70 189 retval = rtc_read_alarm(rtc, &alm);
3925a5ce
DB
190 if (retval < 0)
191 return retval;
1df0a471
BT
192 if (alm.enabled) {
193 if (push) {
9a06da2e 194 push = rtc_tm_to_time64(&alm.time);
1df0a471
BT
195 alarm += push;
196 } else
197 return -EBUSY;
198 } else if (push)
199 return -EINVAL;
3925a5ce
DB
200 alm.enabled = 1;
201 } else {
202 alm.enabled = 0;
203
204 /* Provide a valid future alarm time. Linux isn't EFI,
205 * this time won't be ignored when disabling the alarm.
206 */
207 alarm = now + 300;
208 }
9a06da2e 209 rtc_time64_to_tm(alarm, &alm.time);
3925a5ce 210
ab6a2d70 211 retval = rtc_set_alarm(rtc, &alm);
3925a5ce
DB
212 return (retval < 0) ? retval : n;
213}
a17ccd1c 214static DEVICE_ATTR_RW(wakealarm);
3925a5ce 215
5495a415
JC
216static ssize_t
217offset_show(struct device *dev, struct device_attribute *attr, char *buf)
218{
219 ssize_t retval;
220 long offset;
221
222 retval = rtc_read_offset(to_rtc_device(dev), &offset);
223 if (retval == 0)
224 retval = sprintf(buf, "%ld\n", offset);
225
226 return retval;
227}
228
229static ssize_t
230offset_store(struct device *dev, struct device_attribute *attr,
231 const char *buf, size_t n)
232{
233 ssize_t retval;
234 long offset;
235
236 retval = kstrtol(buf, 10, &offset);
237 if (retval == 0)
238 retval = rtc_set_offset(to_rtc_device(dev), offset);
239
240 return (retval < 0) ? retval : n;
241}
242static DEVICE_ATTR_RW(offset);
243
71db049e
AB
244static ssize_t
245range_show(struct device *dev, struct device_attribute *attr, char *buf)
246{
247 return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
248 to_rtc_device(dev)->range_max);
249}
250static DEVICE_ATTR_RO(range);
251
3ee2c40b
DT
252static struct attribute *rtc_attrs[] = {
253 &dev_attr_name.attr,
254 &dev_attr_date.attr,
255 &dev_attr_time.attr,
256 &dev_attr_since_epoch.attr,
257 &dev_attr_max_user_freq.attr,
258 &dev_attr_hctosys.attr,
259 &dev_attr_wakealarm.attr,
5495a415 260 &dev_attr_offset.attr,
71db049e 261 &dev_attr_range.attr,
3ee2c40b
DT
262 NULL,
263};
3925a5ce
DB
264
265/* The reason to trigger an alarm with no process watching it (via sysfs)
266 * is its side effect: waking from a system state like suspend-to-RAM or
267 * suspend-to-disk. So: no attribute unless that side effect is possible.
268 * (Userspace may disable that mechanism later.)
269 */
df100c01 270static bool rtc_does_wakealarm(struct rtc_device *rtc)
3925a5ce 271{
cd966209 272 if (!device_can_wakeup(rtc->dev.parent))
df100c01
DT
273 return false;
274
3925a5ce
DB
275 return rtc->ops->set_alarm != NULL;
276}
277
3ee2c40b
DT
278static umode_t rtc_attr_is_visible(struct kobject *kobj,
279 struct attribute *attr, int n)
c5c3e192 280{
3ee2c40b
DT
281 struct device *dev = container_of(kobj, struct device, kobj);
282 struct rtc_device *rtc = to_rtc_device(dev);
283 umode_t mode = attr->mode;
c5c3e192 284
5495a415 285 if (attr == &dev_attr_wakealarm.attr) {
3ee2c40b
DT
286 if (!rtc_does_wakealarm(rtc))
287 mode = 0;
5495a415
JC
288 } else if (attr == &dev_attr_offset.attr) {
289 if (!rtc->ops->set_offset)
290 mode = 0;
71db049e
AB
291 } else if (attr == &dev_attr_range.attr) {
292 if (!(rtc->range_max - rtc->range_min))
293 mode = 0;
5495a415 294 }
c5c3e192 295
3ee2c40b 296 return mode;
c5c3e192
AZ
297}
298
3ee2c40b
DT
299static struct attribute_group rtc_attr_group = {
300 .is_visible = rtc_attr_is_visible,
301 .attrs = rtc_attrs,
302};
303
304static const struct attribute_group *rtc_attr_groups[] = {
305 &rtc_attr_group,
306 NULL
307};
c5c3e192 308
3ee2c40b 309const struct attribute_group **rtc_get_dev_attribute_groups(void)
c5c3e192 310{
3ee2c40b 311 return rtc_attr_groups;
c5c3e192 312}
a0a1a1ba
DO
313
314int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
315{
316 size_t old_cnt = 0, add_cnt = 0, new_cnt;
317 const struct attribute_group **groups, **old;
318
319 if (rtc->registered)
320 return -EINVAL;
321 if (!grps)
322 return -EINVAL;
323
324 groups = rtc->dev.groups;
325 if (groups)
326 for (; *groups; groups++)
327 old_cnt++;
328
329 for (groups = grps; *groups; groups++)
330 add_cnt++;
331
332 new_cnt = old_cnt + add_cnt + 1;
333 groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
777d8ae5
DC
334 if (!groups)
335 return -ENOMEM;
a0a1a1ba
DO
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;
339
340 old = rtc->dev.groups;
341 rtc->dev.groups = groups;
342 if (old && old != rtc_attr_groups)
343 devm_kfree(&rtc->dev, old);
344
345 return 0;
346}
347EXPORT_SYMBOL(rtc_add_groups);
348
349int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
350{
351 const struct attribute_group *groups[] = { grp, NULL };
352
353 return rtc_add_groups(rtc, groups);
354}
355EXPORT_SYMBOL(rtc_add_group);