Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 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 |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 8 | * @target_nsec: pointer for desired now->tv_nsec value |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 9 | * |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 10 | * Replacement for the NTP platform function update_persistent_clock64 |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 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 | * |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 17 | * -EPROTO is returned if now.tv_nsec is not close enough to *target_nsec. |
Mathieu Malaterre | c7d50d2 | 2018-02-01 17:47:14 +0100 | [diff] [blame] | 18 | * |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 19 | * If temporary failure is indicated the caller should try again 'soon' |
| 20 | */ |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 21 | int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec) |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 22 | { |
| 23 | struct rtc_device *rtc; |
| 24 | struct rtc_time tm; |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 25 | struct timespec64 to_set; |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 26 | int err = -ENODEV; |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 27 | bool ok; |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 28 | |
Xunlei Pang | 9c5150b | 2015-06-12 11:10:16 +0800 | [diff] [blame] | 29 | rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE); |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 30 | if (!rtc) |
| 31 | goto out_err; |
| 32 | |
Alexandre Belloni | a01ab06 | 2019-04-30 16:16:51 +0200 | [diff] [blame] | 33 | if (!rtc->ops || !rtc->ops->set_time) |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 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; |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 53 | rtc_time64_to_tm(to_set.tv_sec, &tm); |
| 54 | |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 55 | err = rtc_set_time(rtc, &tm); |
| 56 | |
| 57 | out_close: |
| 58 | rtc_class_close(rtc); |
| 59 | out_err: |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 60 | return err; |
| 61 | } |