2012-12-18 01:30:53 +04:00
/*
* This program is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation .
*
*/
# include <linux/rtc.h>
# include <linux/time.h>
/**
* rtc_set_ntp_time - Save NTP synchronized time to the RTC
* @ now : Current time of day
2017-10-13 20:54:33 +03:00
* @ target_nsec : pointer for desired now - > tv_nsec value
2012-12-18 01:30:53 +04:00
*
2015-04-02 06:34:23 +03:00
* Replacement for the NTP platform function update_persistent_clock64
2012-12-18 01:30:53 +04:00
* that stores time for later retrieval by rtc_hctosys .
*
* Returns 0 on successful RTC update , - ENODEV if a RTC update is not
* possible at all , and various other - errno for specific temporary failure
* cases .
*
2017-10-13 20:54:33 +03:00
* - EPROTO is returned if now . tv_nsec is not close enough to * target_nsec .
2018-02-01 19:47:14 +03:00
*
2012-12-18 01:30:53 +04:00
* If temporary failure is indicated the caller should try again ' soon '
*/
2017-10-13 20:54:33 +03:00
int rtc_set_ntp_time ( struct timespec64 now , unsigned long * target_nsec )
2012-12-18 01:30:53 +04:00
{
struct rtc_device * rtc ;
struct rtc_time tm ;
2017-10-13 20:54:33 +03:00
struct timespec64 to_set ;
2012-12-18 01:30:53 +04:00
int err = - ENODEV ;
2017-10-13 20:54:33 +03:00
bool ok ;
2012-12-18 01:30:53 +04:00
2015-06-12 06:10:16 +03:00
rtc = rtc_class_open ( CONFIG_RTC_SYSTOHC_DEVICE ) ;
2017-10-13 20:54:33 +03:00
if ( ! rtc )
goto out_err ;
if ( ! rtc - > ops | | ( ! rtc - > ops - > set_time & & ! rtc - > ops - > set_mmss64 & &
! rtc - > ops - > set_mmss ) )
goto out_close ;
/* Compute the value of tv_nsec we require the caller to supply in
* now . tv_nsec . This is the value such that ( now +
* set_offset_nsec ) . tv_nsec = = 0.
*/
set_normalized_timespec64 ( & to_set , 0 , - rtc - > set_offset_nsec ) ;
* target_nsec = to_set . tv_nsec ;
/* The ntp code must call this with the correct value in tv_nsec, if
* it does not we update target_nsec and return EPROTO to make the ntp
* code try again later .
*/
ok = rtc_tv_nsec_ok ( rtc - > set_offset_nsec , & to_set , & now ) ;
if ( ! ok ) {
err = - EPROTO ;
goto out_close ;
2012-12-18 01:30:53 +04:00
}
2017-10-13 20:54:33 +03:00
rtc_time64_to_tm ( to_set . tv_sec , & tm ) ;
/* rtc_hctosys exclusively uses UTC, so we call set_time here, not
* set_mmss .
*/
err = rtc_set_time ( rtc , & tm ) ;
out_close :
rtc_class_close ( rtc ) ;
out_err :
2012-12-18 01:30:53 +04:00
return err ;
}