1 // SPDX-License-Identifier: GPL-2.0
3 * Real Time Clock Periodic Interrupt test program
5 * Since commit 6610e0893b8bc ("RTC: Rework RTC code to use timerqueue for
6 * events"), PIE are completely handled using hrtimers, without actually using
7 * any underlying hardware RTC.
12 #include <linux/rtc.h>
13 #include <sys/ioctl.h>
15 #include <sys/types.h>
21 #include "../kselftest.h"
24 * This expects the new RTC class driver framework, working with
25 * clocks that will often not be clones of what the PC-AT had.
26 * Use the command line to specify another RTC if you need one.
28 static const char default_rtc[] = "/dev/rtc0";
30 int main(int argc, char **argv)
32 int i, fd, retval, irqcount = 0;
33 unsigned long tmp, data, old_pie_rate;
34 const char *rtc = default_rtc;
35 struct timeval start, end, diff;
42 fd = open(default_rtc, O_RDONLY);
44 printf("Default RTC %s does not exist. Test Skipped!\n", default_rtc);
50 fprintf(stderr, "usage: rtctest [rtcdev] [d]\n");
54 fd = open(rtc, O_RDONLY);
61 /* Read periodic IRQ rate */
62 retval = ioctl(fd, RTC_IRQP_READ, &old_pie_rate);
64 /* not all RTCs support periodic IRQs */
65 if (errno == EINVAL) {
66 fprintf(stderr, "\nNo periodic IRQ support\n");
69 perror("RTC_IRQP_READ ioctl");
72 fprintf(stderr, "\nPeriodic IRQ rate is %ldHz.\n", old_pie_rate);
74 fprintf(stderr, "Counting 20 interrupts at:");
77 /* The frequencies 128Hz, 256Hz, ... 8192Hz are only allowed for root. */
78 for (tmp=2; tmp<=64; tmp*=2) {
80 retval = ioctl(fd, RTC_IRQP_SET, tmp);
82 /* not all RTCs can change their periodic IRQ rate */
83 if (errno == EINVAL) {
85 "\n...Periodic IRQ rate is fixed\n");
88 perror("RTC_IRQP_SET ioctl");
92 fprintf(stderr, "\n%ldHz:\t", tmp);
95 /* Enable periodic interrupts */
96 retval = ioctl(fd, RTC_PIE_ON, 0);
98 perror("RTC_PIE_ON ioctl");
102 for (i=1; i<21; i++) {
103 gettimeofday(&start, NULL);
105 retval = read(fd, &data, sizeof(unsigned long));
110 gettimeofday(&end, NULL);
111 timersub(&end, &start, &diff);
112 if (diff.tv_sec > 0 ||
113 diff.tv_usec > ((1000000L / tmp) * 1.10)) {
114 fprintf(stderr, "\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n",
115 diff.tv_sec, diff.tv_usec,
121 fprintf(stderr, " %d",i);
126 /* Disable periodic interrupts */
127 retval = ioctl(fd, RTC_PIE_OFF, 0);
129 perror("RTC_PIE_OFF ioctl");
135 ioctl(fd, RTC_IRQP_SET, old_pie_rate);
137 fprintf(stderr, "\n\n\t\t\t *** Test complete ***\n");