blob: 22190ad28e8b654ac0a52700502881a1b4e10761 [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08002/*
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
Alexandre Bellonicdf75452019-03-13 23:02:48 +01009 */
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080010
Jingoo Hanc100a5e2013-02-21 16:45:23 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080013#include <linux/module.h>
Stephen Warren9d2b7e52014-01-23 15:55:14 -080014#include <linux/of.h>
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080015#include <linux/rtc.h>
16#include <linux/kdev_t.h>
17#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
John Stultz6610e082010-09-23 15:07:34 -070019#include <linux/workqueue.h>
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080020
David Brownell5726fb22007-05-08 00:33:27 -070021#include "rtc-core.h"
22
23
Jonathan Cameron6d03d062011-11-02 13:37:49 -070024static DEFINE_IDA(rtc_ida);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080025struct class *rtc_class;
26
David Brownellcd966202007-05-08 00:33:40 -070027static void rtc_device_release(struct device *dev)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080028{
David Brownellcd966202007-05-08 00:33:40 -070029 struct rtc_device *rtc = to_rtc_device(dev);
Jonathan Cameron6d03d062011-11-02 13:37:49 -070030 ida_simple_remove(&rtc_ida, rtc->id);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080031 kfree(rtc);
32}
33
David Fries4c24e292012-10-04 17:14:12 -070034#ifdef CONFIG_RTC_HCTOSYS_DEVICE
35/* Result of the last RTC to system clock attempt. */
36int rtc_hctosys_ret = -ENODEV;
37#endif
David Brownell7ca1d482007-05-08 00:33:42 -070038
Shuah Khan92e7f042013-07-03 15:07:59 -070039#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
David Brownell7ca1d482007-05-08 00:33:42 -070040/*
41 * On suspend(), measure the delta between one RTC and the
42 * system's wall clock; restore it on resume().
43 */
44
John Stultzd4bda8f2014-11-20 21:06:12 -080045static struct timespec64 old_rtc, old_system, old_delta;
John Stultz3dcad5f2011-05-27 11:33:18 -070046
David Brownell7ca1d482007-05-08 00:33:42 -070047
Shuah Khan92e7f042013-07-03 15:07:59 -070048static int rtc_suspend(struct device *dev)
David Brownell7ca1d482007-05-08 00:33:42 -070049{
50 struct rtc_device *rtc = to_rtc_device(dev);
51 struct rtc_time tm;
John Stultzd4bda8f2014-11-20 21:06:12 -080052 struct timespec64 delta, delta_delta;
Hyogi Gime1d60092014-08-08 14:20:24 -070053 int err;
Feng Tang9ecf37e2013-01-16 00:09:48 +080054
Xunlei Pang0fa88cb2015-04-01 20:34:38 -070055 if (timekeeping_rtc_skipsuspend())
Feng Tang9ecf37e2013-01-16 00:09:48 +080056 return 0;
57
Kay Sieversd4afc762009-01-06 14:42:11 -080058 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -070059 return 0;
60
John Stultz3dcad5f2011-05-27 11:33:18 -070061 /* snapshot the current RTC and system time at suspend*/
Hyogi Gime1d60092014-08-08 14:20:24 -070062 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
Arnd Bergmann5089ea12018-07-11 14:47:12 +020068 ktime_get_real_ts64(&old_system);
John Stultzd4bda8f2014-11-20 21:06:12 -080069 old_rtc.tv_sec = rtc_tm_to_time64(&tm);
John Stultz3dcad5f2011-05-27 11:33:18 -070070
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 */
John Stultzd4bda8f2014-11-20 21:06:12 -080078 delta = timespec64_sub(old_system, old_rtc);
79 delta_delta = timespec64_sub(delta, old_delta);
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -080080 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
John Stultz3dcad5f2011-05-27 11:33:18 -070081 /*
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 */
John Stultzd4bda8f2014-11-20 21:06:12 -080088 old_system = timespec64_sub(old_system, delta_delta);
John Stultz3dcad5f2011-05-27 11:33:18 -070089 }
David Brownell7ca1d482007-05-08 00:33:42 -070090
David Brownell7ca1d482007-05-08 00:33:42 -070091 return 0;
92}
93
94static int rtc_resume(struct device *dev)
95{
96 struct rtc_device *rtc = to_rtc_device(dev);
97 struct rtc_time tm;
John Stultzd4bda8f2014-11-20 21:06:12 -080098 struct timespec64 new_system, new_rtc;
99 struct timespec64 sleep_time;
Hyogi Gime1d60092014-08-08 14:20:24 -0700100 int err;
David Brownell7ca1d482007-05-08 00:33:42 -0700101
Xunlei Pang0fa88cb2015-04-01 20:34:38 -0700102 if (timekeeping_rtc_skipresume())
Feng Tang9ecf37e2013-01-16 00:09:48 +0800103 return 0;
104
David Fries4c24e292012-10-04 17:14:12 -0700105 rtc_hctosys_ret = -ENODEV;
Kay Sieversd4afc762009-01-06 14:42:11 -0800106 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
David Brownell7ca1d482007-05-08 00:33:42 -0700107 return 0;
108
John Stultz3dcad5f2011-05-27 11:33:18 -0700109 /* snapshot the current rtc and system time at resume */
Arnd Bergmann5089ea12018-07-11 14:47:12 +0200110 ktime_get_real_ts64(&new_system);
Hyogi Gime1d60092014-08-08 14:20:24 -0700111 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
John Stultzd4bda8f2014-11-20 21:06:12 -0800117 new_rtc.tv_sec = rtc_tm_to_time64(&tm);
John Stultz3dcad5f2011-05-27 11:33:18 -0700118 new_rtc.tv_nsec = 0;
119
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800120 if (new_rtc.tv_sec < old_rtc.tv_sec) {
121 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
David Brownell7ca1d482007-05-08 00:33:42 -0700122 return 0;
123 }
124
John Stultz3dcad5f2011-05-27 11:33:18 -0700125 /* calculate the RTC time delta (sleep time)*/
John Stultzd4bda8f2014-11-20 21:06:12 -0800126 sleep_time = timespec64_sub(new_rtc, old_rtc);
David Brownell7ca1d482007-05-08 00:33:42 -0700127
John Stultz3dcad5f2011-05-27 11:33:18 -0700128 /*
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 */
John Stultzd4bda8f2014-11-20 21:06:12 -0800135 sleep_time = timespec64_sub(sleep_time,
136 timespec64_sub(new_system, old_system));
John Stultz3dcad5f2011-05-27 11:33:18 -0700137
Arve Hjønnevåg6a8943d2011-11-22 18:24:51 -0800138 if (sleep_time.tv_sec >= 0)
John Stultzd4bda8f2014-11-20 21:06:12 -0800139 timekeeping_inject_sleeptime64(&sleep_time);
David Fries4c24e292012-10-04 17:14:12 -0700140 rtc_hctosys_ret = 0;
David Brownell7ca1d482007-05-08 00:33:42 -0700141 return 0;
142}
143
Shuah Khan92e7f042013-07-03 15:07:59 -0700144static 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)
David Brownell7ca1d482007-05-08 00:33:42 -0700146#else
Shuah Khan92e7f042013-07-03 15:07:59 -0700147#define RTC_CLASS_DEV_PM_OPS NULL
David Brownell7ca1d482007-05-08 00:33:42 -0700148#endif
149
Alexandre Belloni3068a252017-07-06 11:42:00 +0200150/* Ensure the caller will set the id before releasing the device */
Alexandre Bellonid1bec202017-07-06 11:41:58 +0200151static 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
Jason Gunthorpe0f295b02017-10-13 11:54:33 -0600161 /* Drivers can revise this default after allocating the device. */
162 rtc->set_offset_nsec = NSEC_PER_SEC / 2;
163
Alexandre Bellonid1bec202017-07-06 11:41:58 +0200164 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);
Alexandre Bellonid1bec202017-07-06 11:41:58 +0200172 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 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100178 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
Alexandre Bellonid1bec202017-07-06 11:41:58 +0200179 /* Init uie timer */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100180 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
Alexandre Bellonid1bec202017-07-06 11:41:58 +0200181 /* 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}
David Brownell7ca1d482007-05-08 00:33:42 -0700188
Alexandre Bellonib91336d2017-07-06 11:41:59 +0200189static 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
Baolin Wang98951562018-01-08 14:04:50 +0800210static 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
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800277/**
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800278 * rtc_device_unregister - removes the previously registered RTC class device
279 *
280 * @rtc: the RTC class device to destroy
281 */
Alexandre Belloni1e479c62018-09-12 22:22:45 +0200282static void rtc_device_unregister(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800283{
Dmitry Torokhovc3b399a42015-07-20 16:02:50 -0700284 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 */
Dmitry Torokhovc3b399a42015-07-20 16:02:50 -0700289 rtc_proc_del_device(rtc);
Logan Gunthorped5ed9172017-03-17 12:48:21 -0600290 cdev_device_del(&rtc->char_dev, &rtc->dev);
Dmitry Torokhovc3b399a42015-07-20 16:02:50 -0700291 rtc->ops = NULL;
292 mutex_unlock(&rtc->ops_lock);
293 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800294}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800295
Alexandre Belloni3068a252017-07-06 11:42:00 +0200296static void devm_rtc_release_device(struct device *dev, void *res)
297{
298 struct rtc_device *rtc = *(struct rtc_device **)res;
299
Alexandre Belloniac757792018-02-28 21:58:02 +0100300 rtc_nvmem_unregister(rtc);
301
Alexandre Belloni3068a252017-07-06 11:42:00 +0200302 if (rtc->registered)
303 rtc_device_unregister(rtc);
304 else
305 put_device(&rtc->dev);
306}
307
308struct 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
338exit_devres:
339 devres_free(ptr);
340exit_ida:
341 ida_simple_remove(&rtc_ida, id);
342 return ERR_PTR(err);
343}
344EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
345
346int __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;
Baolin Wang98951562018-01-08 14:04:50 +0800355 rtc_device_get_offset(rtc);
Alexandre Belloni3068a252017-07-06 11:42:00 +0200356
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}
380EXPORT_SYMBOL_GPL(__rtc_register_device);
381
Alexandre Bellonia2694412018-12-18 22:15:58 +0100382/**
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 */
396struct 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}
416EXPORT_SYMBOL_GPL(devm_rtc_device_register);
417
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800418static int __init rtc_init(void)
419{
420 rtc_class = class_create(THIS_MODULE, "rtc");
421 if (IS_ERR(rtc_class)) {
Jingoo Hanc100a5e2013-02-21 16:45:23 -0800422 pr_err("couldn't create class\n");
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800423 return PTR_ERR(rtc_class);
424 }
Shuah Khan92e7f042013-07-03 15:07:59 -0700425 rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
David Brownell5726fb22007-05-08 00:33:27 -0700426 rtc_dev_init();
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800427 return 0;
428}
David Brownell818a8672006-09-30 23:28:15 -0700429subsys_initcall(rtc_init);