Linux Audio

Check our new training course

Loading...
v3.5.6
 
 1/*
 2 * RTC subsystem, initialize system time on startup
 3 *
 4 * Copyright (C) 2005 Tower Technologies
 5 * Author: Alessandro Zummo <a.zummo@towertech.it>
 6 *
 7 * This program is free software; you can redistribute it and/or modify
 8 * it under the terms of the GNU General Public License version 2 as
 9 * published by the Free Software Foundation.
10*/
11
12#include <linux/rtc.h>
13
14/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
15 * whether it stores the most close value or the value with partial
16 * seconds truncated. However, it is important that we use it to store
17 * the truncated value. This is because otherwise it is necessary,
18 * in an rtc sync function, to read both xtime.tv_sec and
19 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
20 * of >32bits is not possible. So storing the most close value would
21 * slow down the sync API. So here we have the truncated value and
22 * the best guess is to add 0.5s.
23 */
24
25int rtc_hctosys_ret = -ENODEV;
26
27static int __init rtc_hctosys(void)
28{
29	int err = -ENODEV;
30	struct rtc_time tm;
31	struct timespec tv = {
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_err("%s: unable to open rtc device (%s)\n",
38			__FILE__, 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	err = rtc_valid_tm(&tm);
51	if (err) {
52		dev_err(rtc->dev.parent,
53			"hctosys: invalid date/time\n");
54		goto err_invalid;
55	}
56
57	rtc_tm_to_time(&tm, &tv.tv_sec);
 
 
 
 
 
58
59	do_settimeofday(&tv);
60
61	dev_info(rtc->dev.parent,
62		"setting system clock to "
63		"%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",
64		tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
65		tm.tm_hour, tm.tm_min, tm.tm_sec,
66		(unsigned int) tv.tv_sec);
67
68err_invalid:
69err_read:
70	rtc_class_close(rtc);
71
72err_open:
73	rtc_hctosys_ret = err;
74
75	return err;
76}
77
78late_initcall(rtc_hctosys);
v5.4
 1// SPDX-License-Identifier: GPL-2.0
 2/*
 3 * RTC subsystem, initialize system time on startup
 4 *
 5 * Copyright (C) 2005 Tower Technologies
 6 * Author: Alessandro Zummo <a.zummo@towertech.it>
 7 */
 8
 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 
10
11#include <linux/rtc.h>
12
13/* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
14 * whether it stores the most close value or the value with partial
15 * seconds truncated. However, it is important that we use it to store
16 * the truncated value. This is because otherwise it is necessary,
17 * in an rtc sync function, to read both xtime.tv_sec and
18 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
19 * of >32bits is not possible. So storing the most close value would
20 * slow down the sync API. So here we have the truncated value and
21 * the best guess is to add 0.5s.
22 */
23
 
 
24static int __init rtc_hctosys(void)
25{
26	int err = -ENODEV;
27	struct rtc_time tm;
28	struct timespec64 tv64 = {
29		.tv_nsec = NSEC_PER_SEC >> 1,
30	};
31	struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
32
33	if (!rtc) {
34		pr_info("unable to open rtc device (%s)\n",
35			CONFIG_RTC_HCTOSYS_DEVICE);
36		goto err_open;
37	}
38
39	err = rtc_read_time(rtc, &tm);
40	if (err) {
41		dev_err(rtc->dev.parent,
42			"hctosys: unable to read the hardware clock\n");
43		goto err_read;
 
44	}
45
46	tv64.tv_sec = rtc_tm_to_time64(&tm);
 
 
 
 
 
47
48#if BITS_PER_LONG == 32
49	if (tv64.tv_sec > INT_MAX) {
50		err = -ERANGE;
51		goto err_read;
52	}
53#endif
54
55	err = do_settimeofday64(&tv64);
56
57	dev_info(rtc->dev.parent, "setting system clock to %ptR UTC (%lld)\n",
58		 &tm, (long long)tv64.tv_sec);
 
 
 
 
59
 
60err_read:
61	rtc_class_close(rtc);
62
63err_open:
64	rtc_hctosys_ret = err;
65
66	return err;
67}
68
69late_initcall(rtc_hctosys);