1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/rtc.h>
15
16
17
18
19
20
21
22
23
24
25
26
27static int __init rtc_hctosys(void)
28{
29 int err = -ENODEV;
30 struct rtc_time tm;
31 struct timespec64 tv64 = {
32 .tv_nsec = NSEC_PER_SEC >> 1,
33 };
34 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
35
36 if (rtc == NULL) {
37 pr_info("unable to open rtc device (%s)\n",
38 CONFIG_RTC_HCTOSYS_DEVICE);
39 goto err_open;
40 }
41
42 err = rtc_read_time(rtc, &tm);
43 if (err) {
44 dev_err(rtc->dev.parent,
45 "hctosys: unable to read the hardware clock\n");
46 goto err_read;
47
48 }
49
50 tv64.tv_sec = rtc_tm_to_time64(&tm);
51
52 err = do_settimeofday64(&tv64);
53
54 dev_info(rtc->dev.parent,
55 "setting system clock to "
56 "%d-%02d-%02d %02d:%02d:%02d UTC (%lld)\n",
57 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
58 tm.tm_hour, tm.tm_min, tm.tm_sec,
59 (long long) tv64.tv_sec);
60
61err_read:
62 rtc_class_close(rtc);
63
64err_open:
65 rtc_hctosys_ret = err;
66
67 return err;
68}
69
70late_initcall(rtc_hctosys);
71