xref: /OK3568_Linux_fs/kernel/drivers/rtc/class.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * RTC subsystem, base class
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005 Tower Technologies
6*4882a593Smuzhiyun  * Author: Alessandro Zummo <a.zummo@towertech.it>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * class skeleton from drivers/hwmon/hwmon.c
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/of.h>
15*4882a593Smuzhiyun #include <linux/rtc.h>
16*4882a593Smuzhiyun #include <linux/kdev_t.h>
17*4882a593Smuzhiyun #include <linux/idr.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/workqueue.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "rtc-core.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun static DEFINE_IDA(rtc_ida);
24*4882a593Smuzhiyun struct class *rtc_class;
25*4882a593Smuzhiyun 
rtc_device_release(struct device * dev)26*4882a593Smuzhiyun static void rtc_device_release(struct device *dev)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	struct rtc_device *rtc = to_rtc_device(dev);
29*4882a593Smuzhiyun 	struct timerqueue_head *head = &rtc->timerqueue;
30*4882a593Smuzhiyun 	struct timerqueue_node *node;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	mutex_lock(&rtc->ops_lock);
33*4882a593Smuzhiyun 	while ((node = timerqueue_getnext(head)))
34*4882a593Smuzhiyun 		timerqueue_del(head, node);
35*4882a593Smuzhiyun 	mutex_unlock(&rtc->ops_lock);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	cancel_work_sync(&rtc->irqwork);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	ida_simple_remove(&rtc_ida, rtc->id);
40*4882a593Smuzhiyun 	kfree(rtc);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #ifdef CONFIG_RTC_HCTOSYS_DEVICE
44*4882a593Smuzhiyun /* Result of the last RTC to system clock attempt. */
45*4882a593Smuzhiyun int rtc_hctosys_ret = -ENODEV;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
48*4882a593Smuzhiyun  * whether it stores the most close value or the value with partial
49*4882a593Smuzhiyun  * seconds truncated. However, it is important that we use it to store
50*4882a593Smuzhiyun  * the truncated value. This is because otherwise it is necessary,
51*4882a593Smuzhiyun  * in an rtc sync function, to read both xtime.tv_sec and
52*4882a593Smuzhiyun  * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
53*4882a593Smuzhiyun  * of >32bits is not possible. So storing the most close value would
54*4882a593Smuzhiyun  * slow down the sync API. So here we have the truncated value and
55*4882a593Smuzhiyun  * the best guess is to add 0.5s.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun 
rtc_hctosys(struct rtc_device * rtc)58*4882a593Smuzhiyun static void rtc_hctosys(struct rtc_device *rtc)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	int err;
61*4882a593Smuzhiyun 	struct rtc_time tm;
62*4882a593Smuzhiyun 	struct timespec64 tv64 = {
63*4882a593Smuzhiyun 		.tv_nsec = NSEC_PER_SEC >> 1,
64*4882a593Smuzhiyun 	};
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	err = rtc_read_time(rtc, &tm);
67*4882a593Smuzhiyun 	if (err) {
68*4882a593Smuzhiyun 		dev_err(rtc->dev.parent,
69*4882a593Smuzhiyun 			"hctosys: unable to read the hardware clock\n");
70*4882a593Smuzhiyun 		goto err_read;
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	tv64.tv_sec = rtc_tm_to_time64(&tm);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #if BITS_PER_LONG == 32
76*4882a593Smuzhiyun 	if (tv64.tv_sec > INT_MAX) {
77*4882a593Smuzhiyun 		err = -ERANGE;
78*4882a593Smuzhiyun 		goto err_read;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	err = do_settimeofday64(&tv64);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
85*4882a593Smuzhiyun 		 &tm, (long long)tv64.tv_sec);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun err_read:
88*4882a593Smuzhiyun 	rtc_hctosys_ret = err;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun  * On suspend(), measure the delta between one RTC and the
95*4882a593Smuzhiyun  * system's wall clock; restore it on resume().
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun static struct timespec64 old_rtc, old_system, old_delta;
99*4882a593Smuzhiyun 
rtc_suspend(struct device * dev)100*4882a593Smuzhiyun static int rtc_suspend(struct device *dev)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct rtc_device	*rtc = to_rtc_device(dev);
103*4882a593Smuzhiyun 	struct rtc_time		tm;
104*4882a593Smuzhiyun 	struct timespec64	delta, delta_delta;
105*4882a593Smuzhiyun 	int err;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (timekeeping_rtc_skipsuspend())
108*4882a593Smuzhiyun 		return 0;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
111*4882a593Smuzhiyun 		return 0;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* snapshot the current RTC and system time at suspend*/
114*4882a593Smuzhiyun 	err = rtc_read_time(rtc, &tm);
115*4882a593Smuzhiyun 	if (err < 0) {
116*4882a593Smuzhiyun 		pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
117*4882a593Smuzhiyun 		return 0;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	ktime_get_real_ts64(&old_system);
121*4882a593Smuzhiyun 	old_rtc.tv_sec = rtc_tm_to_time64(&tm);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/*
124*4882a593Smuzhiyun 	 * To avoid drift caused by repeated suspend/resumes,
125*4882a593Smuzhiyun 	 * which each can add ~1 second drift error,
126*4882a593Smuzhiyun 	 * try to compensate so the difference in system time
127*4882a593Smuzhiyun 	 * and rtc time stays close to constant.
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	delta = timespec64_sub(old_system, old_rtc);
130*4882a593Smuzhiyun 	delta_delta = timespec64_sub(delta, old_delta);
131*4882a593Smuzhiyun 	if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
132*4882a593Smuzhiyun 		/*
133*4882a593Smuzhiyun 		 * if delta_delta is too large, assume time correction
134*4882a593Smuzhiyun 		 * has occurred and set old_delta to the current delta.
135*4882a593Smuzhiyun 		 */
136*4882a593Smuzhiyun 		old_delta = delta;
137*4882a593Smuzhiyun 	} else {
138*4882a593Smuzhiyun 		/* Otherwise try to adjust old_system to compensate */
139*4882a593Smuzhiyun 		old_system = timespec64_sub(old_system, delta_delta);
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return 0;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
rtc_resume(struct device * dev)145*4882a593Smuzhiyun static int rtc_resume(struct device *dev)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct rtc_device	*rtc = to_rtc_device(dev);
148*4882a593Smuzhiyun 	struct rtc_time		tm;
149*4882a593Smuzhiyun 	struct timespec64	new_system, new_rtc;
150*4882a593Smuzhiyun 	struct timespec64	sleep_time;
151*4882a593Smuzhiyun 	int err;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	if (timekeeping_rtc_skipresume())
154*4882a593Smuzhiyun 		return 0;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	rtc_hctosys_ret = -ENODEV;
157*4882a593Smuzhiyun 	if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
158*4882a593Smuzhiyun 		return 0;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* snapshot the current rtc and system time at resume */
161*4882a593Smuzhiyun 	ktime_get_real_ts64(&new_system);
162*4882a593Smuzhiyun 	err = rtc_read_time(rtc, &tm);
163*4882a593Smuzhiyun 	if (err < 0) {
164*4882a593Smuzhiyun 		pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
165*4882a593Smuzhiyun 		return 0;
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	new_rtc.tv_sec = rtc_tm_to_time64(&tm);
169*4882a593Smuzhiyun 	new_rtc.tv_nsec = 0;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	if (new_rtc.tv_sec < old_rtc.tv_sec) {
172*4882a593Smuzhiyun 		pr_debug("%s:  time travel!\n", dev_name(&rtc->dev));
173*4882a593Smuzhiyun 		return 0;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* calculate the RTC time delta (sleep time)*/
177*4882a593Smuzhiyun 	sleep_time = timespec64_sub(new_rtc, old_rtc);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/*
180*4882a593Smuzhiyun 	 * Since these RTC suspend/resume handlers are not called
181*4882a593Smuzhiyun 	 * at the very end of suspend or the start of resume,
182*4882a593Smuzhiyun 	 * some run-time may pass on either sides of the sleep time
183*4882a593Smuzhiyun 	 * so subtract kernel run-time between rtc_suspend to rtc_resume
184*4882a593Smuzhiyun 	 * to keep things accurate.
185*4882a593Smuzhiyun 	 */
186*4882a593Smuzhiyun 	sleep_time = timespec64_sub(sleep_time,
187*4882a593Smuzhiyun 				    timespec64_sub(new_system, old_system));
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (sleep_time.tv_sec >= 0)
190*4882a593Smuzhiyun 		timekeeping_inject_sleeptime64(&sleep_time);
191*4882a593Smuzhiyun 	rtc_hctosys_ret = 0;
192*4882a593Smuzhiyun 	return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
196*4882a593Smuzhiyun #define RTC_CLASS_DEV_PM_OPS	(&rtc_class_dev_pm_ops)
197*4882a593Smuzhiyun #else
198*4882a593Smuzhiyun #define RTC_CLASS_DEV_PM_OPS	NULL
199*4882a593Smuzhiyun #endif
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /* Ensure the caller will set the id before releasing the device */
rtc_allocate_device(void)202*4882a593Smuzhiyun static struct rtc_device *rtc_allocate_device(void)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct rtc_device *rtc;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
207*4882a593Smuzhiyun 	if (!rtc)
208*4882a593Smuzhiyun 		return NULL;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	device_initialize(&rtc->dev);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/* Drivers can revise this default after allocating the device. */
213*4882a593Smuzhiyun 	rtc->set_offset_nsec =  NSEC_PER_SEC / 2;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	rtc->irq_freq = 1;
216*4882a593Smuzhiyun 	rtc->max_user_freq = 64;
217*4882a593Smuzhiyun 	rtc->dev.class = rtc_class;
218*4882a593Smuzhiyun 	rtc->dev.groups = rtc_get_dev_attribute_groups();
219*4882a593Smuzhiyun 	rtc->dev.release = rtc_device_release;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	mutex_init(&rtc->ops_lock);
222*4882a593Smuzhiyun 	spin_lock_init(&rtc->irq_lock);
223*4882a593Smuzhiyun 	init_waitqueue_head(&rtc->irq_queue);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* Init timerqueue */
226*4882a593Smuzhiyun 	timerqueue_init_head(&rtc->timerqueue);
227*4882a593Smuzhiyun 	INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
228*4882a593Smuzhiyun 	/* Init aie timer */
229*4882a593Smuzhiyun 	rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
230*4882a593Smuzhiyun 	/* Init uie timer */
231*4882a593Smuzhiyun 	rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
232*4882a593Smuzhiyun 	/* Init pie timer */
233*4882a593Smuzhiyun 	hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
234*4882a593Smuzhiyun 	rtc->pie_timer.function = rtc_pie_update_irq;
235*4882a593Smuzhiyun 	rtc->pie_enabled = 0;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	return rtc;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
rtc_device_get_id(struct device * dev)240*4882a593Smuzhiyun static int rtc_device_get_id(struct device *dev)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	int of_id = -1, id = -1;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	if (dev->of_node)
245*4882a593Smuzhiyun 		of_id = of_alias_get_id(dev->of_node, "rtc");
246*4882a593Smuzhiyun 	else if (dev->parent && dev->parent->of_node)
247*4882a593Smuzhiyun 		of_id = of_alias_get_id(dev->parent->of_node, "rtc");
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	if (of_id >= 0) {
250*4882a593Smuzhiyun 		id = ida_simple_get(&rtc_ida, of_id, of_id + 1, GFP_KERNEL);
251*4882a593Smuzhiyun 		if (id < 0)
252*4882a593Smuzhiyun 			dev_warn(dev, "/aliases ID %d not available\n", of_id);
253*4882a593Smuzhiyun 	}
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (id < 0)
256*4882a593Smuzhiyun 		id = ida_simple_get(&rtc_ida, 0, 0, GFP_KERNEL);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	return id;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
rtc_device_get_offset(struct rtc_device * rtc)261*4882a593Smuzhiyun static void rtc_device_get_offset(struct rtc_device *rtc)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	time64_t range_secs;
264*4882a593Smuzhiyun 	u32 start_year;
265*4882a593Smuzhiyun 	int ret;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/*
268*4882a593Smuzhiyun 	 * If RTC driver did not implement the range of RTC hardware device,
269*4882a593Smuzhiyun 	 * then we can not expand the RTC range by adding or subtracting one
270*4882a593Smuzhiyun 	 * offset.
271*4882a593Smuzhiyun 	 */
272*4882a593Smuzhiyun 	if (rtc->range_min == rtc->range_max)
273*4882a593Smuzhiyun 		return;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	ret = device_property_read_u32(rtc->dev.parent, "start-year",
276*4882a593Smuzhiyun 				       &start_year);
277*4882a593Smuzhiyun 	if (!ret) {
278*4882a593Smuzhiyun 		rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
279*4882a593Smuzhiyun 		rtc->set_start_time = true;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	/*
283*4882a593Smuzhiyun 	 * If user did not implement the start time for RTC driver, then no
284*4882a593Smuzhiyun 	 * need to expand the RTC range.
285*4882a593Smuzhiyun 	 */
286*4882a593Smuzhiyun 	if (!rtc->set_start_time)
287*4882a593Smuzhiyun 		return;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	range_secs = rtc->range_max - rtc->range_min + 1;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/*
292*4882a593Smuzhiyun 	 * If the start_secs is larger than the maximum seconds (rtc->range_max)
293*4882a593Smuzhiyun 	 * supported by RTC hardware or the maximum seconds of new expanded
294*4882a593Smuzhiyun 	 * range (start_secs + rtc->range_max - rtc->range_min) is less than
295*4882a593Smuzhiyun 	 * rtc->range_min, which means the minimum seconds (rtc->range_min) of
296*4882a593Smuzhiyun 	 * RTC hardware will be mapped to start_secs by adding one offset, so
297*4882a593Smuzhiyun 	 * the offset seconds calculation formula should be:
298*4882a593Smuzhiyun 	 * rtc->offset_secs = rtc->start_secs - rtc->range_min;
299*4882a593Smuzhiyun 	 *
300*4882a593Smuzhiyun 	 * If the start_secs is larger than the minimum seconds (rtc->range_min)
301*4882a593Smuzhiyun 	 * supported by RTC hardware, then there is one region is overlapped
302*4882a593Smuzhiyun 	 * between the original RTC hardware range and the new expanded range,
303*4882a593Smuzhiyun 	 * and this overlapped region do not need to be mapped into the new
304*4882a593Smuzhiyun 	 * expanded range due to it is valid for RTC device. So the minimum
305*4882a593Smuzhiyun 	 * seconds of RTC hardware (rtc->range_min) should be mapped to
306*4882a593Smuzhiyun 	 * rtc->range_max + 1, then the offset seconds formula should be:
307*4882a593Smuzhiyun 	 * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
308*4882a593Smuzhiyun 	 *
309*4882a593Smuzhiyun 	 * If the start_secs is less than the minimum seconds (rtc->range_min),
310*4882a593Smuzhiyun 	 * which is similar to case 2. So the start_secs should be mapped to
311*4882a593Smuzhiyun 	 * start_secs + rtc->range_max - rtc->range_min + 1, then the
312*4882a593Smuzhiyun 	 * offset seconds formula should be:
313*4882a593Smuzhiyun 	 * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
314*4882a593Smuzhiyun 	 *
315*4882a593Smuzhiyun 	 * Otherwise the offset seconds should be 0.
316*4882a593Smuzhiyun 	 */
317*4882a593Smuzhiyun 	if (rtc->start_secs > rtc->range_max ||
318*4882a593Smuzhiyun 	    rtc->start_secs + range_secs - 1 < rtc->range_min)
319*4882a593Smuzhiyun 		rtc->offset_secs = rtc->start_secs - rtc->range_min;
320*4882a593Smuzhiyun 	else if (rtc->start_secs > rtc->range_min)
321*4882a593Smuzhiyun 		rtc->offset_secs = range_secs;
322*4882a593Smuzhiyun 	else if (rtc->start_secs < rtc->range_min)
323*4882a593Smuzhiyun 		rtc->offset_secs = -range_secs;
324*4882a593Smuzhiyun 	else
325*4882a593Smuzhiyun 		rtc->offset_secs = 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /**
329*4882a593Smuzhiyun  * rtc_device_unregister - removes the previously registered RTC class device
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * @rtc: the RTC class device to destroy
332*4882a593Smuzhiyun  */
rtc_device_unregister(struct rtc_device * rtc)333*4882a593Smuzhiyun static void rtc_device_unregister(struct rtc_device *rtc)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	mutex_lock(&rtc->ops_lock);
336*4882a593Smuzhiyun 	/*
337*4882a593Smuzhiyun 	 * Remove innards of this RTC, then disable it, before
338*4882a593Smuzhiyun 	 * letting any rtc_class_open() users access it again
339*4882a593Smuzhiyun 	 */
340*4882a593Smuzhiyun 	rtc_proc_del_device(rtc);
341*4882a593Smuzhiyun 	cdev_device_del(&rtc->char_dev, &rtc->dev);
342*4882a593Smuzhiyun 	rtc->ops = NULL;
343*4882a593Smuzhiyun 	mutex_unlock(&rtc->ops_lock);
344*4882a593Smuzhiyun 	put_device(&rtc->dev);
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
devm_rtc_release_device(struct device * dev,void * res)347*4882a593Smuzhiyun static void devm_rtc_release_device(struct device *dev, void *res)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	struct rtc_device *rtc = *(struct rtc_device **)res;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	rtc_nvmem_unregister(rtc);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	if (rtc->registered)
354*4882a593Smuzhiyun 		rtc_device_unregister(rtc);
355*4882a593Smuzhiyun 	else
356*4882a593Smuzhiyun 		put_device(&rtc->dev);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
devm_rtc_allocate_device(struct device * dev)359*4882a593Smuzhiyun struct rtc_device *devm_rtc_allocate_device(struct device *dev)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	struct rtc_device **ptr, *rtc;
362*4882a593Smuzhiyun 	int id, err;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	id = rtc_device_get_id(dev);
365*4882a593Smuzhiyun 	if (id < 0)
366*4882a593Smuzhiyun 		return ERR_PTR(id);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	ptr = devres_alloc(devm_rtc_release_device, sizeof(*ptr), GFP_KERNEL);
369*4882a593Smuzhiyun 	if (!ptr) {
370*4882a593Smuzhiyun 		err = -ENOMEM;
371*4882a593Smuzhiyun 		goto exit_ida;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	rtc = rtc_allocate_device();
375*4882a593Smuzhiyun 	if (!rtc) {
376*4882a593Smuzhiyun 		err = -ENOMEM;
377*4882a593Smuzhiyun 		goto exit_devres;
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	*ptr = rtc;
381*4882a593Smuzhiyun 	devres_add(dev, ptr);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	rtc->id = id;
384*4882a593Smuzhiyun 	rtc->dev.parent = dev;
385*4882a593Smuzhiyun 	dev_set_name(&rtc->dev, "rtc%d", id);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	return rtc;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun exit_devres:
390*4882a593Smuzhiyun 	devres_free(ptr);
391*4882a593Smuzhiyun exit_ida:
392*4882a593Smuzhiyun 	ida_simple_remove(&rtc_ida, id);
393*4882a593Smuzhiyun 	return ERR_PTR(err);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
396*4882a593Smuzhiyun 
__rtc_register_device(struct module * owner,struct rtc_device * rtc)397*4882a593Smuzhiyun int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	struct rtc_wkalrm alrm;
400*4882a593Smuzhiyun 	int err;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	if (!rtc->ops) {
403*4882a593Smuzhiyun 		dev_dbg(&rtc->dev, "no ops set\n");
404*4882a593Smuzhiyun 		return -EINVAL;
405*4882a593Smuzhiyun 	}
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	rtc->owner = owner;
408*4882a593Smuzhiyun 	rtc_device_get_offset(rtc);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	/* Check to see if there is an ALARM already set in hw */
411*4882a593Smuzhiyun 	err = __rtc_read_alarm(rtc, &alrm);
412*4882a593Smuzhiyun 	if (!err && !rtc_valid_tm(&alrm.time))
413*4882a593Smuzhiyun 		rtc_initialize_alarm(rtc, &alrm);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	rtc_dev_prepare(rtc);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	err = cdev_device_add(&rtc->char_dev, &rtc->dev);
418*4882a593Smuzhiyun 	if (err)
419*4882a593Smuzhiyun 		dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
420*4882a593Smuzhiyun 			 MAJOR(rtc->dev.devt), rtc->id);
421*4882a593Smuzhiyun 	else
422*4882a593Smuzhiyun 		dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
423*4882a593Smuzhiyun 			MAJOR(rtc->dev.devt), rtc->id);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	rtc_proc_add_device(rtc);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	rtc->registered = true;
428*4882a593Smuzhiyun 	dev_info(rtc->dev.parent, "registered as %s\n",
429*4882a593Smuzhiyun 		 dev_name(&rtc->dev));
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun #ifdef CONFIG_RTC_HCTOSYS_DEVICE
432*4882a593Smuzhiyun 	if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
433*4882a593Smuzhiyun 		rtc_hctosys(rtc);
434*4882a593Smuzhiyun #endif
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	return 0;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__rtc_register_device);
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /**
441*4882a593Smuzhiyun  * devm_rtc_device_register - resource managed rtc_device_register()
442*4882a593Smuzhiyun  * @dev: the device to register
443*4882a593Smuzhiyun  * @name: the name of the device (unused)
444*4882a593Smuzhiyun  * @ops: the rtc operations structure
445*4882a593Smuzhiyun  * @owner: the module owner
446*4882a593Smuzhiyun  *
447*4882a593Smuzhiyun  * @return a struct rtc on success, or an ERR_PTR on error
448*4882a593Smuzhiyun  *
449*4882a593Smuzhiyun  * Managed rtc_device_register(). The rtc_device returned from this function
450*4882a593Smuzhiyun  * are automatically freed on driver detach.
451*4882a593Smuzhiyun  * This function is deprecated, use devm_rtc_allocate_device and
452*4882a593Smuzhiyun  * rtc_register_device instead
453*4882a593Smuzhiyun  */
devm_rtc_device_register(struct device * dev,const char * name,const struct rtc_class_ops * ops,struct module * owner)454*4882a593Smuzhiyun struct rtc_device *devm_rtc_device_register(struct device *dev,
455*4882a593Smuzhiyun 					    const char *name,
456*4882a593Smuzhiyun 					    const struct rtc_class_ops *ops,
457*4882a593Smuzhiyun 					    struct module *owner)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	struct rtc_device *rtc;
460*4882a593Smuzhiyun 	int err;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	rtc = devm_rtc_allocate_device(dev);
463*4882a593Smuzhiyun 	if (IS_ERR(rtc))
464*4882a593Smuzhiyun 		return rtc;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	rtc->ops = ops;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	err = __rtc_register_device(owner, rtc);
469*4882a593Smuzhiyun 	if (err)
470*4882a593Smuzhiyun 		return ERR_PTR(err);
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	return rtc;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_rtc_device_register);
475*4882a593Smuzhiyun 
rtc_init(void)476*4882a593Smuzhiyun static int __init rtc_init(void)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	rtc_class = class_create(THIS_MODULE, "rtc");
479*4882a593Smuzhiyun 	if (IS_ERR(rtc_class)) {
480*4882a593Smuzhiyun 		pr_err("couldn't create class\n");
481*4882a593Smuzhiyun 		return PTR_ERR(rtc_class);
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 	rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
484*4882a593Smuzhiyun 	rtc_dev_init();
485*4882a593Smuzhiyun 	return 0;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun subsys_initcall(rtc_init);
488