#ifdef CONFIG_RTC_HCTOSYS_DEVICE /* Result of the last RTC to system clock attempt. */ int rtc_hctosys_ret = -ENODEV;
/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary * whether it stores the most close value or the value with partial * seconds truncated. However, it is important that we use it to store * the truncated value. This is because otherwise it is necessary, * in an rtc sync function, to read both xtime.tv_sec and * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read * of >32bits is not possible. So storing the most close value would * slow down the sync API. So here we have the truncated value and * the best guess is to add 0.5s.
*/
dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
&tm, (longlong)tv64.tv_sec);
err_read:
rtc_hctosys_ret = err;
} #endif
#ifdefined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) /* * On suspend(), measure the delta between one RTC and the * system's wall clock; restore it on resume().
*/
if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) return 0;
/* snapshot the current RTC and system time at suspend*/
err = rtc_read_time(rtc, &tm); if (err < 0) {
pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev)); return 0;
}
/* * To avoid drift caused by repeated suspend/resumes, * which each can add ~1 second drift error, * try to compensate so the difference in system time * and rtc time stays close to constant.
*/
delta = timespec64_sub(old_system, old_rtc);
delta_delta = timespec64_sub(delta, old_delta); if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) { /* * if delta_delta is too large, assume time correction * has occurred and set old_delta to the current delta.
*/
old_delta = delta;
} else { /* Otherwise try to adjust old_system to compensate */
old_system = timespec64_sub(old_system, delta_delta);
}
rtc_hctosys_ret = -ENODEV; if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) return 0;
/* snapshot the current rtc and system time at resume */
ktime_get_real_ts64(&new_system);
err = rtc_read_time(rtc, &tm); if (err < 0) {
pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev)); return 0;
}
if (new_rtc.tv_sec < old_rtc.tv_sec) {
pr_debug("%s: time travel!\n", dev_name(&rtc->dev)); return 0;
}
/* calculate the RTC time delta (sleep time)*/
sleep_time = timespec64_sub(new_rtc, old_rtc);
/* * Since these RTC suspend/resume handlers are not called * at the very end of suspend or the start of resume, * some run-time may pass on either sides of the sleep time * so subtract kernel run-time between rtc_suspend to rtc_resume * to keep things accurate.
*/
sleep_time = timespec64_sub(sleep_time,
timespec64_sub(new_system, old_system));
/* Ensure the caller will set the id before releasing the device */ staticstruct rtc_device *rtc_allocate_device(void)
{ struct rtc_device *rtc;
rtc = kzalloc(sizeof(*rtc), GFP_KERNEL); if (!rtc) return NULL;
device_initialize(&rtc->dev);
/* * Drivers can revise this default after allocating the device. * The default is what most RTCs do: Increment seconds exactly one * second after the write happened. This adds a default transport * time of 5ms which is at least halfways close to reality.
*/
rtc->set_offset_nsec = NSEC_PER_SEC + 5 * NSEC_PER_MSEC;
/* * If RTC driver did not implement the range of RTC hardware device, * then we can not expand the RTC range by adding or subtracting one * offset.
*/ if (rtc->range_min == rtc->range_max) return;
ret = device_property_read_u32(rtc->dev.parent, "start-year",
&start_year); if (!ret) {
rtc->start_secs = mktime64(start_year, 1, 1, 0, 0, 0);
rtc->set_start_time = true;
}
/* * If user did not implement the start time for RTC driver, then no * need to expand the RTC range.
*/ if (!rtc->set_start_time) return;
range_secs = rtc->range_max - rtc->range_min + 1;
/* * If the start_secs is larger than the maximum seconds (rtc->range_max) * supported by RTC hardware or the maximum seconds of new expanded * range (start_secs + rtc->range_max - rtc->range_min) is less than * rtc->range_min, which means the minimum seconds (rtc->range_min) of * RTC hardware will be mapped to start_secs by adding one offset, so * the offset seconds calculation formula should be: * rtc->offset_secs = rtc->start_secs - rtc->range_min; * * If the start_secs is larger than the minimum seconds (rtc->range_min) * supported by RTC hardware, then there is one region is overlapped * between the original RTC hardware range and the new expanded range, * and this overlapped region do not need to be mapped into the new * expanded range due to it is valid for RTC device. So the minimum * seconds of RTC hardware (rtc->range_min) should be mapped to * rtc->range_max + 1, then the offset seconds formula should be: * rtc->offset_secs = rtc->range_max - rtc->range_min + 1; * * If the start_secs is less than the minimum seconds (rtc->range_min), * which is similar to case 2. So the start_secs should be mapped to * start_secs + rtc->range_max - rtc->range_min + 1, then the * offset seconds formula should be: * rtc->offset_secs = -(rtc->range_max - rtc->range_min + 1); * * Otherwise the offset seconds should be 0.
*/ if ((rtc->start_secs >= 0 && rtc->start_secs > rtc->range_max) ||
rtc->start_secs + range_secs - 1 < rtc->range_min)
rtc->offset_secs = rtc->start_secs - rtc->range_min; elseif (rtc->start_secs > rtc->range_min)
rtc->offset_secs = range_secs; elseif (rtc->start_secs < rtc->range_min)
rtc->offset_secs = -range_secs; else
rtc->offset_secs = 0;
}
mutex_lock(&rtc->ops_lock); /* * Remove innards of this RTC, then disable it, before * letting any rtc_class_open() users access it again
*/
rtc_proc_del_device(rtc); if (!test_bit(RTC_NO_CDEV, &rtc->flags))
cdev_device_del(&rtc->char_dev, &rtc->dev);
rtc->ops = NULL;
mutex_unlock(&rtc->ops_lock);
}
int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
{ struct rtc_wkalrm alrm; int err;
if (!rtc->ops) {
dev_dbg(&rtc->dev, "no ops set\n"); return -EINVAL;
}
if (!rtc->ops->set_alarm)
clear_bit(RTC_FEATURE_ALARM, rtc->features);
if (rtc->ops->set_offset)
set_bit(RTC_FEATURE_CORRECTION, rtc->features);
rtc->owner = owner;
rtc_device_get_offset(rtc);
/* Check to see if there is an ALARM already set in hw */
err = __rtc_read_alarm(rtc, &alrm); if (!err && !rtc_valid_tm(&alrm.time))
rtc_initialize_alarm(rtc, &alrm);
/** * devm_rtc_device_register - resource managed rtc_device_register() * @dev: the device to register * @name: the name of the device (unused) * @ops: the rtc operations structure * @owner: the module owner * * @return a struct rtc on success, or an ERR_PTR on error * * Managed rtc_device_register(). The rtc_device returned from this function * are automatically freed on driver detach. * This function is deprecated, use devm_rtc_allocate_device and * rtc_register_device instead
*/ struct rtc_device *devm_rtc_device_register(struct device *dev, constchar *name, conststruct rtc_class_ops *ops, struct module *owner)
{ struct rtc_device *rtc; int err;
rtc = devm_rtc_allocate_device(dev); if (IS_ERR(rtc)) return rtc;
rtc->ops = ops;
err = __devm_rtc_register_device(owner, rtc); if (err) return ERR_PTR(err);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.