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