Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 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 |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 13 | * @target_nsec: pointer for desired now->tv_nsec value |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 14 | * |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 15 | * Replacement for the NTP platform function update_persistent_clock64 |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 16 | * that stores time for later retrieval by rtc_hctosys. |
| 17 | * |
| 18 | * Returns 0 on successful RTC update, -ENODEV if a RTC update is not |
| 19 | * possible at all, and various other -errno for specific temporary failure |
| 20 | * cases. |
| 21 | * |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 22 | * -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^] | 23 | * |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 24 | * If temporary failure is indicated the caller should try again 'soon' |
| 25 | */ |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 26 | int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec) |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 27 | { |
| 28 | struct rtc_device *rtc; |
| 29 | struct rtc_time tm; |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 30 | struct timespec64 to_set; |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 31 | int err = -ENODEV; |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 32 | bool ok; |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 33 | |
Xunlei Pang | 9c5150b | 2015-06-12 11:10:16 +0800 | [diff] [blame] | 34 | rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE); |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 35 | if (!rtc) |
| 36 | goto out_err; |
| 37 | |
| 38 | if (!rtc->ops || (!rtc->ops->set_time && !rtc->ops->set_mmss64 && |
| 39 | !rtc->ops->set_mmss)) |
| 40 | goto out_close; |
| 41 | |
| 42 | /* Compute the value of tv_nsec we require the caller to supply in |
| 43 | * now.tv_nsec. This is the value such that (now + |
| 44 | * set_offset_nsec).tv_nsec == 0. |
| 45 | */ |
| 46 | set_normalized_timespec64(&to_set, 0, -rtc->set_offset_nsec); |
| 47 | *target_nsec = to_set.tv_nsec; |
| 48 | |
| 49 | /* The ntp code must call this with the correct value in tv_nsec, if |
| 50 | * it does not we update target_nsec and return EPROTO to make the ntp |
| 51 | * code try again later. |
| 52 | */ |
| 53 | ok = rtc_tv_nsec_ok(rtc->set_offset_nsec, &to_set, &now); |
| 54 | if (!ok) { |
| 55 | err = -EPROTO; |
| 56 | goto out_close; |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jason Gunthorpe | 0f295b0 | 2017-10-13 11:54:33 -0600 | [diff] [blame] | 59 | rtc_time64_to_tm(to_set.tv_sec, &tm); |
| 60 | |
| 61 | /* rtc_hctosys exclusively uses UTC, so we call set_time here, not |
| 62 | * set_mmss. |
| 63 | */ |
| 64 | err = rtc_set_time(rtc, &tm); |
| 65 | |
| 66 | out_close: |
| 67 | rtc_class_close(rtc); |
| 68 | out_err: |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 69 | return err; |
| 70 | } |