xref: /linux-6.15/drivers/rtc/sysfs.c (revision e59b3c73)
1cdf7545aSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
236e14f5fSAlexandre Belloni /*
336e14f5fSAlexandre Belloni  * RTC subsystem, sysfs interface
436e14f5fSAlexandre Belloni  *
536e14f5fSAlexandre Belloni  * Copyright (C) 2005 Tower Technologies
636e14f5fSAlexandre Belloni  * Author: Alessandro Zummo <[email protected]>
736e14f5fSAlexandre Belloni  */
836e14f5fSAlexandre Belloni 
9*e59b3c73SChristophe JAILLET #include <linux/kstrtox.h>
1036e14f5fSAlexandre Belloni #include <linux/module.h>
1136e14f5fSAlexandre Belloni #include <linux/rtc.h>
1236e14f5fSAlexandre Belloni 
1336e14f5fSAlexandre Belloni #include "rtc-core.h"
1436e14f5fSAlexandre Belloni 
1536e14f5fSAlexandre Belloni /* device attributes */
1636e14f5fSAlexandre Belloni 
1736e14f5fSAlexandre Belloni /*
1836e14f5fSAlexandre Belloni  * NOTE:  RTC times displayed in sysfs use the RTC's timezone.  That's
1936e14f5fSAlexandre Belloni  * ideally UTC.  However, PCs that also boot to MS-Windows normally use
2036e14f5fSAlexandre Belloni  * the local time and change to match daylight savings time.  That affects
2136e14f5fSAlexandre Belloni  * attributes including date, time, since_epoch, and wakealarm.
2236e14f5fSAlexandre Belloni  */
2336e14f5fSAlexandre Belloni 
2436e14f5fSAlexandre Belloni static ssize_t
name_show(struct device * dev,struct device_attribute * attr,char * buf)2536e14f5fSAlexandre Belloni name_show(struct device *dev, struct device_attribute *attr, char *buf)
2636e14f5fSAlexandre Belloni {
2736e14f5fSAlexandre Belloni 	return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
2836e14f5fSAlexandre Belloni 		       dev_name(dev->parent));
2936e14f5fSAlexandre Belloni }
3036e14f5fSAlexandre Belloni static DEVICE_ATTR_RO(name);
3136e14f5fSAlexandre Belloni 
3236e14f5fSAlexandre Belloni static ssize_t
date_show(struct device * dev,struct device_attribute * attr,char * buf)3336e14f5fSAlexandre Belloni date_show(struct device *dev, struct device_attribute *attr, char *buf)
3436e14f5fSAlexandre Belloni {
3536e14f5fSAlexandre Belloni 	ssize_t retval;
3636e14f5fSAlexandre Belloni 	struct rtc_time tm;
3736e14f5fSAlexandre Belloni 
3836e14f5fSAlexandre Belloni 	retval = rtc_read_time(to_rtc_device(dev), &tm);
3936e14f5fSAlexandre Belloni 	if (retval)
4036e14f5fSAlexandre Belloni 		return retval;
4136e14f5fSAlexandre Belloni 
4236e14f5fSAlexandre Belloni 	return sprintf(buf, "%ptRd\n", &tm);
4336e14f5fSAlexandre Belloni }
4436e14f5fSAlexandre Belloni static DEVICE_ATTR_RO(date);
4536e14f5fSAlexandre Belloni 
4636e14f5fSAlexandre Belloni static ssize_t
time_show(struct device * dev,struct device_attribute * attr,char * buf)4736e14f5fSAlexandre Belloni time_show(struct device *dev, struct device_attribute *attr, char *buf)
4836e14f5fSAlexandre Belloni {
4936e14f5fSAlexandre Belloni 	ssize_t retval;
5036e14f5fSAlexandre Belloni 	struct rtc_time tm;
5136e14f5fSAlexandre Belloni 
5236e14f5fSAlexandre Belloni 	retval = rtc_read_time(to_rtc_device(dev), &tm);
5336e14f5fSAlexandre Belloni 	if (retval)
5436e14f5fSAlexandre Belloni 		return retval;
5536e14f5fSAlexandre Belloni 
5636e14f5fSAlexandre Belloni 	return sprintf(buf, "%ptRt\n", &tm);
5736e14f5fSAlexandre Belloni }
5836e14f5fSAlexandre Belloni static DEVICE_ATTR_RO(time);
5936e14f5fSAlexandre Belloni 
6036e14f5fSAlexandre Belloni static ssize_t
since_epoch_show(struct device * dev,struct device_attribute * attr,char * buf)6136e14f5fSAlexandre Belloni since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
6236e14f5fSAlexandre Belloni {
6336e14f5fSAlexandre Belloni 	ssize_t retval;
6436e14f5fSAlexandre Belloni 	struct rtc_time tm;
6536e14f5fSAlexandre Belloni 
6636e14f5fSAlexandre Belloni 	retval = rtc_read_time(to_rtc_device(dev), &tm);
6736e14f5fSAlexandre Belloni 	if (retval == 0) {
6836e14f5fSAlexandre Belloni 		time64_t time;
6936e14f5fSAlexandre Belloni 
7036e14f5fSAlexandre Belloni 		time = rtc_tm_to_time64(&tm);
7136e14f5fSAlexandre Belloni 		retval = sprintf(buf, "%lld\n", time);
7236e14f5fSAlexandre Belloni 	}
7336e14f5fSAlexandre Belloni 
7436e14f5fSAlexandre Belloni 	return retval;
7536e14f5fSAlexandre Belloni }
7636e14f5fSAlexandre Belloni static DEVICE_ATTR_RO(since_epoch);
7736e14f5fSAlexandre Belloni 
7836e14f5fSAlexandre Belloni static ssize_t
max_user_freq_show(struct device * dev,struct device_attribute * attr,char * buf)7936e14f5fSAlexandre Belloni max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
8036e14f5fSAlexandre Belloni {
8136e14f5fSAlexandre Belloni 	return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
8236e14f5fSAlexandre Belloni }
8336e14f5fSAlexandre Belloni 
8436e14f5fSAlexandre Belloni static ssize_t
max_user_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)8536e14f5fSAlexandre Belloni max_user_freq_store(struct device *dev, struct device_attribute *attr,
8636e14f5fSAlexandre Belloni 		    const char *buf, size_t n)
8736e14f5fSAlexandre Belloni {
8836e14f5fSAlexandre Belloni 	struct rtc_device *rtc = to_rtc_device(dev);
8936e14f5fSAlexandre Belloni 	unsigned long val;
9036e14f5fSAlexandre Belloni 	int err;
9136e14f5fSAlexandre Belloni 
9236e14f5fSAlexandre Belloni 	err = kstrtoul(buf, 0, &val);
9336e14f5fSAlexandre Belloni 	if (err)
9436e14f5fSAlexandre Belloni 		return err;
9536e14f5fSAlexandre Belloni 
9636e14f5fSAlexandre Belloni 	if (val >= 4096 || val == 0)
9736e14f5fSAlexandre Belloni 		return -EINVAL;
9836e14f5fSAlexandre Belloni 
9936e14f5fSAlexandre Belloni 	rtc->max_user_freq = (int)val;
10036e14f5fSAlexandre Belloni 
10136e14f5fSAlexandre Belloni 	return n;
10236e14f5fSAlexandre Belloni }
10336e14f5fSAlexandre Belloni static DEVICE_ATTR_RW(max_user_freq);
10436e14f5fSAlexandre Belloni 
10536e14f5fSAlexandre Belloni /**
106a8fdbefdSYang Yingliang  * hctosys_show - indicate if the given RTC set the system time
1076f693192SAlexandre Belloni  * @dev: The device that the attribute belongs to.
1086f693192SAlexandre Belloni  * @attr: The attribute being read.
1096f693192SAlexandre Belloni  * @buf: The result buffer.
11036e14f5fSAlexandre Belloni  *
1116f693192SAlexandre Belloni  * buf is "1" if the system clock was set by this RTC at the last
11236e14f5fSAlexandre Belloni  * boot or resume event.
11336e14f5fSAlexandre Belloni  */
11436e14f5fSAlexandre Belloni static ssize_t
hctosys_show(struct device * dev,struct device_attribute * attr,char * buf)11536e14f5fSAlexandre Belloni hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
11636e14f5fSAlexandre Belloni {
11736e14f5fSAlexandre Belloni #ifdef CONFIG_RTC_HCTOSYS_DEVICE
11836e14f5fSAlexandre Belloni 	if (rtc_hctosys_ret == 0 &&
11936e14f5fSAlexandre Belloni 	    strcmp(dev_name(&to_rtc_device(dev)->dev),
12036e14f5fSAlexandre Belloni 		   CONFIG_RTC_HCTOSYS_DEVICE) == 0)
12136e14f5fSAlexandre Belloni 		return sprintf(buf, "1\n");
12236e14f5fSAlexandre Belloni #endif
12336e14f5fSAlexandre Belloni 	return sprintf(buf, "0\n");
12436e14f5fSAlexandre Belloni }
12536e14f5fSAlexandre Belloni static DEVICE_ATTR_RO(hctosys);
12636e14f5fSAlexandre Belloni 
12736e14f5fSAlexandre Belloni static ssize_t
wakealarm_show(struct device * dev,struct device_attribute * attr,char * buf)12836e14f5fSAlexandre Belloni wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
12936e14f5fSAlexandre Belloni {
13036e14f5fSAlexandre Belloni 	ssize_t retval;
13136e14f5fSAlexandre Belloni 	time64_t alarm;
13236e14f5fSAlexandre Belloni 	struct rtc_wkalrm alm;
13336e14f5fSAlexandre Belloni 
13436e14f5fSAlexandre Belloni 	/* Don't show disabled alarms.  For uniformity, RTC alarms are
13536e14f5fSAlexandre Belloni 	 * conceptually one-shot, even though some common RTCs (on PCs)
13636e14f5fSAlexandre Belloni 	 * don't actually work that way.
13736e14f5fSAlexandre Belloni 	 *
13836e14f5fSAlexandre Belloni 	 * NOTE: RTC implementations where the alarm doesn't match an
13936e14f5fSAlexandre Belloni 	 * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
14036e14f5fSAlexandre Belloni 	 * alarms after they trigger, to ensure one-shot semantics.
14136e14f5fSAlexandre Belloni 	 */
14236e14f5fSAlexandre Belloni 	retval = rtc_read_alarm(to_rtc_device(dev), &alm);
14336e14f5fSAlexandre Belloni 	if (retval == 0 && alm.enabled) {
14436e14f5fSAlexandre Belloni 		alarm = rtc_tm_to_time64(&alm.time);
14536e14f5fSAlexandre Belloni 		retval = sprintf(buf, "%lld\n", alarm);
14636e14f5fSAlexandre Belloni 	}
14736e14f5fSAlexandre Belloni 
14836e14f5fSAlexandre Belloni 	return retval;
14936e14f5fSAlexandre Belloni }
15036e14f5fSAlexandre Belloni 
15136e14f5fSAlexandre Belloni static ssize_t
wakealarm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)15236e14f5fSAlexandre Belloni wakealarm_store(struct device *dev, struct device_attribute *attr,
15336e14f5fSAlexandre Belloni 		const char *buf, size_t n)
15436e14f5fSAlexandre Belloni {
15536e14f5fSAlexandre Belloni 	ssize_t retval;
15636e14f5fSAlexandre Belloni 	time64_t now, alarm;
15736e14f5fSAlexandre Belloni 	time64_t push = 0;
15836e14f5fSAlexandre Belloni 	struct rtc_wkalrm alm;
15936e14f5fSAlexandre Belloni 	struct rtc_device *rtc = to_rtc_device(dev);
16036e14f5fSAlexandre Belloni 	const char *buf_ptr;
16136e14f5fSAlexandre Belloni 	int adjust = 0;
16236e14f5fSAlexandre Belloni 
16336e14f5fSAlexandre Belloni 	/* Only request alarms that trigger in the future.  Disable them
16436e14f5fSAlexandre Belloni 	 * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
16536e14f5fSAlexandre Belloni 	 */
16636e14f5fSAlexandre Belloni 	retval = rtc_read_time(rtc, &alm.time);
16736e14f5fSAlexandre Belloni 	if (retval < 0)
16836e14f5fSAlexandre Belloni 		return retval;
16936e14f5fSAlexandre Belloni 	now = rtc_tm_to_time64(&alm.time);
17036e14f5fSAlexandre Belloni 
17136e14f5fSAlexandre Belloni 	buf_ptr = buf;
17236e14f5fSAlexandre Belloni 	if (*buf_ptr == '+') {
17336e14f5fSAlexandre Belloni 		buf_ptr++;
17436e14f5fSAlexandre Belloni 		if (*buf_ptr == '=') {
17536e14f5fSAlexandre Belloni 			buf_ptr++;
17636e14f5fSAlexandre Belloni 			push = 1;
177606cc43cSAlexandre Belloni 		} else {
17836e14f5fSAlexandre Belloni 			adjust = 1;
17936e14f5fSAlexandre Belloni 		}
180606cc43cSAlexandre Belloni 	}
18136e14f5fSAlexandre Belloni 	retval = kstrtos64(buf_ptr, 0, &alarm);
18236e14f5fSAlexandre Belloni 	if (retval)
18336e14f5fSAlexandre Belloni 		return retval;
184606cc43cSAlexandre Belloni 	if (adjust)
18536e14f5fSAlexandre Belloni 		alarm += now;
18636e14f5fSAlexandre Belloni 	if (alarm > now || push) {
18736e14f5fSAlexandre Belloni 		/* Avoid accidentally clobbering active alarms; we can't
18836e14f5fSAlexandre Belloni 		 * entirely prevent that here, without even the minimal
18936e14f5fSAlexandre Belloni 		 * locking from the /dev/rtcN api.
19036e14f5fSAlexandre Belloni 		 */
19136e14f5fSAlexandre Belloni 		retval = rtc_read_alarm(rtc, &alm);
19236e14f5fSAlexandre Belloni 		if (retval < 0)
19336e14f5fSAlexandre Belloni 			return retval;
19436e14f5fSAlexandre Belloni 		if (alm.enabled) {
19536e14f5fSAlexandre Belloni 			if (push) {
19636e14f5fSAlexandre Belloni 				push = rtc_tm_to_time64(&alm.time);
19736e14f5fSAlexandre Belloni 				alarm += push;
19836e14f5fSAlexandre Belloni 			} else
19936e14f5fSAlexandre Belloni 				return -EBUSY;
20036e14f5fSAlexandre Belloni 		} else if (push)
20136e14f5fSAlexandre Belloni 			return -EINVAL;
20236e14f5fSAlexandre Belloni 		alm.enabled = 1;
20336e14f5fSAlexandre Belloni 	} else {
20436e14f5fSAlexandre Belloni 		alm.enabled = 0;
20536e14f5fSAlexandre Belloni 
20636e14f5fSAlexandre Belloni 		/* Provide a valid future alarm time.  Linux isn't EFI,
20736e14f5fSAlexandre Belloni 		 * this time won't be ignored when disabling the alarm.
20836e14f5fSAlexandre Belloni 		 */
20936e14f5fSAlexandre Belloni 		alarm = now + 300;
21036e14f5fSAlexandre Belloni 	}
21136e14f5fSAlexandre Belloni 	rtc_time64_to_tm(alarm, &alm.time);
21236e14f5fSAlexandre Belloni 
21336e14f5fSAlexandre Belloni 	retval = rtc_set_alarm(rtc, &alm);
21436e14f5fSAlexandre Belloni 	return (retval < 0) ? retval : n;
21536e14f5fSAlexandre Belloni }
21636e14f5fSAlexandre Belloni static DEVICE_ATTR_RW(wakealarm);
21736e14f5fSAlexandre Belloni 
21836e14f5fSAlexandre Belloni static ssize_t
offset_show(struct device * dev,struct device_attribute * attr,char * buf)21936e14f5fSAlexandre Belloni offset_show(struct device *dev, struct device_attribute *attr, char *buf)
22036e14f5fSAlexandre Belloni {
22136e14f5fSAlexandre Belloni 	ssize_t retval;
22236e14f5fSAlexandre Belloni 	long offset;
22336e14f5fSAlexandre Belloni 
22436e14f5fSAlexandre Belloni 	retval = rtc_read_offset(to_rtc_device(dev), &offset);
22536e14f5fSAlexandre Belloni 	if (retval == 0)
22636e14f5fSAlexandre Belloni 		retval = sprintf(buf, "%ld\n", offset);
22736e14f5fSAlexandre Belloni 
22836e14f5fSAlexandre Belloni 	return retval;
22936e14f5fSAlexandre Belloni }
23036e14f5fSAlexandre Belloni 
23136e14f5fSAlexandre Belloni static ssize_t
offset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)23236e14f5fSAlexandre Belloni offset_store(struct device *dev, struct device_attribute *attr,
23336e14f5fSAlexandre Belloni 	     const char *buf, size_t n)
23436e14f5fSAlexandre Belloni {
23536e14f5fSAlexandre Belloni 	ssize_t retval;
23636e14f5fSAlexandre Belloni 	long offset;
23736e14f5fSAlexandre Belloni 
23836e14f5fSAlexandre Belloni 	retval = kstrtol(buf, 10, &offset);
23936e14f5fSAlexandre Belloni 	if (retval == 0)
24036e14f5fSAlexandre Belloni 		retval = rtc_set_offset(to_rtc_device(dev), offset);
24136e14f5fSAlexandre Belloni 
24236e14f5fSAlexandre Belloni 	return (retval < 0) ? retval : n;
24336e14f5fSAlexandre Belloni }
24436e14f5fSAlexandre Belloni static DEVICE_ATTR_RW(offset);
24536e14f5fSAlexandre Belloni 
24636e14f5fSAlexandre Belloni static ssize_t
range_show(struct device * dev,struct device_attribute * attr,char * buf)24736e14f5fSAlexandre Belloni range_show(struct device *dev, struct device_attribute *attr, char *buf)
24836e14f5fSAlexandre Belloni {
24936e14f5fSAlexandre Belloni 	return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
25036e14f5fSAlexandre Belloni 		       to_rtc_device(dev)->range_max);
25136e14f5fSAlexandre Belloni }
25236e14f5fSAlexandre Belloni static DEVICE_ATTR_RO(range);
25336e14f5fSAlexandre Belloni 
25436e14f5fSAlexandre Belloni static struct attribute *rtc_attrs[] = {
25536e14f5fSAlexandre Belloni 	&dev_attr_name.attr,
25636e14f5fSAlexandre Belloni 	&dev_attr_date.attr,
25736e14f5fSAlexandre Belloni 	&dev_attr_time.attr,
25836e14f5fSAlexandre Belloni 	&dev_attr_since_epoch.attr,
25936e14f5fSAlexandre Belloni 	&dev_attr_max_user_freq.attr,
26036e14f5fSAlexandre Belloni 	&dev_attr_hctosys.attr,
26136e14f5fSAlexandre Belloni 	&dev_attr_wakealarm.attr,
26236e14f5fSAlexandre Belloni 	&dev_attr_offset.attr,
26336e14f5fSAlexandre Belloni 	&dev_attr_range.attr,
26436e14f5fSAlexandre Belloni 	NULL,
26536e14f5fSAlexandre Belloni };
26636e14f5fSAlexandre Belloni 
26736e14f5fSAlexandre Belloni /* The reason to trigger an alarm with no process watching it (via sysfs)
26836e14f5fSAlexandre Belloni  * is its side effect:  waking from a system state like suspend-to-RAM or
26936e14f5fSAlexandre Belloni  * suspend-to-disk.  So: no attribute unless that side effect is possible.
27036e14f5fSAlexandre Belloni  * (Userspace may disable that mechanism later.)
27136e14f5fSAlexandre Belloni  */
rtc_does_wakealarm(struct rtc_device * rtc)27236e14f5fSAlexandre Belloni static bool rtc_does_wakealarm(struct rtc_device *rtc)
27336e14f5fSAlexandre Belloni {
27436e14f5fSAlexandre Belloni 	if (!device_can_wakeup(rtc->dev.parent))
27536e14f5fSAlexandre Belloni 		return false;
27636e14f5fSAlexandre Belloni 
2774d0185e6SAlexandre Belloni 	return !!test_bit(RTC_FEATURE_ALARM, rtc->features);
27836e14f5fSAlexandre Belloni }
27936e14f5fSAlexandre Belloni 
rtc_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)28036e14f5fSAlexandre Belloni static umode_t rtc_attr_is_visible(struct kobject *kobj,
28136e14f5fSAlexandre Belloni 				   struct attribute *attr, int n)
28236e14f5fSAlexandre Belloni {
283ae243ef0Ssuguosong 	struct device *dev = kobj_to_dev(kobj);
28436e14f5fSAlexandre Belloni 	struct rtc_device *rtc = to_rtc_device(dev);
28536e14f5fSAlexandre Belloni 	umode_t mode = attr->mode;
28636e14f5fSAlexandre Belloni 
28736e14f5fSAlexandre Belloni 	if (attr == &dev_attr_wakealarm.attr) {
28836e14f5fSAlexandre Belloni 		if (!rtc_does_wakealarm(rtc))
28936e14f5fSAlexandre Belloni 			mode = 0;
29036e14f5fSAlexandre Belloni 	} else if (attr == &dev_attr_offset.attr) {
29136e14f5fSAlexandre Belloni 		if (!rtc->ops->set_offset)
29236e14f5fSAlexandre Belloni 			mode = 0;
29336e14f5fSAlexandre Belloni 	} else if (attr == &dev_attr_range.attr) {
29436e14f5fSAlexandre Belloni 		if (!(rtc->range_max - rtc->range_min))
29536e14f5fSAlexandre Belloni 			mode = 0;
29636e14f5fSAlexandre Belloni 	}
29736e14f5fSAlexandre Belloni 
29836e14f5fSAlexandre Belloni 	return mode;
29936e14f5fSAlexandre Belloni }
30036e14f5fSAlexandre Belloni 
30136e14f5fSAlexandre Belloni static struct attribute_group rtc_attr_group = {
30236e14f5fSAlexandre Belloni 	.is_visible	= rtc_attr_is_visible,
30336e14f5fSAlexandre Belloni 	.attrs		= rtc_attrs,
30436e14f5fSAlexandre Belloni };
30536e14f5fSAlexandre Belloni 
30636e14f5fSAlexandre Belloni static const struct attribute_group *rtc_attr_groups[] = {
30736e14f5fSAlexandre Belloni 	&rtc_attr_group,
30836e14f5fSAlexandre Belloni 	NULL
30936e14f5fSAlexandre Belloni };
31036e14f5fSAlexandre Belloni 
rtc_get_dev_attribute_groups(void)31136e14f5fSAlexandre Belloni const struct attribute_group **rtc_get_dev_attribute_groups(void)
31236e14f5fSAlexandre Belloni {
31336e14f5fSAlexandre Belloni 	return rtc_attr_groups;
31436e14f5fSAlexandre Belloni }
31536e14f5fSAlexandre Belloni 
rtc_add_groups(struct rtc_device * rtc,const struct attribute_group ** grps)31636e14f5fSAlexandre Belloni int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
31736e14f5fSAlexandre Belloni {
31836e14f5fSAlexandre Belloni 	size_t old_cnt = 0, add_cnt = 0, new_cnt;
31936e14f5fSAlexandre Belloni 	const struct attribute_group **groups, **old;
32036e14f5fSAlexandre Belloni 
32136e14f5fSAlexandre Belloni 	if (!grps)
32236e14f5fSAlexandre Belloni 		return -EINVAL;
32336e14f5fSAlexandre Belloni 
32436e14f5fSAlexandre Belloni 	groups = rtc->dev.groups;
32536e14f5fSAlexandre Belloni 	if (groups)
32636e14f5fSAlexandre Belloni 		for (; *groups; groups++)
32736e14f5fSAlexandre Belloni 			old_cnt++;
32836e14f5fSAlexandre Belloni 
32936e14f5fSAlexandre Belloni 	for (groups = grps; *groups; groups++)
33036e14f5fSAlexandre Belloni 		add_cnt++;
33136e14f5fSAlexandre Belloni 
33236e14f5fSAlexandre Belloni 	new_cnt = old_cnt + add_cnt + 1;
33336e14f5fSAlexandre Belloni 	groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
33436e14f5fSAlexandre Belloni 	if (!groups)
33536e14f5fSAlexandre Belloni 		return -ENOMEM;
33636e14f5fSAlexandre Belloni 	memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
33736e14f5fSAlexandre Belloni 	memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
33836e14f5fSAlexandre Belloni 	groups[old_cnt + add_cnt] = NULL;
33936e14f5fSAlexandre Belloni 
34036e14f5fSAlexandre Belloni 	old = rtc->dev.groups;
34136e14f5fSAlexandre Belloni 	rtc->dev.groups = groups;
34236e14f5fSAlexandre Belloni 	if (old && old != rtc_attr_groups)
34336e14f5fSAlexandre Belloni 		devm_kfree(&rtc->dev, old);
34436e14f5fSAlexandre Belloni 
34536e14f5fSAlexandre Belloni 	return 0;
34636e14f5fSAlexandre Belloni }
34736e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_add_groups);
34836e14f5fSAlexandre Belloni 
rtc_add_group(struct rtc_device * rtc,const struct attribute_group * grp)34936e14f5fSAlexandre Belloni int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
35036e14f5fSAlexandre Belloni {
35136e14f5fSAlexandre Belloni 	const struct attribute_group *groups[] = { grp, NULL };
35236e14f5fSAlexandre Belloni 
35336e14f5fSAlexandre Belloni 	return rtc_add_groups(rtc, groups);
35436e14f5fSAlexandre Belloni }
35536e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_add_group);
356