xref: /linux-6.15/drivers/rtc/class.c (revision c9269791)
1cdf7545aSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
20c86edc0SAlessandro Zummo /*
30c86edc0SAlessandro Zummo  * RTC subsystem, base class
40c86edc0SAlessandro Zummo  *
50c86edc0SAlessandro Zummo  * Copyright (C) 2005 Tower Technologies
60c86edc0SAlessandro Zummo  * Author: Alessandro Zummo <[email protected]>
70c86edc0SAlessandro Zummo  *
80c86edc0SAlessandro Zummo  * class skeleton from drivers/hwmon/hwmon.c
90c86edc0SAlessandro Zummo  */
100c86edc0SAlessandro Zummo 
11c100a5e0SJingoo Han #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12c100a5e0SJingoo Han 
130c86edc0SAlessandro Zummo #include <linux/module.h>
149d2b7e53SStephen Warren #include <linux/of.h>
150c86edc0SAlessandro Zummo #include <linux/rtc.h>
160c86edc0SAlessandro Zummo #include <linux/kdev_t.h>
170c86edc0SAlessandro Zummo #include <linux/idr.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
196610e089SJohn Stultz #include <linux/workqueue.h>
200c86edc0SAlessandro Zummo 
215726fb20SDavid Brownell #include "rtc-core.h"
225726fb20SDavid Brownell 
236d03d06dSJonathan Cameron static DEFINE_IDA(rtc_ida);
240c86edc0SAlessandro Zummo 
rtc_device_release(struct device * dev)25cd966209SDavid Brownell static void rtc_device_release(struct device *dev)
260c86edc0SAlessandro Zummo {
27cd966209SDavid Brownell 	struct rtc_device *rtc = to_rtc_device(dev);
28c8fa17d9SVincent Whitchurch 	struct timerqueue_head *head = &rtc->timerqueue;
29c8fa17d9SVincent Whitchurch 	struct timerqueue_node *node;
30c8fa17d9SVincent Whitchurch 
31c8fa17d9SVincent Whitchurch 	mutex_lock(&rtc->ops_lock);
32c8fa17d9SVincent Whitchurch 	while ((node = timerqueue_getnext(head)))
33c8fa17d9SVincent Whitchurch 		timerqueue_del(head, node);
34c8fa17d9SVincent Whitchurch 	mutex_unlock(&rtc->ops_lock);
35c8fa17d9SVincent Whitchurch 
36c8fa17d9SVincent Whitchurch 	cancel_work_sync(&rtc->irqwork);
37606cc43cSAlexandre Belloni 
38592ff0c8Skeliu 	ida_free(&rtc_ida, rtc->id);
390d6d7a39SBartosz Golaszewski 	mutex_destroy(&rtc->ops_lock);
400c86edc0SAlessandro Zummo 	kfree(rtc);
410c86edc0SAlessandro Zummo }
420c86edc0SAlessandro Zummo 
434c24e29eSDavid Fries #ifdef CONFIG_RTC_HCTOSYS_DEVICE
444c24e29eSDavid Fries /* Result of the last RTC to system clock attempt. */
454c24e29eSDavid Fries int rtc_hctosys_ret = -ENODEV;
46f9b2a4d6SSteve Muckle 
47f9b2a4d6SSteve Muckle /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
48f9b2a4d6SSteve Muckle  * whether it stores the most close value or the value with partial
49f9b2a4d6SSteve Muckle  * seconds truncated. However, it is important that we use it to store
50f9b2a4d6SSteve Muckle  * the truncated value. This is because otherwise it is necessary,
51f9b2a4d6SSteve Muckle  * in an rtc sync function, to read both xtime.tv_sec and
52f9b2a4d6SSteve Muckle  * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
53f9b2a4d6SSteve Muckle  * of >32bits is not possible. So storing the most close value would
54f9b2a4d6SSteve Muckle  * slow down the sync API. So here we have the truncated value and
55f9b2a4d6SSteve Muckle  * the best guess is to add 0.5s.
56f9b2a4d6SSteve Muckle  */
57f9b2a4d6SSteve Muckle 
rtc_hctosys(struct rtc_device * rtc)585614a4a3SAlexandre Belloni static void rtc_hctosys(struct rtc_device *rtc)
59f9b2a4d6SSteve Muckle {
603edf29d9SColin Ian King 	int err;
61f9b2a4d6SSteve Muckle 	struct rtc_time tm;
62f9b2a4d6SSteve Muckle 	struct timespec64 tv64 = {
63f9b2a4d6SSteve Muckle 		.tv_nsec = NSEC_PER_SEC >> 1,
64f9b2a4d6SSteve Muckle 	};
65f9b2a4d6SSteve Muckle 
66f9b2a4d6SSteve Muckle 	err = rtc_read_time(rtc, &tm);
67f9b2a4d6SSteve Muckle 	if (err) {
68f9b2a4d6SSteve Muckle 		dev_err(rtc->dev.parent,
69f9b2a4d6SSteve Muckle 			"hctosys: unable to read the hardware clock\n");
70f9b2a4d6SSteve Muckle 		goto err_read;
71f9b2a4d6SSteve Muckle 	}
72f9b2a4d6SSteve Muckle 
73f9b2a4d6SSteve Muckle 	tv64.tv_sec = rtc_tm_to_time64(&tm);
74f9b2a4d6SSteve Muckle 
75f9b2a4d6SSteve Muckle #if BITS_PER_LONG == 32
76f9b2a4d6SSteve Muckle 	if (tv64.tv_sec > INT_MAX) {
77f9b2a4d6SSteve Muckle 		err = -ERANGE;
78f9b2a4d6SSteve Muckle 		goto err_read;
79f9b2a4d6SSteve Muckle 	}
80f9b2a4d6SSteve Muckle #endif
81f9b2a4d6SSteve Muckle 
82f9b2a4d6SSteve Muckle 	err = do_settimeofday64(&tv64);
83f9b2a4d6SSteve Muckle 
84f9b2a4d6SSteve Muckle 	dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
85f9b2a4d6SSteve Muckle 		 &tm, (long long)tv64.tv_sec);
86f9b2a4d6SSteve Muckle 
87f9b2a4d6SSteve Muckle err_read:
88f9b2a4d6SSteve Muckle 	rtc_hctosys_ret = err;
89f9b2a4d6SSteve Muckle }
904c24e29eSDavid Fries #endif
917ca1d488SDavid Brownell 
9292e7f04aSShuah Khan #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
937ca1d488SDavid Brownell /*
947ca1d488SDavid Brownell  * On suspend(), measure the delta between one RTC and the
957ca1d488SDavid Brownell  * system's wall clock; restore it on resume().
967ca1d488SDavid Brownell  */
977ca1d488SDavid Brownell 
98d4bda8f8SJohn Stultz static struct timespec64 old_rtc, old_system, old_delta;
993dcad5ffSJohn Stultz 
rtc_suspend(struct device * dev)10092e7f04aSShuah Khan static int rtc_suspend(struct device *dev)
1017ca1d488SDavid Brownell {
1027ca1d488SDavid Brownell 	struct rtc_device	*rtc = to_rtc_device(dev);
1037ca1d488SDavid Brownell 	struct rtc_time		tm;
104d4bda8f8SJohn Stultz 	struct timespec64	delta, delta_delta;
105e1d60093SHyogi Gim 	int err;
1069ecf37ebSFeng Tang 
1070fa88cb4SXunlei Pang 	if (timekeeping_rtc_skipsuspend())
1089ecf37ebSFeng Tang 		return 0;
1099ecf37ebSFeng Tang 
110d4afc76cSKay Sievers 	if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
1117ca1d488SDavid Brownell 		return 0;
1127ca1d488SDavid Brownell 
1133dcad5ffSJohn Stultz 	/* snapshot the current RTC and system time at suspend*/
114e1d60093SHyogi Gim 	err = rtc_read_time(rtc, &tm);
115e1d60093SHyogi Gim 	if (err < 0) {
116e1d60093SHyogi Gim 		pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
117e1d60093SHyogi Gim 		return 0;
118e1d60093SHyogi Gim 	}
119e1d60093SHyogi Gim 
1205089ea15SArnd Bergmann 	ktime_get_real_ts64(&old_system);
121d4bda8f8SJohn Stultz 	old_rtc.tv_sec = rtc_tm_to_time64(&tm);
1223dcad5ffSJohn Stultz 
1233dcad5ffSJohn Stultz 	/*
1243dcad5ffSJohn Stultz 	 * To avoid drift caused by repeated suspend/resumes,
1253dcad5ffSJohn Stultz 	 * which each can add ~1 second drift error,
1263dcad5ffSJohn Stultz 	 * try to compensate so the difference in system time
1273dcad5ffSJohn Stultz 	 * and rtc time stays close to constant.
1283dcad5ffSJohn Stultz 	 */
129d4bda8f8SJohn Stultz 	delta = timespec64_sub(old_system, old_rtc);
130d4bda8f8SJohn Stultz 	delta_delta = timespec64_sub(delta, old_delta);
1316a8943d9SArve Hjønnevåg 	if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
1323dcad5ffSJohn Stultz 		/*
1333dcad5ffSJohn Stultz 		 * if delta_delta is too large, assume time correction
134606cc43cSAlexandre Belloni 		 * has occurred and set old_delta to the current delta.
1353dcad5ffSJohn Stultz 		 */
1363dcad5ffSJohn Stultz 		old_delta = delta;
1373dcad5ffSJohn Stultz 	} else {
1383dcad5ffSJohn Stultz 		/* Otherwise try to adjust old_system to compensate */
139d4bda8f8SJohn Stultz 		old_system = timespec64_sub(old_system, delta_delta);
1403dcad5ffSJohn Stultz 	}
1417ca1d488SDavid Brownell 
1427ca1d488SDavid Brownell 	return 0;
1437ca1d488SDavid Brownell }
1447ca1d488SDavid Brownell 
rtc_resume(struct device * dev)1457ca1d488SDavid Brownell static int rtc_resume(struct device *dev)
1467ca1d488SDavid Brownell {
1477ca1d488SDavid Brownell 	struct rtc_device	*rtc = to_rtc_device(dev);
1487ca1d488SDavid Brownell 	struct rtc_time		tm;
149d4bda8f8SJohn Stultz 	struct timespec64	new_system, new_rtc;
150d4bda8f8SJohn Stultz 	struct timespec64	sleep_time;
151e1d60093SHyogi Gim 	int err;
1527ca1d488SDavid Brownell 
1530fa88cb4SXunlei Pang 	if (timekeeping_rtc_skipresume())
1549ecf37ebSFeng Tang 		return 0;
1559ecf37ebSFeng Tang 
1564c24e29eSDavid Fries 	rtc_hctosys_ret = -ENODEV;
157d4afc76cSKay Sievers 	if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
1587ca1d488SDavid Brownell 		return 0;
1597ca1d488SDavid Brownell 
1603dcad5ffSJohn Stultz 	/* snapshot the current rtc and system time at resume */
1615089ea15SArnd Bergmann 	ktime_get_real_ts64(&new_system);
162e1d60093SHyogi Gim 	err = rtc_read_time(rtc, &tm);
163e1d60093SHyogi Gim 	if (err < 0) {
164e1d60093SHyogi Gim 		pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
165e1d60093SHyogi Gim 		return 0;
166e1d60093SHyogi Gim 	}
167e1d60093SHyogi Gim 
168d4bda8f8SJohn Stultz 	new_rtc.tv_sec = rtc_tm_to_time64(&tm);
1693dcad5ffSJohn Stultz 	new_rtc.tv_nsec = 0;
1703dcad5ffSJohn Stultz 
1716a8943d9SArve Hjønnevåg 	if (new_rtc.tv_sec < old_rtc.tv_sec) {
172d4afc76cSKay Sievers 		pr_debug("%s:  time travel!\n", dev_name(&rtc->dev));
1737ca1d488SDavid Brownell 		return 0;
1747ca1d488SDavid Brownell 	}
1757ca1d488SDavid Brownell 
1763dcad5ffSJohn Stultz 	/* calculate the RTC time delta (sleep time)*/
177d4bda8f8SJohn Stultz 	sleep_time = timespec64_sub(new_rtc, old_rtc);
1787ca1d488SDavid Brownell 
1793dcad5ffSJohn Stultz 	/*
1803dcad5ffSJohn Stultz 	 * Since these RTC suspend/resume handlers are not called
1813dcad5ffSJohn Stultz 	 * at the very end of suspend or the start of resume,
1823dcad5ffSJohn Stultz 	 * some run-time may pass on either sides of the sleep time
1833dcad5ffSJohn Stultz 	 * so subtract kernel run-time between rtc_suspend to rtc_resume
1843dcad5ffSJohn Stultz 	 * to keep things accurate.
1853dcad5ffSJohn Stultz 	 */
186d4bda8f8SJohn Stultz 	sleep_time = timespec64_sub(sleep_time,
187d4bda8f8SJohn Stultz 				    timespec64_sub(new_system, old_system));
1883dcad5ffSJohn Stultz 
1896a8943d9SArve Hjønnevåg 	if (sleep_time.tv_sec >= 0)
190d4bda8f8SJohn Stultz 		timekeeping_inject_sleeptime64(&sleep_time);
1914c24e29eSDavid Fries 	rtc_hctosys_ret = 0;
1927ca1d488SDavid Brownell 	return 0;
1937ca1d488SDavid Brownell }
1947ca1d488SDavid Brownell 
19592e7f04aSShuah Khan static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
19692e7f04aSShuah Khan #define RTC_CLASS_DEV_PM_OPS	(&rtc_class_dev_pm_ops)
1977ca1d488SDavid Brownell #else
19892e7f04aSShuah Khan #define RTC_CLASS_DEV_PM_OPS	NULL
1997ca1d488SDavid Brownell #endif
2007ca1d488SDavid Brownell 
2016b6ca096SRicardo B. Marliere const struct class rtc_class = {
2026b6ca096SRicardo B. Marliere 	.name = "rtc",
2036b6ca096SRicardo B. Marliere 	.pm = RTC_CLASS_DEV_PM_OPS,
2046b6ca096SRicardo B. Marliere };
2056b6ca096SRicardo B. Marliere 
2063068a254SAlexandre Belloni /* Ensure the caller will set the id before releasing the device */
rtc_allocate_device(void)207d1bec20fSAlexandre Belloni static struct rtc_device *rtc_allocate_device(void)
208d1bec20fSAlexandre Belloni {
209d1bec20fSAlexandre Belloni 	struct rtc_device *rtc;
210d1bec20fSAlexandre Belloni 
211d1bec20fSAlexandre Belloni 	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
212d1bec20fSAlexandre Belloni 	if (!rtc)
213d1bec20fSAlexandre Belloni 		return NULL;
214d1bec20fSAlexandre Belloni 
215d1bec20fSAlexandre Belloni 	device_initialize(&rtc->dev);
216d1bec20fSAlexandre Belloni 
21769eca258SThomas Gleixner 	/*
21869eca258SThomas Gleixner 	 * Drivers can revise this default after allocating the device.
21969eca258SThomas Gleixner 	 * The default is what most RTCs do: Increment seconds exactly one
22069eca258SThomas Gleixner 	 * second after the write happened. This adds a default transport
22169eca258SThomas Gleixner 	 * time of 5ms which is at least halfways close to reality.
22269eca258SThomas Gleixner 	 */
22369eca258SThomas Gleixner 	rtc->set_offset_nsec = NSEC_PER_SEC + 5 * NSEC_PER_MSEC;
2240f295b06SJason Gunthorpe 
225d1bec20fSAlexandre Belloni 	rtc->irq_freq = 1;
226d1bec20fSAlexandre Belloni 	rtc->max_user_freq = 64;
2276b6ca096SRicardo B. Marliere 	rtc->dev.class = &rtc_class;
228d1bec20fSAlexandre Belloni 	rtc->dev.groups = rtc_get_dev_attribute_groups();
229d1bec20fSAlexandre Belloni 	rtc->dev.release = rtc_device_release;
230d1bec20fSAlexandre Belloni 
231d1bec20fSAlexandre Belloni 	mutex_init(&rtc->ops_lock);
232d1bec20fSAlexandre Belloni 	spin_lock_init(&rtc->irq_lock);
233d1bec20fSAlexandre Belloni 	init_waitqueue_head(&rtc->irq_queue);
234d1bec20fSAlexandre Belloni 
235d1bec20fSAlexandre Belloni 	/* Init timerqueue */
236d1bec20fSAlexandre Belloni 	timerqueue_init_head(&rtc->timerqueue);
237d1bec20fSAlexandre Belloni 	INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
238d1bec20fSAlexandre Belloni 	/* Init aie timer */
2399a032011SAlexandre Belloni 	rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
240d1bec20fSAlexandre Belloni 	/* Init uie timer */
2419a032011SAlexandre Belloni 	rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
242d1bec20fSAlexandre Belloni 	/* Init pie timer */
243*c9269791SNam Cao 	hrtimer_setup(&rtc->pie_timer, rtc_pie_update_irq, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
244d1bec20fSAlexandre Belloni 	rtc->pie_enabled = 0;
245d1bec20fSAlexandre Belloni 
2467ae41220SAlexandre Belloni 	set_bit(RTC_FEATURE_ALARM, rtc->features);
247adb17a05SAlexandre Belloni 	set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
2487ae41220SAlexandre Belloni 
249d1bec20fSAlexandre Belloni 	return rtc;
250d1bec20fSAlexandre Belloni }
2517ca1d488SDavid Brownell 
rtc_device_get_id(struct device * dev)252b91336dfSAlexandre Belloni static int rtc_device_get_id(struct device *dev)
253b91336dfSAlexandre Belloni {
254b91336dfSAlexandre Belloni 	int of_id = -1, id = -1;
255b91336dfSAlexandre Belloni 
256b91336dfSAlexandre Belloni 	if (dev->of_node)
257b91336dfSAlexandre Belloni 		of_id = of_alias_get_id(dev->of_node, "rtc");
258b91336dfSAlexandre Belloni 	else if (dev->parent && dev->parent->of_node)
259b91336dfSAlexandre Belloni 		of_id = of_alias_get_id(dev->parent->of_node, "rtc");
260b91336dfSAlexandre Belloni 
261b91336dfSAlexandre Belloni 	if (of_id >= 0) {
262e3d3fe7eSChristophe JAILLET 		id = ida_alloc_range(&rtc_ida, of_id, of_id, GFP_KERNEL);
263b91336dfSAlexandre Belloni 		if (id < 0)
264b91336dfSAlexandre Belloni 			dev_warn(dev, "/aliases ID %d not available\n", of_id);
265b91336dfSAlexandre Belloni 	}
266b91336dfSAlexandre Belloni 
267b91336dfSAlexandre Belloni 	if (id < 0)
268592ff0c8Skeliu 		id = ida_alloc(&rtc_ida, GFP_KERNEL);
269b91336dfSAlexandre Belloni 
270b91336dfSAlexandre Belloni 	return id;
271b91336dfSAlexandre Belloni }
272b91336dfSAlexandre Belloni 
rtc_device_get_offset(struct rtc_device * rtc)27398951564SBaolin Wang static void rtc_device_get_offset(struct rtc_device *rtc)
27498951564SBaolin Wang {
27598951564SBaolin Wang 	time64_t range_secs;
27698951564SBaolin Wang 	u32 start_year;
27798951564SBaolin Wang 	int ret;
27898951564SBaolin Wang 
27998951564SBaolin Wang 	/*
28098951564SBaolin Wang 	 * If RTC driver did not implement the range of RTC hardware device,
28198951564SBaolin Wang 	 * then we can not expand the RTC range by adding or subtracting one
28298951564SBaolin Wang 	 * offset.
28398951564SBaolin Wang 	 */
28498951564SBaolin Wang 	if (rtc->range_min == rtc->range_max)
28598951564SBaolin Wang 		return;
28698951564SBaolin Wang 
28798951564SBaolin Wang 	ret = device_property_read_u32(rtc->dev.parent, "start-year",
28898951564SBaolin Wang 				       &start_year);
28998951564SBaolin Wang 	if (!ret) {
29098951564SBaolin Wang 		rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
29198951564SBaolin Wang 		rtc->set_start_time = true;
29298951564SBaolin Wang 	}
29398951564SBaolin Wang 
29498951564SBaolin Wang 	/*
29598951564SBaolin Wang 	 * If user did not implement the start time for RTC driver, then no
29698951564SBaolin Wang 	 * need to expand the RTC range.
29798951564SBaolin Wang 	 */
29898951564SBaolin Wang 	if (!rtc->set_start_time)
29998951564SBaolin Wang 		return;
30098951564SBaolin Wang 
30198951564SBaolin Wang 	range_secs = rtc->range_max - rtc->range_min + 1;
30298951564SBaolin Wang 
30398951564SBaolin Wang 	/*
30498951564SBaolin Wang 	 * If the start_secs is larger than the maximum seconds (rtc->range_max)
30598951564SBaolin Wang 	 * supported by RTC hardware or the maximum seconds of new expanded
30698951564SBaolin Wang 	 * range (start_secs + rtc->range_max - rtc->range_min) is less than
30798951564SBaolin Wang 	 * rtc->range_min, which means the minimum seconds (rtc->range_min) of
30898951564SBaolin Wang 	 * RTC hardware will be mapped to start_secs by adding one offset, so
30998951564SBaolin Wang 	 * the offset seconds calculation formula should be:
31098951564SBaolin Wang 	 * rtc->offset_secs = rtc->start_secs - rtc->range_min;
31198951564SBaolin Wang 	 *
31298951564SBaolin Wang 	 * If the start_secs is larger than the minimum seconds (rtc->range_min)
31398951564SBaolin Wang 	 * supported by RTC hardware, then there is one region is overlapped
31498951564SBaolin Wang 	 * between the original RTC hardware range and the new expanded range,
31598951564SBaolin Wang 	 * and this overlapped region do not need to be mapped into the new
31698951564SBaolin Wang 	 * expanded range due to it is valid for RTC device. So the minimum
31798951564SBaolin Wang 	 * seconds of RTC hardware (rtc->range_min) should be mapped to
31898951564SBaolin Wang 	 * rtc->range_max + 1, then the offset seconds formula should be:
31998951564SBaolin Wang 	 * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
32098951564SBaolin Wang 	 *
32198951564SBaolin Wang 	 * If the start_secs is less than the minimum seconds (rtc->range_min),
32298951564SBaolin Wang 	 * which is similar to case 2. So the start_secs should be mapped to
32398951564SBaolin Wang 	 * start_secs + rtc->range_max - rtc->range_min + 1, then the
32498951564SBaolin Wang 	 * offset seconds formula should be:
32598951564SBaolin Wang 	 * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
32698951564SBaolin Wang 	 *
32798951564SBaolin Wang 	 * Otherwise the offset seconds should be 0.
32898951564SBaolin Wang 	 */
32998951564SBaolin Wang 	if (rtc->start_secs > rtc->range_max ||
33098951564SBaolin Wang 	    rtc->start_secs + range_secs - 1 < rtc->range_min)
33198951564SBaolin Wang 		rtc->offset_secs = rtc->start_secs - rtc->range_min;
33298951564SBaolin Wang 	else if (rtc->start_secs > rtc->range_min)
33398951564SBaolin Wang 		rtc->offset_secs = range_secs;
33498951564SBaolin Wang 	else if (rtc->start_secs < rtc->range_min)
33598951564SBaolin Wang 		rtc->offset_secs = -range_secs;
33698951564SBaolin Wang 	else
33798951564SBaolin Wang 		rtc->offset_secs = 0;
33898951564SBaolin Wang }
33998951564SBaolin Wang 
devm_rtc_unregister_device(void * data)340fdcfd854SBartosz Golaszewski static void devm_rtc_unregister_device(void *data)
3410c86edc0SAlessandro Zummo {
342fdcfd854SBartosz Golaszewski 	struct rtc_device *rtc = data;
343fdcfd854SBartosz Golaszewski 
3440c86edc0SAlessandro Zummo 	mutex_lock(&rtc->ops_lock);
345c3b399a4SDmitry Torokhov 	/*
346c3b399a4SDmitry Torokhov 	 * Remove innards of this RTC, then disable it, before
347e109ebd1SDavid Brownell 	 * letting any rtc_class_open() users access it again
348e109ebd1SDavid Brownell 	 */
3497d9f99ecSDavid Brownell 	rtc_proc_del_device(rtc);
350789c1093SYang Yingliang 	if (!test_bit(RTC_NO_CDEV, &rtc->flags))
351d5ed9177SLogan Gunthorpe 		cdev_device_del(&rtc->char_dev, &rtc->dev);
3520c86edc0SAlessandro Zummo 	rtc->ops = NULL;
3530c86edc0SAlessandro Zummo 	mutex_unlock(&rtc->ops_lock);
354e109ebd1SDavid Brownell }
3550c86edc0SAlessandro Zummo 
devm_rtc_release_device(void * res)3561bfc485bSBartosz Golaszewski static void devm_rtc_release_device(void *res)
3573068a254SAlexandre Belloni {
3581bfc485bSBartosz Golaszewski 	struct rtc_device *rtc = res;
3593068a254SAlexandre Belloni 
3603068a254SAlexandre Belloni 	put_device(&rtc->dev);
3613068a254SAlexandre Belloni }
3623068a254SAlexandre Belloni 
devm_rtc_allocate_device(struct device * dev)3633068a254SAlexandre Belloni struct rtc_device *devm_rtc_allocate_device(struct device *dev)
3643068a254SAlexandre Belloni {
3651bfc485bSBartosz Golaszewski 	struct rtc_device *rtc;
3663068a254SAlexandre Belloni 	int id, err;
3673068a254SAlexandre Belloni 
3683068a254SAlexandre Belloni 	id = rtc_device_get_id(dev);
3693068a254SAlexandre Belloni 	if (id < 0)
3703068a254SAlexandre Belloni 		return ERR_PTR(id);
3713068a254SAlexandre Belloni 
3723068a254SAlexandre Belloni 	rtc = rtc_allocate_device();
3733068a254SAlexandre Belloni 	if (!rtc) {
374592ff0c8Skeliu 		ida_free(&rtc_ida, id);
3751bfc485bSBartosz Golaszewski 		return ERR_PTR(-ENOMEM);
3763068a254SAlexandre Belloni 	}
3773068a254SAlexandre Belloni 
3783068a254SAlexandre Belloni 	rtc->id = id;
3793068a254SAlexandre Belloni 	rtc->dev.parent = dev;
38060da7380SShang XiaoJing 	err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
38124d23181SYang Yingliang 	if (err)
38224d23181SYang Yingliang 		return ERR_PTR(err);
3833068a254SAlexandre Belloni 
38460da7380SShang XiaoJing 	err = dev_set_name(&rtc->dev, "rtc%d", id);
3851bfc485bSBartosz Golaszewski 	if (err)
3863068a254SAlexandre Belloni 		return ERR_PTR(err);
3871bfc485bSBartosz Golaszewski 
3881bfc485bSBartosz Golaszewski 	return rtc;
3893068a254SAlexandre Belloni }
3903068a254SAlexandre Belloni EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
3913068a254SAlexandre Belloni 
__devm_rtc_register_device(struct module * owner,struct rtc_device * rtc)392fdcfd854SBartosz Golaszewski int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
3933068a254SAlexandre Belloni {
3943068a254SAlexandre Belloni 	struct rtc_wkalrm alrm;
3953068a254SAlexandre Belloni 	int err;
3963068a254SAlexandre Belloni 
397924068e5SAlexandre Belloni 	if (!rtc->ops) {
398924068e5SAlexandre Belloni 		dev_dbg(&rtc->dev, "no ops set\n");
3993068a254SAlexandre Belloni 		return -EINVAL;
400924068e5SAlexandre Belloni 	}
4013068a254SAlexandre Belloni 
4027ae41220SAlexandre Belloni 	if (!rtc->ops->set_alarm)
4037ae41220SAlexandre Belloni 		clear_bit(RTC_FEATURE_ALARM, rtc->features);
4047ae41220SAlexandre Belloni 
40522685519SAlexandre Belloni 	if (rtc->ops->set_offset)
40622685519SAlexandre Belloni 		set_bit(RTC_FEATURE_CORRECTION, rtc->features);
40722685519SAlexandre Belloni 
4083068a254SAlexandre Belloni 	rtc->owner = owner;
40998951564SBaolin Wang 	rtc_device_get_offset(rtc);
4103068a254SAlexandre Belloni 
4113068a254SAlexandre Belloni 	/* Check to see if there is an ALARM already set in hw */
4123068a254SAlexandre Belloni 	err = __rtc_read_alarm(rtc, &alrm);
4133068a254SAlexandre Belloni 	if (!err && !rtc_valid_tm(&alrm.time))
4143068a254SAlexandre Belloni 		rtc_initialize_alarm(rtc, &alrm);
4153068a254SAlexandre Belloni 
4163068a254SAlexandre Belloni 	rtc_dev_prepare(rtc);
4173068a254SAlexandre Belloni 
4183068a254SAlexandre Belloni 	err = cdev_device_add(&rtc->char_dev, &rtc->dev);
419789c1093SYang Yingliang 	if (err) {
420789c1093SYang Yingliang 		set_bit(RTC_NO_CDEV, &rtc->flags);
4213068a254SAlexandre Belloni 		dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
4223068a254SAlexandre Belloni 			 MAJOR(rtc->dev.devt), rtc->id);
423789c1093SYang Yingliang 	} else {
4243068a254SAlexandre Belloni 		dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
4253068a254SAlexandre Belloni 			MAJOR(rtc->dev.devt), rtc->id);
426789c1093SYang Yingliang 	}
4273068a254SAlexandre Belloni 
4283068a254SAlexandre Belloni 	rtc_proc_add_device(rtc);
4293068a254SAlexandre Belloni 
4303068a254SAlexandre Belloni 	dev_info(rtc->dev.parent, "registered as %s\n",
4313068a254SAlexandre Belloni 		 dev_name(&rtc->dev));
4323068a254SAlexandre Belloni 
433f9b2a4d6SSteve Muckle #ifdef CONFIG_RTC_HCTOSYS_DEVICE
434f9b2a4d6SSteve Muckle 	if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
4355614a4a3SAlexandre Belloni 		rtc_hctosys(rtc);
436f9b2a4d6SSteve Muckle #endif
437f9b2a4d6SSteve Muckle 
438fdcfd854SBartosz Golaszewski 	return devm_add_action_or_reset(rtc->dev.parent,
439fdcfd854SBartosz Golaszewski 					devm_rtc_unregister_device, rtc);
4403068a254SAlexandre Belloni }
441fdcfd854SBartosz Golaszewski EXPORT_SYMBOL_GPL(__devm_rtc_register_device);
4423068a254SAlexandre Belloni 
443a2694414SAlexandre Belloni /**
444a2694414SAlexandre Belloni  * devm_rtc_device_register - resource managed rtc_device_register()
445a2694414SAlexandre Belloni  * @dev: the device to register
446a2694414SAlexandre Belloni  * @name: the name of the device (unused)
447a2694414SAlexandre Belloni  * @ops: the rtc operations structure
448a2694414SAlexandre Belloni  * @owner: the module owner
449a2694414SAlexandre Belloni  *
450a2694414SAlexandre Belloni  * @return a struct rtc on success, or an ERR_PTR on error
451a2694414SAlexandre Belloni  *
452a2694414SAlexandre Belloni  * Managed rtc_device_register(). The rtc_device returned from this function
453a2694414SAlexandre Belloni  * are automatically freed on driver detach.
454a2694414SAlexandre Belloni  * This function is deprecated, use devm_rtc_allocate_device and
455a2694414SAlexandre Belloni  * rtc_register_device instead
456a2694414SAlexandre Belloni  */
devm_rtc_device_register(struct device * dev,const char * name,const struct rtc_class_ops * ops,struct module * owner)457a2694414SAlexandre Belloni struct rtc_device *devm_rtc_device_register(struct device *dev,
458a2694414SAlexandre Belloni 					    const char *name,
459a2694414SAlexandre Belloni 					    const struct rtc_class_ops *ops,
460a2694414SAlexandre Belloni 					    struct module *owner)
461a2694414SAlexandre Belloni {
462a2694414SAlexandre Belloni 	struct rtc_device *rtc;
463a2694414SAlexandre Belloni 	int err;
464a2694414SAlexandre Belloni 
465a2694414SAlexandre Belloni 	rtc = devm_rtc_allocate_device(dev);
466a2694414SAlexandre Belloni 	if (IS_ERR(rtc))
467a2694414SAlexandre Belloni 		return rtc;
468a2694414SAlexandre Belloni 
469a2694414SAlexandre Belloni 	rtc->ops = ops;
470a2694414SAlexandre Belloni 
471fdcfd854SBartosz Golaszewski 	err = __devm_rtc_register_device(owner, rtc);
472a2694414SAlexandre Belloni 	if (err)
473a2694414SAlexandre Belloni 		return ERR_PTR(err);
474a2694414SAlexandre Belloni 
475a2694414SAlexandre Belloni 	return rtc;
476a2694414SAlexandre Belloni }
477a2694414SAlexandre Belloni EXPORT_SYMBOL_GPL(devm_rtc_device_register);
478a2694414SAlexandre Belloni 
rtc_init(void)4790c86edc0SAlessandro Zummo static int __init rtc_init(void)
4800c86edc0SAlessandro Zummo {
4816b6ca096SRicardo B. Marliere 	int err;
4826b6ca096SRicardo B. Marliere 
4836b6ca096SRicardo B. Marliere 	err = class_register(&rtc_class);
4846b6ca096SRicardo B. Marliere 	if (err)
4856b6ca096SRicardo B. Marliere 		return err;
4866b6ca096SRicardo B. Marliere 
4875726fb20SDavid Brownell 	rtc_dev_init();
4886b6ca096SRicardo B. Marliere 
4890c86edc0SAlessandro Zummo 	return 0;
4900c86edc0SAlessandro Zummo }
491818a8674SDavid Brownell subsys_initcall(rtc_init);
492