Blame view

drivers/rtc/systohc.c 1.62 KB
cdf7545ae   Alexandre Belloni   rtc: convert core...
1
  // SPDX-License-Identifier: GPL-2.0
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
2
3
4
5
6
7
  #include <linux/rtc.h>
  #include <linux/time.h>
  
  /**
   * rtc_set_ntp_time - Save NTP synchronized time to the RTC
   * @now: Current time of day
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
8
   * @target_nsec: pointer for desired now->tv_nsec value
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
9
   *
3c00a1fe8   Xunlei Pang   time: Add y2038 s...
10
   * Replacement for the NTP platform function update_persistent_clock64
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
11
12
13
14
15
16
   * 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.
   *
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
17
   * -EPROTO is returned if now.tv_nsec is not close enough to *target_nsec.
c7d50d2b7   Mathieu Malaterre   rtc: remove a war...
18
   *
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
19
20
   * If temporary failure is indicated the caller should try again 'soon'
   */
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
21
  int rtc_set_ntp_time(struct timespec64 now, unsigned long *target_nsec)
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
22
23
24
  {
  	struct rtc_device *rtc;
  	struct rtc_time tm;
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
25
  	struct timespec64 to_set;
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
26
  	int err = -ENODEV;
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
27
  	bool ok;
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
28

9c5150b31   Xunlei Pang   rtc: NTP: Add CON...
29
  	rtc = rtc_class_open(CONFIG_RTC_SYSTOHC_DEVICE);
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
30
31
  	if (!rtc)
  		goto out_err;
a01ab0669   Alexandre Belloni   rtc: drop set_mms...
32
  	if (!rtc->ops || !rtc->ops->set_time)
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  		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;
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
50
  	}
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
51
  	rtc_time64_to_tm(to_set.tv_sec, &tm);
0f295b065   Jason Gunthorpe   rtc: Allow rtc dr...
52
53
54
55
56
  	err = rtc_set_time(rtc, &tm);
  
  out_close:
  	rtc_class_close(rtc);
  out_err:
023f333a9   Jason Gunthorpe   NTP: Add a CONFIG...
57
58
  	return err;
  }