Monday, June 3, 2013

RTC when to update the system time

RTC - real time clock maintains the system hardware time when linux boot when needed RTC hardware clock setting system time.
This process is in drivers / rtc / hctosys.c drive to achieve, and the driver is actually only an init function, and put its own init function is declared as
late_initcall, so you can ensure the normal operation of RTC driver already.
init function from the RTC device reads the current hardware clock, then call do_settimeofday rewrite the system clock.

27 static int __ init rtc_hctosys (void)
 28 {
 29 int ERR =-ENODEV;
 30 rtc_time struct 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
 68 err_invalid:
 69 err_read:
 70 rtc_class_close (rtc );
 71
 72 err_open:
 73 rtc_hctosys_ret = ERR;
 74
 75 return ERR;
 76}
 77
 78 late_initcall (rtc_hctosys);

34 Get RTC device
Read the time from the RTC device 42, rtc_read_time just read rtc time subsystem interface functions, the real read operation is in particular implemented RTC hardware drivers
50 The return value is a tm structure, rtc_valid_tm verify the legality of the return value
57 is converted to UTC seconds format
59 Setting the System Time
78 to ensure that this is the last call initialization function, at least in the RTC driver called after initialization

No comments:

Post a Comment