Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * RTC subsystem, base class
0004  *
0005  * Copyright (C) 2005 Tower Technologies
0006  * Author: Alessandro Zummo <a.zummo@towertech.it>
0007  *
0008  * class skeleton from drivers/hwmon/hwmon.c
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/rtc.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/idr.h>
0018 #include <linux/slab.h>
0019 #include <linux/workqueue.h>
0020 
0021 #include "rtc-core.h"
0022 
0023 static DEFINE_IDA(rtc_ida);
0024 struct class *rtc_class;
0025 
0026 static void rtc_device_release(struct device *dev)
0027 {
0028     struct rtc_device *rtc = to_rtc_device(dev);
0029     struct timerqueue_head *head = &rtc->timerqueue;
0030     struct timerqueue_node *node;
0031 
0032     mutex_lock(&rtc->ops_lock);
0033     while ((node = timerqueue_getnext(head)))
0034         timerqueue_del(head, node);
0035     mutex_unlock(&rtc->ops_lock);
0036 
0037     cancel_work_sync(&rtc->irqwork);
0038 
0039     ida_free(&rtc_ida, rtc->id);
0040     mutex_destroy(&rtc->ops_lock);
0041     kfree(rtc);
0042 }
0043 
0044 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
0045 /* Result of the last RTC to system clock attempt. */
0046 int rtc_hctosys_ret = -ENODEV;
0047 
0048 /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
0049  * whether it stores the most close value or the value with partial
0050  * seconds truncated. However, it is important that we use it to store
0051  * the truncated value. This is because otherwise it is necessary,
0052  * in an rtc sync function, to read both xtime.tv_sec and
0053  * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
0054  * of >32bits is not possible. So storing the most close value would
0055  * slow down the sync API. So here we have the truncated value and
0056  * the best guess is to add 0.5s.
0057  */
0058 
0059 static void rtc_hctosys(struct rtc_device *rtc)
0060 {
0061     int err;
0062     struct rtc_time tm;
0063     struct timespec64 tv64 = {
0064         .tv_nsec = NSEC_PER_SEC >> 1,
0065     };
0066 
0067     err = rtc_read_time(rtc, &tm);
0068     if (err) {
0069         dev_err(rtc->dev.parent,
0070             "hctosys: unable to read the hardware clock\n");
0071         goto err_read;
0072     }
0073 
0074     tv64.tv_sec = rtc_tm_to_time64(&tm);
0075 
0076 #if BITS_PER_LONG == 32
0077     if (tv64.tv_sec > INT_MAX) {
0078         err = -ERANGE;
0079         goto err_read;
0080     }
0081 #endif
0082 
0083     err = do_settimeofday64(&tv64);
0084 
0085     dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
0086          &tm, (long long)tv64.tv_sec);
0087 
0088 err_read:
0089     rtc_hctosys_ret = err;
0090 }
0091 #endif
0092 
0093 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
0094 /*
0095  * On suspend(), measure the delta between one RTC and the
0096  * system's wall clock; restore it on resume().
0097  */
0098 
0099 static struct timespec64 old_rtc, old_system, old_delta;
0100 
0101 static int rtc_suspend(struct device *dev)
0102 {
0103     struct rtc_device   *rtc = to_rtc_device(dev);
0104     struct rtc_time     tm;
0105     struct timespec64   delta, delta_delta;
0106     int err;
0107 
0108     if (timekeeping_rtc_skipsuspend())
0109         return 0;
0110 
0111     if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
0112         return 0;
0113 
0114     /* snapshot the current RTC and system time at suspend*/
0115     err = rtc_read_time(rtc, &tm);
0116     if (err < 0) {
0117         pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
0118         return 0;
0119     }
0120 
0121     ktime_get_real_ts64(&old_system);
0122     old_rtc.tv_sec = rtc_tm_to_time64(&tm);
0123 
0124     /*
0125      * To avoid drift caused by repeated suspend/resumes,
0126      * which each can add ~1 second drift error,
0127      * try to compensate so the difference in system time
0128      * and rtc time stays close to constant.
0129      */
0130     delta = timespec64_sub(old_system, old_rtc);
0131     delta_delta = timespec64_sub(delta, old_delta);
0132     if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
0133         /*
0134          * if delta_delta is too large, assume time correction
0135          * has occurred and set old_delta to the current delta.
0136          */
0137         old_delta = delta;
0138     } else {
0139         /* Otherwise try to adjust old_system to compensate */
0140         old_system = timespec64_sub(old_system, delta_delta);
0141     }
0142 
0143     return 0;
0144 }
0145 
0146 static int rtc_resume(struct device *dev)
0147 {
0148     struct rtc_device   *rtc = to_rtc_device(dev);
0149     struct rtc_time     tm;
0150     struct timespec64   new_system, new_rtc;
0151     struct timespec64   sleep_time;
0152     int err;
0153 
0154     if (timekeeping_rtc_skipresume())
0155         return 0;
0156 
0157     rtc_hctosys_ret = -ENODEV;
0158     if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
0159         return 0;
0160 
0161     /* snapshot the current rtc and system time at resume */
0162     ktime_get_real_ts64(&new_system);
0163     err = rtc_read_time(rtc, &tm);
0164     if (err < 0) {
0165         pr_debug("%s:  fail to read rtc time\n", dev_name(&rtc->dev));
0166         return 0;
0167     }
0168 
0169     new_rtc.tv_sec = rtc_tm_to_time64(&tm);
0170     new_rtc.tv_nsec = 0;
0171 
0172     if (new_rtc.tv_sec < old_rtc.tv_sec) {
0173         pr_debug("%s:  time travel!\n", dev_name(&rtc->dev));
0174         return 0;
0175     }
0176 
0177     /* calculate the RTC time delta (sleep time)*/
0178     sleep_time = timespec64_sub(new_rtc, old_rtc);
0179 
0180     /*
0181      * Since these RTC suspend/resume handlers are not called
0182      * at the very end of suspend or the start of resume,
0183      * some run-time may pass on either sides of the sleep time
0184      * so subtract kernel run-time between rtc_suspend to rtc_resume
0185      * to keep things accurate.
0186      */
0187     sleep_time = timespec64_sub(sleep_time,
0188                     timespec64_sub(new_system, old_system));
0189 
0190     if (sleep_time.tv_sec >= 0)
0191         timekeeping_inject_sleeptime64(&sleep_time);
0192     rtc_hctosys_ret = 0;
0193     return 0;
0194 }
0195 
0196 static SIMPLE_DEV_PM_OPS(rtc_class_dev_pm_ops, rtc_suspend, rtc_resume);
0197 #define RTC_CLASS_DEV_PM_OPS    (&rtc_class_dev_pm_ops)
0198 #else
0199 #define RTC_CLASS_DEV_PM_OPS    NULL
0200 #endif
0201 
0202 /* Ensure the caller will set the id before releasing the device */
0203 static struct rtc_device *rtc_allocate_device(void)
0204 {
0205     struct rtc_device *rtc;
0206 
0207     rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
0208     if (!rtc)
0209         return NULL;
0210 
0211     device_initialize(&rtc->dev);
0212 
0213     /*
0214      * Drivers can revise this default after allocating the device.
0215      * The default is what most RTCs do: Increment seconds exactly one
0216      * second after the write happened. This adds a default transport
0217      * time of 5ms which is at least halfways close to reality.
0218      */
0219     rtc->set_offset_nsec = NSEC_PER_SEC + 5 * NSEC_PER_MSEC;
0220 
0221     rtc->irq_freq = 1;
0222     rtc->max_user_freq = 64;
0223     rtc->dev.class = rtc_class;
0224     rtc->dev.groups = rtc_get_dev_attribute_groups();
0225     rtc->dev.release = rtc_device_release;
0226 
0227     mutex_init(&rtc->ops_lock);
0228     spin_lock_init(&rtc->irq_lock);
0229     init_waitqueue_head(&rtc->irq_queue);
0230 
0231     /* Init timerqueue */
0232     timerqueue_init_head(&rtc->timerqueue);
0233     INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
0234     /* Init aie timer */
0235     rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, rtc);
0236     /* Init uie timer */
0237     rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, rtc);
0238     /* Init pie timer */
0239     hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
0240     rtc->pie_timer.function = rtc_pie_update_irq;
0241     rtc->pie_enabled = 0;
0242 
0243     set_bit(RTC_FEATURE_ALARM, rtc->features);
0244     set_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
0245 
0246     return rtc;
0247 }
0248 
0249 static int rtc_device_get_id(struct device *dev)
0250 {
0251     int of_id = -1, id = -1;
0252 
0253     if (dev->of_node)
0254         of_id = of_alias_get_id(dev->of_node, "rtc");
0255     else if (dev->parent && dev->parent->of_node)
0256         of_id = of_alias_get_id(dev->parent->of_node, "rtc");
0257 
0258     if (of_id >= 0) {
0259         id = ida_simple_get(&rtc_ida, of_id, of_id + 1, GFP_KERNEL);
0260         if (id < 0)
0261             dev_warn(dev, "/aliases ID %d not available\n", of_id);
0262     }
0263 
0264     if (id < 0)
0265         id = ida_alloc(&rtc_ida, GFP_KERNEL);
0266 
0267     return id;
0268 }
0269 
0270 static void rtc_device_get_offset(struct rtc_device *rtc)
0271 {
0272     time64_t range_secs;
0273     u32 start_year;
0274     int ret;
0275 
0276     /*
0277      * If RTC driver did not implement the range of RTC hardware device,
0278      * then we can not expand the RTC range by adding or subtracting one
0279      * offset.
0280      */
0281     if (rtc->range_min == rtc->range_max)
0282         return;
0283 
0284     ret = device_property_read_u32(rtc->dev.parent, "start-year",
0285                        &start_year);
0286     if (!ret) {
0287         rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
0288         rtc->set_start_time = true;
0289     }
0290 
0291     /*
0292      * If user did not implement the start time for RTC driver, then no
0293      * need to expand the RTC range.
0294      */
0295     if (!rtc->set_start_time)
0296         return;
0297 
0298     range_secs = rtc->range_max - rtc->range_min + 1;
0299 
0300     /*
0301      * If the start_secs is larger than the maximum seconds (rtc->range_max)
0302      * supported by RTC hardware or the maximum seconds of new expanded
0303      * range (start_secs + rtc->range_max - rtc->range_min) is less than
0304      * rtc->range_min, which means the minimum seconds (rtc->range_min) of
0305      * RTC hardware will be mapped to start_secs by adding one offset, so
0306      * the offset seconds calculation formula should be:
0307      * rtc->offset_secs = rtc->start_secs - rtc->range_min;
0308      *
0309      * If the start_secs is larger than the minimum seconds (rtc->range_min)
0310      * supported by RTC hardware, then there is one region is overlapped
0311      * between the original RTC hardware range and the new expanded range,
0312      * and this overlapped region do not need to be mapped into the new
0313      * expanded range due to it is valid for RTC device. So the minimum
0314      * seconds of RTC hardware (rtc->range_min) should be mapped to
0315      * rtc->range_max + 1, then the offset seconds formula should be:
0316      * rtc->offset_secs = rtc->range_max - rtc->range_min + 1;
0317      *
0318      * If the start_secs is less than the minimum seconds (rtc->range_min),
0319      * which is similar to case 2. So the start_secs should be mapped to
0320      * start_secs + rtc->range_max - rtc->range_min + 1, then the
0321      * offset seconds formula should be:
0322      * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1);
0323      *
0324      * Otherwise the offset seconds should be 0.
0325      */
0326     if (rtc->start_secs > rtc->range_max ||
0327         rtc->start_secs + range_secs - 1 < rtc->range_min)
0328         rtc->offset_secs = rtc->start_secs - rtc->range_min;
0329     else if (rtc->start_secs > rtc->range_min)
0330         rtc->offset_secs = range_secs;
0331     else if (rtc->start_secs < rtc->range_min)
0332         rtc->offset_secs = -range_secs;
0333     else
0334         rtc->offset_secs = 0;
0335 }
0336 
0337 static void devm_rtc_unregister_device(void *data)
0338 {
0339     struct rtc_device *rtc = data;
0340 
0341     mutex_lock(&rtc->ops_lock);
0342     /*
0343      * Remove innards of this RTC, then disable it, before
0344      * letting any rtc_class_open() users access it again
0345      */
0346     rtc_proc_del_device(rtc);
0347     if (!test_bit(RTC_NO_CDEV, &rtc->flags))
0348         cdev_device_del(&rtc->char_dev, &rtc->dev);
0349     rtc->ops = NULL;
0350     mutex_unlock(&rtc->ops_lock);
0351 }
0352 
0353 static void devm_rtc_release_device(void *res)
0354 {
0355     struct rtc_device *rtc = res;
0356 
0357     put_device(&rtc->dev);
0358 }
0359 
0360 struct rtc_device *devm_rtc_allocate_device(struct device *dev)
0361 {
0362     struct rtc_device *rtc;
0363     int id, err;
0364 
0365     id = rtc_device_get_id(dev);
0366     if (id < 0)
0367         return ERR_PTR(id);
0368 
0369     rtc = rtc_allocate_device();
0370     if (!rtc) {
0371         ida_free(&rtc_ida, id);
0372         return ERR_PTR(-ENOMEM);
0373     }
0374 
0375     rtc->id = id;
0376     rtc->dev.parent = dev;
0377     err = dev_set_name(&rtc->dev, "rtc%d", id);
0378     if (err)
0379         return ERR_PTR(err);
0380 
0381     err = devm_add_action_or_reset(dev, devm_rtc_release_device, rtc);
0382     if (err)
0383         return ERR_PTR(err);
0384 
0385     return rtc;
0386 }
0387 EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);
0388 
0389 int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
0390 {
0391     struct rtc_wkalrm alrm;
0392     int err;
0393 
0394     if (!rtc->ops) {
0395         dev_dbg(&rtc->dev, "no ops set\n");
0396         return -EINVAL;
0397     }
0398 
0399     if (!rtc->ops->set_alarm)
0400         clear_bit(RTC_FEATURE_ALARM, rtc->features);
0401 
0402     if (rtc->ops->set_offset)
0403         set_bit(RTC_FEATURE_CORRECTION, rtc->features);
0404 
0405     rtc->owner = owner;
0406     rtc_device_get_offset(rtc);
0407 
0408     /* Check to see if there is an ALARM already set in hw */
0409     err = __rtc_read_alarm(rtc, &alrm);
0410     if (!err && !rtc_valid_tm(&alrm.time))
0411         rtc_initialize_alarm(rtc, &alrm);
0412 
0413     rtc_dev_prepare(rtc);
0414 
0415     err = cdev_device_add(&rtc->char_dev, &rtc->dev);
0416     if (err) {
0417         set_bit(RTC_NO_CDEV, &rtc->flags);
0418         dev_warn(rtc->dev.parent, "failed to add char device %d:%d\n",
0419              MAJOR(rtc->dev.devt), rtc->id);
0420     } else {
0421         dev_dbg(rtc->dev.parent, "char device (%d:%d)\n",
0422             MAJOR(rtc->dev.devt), rtc->id);
0423     }
0424 
0425     rtc_proc_add_device(rtc);
0426 
0427     dev_info(rtc->dev.parent, "registered as %s\n",
0428          dev_name(&rtc->dev));
0429 
0430 #ifdef CONFIG_RTC_HCTOSYS_DEVICE
0431     if (!strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE))
0432         rtc_hctosys(rtc);
0433 #endif
0434 
0435     return devm_add_action_or_reset(rtc->dev.parent,
0436                     devm_rtc_unregister_device, rtc);
0437 }
0438 EXPORT_SYMBOL_GPL(__devm_rtc_register_device);
0439 
0440 /**
0441  * devm_rtc_device_register - resource managed rtc_device_register()
0442  * @dev: the device to register
0443  * @name: the name of the device (unused)
0444  * @ops: the rtc operations structure
0445  * @owner: the module owner
0446  *
0447  * @return a struct rtc on success, or an ERR_PTR on error
0448  *
0449  * Managed rtc_device_register(). The rtc_device returned from this function
0450  * are automatically freed on driver detach.
0451  * This function is deprecated, use devm_rtc_allocate_device and
0452  * rtc_register_device instead
0453  */
0454 struct rtc_device *devm_rtc_device_register(struct device *dev,
0455                         const char *name,
0456                         const struct rtc_class_ops *ops,
0457                         struct module *owner)
0458 {
0459     struct rtc_device *rtc;
0460     int err;
0461 
0462     rtc = devm_rtc_allocate_device(dev);
0463     if (IS_ERR(rtc))
0464         return rtc;
0465 
0466     rtc->ops = ops;
0467 
0468     err = __devm_rtc_register_device(owner, rtc);
0469     if (err)
0470         return ERR_PTR(err);
0471 
0472     return rtc;
0473 }
0474 EXPORT_SYMBOL_GPL(devm_rtc_device_register);
0475 
0476 static int __init rtc_init(void)
0477 {
0478     rtc_class = class_create(THIS_MODULE, "rtc");
0479     if (IS_ERR(rtc_class)) {
0480         pr_err("couldn't create class\n");
0481         return PTR_ERR(rtc_class);
0482     }
0483     rtc_class->pm = RTC_CLASS_DEV_PM_OPS;
0484     rtc_dev_init();
0485     return 0;
0486 }
0487 subsys_initcall(rtc_init);