Linux's RTC driver is relatively simple. It can be used as a common generic device, a misc device, or a platform device, it is mainly used to fill in the Members in the structure of the rtc_ops file operation, which involves two important aspects:
1.In Linux, there are two types of Clock: hardware clock and system clock. The hardware clock refers to the clock device on the motherboard, that is, the clock that can usually be set on the BIOS screen. The system clock is the clock in the kernel. When Linux is started, the system clock reads the hardware clock settings, and then the system clock runs independently. All Linux-related commands and functions are read system clock settings.
The system clock settings are commonly used date commands, and the RTC driver we write serves the hardware clock. It has its own command hwclock, therefore, it is impossible to use the date command to call our driver. (At this point, I started to get depressed. After writing the driver, I was dumbly using the date command for testing, of course, the result is nothing). We can update the RTC clock through some commands of hwclock-that is, the interaction between the system clock and the hardware clock.
Hwclock-r display hardware clock and date
Hwclock-s adjusts the system clock to be consistent with the current hardware clock.
Hwclock-W adjusts the hardware clock to be consistent with the current system clock.
2.The second point is the interaction between the kernel space and the user space. At the end of the system startup, we are actually in the user State, so the content we input using commands is also in the user State, our driver is in the kernel state, and the kernel state and the user State are invisible to each other. Therefore, we need special functions to implement interaction between these two forms, these are the following two functions:
Copy_from_user (from user to kernel)
Copy_to_user (from kernel to user)
Of course, these two functions need to be implemented in the kernel driver.
The two most basic commands of RTC are to set the time and read time.
Set the time -- set the time to call the default rtc_set_time of the system. It is obvious that users in the user State will pass the time information they want to set to the kernel state,
Case rtc_set_time:
{
Struct rtc_time rtc_tm;
If (copy_from_user (& rtc_tm, (struct rtc_time *) Arg, sizeof (struct rtc_time )))
Return-efault;
Sep4020_rtc_settime (& rtc_tm); // transmits the information obtained from the user State to the set time function.
Return 0;
}
Read time -- set the time to call the default rtc_rd_time of the system. It is obvious that the chip clock needs to be taken out through the kernel-state driver and transmitted to the user State.
Case rtc_rd_time:/* read the time/date from RTC */
{
Sep4020_rtc_gettime (& Septime); // read the chip clock through the driver's read Function
Copy_to_user (void *) Arg, & Septime, sizeof Septime); // pass to user State
}