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