Linux Audio

Check our new training course

Loading...
v4.6
 1/*
 2 * This program is free software; you can redistribute it and/or modify it
 3 * under the terms of the GNU General Public License version 2 as published by
 4 * the Free Software Foundation.
 5 *
 6 */
 7#include <linux/rtc.h>
 8#include <linux/time.h>
 9
10/**
11 * rtc_set_ntp_time - Save NTP synchronized time to the RTC
12 * @now: Current time of day
 
13 *
14 * Replacement for the NTP platform function update_persistent_clock64
15 * that stores time for later retrieval by rtc_hctosys.
16 *
17 * Returns 0 on successful RTC update, -ENODEV if a RTC update is not
18 * possible at all, and various other -errno for specific temporary failure
19 * cases.
20 *
 
 
21 * If temporary failure is indicated the caller should try again 'soon'
22 */
23int rtc_set_ntp_time(struct timespec64 now)
24{
25	struct rtc_device *rtc;
26	struct rtc_time tm;
 
27	int err = -ENODEV;
28
29	if (now.tv_nsec < (NSEC_PER_SEC >> 1))
30		rtc_time64_to_tm(now.tv_sec, &tm);
31	else
32		rtc_time64_to_tm(now.tv_sec + 1, &tm);
33
34	rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE);
35	if (rtc) {
36		/* rtc_hctosys exclusively uses UTC, so we call set_time here,
37		 * not set_mmss. */
38		if (rtc->ops &&
39		    (rtc->ops->set_time ||
40		     rtc->ops->set_mmss64 ||
41		     rtc->ops->set_mmss))
42			err = rtc_set_time(rtc, &tm);
43		rtc_class_close(rtc);
 
 
 
 
 
 
 
 
 
 
 
 
44	}
45
 
 
 
 
 
 
 
46	return err;
47}
v5.9
 1// SPDX-License-Identifier: GPL-2.0
 
 
 
 
 
 2#include <linux/rtc.h>
 3#include <linux/time.h>
 4
 5/**
 6 * rtc_set_ntp_time - Save NTP synchronized time to the RTC
 7 * @now: Current time of day
 8 * @target_nsec: pointer for desired now->tv_nsec value
 9 *
10 * Replacement for the NTP platform function update_persistent_clock64
11 * that stores time for later retrieval by rtc_hctosys.
12 *
13 * Returns 0 on successful RTC update, -ENODEV if a RTC update is not
14 * possible at all, and various other -errno for specific temporary failure
15 * cases.
16 *
17 * -EPROTO is returned if now.tv_nsec is not close enough to *target_nsec.
18 *
19 * If temporary failure is indicated the caller should try again 'soon'
20 */
21int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec)
22{
23	struct rtc_device *rtc;
24	struct rtc_time tm;
25	struct timespec64 to_set;
26	int err = -ENODEV;
27	bool ok;
 
 
 
 
28
29	rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE);
30	if (!rtc)
31		goto out_err;
32
33	if (!rtc->ops || !rtc->ops->set_time)
34		goto out_close;
35
36	/* Compute the value of tv_nsec we require the caller to supply in
37	 * now.tv_nsec.  This is the value such that (now +
38	 * set_offset_nsec).tv_nsec == 0.
39	 */
40	set_normalized_timespec64(&to_set, 0, -rtc->set_offset_nsec);
41	*target_nsec = to_set.tv_nsec;
42
43	/* The ntp code must call this with the correct value in tv_nsec, if
44	 * it does not we update target_nsec and return EPROTO to make the ntp
45	 * code try again later.
46	 */
47	ok = rtc_tv_nsec_ok(rtc->set_offset_nsec, &to_set, &now);
48	if (!ok) {
49		err = -EPROTO;
50		goto out_close;
51	}
52
53	rtc_time64_to_tm(to_set.tv_sec, &tm);
54
55	err = rtc_set_time(rtc, &tm);
56
57out_close:
58	rtc_class_close(rtc);
59out_err:
60	return err;
61}