systohc.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. * rtc_set_ntp_time - Save NTP synchronized time to the RTC
  11. * @now: Current time of day
  12. *
  13. * Replacement for the NTP platform function update_persistent_clock64
  14. * that stores time for later retrieval by rtc_hctosys.
  15. *
  16. * Returns 0 on successful RTC update, -ENODEV if a RTC update is not
  17. * possible at all, and various other -errno for specific temporary failure
  18. * cases.
  19. *
  20. * If temporary failure is indicated the caller should try again 'soon'
  21. */
  22. int rtc_set_ntp_time(struct timespec64 now)
  23. {
  24. struct rtc_device *rtc;
  25. struct rtc_time tm;
  26. int err = -ENODEV;
  27. if (now.tv_nsec < (NSEC_PER_SEC >> 1))
  28. rtc_time64_to_tm(now.tv_sec, &tm);
  29. else
  30. rtc_time64_to_tm(now.tv_sec + 1, &tm);
  31. rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE);
  32. if (rtc) {
  33. /* rtc_hctosys exclusively uses UTC, so we call set_time here,
  34. * not set_mmss. */
  35. if (rtc->ops &&
  36. (rtc->ops->set_time ||
  37. rtc->ops->set_mmss64 ||
  38. rtc->ops->set_mmss))
  39. err = rtc_set_time(rtc, &tm);
  40. rtc_class_close(rtc);
  41. }
  42. return err;
  43. }