xref: /DragonOS/kernel/src/driver/rtc/interface.rs (revision da152319797436368304cbc3f85a3b9ec049134b)
1*da152319SLoGin use alloc::sync::Arc;
2*da152319SLoGin use system_error::SystemError;
3*da152319SLoGin 
4*da152319SLoGin use crate::driver::base::kobject::KObject;
5*da152319SLoGin 
6*da152319SLoGin use super::{global_default_rtc, sysfs::RtcGeneralDevice, utils::kobj2rtc_device, RtcTime};
7*da152319SLoGin 
8*da152319SLoGin /// 根据rtc general device, 读取真实时间
rtc_read_time(general_dev: &Arc<RtcGeneralDevice>) -> Result<RtcTime, SystemError>9*da152319SLoGin pub fn rtc_read_time(general_dev: &Arc<RtcGeneralDevice>) -> Result<RtcTime, SystemError> {
10*da152319SLoGin     let class_ops = general_dev.class_ops().ok_or(SystemError::EINVAL)?;
11*da152319SLoGin 
12*da152319SLoGin     let real_dev = general_dev
13*da152319SLoGin         .parent()
14*da152319SLoGin         .and_then(|p| p.upgrade())
15*da152319SLoGin         .ok_or(SystemError::ENODEV)?;
16*da152319SLoGin 
17*da152319SLoGin     let real_dev = kobj2rtc_device(real_dev).ok_or(SystemError::EINVAL)?;
18*da152319SLoGin 
19*da152319SLoGin     let time = class_ops.read_time(&real_dev)?;
20*da152319SLoGin 
21*da152319SLoGin     if !time.valid() {
22*da152319SLoGin         return Err(SystemError::EINVAL);
23*da152319SLoGin     }
24*da152319SLoGin 
25*da152319SLoGin     return Ok(time);
26*da152319SLoGin }
27*da152319SLoGin 
28*da152319SLoGin /// 从全局默认的rtc时钟那里读取时间
rtc_read_time_default() -> Result<RtcTime, SystemError>29*da152319SLoGin pub fn rtc_read_time_default() -> Result<RtcTime, SystemError> {
30*da152319SLoGin     rtc_read_time(&global_default_rtc().ok_or(SystemError::ENODEV)?)
31*da152319SLoGin }
32