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