Blame man4/rtc.4

Packit 7cfc04
.\" rtc.4
Packit 7cfc04
.\" Copyright 2002 Urs Thuermann (urs@isnogud.escape.de)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\"
Packit 7cfc04
.\" The GNU General Public License's references to "object code"
Packit 7cfc04
.\" and "executables" are to be interpreted as the output of any
Packit 7cfc04
.\" document formatting or typesetting system, including
Packit 7cfc04
.\" intermediate and printed output.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This manual is distributed in the hope that it will be useful,
Packit 7cfc04
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7cfc04
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7cfc04
.\" GNU General Public License for more details.
Packit 7cfc04
.\"
Packit 7cfc04
.\" You should have received a copy of the GNU General Public
Packit 7cfc04
.\" License along with this manual; if not, see
Packit 7cfc04
.\" <http://www.gnu.org/licenses/>.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" $Id: rtc.4,v 1.4 2005/12/05 17:19:49 urs Exp $
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2006-02-08 Various additions by mtk
Packit 7cfc04
.\" 2006-11-26 cleanup, cover the generic rtc framework; David Brownell
Packit 7cfc04
.\"
Packit 7cfc04
.TH RTC 4 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
rtc \- real-time clock
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
#include <linux/rtc.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int ioctl(" fd ", RTC_" request ", " param ");"
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
This is the interface to drivers for real-time clocks (RTCs).
Packit 7cfc04
.PP
Packit 7cfc04
Most computers have one or more hardware clocks which record the
Packit 7cfc04
current "wall clock" time.
Packit 7cfc04
These are called "Real Time Clocks" (RTCs).
Packit 7cfc04
One of these usually has battery backup power so that it tracks the time
Packit 7cfc04
even while the computer is turned off.
Packit 7cfc04
RTCs often provide alarms and other interrupts.
Packit 7cfc04
.PP
Packit 7cfc04
All i386 PCs, and ACPI-based systems, have an RTC that is compatible with
Packit 7cfc04
the Motorola MC146818 chip on the original PC/AT.
Packit 7cfc04
Today such an RTC is usually integrated into the mainboard's chipset
Packit 7cfc04
(south bridge), and uses a replaceable coin-sized backup battery.
Packit 7cfc04
.PP
Packit 7cfc04
Non-PC systems, such as embedded systems built around system-on-chip
Packit 7cfc04
processors, use other implementations.
Packit 7cfc04
They usually won't offer the same functionality as the RTC from a PC/AT.
Packit 7cfc04
.SS RTC vs system clock
Packit 7cfc04
RTCs should not be confused with the system clock, which is
Packit 7cfc04
a software clock maintained by the kernel and used to implement
Packit 7cfc04
.BR gettimeofday (2)
Packit 7cfc04
and
Packit 7cfc04
.BR time (2),
Packit 7cfc04
as well as setting timestamps on files, and so on.
Packit 7cfc04
The system clock reports seconds and microseconds since a start point,
Packit 7cfc04
defined to be the POSIX Epoch: 1970-01-01 00:00:00 +0000 (UTC).
Packit 7cfc04
(One common implementation counts timer interrupts, once
Packit 7cfc04
per "jiffy", at a frequency of 100, 250, or 1000 Hz.)
Packit 7cfc04
That is, it is supposed to report wall clock time, which RTCs also do.
Packit 7cfc04
.PP
Packit 7cfc04
A key difference between an RTC and the system clock is that RTCs
Packit 7cfc04
run even when the system is in a low power state (including "off"),
Packit 7cfc04
and the system clock can't.
Packit 7cfc04
Until it is initialized, the system clock can only report time since
Packit 7cfc04
system boot ... not since the POSIX Epoch.
Packit 7cfc04
So at boot time, and after resuming from a system low power state, the
Packit 7cfc04
system clock will often be set to the current wall clock time using an RTC.
Packit 7cfc04
Systems without an RTC need to set the system clock using another clock,
Packit 7cfc04
maybe across the network or by entering that data manually.
Packit 7cfc04
.SS RTC functionality
Packit 7cfc04
RTCs can be read and written with
Packit 7cfc04
.BR hwclock (8),
Packit 7cfc04
or directly with the ioctl requests listed below.
Packit 7cfc04
.PP
Packit 7cfc04
Besides tracking the date and time, many RTCs can also generate
Packit 7cfc04
interrupts
Packit 7cfc04
.IP * 3
Packit 7cfc04
on every clock update (i.e., once per second);
Packit 7cfc04
.IP *
Packit 7cfc04
at periodic intervals with a frequency that can be set to
Packit 7cfc04
any power-of-2 multiple in the range 2 Hz to 8192 Hz;
Packit 7cfc04
.IP *
Packit 7cfc04
on reaching a previously specified alarm time.
Packit 7cfc04
.PP
Packit 7cfc04
Each of those interrupt sources can be enabled or disabled separately.
Packit 7cfc04
On many systems, the alarm interrupt can be configured as a system wakeup
Packit 7cfc04
event, which can resume the system from a low power state such as
Packit 7cfc04
Suspend-to-RAM (STR, called S3 in ACPI systems),
Packit 7cfc04
Hibernation (called S4 in ACPI systems),
Packit 7cfc04
or even "off" (called S5 in ACPI systems).
Packit 7cfc04
On some systems, the battery backed RTC can't issue
Packit 7cfc04
interrupts, but another one can.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I /dev/rtc
Packit 7cfc04
(or
Packit 7cfc04
.IR /dev/rtc0 ,
Packit 7cfc04
.IR /dev/rtc1 ,
Packit 7cfc04
etc.)
Packit 7cfc04
device can be opened only once (until it is closed) and it is read-only.
Packit 7cfc04
On
Packit 7cfc04
.BR read (2)
Packit 7cfc04
and
Packit 7cfc04
.BR select (2)
Packit 7cfc04
the calling process is blocked until the next interrupt from that RTC
Packit 7cfc04
is received.
Packit 7cfc04
Following the interrupt, the process can read a long integer, of which
Packit 7cfc04
the least significant byte contains a bit mask encoding
Packit 7cfc04
the types of interrupt that occurred,
Packit 7cfc04
while the remaining 3 bytes contain the number of interrupts since the
Packit 7cfc04
last
Packit 7cfc04
.BR read (2).
Packit 7cfc04
.SS ioctl(2) interface
Packit 7cfc04
The following
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
requests are defined on file descriptors connected to RTC devices:
Packit 7cfc04
.TP
Packit 7cfc04
.B RTC_RD_TIME
Packit 7cfc04
Returns this RTC's time in the following structure:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct rtc_time {
Packit 7cfc04
    int tm_sec;
Packit 7cfc04
    int tm_min;
Packit 7cfc04
    int tm_hour;
Packit 7cfc04
    int tm_mday;
Packit 7cfc04
    int tm_mon;
Packit 7cfc04
    int tm_year;
Packit 7cfc04
    int tm_wday;     /* unused */
Packit 7cfc04
    int tm_yday;     /* unused */
Packit 7cfc04
    int tm_isdst;    /* unused */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The fields in this structure have the same meaning and ranges as for the
Packit 7cfc04
.I tm
Packit 7cfc04
structure described in
Packit 7cfc04
.BR gmtime (3).
Packit 7cfc04
A pointer to this structure should be passed as the third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.B RTC_SET_TIME
Packit 7cfc04
Sets this RTC's time to the time specified by the
Packit 7cfc04
.I rtc_time
Packit 7cfc04
structure pointed to by the third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
To set the
Packit 7cfc04
RTC's time the process must be privileged (i.e., have the
Packit 7cfc04
.B CAP_SYS_TIME
Packit 7cfc04
capability).
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_ALM_READ ", " RTC_ALM_SET
Packit 7cfc04
Read and set the alarm time, for RTCs that support alarms.
Packit 7cfc04
The alarm interrupt must be separately enabled or disabled using the
Packit 7cfc04
.BR RTC_AIE_ON ", " RTC_AIE_OFF
Packit 7cfc04
requests.
Packit 7cfc04
The third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument is a pointer to an
Packit 7cfc04
.I rtc_time
Packit 7cfc04
structure.
Packit 7cfc04
Only the
Packit 7cfc04
.IR tm_sec ,
Packit 7cfc04
.IR tm_min ,
Packit 7cfc04
and
Packit 7cfc04
.I tm_hour
Packit 7cfc04
fields of this structure are used.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_IRQP_READ ", " RTC_IRQP_SET
Packit 7cfc04
Read and set the frequency for periodic interrupts,
Packit 7cfc04
for RTCs that support periodic interrupts.
Packit 7cfc04
The periodic interrupt must be separately enabled or disabled using the
Packit 7cfc04
.BR RTC_PIE_ON ", " RTC_PIE_OFF
Packit 7cfc04
requests.
Packit 7cfc04
The third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument is an
Packit 7cfc04
.I "unsigned long\ *"
Packit 7cfc04
or an
Packit 7cfc04
.IR "unsigned long" ,
Packit 7cfc04
respectively.
Packit 7cfc04
The value is the frequency in interrupts per second.
Packit 7cfc04
The set of allowable frequencies is the multiples of two
Packit 7cfc04
in the range 2 to 8192.
Packit 7cfc04
Only a privileged process (i.e., one having the
Packit 7cfc04
.B CAP_SYS_RESOURCE
Packit 7cfc04
capability) can set frequencies above the value specified in
Packit 7cfc04
.IR /proc/sys/dev/rtc/max-user-freq .
Packit 7cfc04
(This file contains the value 64 by default.)
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_AIE_ON ", " RTC_AIE_OFF
Packit 7cfc04
Enable or disable the alarm interrupt, for RTCs that support alarms.
Packit 7cfc04
The third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument is ignored.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_UIE_ON ", " RTC_UIE_OFF
Packit 7cfc04
Enable or disable the interrupt on every clock update,
Packit 7cfc04
for RTCs that support this once-per-second interrupt.
Packit 7cfc04
The third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument is ignored.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_PIE_ON ", " RTC_PIE_OFF
Packit 7cfc04
Enable or disable the periodic interrupt,
Packit 7cfc04
for RTCs that support these periodic interrupts.
Packit 7cfc04
The third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument is ignored.
Packit 7cfc04
Only a privileged process (i.e., one having the
Packit 7cfc04
.B CAP_SYS_RESOURCE
Packit 7cfc04
capability) can enable the periodic interrupt if the frequency is
Packit 7cfc04
currently set above the value specified in
Packit 7cfc04
.IR /proc/sys/dev/rtc/max-user-freq .
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_EPOCH_READ ", " RTC_EPOCH_SET
Packit 7cfc04
Many RTCs encode the year in an 8-bit register which is either
Packit 7cfc04
interpreted as an 8-bit binary number or as a BCD number.
Packit 7cfc04
In both cases,
Packit 7cfc04
the number is interpreted relative to this RTC's Epoch.
Packit 7cfc04
The RTC's Epoch is
Packit 7cfc04
initialized to 1900 on most systems but on Alpha and MIPS it might
Packit 7cfc04
also be initialized to 1952, 1980, or 2000, depending on the value of
Packit 7cfc04
an RTC register for the year.
Packit 7cfc04
With some RTCs,
Packit 7cfc04
these operations can be used to read or to set the RTC's Epoch,
Packit 7cfc04
respectively.
Packit 7cfc04
The third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument is an
Packit 7cfc04
.I "unsigned long\ *"
Packit 7cfc04
or an
Packit 7cfc04
.IR "unsigned long" ,
Packit 7cfc04
respectively, and the value returned (or assigned) is the Epoch.
Packit 7cfc04
To set the RTC's Epoch the process must be privileged (i.e., have the
Packit 7cfc04
.B CAP_SYS_TIME
Packit 7cfc04
capability).
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTC_WKALM_RD ", " RTC_WKALM_SET
Packit 7cfc04
Some RTCs support a more powerful alarm interface, using these ioctls
Packit 7cfc04
to read or write the RTC's alarm time (respectively) with this structure:
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct rtc_wkalrm {
Packit 7cfc04
    unsigned char enabled;
Packit 7cfc04
    unsigned char pending;
Packit 7cfc04
    struct rtc_time time;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I enabled
Packit 7cfc04
flag is used to enable or disable the alarm interrupt,
Packit 7cfc04
or to read its current status; when using these calls,
Packit 7cfc04
.BR RTC_AIE_ON " and " RTC_AIE_OFF
Packit 7cfc04
are not used.
Packit 7cfc04
The
Packit 7cfc04
.I pending
Packit 7cfc04
flag is used by
Packit 7cfc04
.B RTC_WKALM_RD
Packit 7cfc04
to report a pending interrupt
Packit 7cfc04
(so it's mostly useless on Linux, except when talking
Packit 7cfc04
to the RTC managed by EFI firmware).
Packit 7cfc04
The
Packit 7cfc04
.I time
Packit 7cfc04
field is as used with
Packit 7cfc04
.B RTC_ALM_READ
Packit 7cfc04
and
Packit 7cfc04
.B RTC_ALM_SET
Packit 7cfc04
except that the
Packit 7cfc04
.IR tm_mday ,
Packit 7cfc04
.IR tm_mon ,
Packit 7cfc04
and
Packit 7cfc04
.I tm_year
Packit 7cfc04
fields are also valid.
Packit 7cfc04
A pointer to this structure should be passed as the third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
.SH FILES
Packit 7cfc04
.TP
Packit 7cfc04
.IR /dev/rtc ", " /dev/rtc0 ", " /dev/rtc1 ", etc."
Packit 7cfc04
RTC special character device files.
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/driver/rtc
Packit 7cfc04
status of the (first) RTC.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
When the kernel's system time is synchronized with an external
Packit 7cfc04
reference using
Packit 7cfc04
.BR adjtimex (2)
Packit 7cfc04
it will update a designated RTC periodically every 11 minutes.
Packit 7cfc04
To do so, the kernel has to briefly turn off periodic interrupts;
Packit 7cfc04
this might affect programs using that RTC.
Packit 7cfc04
.PP
Packit 7cfc04
An RTC's Epoch has nothing to do with the POSIX Epoch which is
Packit 7cfc04
used only for the system clock.
Packit 7cfc04
.PP
Packit 7cfc04
If the year according to the RTC's Epoch and the year register is
Packit 7cfc04
less than 1970 it is assumed to be 100 years later, that is, between 2000
Packit 7cfc04
and 2069.
Packit 7cfc04
.PP
Packit 7cfc04
Some RTCs support "wildcard" values in alarm fields, to support
Packit 7cfc04
scenarios like periodic alarms at fifteen minutes after every hour,
Packit 7cfc04
or on the first day of each month.
Packit 7cfc04
Such usage is nonportable;
Packit 7cfc04
portable user-space code expects only a single alarm interrupt, and
Packit 7cfc04
will either disable or reinitialize the alarm after receiving it.
Packit 7cfc04
.PP
Packit 7cfc04
Some RTCs support periodic interrupts with periods that are multiples
Packit 7cfc04
of a second rather than fractions of a second;
Packit 7cfc04
multiple alarms;
Packit 7cfc04
programmable output clock signals;
Packit 7cfc04
nonvolatile memory;
Packit 7cfc04
and other hardware
Packit 7cfc04
capabilities that are not currently exposed by this API.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR date (1),
Packit 7cfc04
.BR adjtimex (2),
Packit 7cfc04
.BR gettimeofday (2),
Packit 7cfc04
.BR settimeofday (2),
Packit 7cfc04
.BR stime (2),
Packit 7cfc04
.BR time (2),
Packit 7cfc04
.BR gmtime (3),
Packit 7cfc04
.BR time (7),
Packit 7cfc04
.BR hwclock (8)
Packit 7cfc04
.PP
Packit 7cfc04
.I Documentation/rtc.txt
Packit 7cfc04
in the Linux kernel source tree
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.