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 |
| 13 | * |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 14 | * Replacement for the NTP platform function update_persistent_clock64 |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 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 | */ |
Xunlei Pang | 9a4a445 | 2015-01-22 02:31:55 +0000 | [diff] [blame] | 23 | int rtc_set_ntp_time(struct timespec64 now) |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 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)) |
Xunlei Pang | 9a4a445 | 2015-01-22 02:31:55 +0000 | [diff] [blame] | 30 | rtc_time64_to_tm(now.tv_sec, &tm); |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 31 | else |
Xunlei Pang | 9a4a445 | 2015-01-22 02:31:55 +0000 | [diff] [blame] | 32 | rtc_time64_to_tm(now.tv_sec + 1, &tm); |
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 | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 35 | if (rtc) { |
| 36 | /* rtc_hctosys exclusively uses UTC, so we call set_time here, |
| 37 | * not set_mmss. */ |
Xunlei Pang | 8e4ff1a | 2015-04-01 20:34:27 -0700 | [diff] [blame] | 38 | if (rtc->ops && |
| 39 | (rtc->ops->set_time || |
| 40 | rtc->ops->set_mmss64 || |
| 41 | rtc->ops->set_mmss)) |
Jason Gunthorpe | 023f333 | 2012-12-17 14:30:53 -0700 | [diff] [blame] | 42 | err = rtc_set_time(rtc, &tm); |
| 43 | rtc_class_close(rtc); |
| 44 | } |
| 45 | |
| 46 | return err; |
| 47 | } |