Blame view

drivers/rtc/hctosys.c 1.93 KB
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   * RTC subsystem, initialize system time on startup
   *
   * Copyright (C) 2005 Tower Technologies
   * Author: Alessandro Zummo <a.zummo@towertech.it>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
  */
  
  #include <linux/rtc.h>
  
  /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
   * whether it stores the most close value or the value with partial
   * seconds truncated. However, it is important that we use it to store
   * the truncated value. This is because otherwise it is necessary,
   * in an rtc sync function, to read both xtime.tv_sec and
   * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
   * of >32bits is not possible. So storing the most close value would
   * slow down the sync API. So here we have the truncated value and
   * the best guess is to add 0.5s.
   */
d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
24
  int rtc_hctosys_ret = -ENODEV;
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
25
26
  static int __init rtc_hctosys(void)
  {
d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
27
  	int err = -ENODEV;
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
28
  	struct rtc_time tm;
d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
29
30
31
  	struct timespec tv = {
  		.tv_nsec = NSEC_PER_SEC >> 1,
  	};
ab6a2d70d   David Brownell   rtc: rtc interfac...
32
  	struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
33

ab6a2d70d   David Brownell   rtc: rtc interfac...
34
  	if (rtc == NULL) {
d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
35
36
  		pr_err("%s: unable to open rtc device (%s)
  ",
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
37
  			__FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
38
  		goto err_open;
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
39
  	}
ab6a2d70d   David Brownell   rtc: rtc interfac...
40
  	err = rtc_read_time(rtc, &tm);
d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
41
42
43
44
45
  	if (err) {
  		dev_err(rtc->dev.parent,
  			"hctosys: unable to read the hardware clock
  ");
  		goto err_read;
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
46

d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
47
  	}
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
48

d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
49
50
51
52
53
54
55
  	err = rtc_valid_tm(&tm);
  	if (err) {
  		dev_err(rtc->dev.parent,
  			"hctosys: invalid date/time
  ");
  		goto err_invalid;
  	}
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
56

d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
57
  	rtc_tm_to_time(&tm, &tv.tv_sec);
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
58

d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
59
  	do_settimeofday(&tv);
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
60

d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
61
62
63
64
65
66
67
68
69
70
  	dev_info(rtc->dev.parent,
  		"setting system clock to "
  		"%d-%02d-%02d %02d:%02d:%02d UTC (%u)
  ",
  		tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  		tm.tm_hour, tm.tm_min, tm.tm_sec,
  		(unsigned int) tv.tv_sec);
  
  err_invalid:
  err_read:
ab6a2d70d   David Brownell   rtc: rtc interfac...
71
  	rtc_class_close(rtc);
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
72

d0ab4a4d5   Uwe Kleine-König   rtc/hctosys: only...
73
74
75
76
  err_open:
  	rtc_hctosys_ret = err;
  
  	return err;
0c86edc0d   Alessandro Zummo   [PATCH] RTC subsy...
77
78
79
  }
  
  late_initcall(rtc_hctosys);