Linux驱动分析之RTC框架

电子说

1.2w人已加入

描述

前言

RTC

当Linux内核启动时,它会从RTC中读取时间与日期,作为基准值。然后通过软件来维护系统时间和日期。Linux系统中提供了RTC核心层,对于驱动开发者而言,操作起来就变得很简单了。我们来看看整体框架。

驱动框架

RTC

下面是整体框架图

RTC

与RTC核心有关的文件有:

文件 描述
/drivers/rtc/class.c 这个文件向linux设备模型核心注册了一个类RTC,然后向驱动程序提供了注册/注销接口
/drivers/rtc/rtc-dev.c 这个文件定义了基本的设备文件操作函数,如:open,read等
/drivers/rtc/interface.c 这个文件主要提供了用户程序与RTC驱动的接口函数,用户程序一般通过ioctl与RTC驱动交互,这里定义了每个ioctl命令需要调用的函数
/drivers/rtc/rtc-sysfs.c 与sysfs有关
/drivers/rtc/rtc-proc.c 与proc文件系统有关
/include/linux/rtc.h 定义了与RTC有关的数据结构

重要结构体

RTC

  • rtc_device
//RTC设备
struct rtc_device {
  struct device dev;
  struct module *owner;


  int id;


  const struct rtc_class_ops *ops; //rtc操作函数
  struct mutex ops_lock;


  struct cdev char_dev;
  unsigned long flags;


  unsigned long irq_data;
  spinlock_t irq_lock;
  wait_queue_head_t irq_queue;
  struct fasync_struct *async_queue;


  int irq_freq;
  int max_user_freq;


  struct timerqueue_head timerqueue;
  struct rtc_timer aie_timer; 
  struct rtc_timer uie_rtctimer;
  struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
  int pie_enabled;
  struct work_struct irqwork;
  /* Some hardware can't support UIE mode */
  int uie_unsupported;


  long set_offset_nsec;


  bool registered;


  struct nvmem_device *nvmem;
  /* Old ABI support */
  bool nvram_old_abi;
  struct bin_attribute *nvram;


  time64_t range_min;
  timeu64_t range_max;
  time64_t start_secs;
  time64_t offset_secs;
  bool set_start_time;


#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  struct work_struct uie_task;
  struct timer_list uie_timer;
  /* Those fields are protected by rtc->irq_lock */
  unsigned int oldsecs;
  unsigned int uie_irq_active:1;
  unsigned int stop_uie_polling:1;
  unsigned int uie_task_active:1;
  unsigned int uie_timer_active:1;
#endif
};

上面的结构体表示一个RTC设备,比较简单,主要就是中断信息,字符设备对象,操作函数等。

  • rtc_class_ops
//RTC操作函数
struct rtc_class_ops {
  int (*ioctl)(struct device *, unsigned int, unsigned long);
  int (*read_time)(struct device *, struct rtc_time *);
  int (*set_time)(struct device *, struct rtc_time *);
  int (*read_alarm)(struct device *, struct rtc_wkalrm *);
  int (*set_alarm)(struct device *, struct rtc_wkalrm *);
  int (*proc)(struct device *, struct seq_file *);
  int (*set_mmss64)(struct device *, time64_t secs);
  int (*set_mmss)(struct device *, unsigned long secs);
  int (*read_callback)(struct device *, int data);
  int (*alarm_irq_enable)(struct device *, unsigned int enabled);
  int (*read_offset)(struct device *, long *offset);
  int (*set_offset)(struct device *, long offset);
};

就是一些设置时间和读取时间,以及闹钟等接口函数。

  • rtc_time
//时间结构体
struct rtc_time {
  int tm_sec;
  int tm_min;
  int tm_hour;
  int tm_mday;
  int tm_mon;
  int tm_year;
  int tm_wday;
  int tm_yday;
  int tm_isdst;
};

API函数

RTC

// 注册RTC class
static struct rtc_device *rtc_device_register(const char *name,
                struct device *dev,
                const struct rtc_class_ops *ops,
                struct module *owner)

struct rtc_device *devm_rtc_device_register(struct device *dev,
          const char *name,
          const struct rtc_class_ops *ops,
          struct module *owner)
//注销RTC      
static void rtc_device_unregister(struct rtc_device *rtc)     
void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc)

总结

RTC

RTC也是字符设备驱动,只是进行了封装,封装完之后我们调用起来其实就很简单了。只要实现好接口函数,填充好结构体,然后进行注册即可。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分