转自:http://helloyesyes.iteye.com/blog/1072433
努力成为linux kernel hacker的人李万鹏原创作品,为梦而战。转载请标明出处
http://blog.csdn.net/woshixingaaa/archive/2011/05/21/6436215.aspx
RTC(实时时钟)是一种典型的字符设备,作为一种字符设备驱动,RTC需要有file_operations中接口函数的实现,如open(),release(),read(),poll(),ioctl()等,而典型的ioctl包括RTC_SET_TIME,RTC_ALM_READ,RTC_ALM_SET,RTC_IRQP_SET,RTC_IRQP_READ等,这些对于所有的RTC是通用的,只有底层的具体实现是设备相关的。如下图可以清楚看出RTC子系统的框架。
下面介绍几个重要的数据结构:
rtc_device用来描述rtc设备:
- struct rtc_device
- {
- struct device dev;
- struct module *owner;
- int id; //RTC设备的次设备号
- char name[RTC_DEVICE_NAME_SIZE];
- const struct rtc_class_ops *ops;
- 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;
- struct rtc_task *irq_task;
- spinlock_t irq_task_lock;
- int irq_freq;
- int max_user_freq;
- #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_time用于get time/set 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;
- };
描述报警状态的结构:
- struct rtc_wkalrm {
- unsigned char enabled; /* 0 = alarm disabled, 1 = alarm enabled */
- unsigned char pending; /* 0 = alarm not pending, 1 = alarm pending */
- struct rtc_time time; /* time the alarm is set to */
- };
- struct rtc_class_ops {
- int (*open)(struct device *); //打开设备时的回调函数,这个函数应该初始化硬件并申请资源
- void (*release)(struct device *); //这个函数是设备关闭时被调用的,应该注销申请的资源
- int (*ioctl)(struct device *, unsigned int, unsigned long); //ioctl函数,对想让RTC自己实现的命令应返回ENOIOCTLCMD
- 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 *); //procfs接口
- int (*set_mmss)(struct device *, unsigned long secs); //将传入的参数secs转换为struct rtc_time然后调用set_time函数。程序员可以不实现这个函数,但
- 前提是定义好了read_time/set_time,因为RTC框架需要用这两个函数来实现这个功能。
- int (*irq_set_state)(struct device *, int enabled); //周期采样中断的开关,根据enabled的值来设置
- int (*irq_set_freq)(struct device *, int freq); //设置周期中断的频率
- int (*read_callback)(struct device *, int data); ///用户空间获得数据后会传入读取的数据,并用这个函数返回的数据更新数据。
- int (*alarm_irq_enable)(struct device *, unsigned int enabled); //alarm中断使能开关,根据enabled的值来设置
- int (*update_irq_enable)(struct device *, unsigned int enabled); //更新中断使能开关,根据enabled的值来设置
- };
现在来看看rtc子系统是怎么注册上的:
- static int __init rtc_init(void)
- {
- rtc_class = class_create(THIS_MODULE, "rtc");
- if (IS_ERR(rtc_class)) {
- printk(KERN_ERR "%s: couldn't create class ", __FILE__);
- return PTR_ERR(rtc_class);
- }
- rtc_class->suspend = rtc_suspend;
- rtc_class->resume = rtc_resume;
- rtc_dev_init();
- rtc_sysfs_init(rtc_class);
- return 0;
- }
- void __init rtc_dev_init(void)
- {
- int err;
- err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
- if (err < 0)
- printk(KERN_ERR "%s: failed to allocate char dev region ",
- __FILE__);
- }
在class.c文件函数rtc_init中生成rtc类,然后调用rtc-dev.c文件中的rtc_dev_init分配设备号。
在rtc-dev.c中声明了file_operations,因为rtc也是一个字符设备:
- static const struct file_operations rtc_dev_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .read = rtc_dev_read,
- .poll = rtc_dev_poll,
- .unlocked_ioctl = rtc_dev_ioctl,
- .open = rtc_dev_open,
- .release = rtc_dev_release,
- .fasync = rtc_dev_fasync,
- };
下面来分析rtc-s3c.c源码:
首先看模块的注册和撤销:
- static int __init s3c_rtc_init(void)
- {
- printk(banner);
- return platform_driver_register(&s3c2410_rtc_driver);
- }
- static void __exit s3c_rtc_exit(void)
- {
- platform_driver_unregister(&s3c2410_rtc_driver);
- }
从上边的代码可以看出rtc driver作为platform_driver注册进内核,挂在platform_bus上。
- static struct platform_driver s3c2410_rtc_driver = {
- .probe = s3c_rtc_probe, //rtc探测函数
- .remove = __devexit_p(s3c_rtc_remove), //rtc移除函数
- .suspend = s3c_rtc_suspend, //rtc挂起函数
- .resume = s3c_rtc_resume, //rtc恢复函数
- .driver = {
- .name = "s3c2410-rtc", //注意这里的名字一定要和系统中定义平台设备的地方一致,这样才能把平台设备和平台驱动关联起来
- .owner = THIS_MODULE,
- },
- };
在arch/arm/plat-s3c24xx/devs.c中定义了rtc的platform_device:
- /* RTC */
- static struct resource s3c_rtc_resource[] = { //定义了rtc平台设备会使用的资源
- [0] = { //IO端口资源范围
- .start = S3C24XX_PA_RTC,
- .end = S3C24XX_PA_RTC + 0xff,
- .flags = IORESOURCE_MEM,
- },
- [1] = { //RTC报警中断资源
- .start = IRQ_RTC,
- .end = IRQ_RTC,
- .flags = IORESOURCE_IRQ,
- },
- [2] = { //TICK节拍时间中断资源
- .start = IRQ_TICK,
- .end = IRQ_TICK,
- .flags = IORESOURCE_IRQ
- }
- };
- struct platform_device s3c_device_rtc = { //定义了平台设备
- .name = "s3c2410-rtc", //设备名
- .id = -1,
- .num_resources = ARRAY_SIZE(s3c_rtc_resource), //资源数量
- .resource = s3c_rtc_resource, //引用上面定义的资源
- };
平台驱动中定义了probe函数,下面来看他的实现:
- static int __devinit s3c_rtc_probe(struct platform_device *pdev)
- {
- struct rtc_device *rtc;
- struct resource *res;
- int ret;
- pr_debug("%s: probe=%p ", __func__, pdev);
- /* find the IRQs */
- /*获得IRQ资源中的第二个,即TICK节拍时间中断号*/
- s3c_rtc_tickno = platform_get_irq(pdev, 1);
- if (s3c_rtc_tickno < 0) {
- dev_err(&pdev->dev, "no irq for rtc tick ");
- return -ENOENT;
- }
- /*获取IRQ资源中的第一个,即RTC报警中断*/
- s3c_rtc_alarmno = platform_get_irq(pdev, 0);
- if (s3c_rtc_alarmno < 0) {
- dev_err(&pdev->dev, "no irq for alarm ");
- return -ENOENT;
- }
- pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d ",
- s3c_rtc_tickno, s3c_rtc_alarmno);
- /* get the memory region */
- /*获取RTC平台设备所使用的IO端口资源*/
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (res == NULL) {
- dev_err(&pdev->dev, "failed to get memory region resource ");
- return -ENOENT;
- }
- /*申请IO端口资源所占用的IO空间*/
- s3c_rtc_mem = request_mem_region(res->start,
- res->end-res->start+1,
- pdev->name);
- if (s3c_rtc_mem == NULL) {
- dev_err(&pdev->dev, "failed to reserve memory region ");
- ret = -ENOENT;
- goto err_nores;
- }
- /*将IO端口占用的IO空间映射到虚拟地址,s3c_rtc_base是这段虚拟地址的起始地址*/
- s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
- if (s3c_rtc_base == NULL) {
- dev_err(&pdev->dev, "failed ioremap() ");
- ret = -EINVAL;
- goto err_nomap;
- }
- /* check to see if everything is setup correctly */
- /*对RTCCON第0位进行操作,使能RTC*/
- s3c_rtc_enable(pdev, 1);
- pr_debug("s3c2410_rtc: RTCCON=%02x ",
- readb(s3c_rtc_base + S3C2410_RTCCON));
- /*对TICNT第7位进行操作,使能节拍时间计数寄存器*/
- s3c_rtc_setfreq(&pdev->dev, 1);
- /*让电源管理支持唤醒功能*/
- device_init_wakeup(&pdev->dev, 1);
- /* register RTC and exit */
- /*注册rtc设备,名为"s3c",与s3c_rtcops这个rtc_class_ops进行关联*/
- rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
- THIS_MODULE);
- if (IS_ERR(rtc)) {
- dev_err(&pdev->dev, "cannot attach rtc ");
- ret = PTR_ERR(rtc);
- goto err_nortc;
- }
- /**/
- rtc->max_user_freq = 128;
- /*将rtc这个rtc_device存放在&pdev->dev->driver_data*/
- platform_set_drvdata(pdev, rtc);
- return 0;
- err_nortc:
- s3c_rtc_enable(pdev, 0);
- iounmap(s3c_rtc_base);
- err_nomap:
- release_resource(s3c_rtc_mem);
- err_nores:
- return ret;
- }
函数rtc_device_register在文件class.c中实现:
- struct rtc_device *rtc_device_register(const char *name, struct device *dev,
- const struct rtc_class_ops *ops,
- struct module *owner)
- {
- struct rtc_device *rtc;
- int id, err;
- /*为idr(rtc_idr)分配内存*/
- if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
- err = -ENOMEM;
- goto exit;
- }
- mutex_lock(&idr_lock);
- /*分配ID号存于id中,该ID号最终将作为该RTC设备的次设备号*/
- err = idr_get_new(&rtc_idr, NULL, &id);
- mutex_unlock(&idr_lock);
- if (err < 0)
- goto exit;
- id = id & MAX_ID_MASK;
- /*为RTC结构分配内存*/
- rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);
- if (rtc == NULL) {
- err = -ENOMEM;
- goto exit_idr;
- }
- rtc->id = id;
- /*指向原始操作函数集*/
- rtc->ops = ops;
- rtc->owner = owner;
- rtc->max_user_freq = 64;
- rtc->dev.parent = dev;
- rtc->dev.class = rtc_class;
- rtc->dev.release = rtc_device_release;
- mutex_init(&rtc->ops_lock);
- spin_lock_init(&rtc->irq_lock);
- spin_lock_init(&rtc->irq_task_lock);
- init_waitqueue_head(&rtc->irq_queue);
- strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
- dev_set_name(&rtc->dev, "rtc%d", id);
- /*rtc->dev.devt = MKDEV(MAJOR(rtc_devt),rtc->id); cdev_init(&rtc->char_dev,&rtc_dev_fops);其中rtc_devt是从调用alloc_chrdev_region时获得的*/
- rtc_dev_prepare(rtc);
- /*注册该RTC设备rtc->dev*/
- err = device_register(&rtc->dev);
- if (err)
- goto exit_kfree;
- /*cdev_add(&rtc->chr_dev,rtc->dev.devt,1);将rtc->chrdev注册到系统中*/
- rtc_dev_add_device(rtc);
- /*在/sys下添加属性文件*/
- rtc_sysfs_add_device(rtc);
- /*在/proc中创建入口项"driver/rtc"*/
- rtc_proc_add_device(rtc);
- dev_info(dev, "rtc core: registered %s as %s ",
- rtc->name, dev_name(&rtc->dev));
- return rtc;
- exit_kfree:
- kfree(rtc);
- exit_idr:
- mutex_lock(&idr_lock);
- idr_remove(&rtc_idr, id);
- mutex_unlock(&idr_lock);
- exit:
- dev_err(dev, "rtc core: unable to register %s, err = %d ",
- name, err);
- return ERR_PTR(err);
- }
下边是s3c_rtc_enable函数的实现:
- static void s3c_rtc_enable(struct platform_device *pdev, int en)
- {
- void __iomem *base = s3c_rtc_base;
- unsigned int tmp;
- if (s3c_rtc_base == NULL)
- return;
- /*如果禁止,就disable RTCCON与TICNT*/
- if (!en) {
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
- tmp = readb(base + S3C2410_TICNT);
- writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
- } else {
- /* re-enable the device, and check it is ok */
- /*如果RTCCON没有使能,则使能之*/
- if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
- dev_info(&pdev->dev, "rtc disabled, re-enabling ");
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
- }
- /*如果BCD的计数选择位为1,则置位0,即Merge BCD counts*/
- if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
- dev_info(&pdev->dev, "removing RTCCON_CNTSEL ");
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
- }
- /*如果BCD的时钟选择为1,则置位0,即XTAL 1/215 divided clock*/
- if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
- dev_info(&pdev->dev, "removing RTCCON_CLKRST ");
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
- }
- }
- }
- static int __devexit s3c_rtc_remove(struct platform_device *dev)
- {
- /*从系统平台设备中获取RTC设备类的数据*/
- struct rtc_device *rtc = platform_get_drvdata(dev);
- /*清空平台设备中RTC驱动数据*/
- platform_set_drvdata(dev, NULL);
- /*注销RTC设备类*/
- rtc_device_unregister(rtc);
- /*禁止RTC节拍时间计数寄存器TICNT的使能功能*/
- s3c_rtc_setpie(&dev->dev, 0);
- /*禁止RTC报警控制寄存器RTCALM的全局报警使能功能*/
- s3c_rtc_setaie(0);
- /*释放RTC虚拟地址映射空间*/
- iounmap(s3c_rtc_base);
- /*释放获取的RTC平台设备的资源*/
- release_resource(s3c_rtc_mem);
- /*销毁保存RTC平台设备的资源内存空间*/
- kfree(s3c_rtc_mem);
- return 0;
- }
这里是电源管理部分,在挂起时保存TICNT的值,并禁止RTCCON,TICNT;在休眠的时候开启RTCCON,并恢复TICNT的值。
- #ifdef CONFIG_PM
- /* RTC Power management control */
- static int ticnt_save;
- static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
- {
- /* save TICNT for anyone using periodic interrupts */
- ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
- s3c_rtc_enable(pdev, 0);
- return 0;
- }
- static int s3c_rtc_resume(struct platform_device *pdev)
- {
- s3c_rtc_enable(pdev, 1);
- writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
- return 0;
- }
- #else
- #define s3c_rtc_suspend NULL
- #define s3c_rtc_resume NULL
- #endif
s3c_rtcops是RTC设备在RTC核心部分注册的对RTC设备进行操作的结构体,类似字符设备在驱动中的file_operations对字符设备进行操作的意思。
- static const struct rtc_class_ops s3c_rtcops = {
- .open = s3c_rtc_open,
- .release = s3c_rtc_release,
- .read_time = s3c_rtc_gettime,
- .set_time = s3c_rtc_settime,
- .read_alarm = s3c_rtc_getalarm,
- .set_alarm = s3c_rtc_setalarm,
- .irq_set_freq = s3c_rtc_setfreq,
- .irq_set_state = s3c_rtc_setpie,
- .proc = s3c_rtc_proc,
- };
这两个是下边会用到的中断处理函数,产生一个时钟中断的时候就更新一下rtc_irq_data的值,也就是说只有当产生一个时钟中断(也就是一个滴答tick)才返回给用户一个时间。
- static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
- {
- struct rtc_device *rdev = id;
- rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
- return IRQ_HANDLED;
- }
- static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
- {
- struct rtc_device *rdev = id;
- rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
- return IRQ_HANDLED;
- }
首先来看打开和关闭函数:
- static int s3c_rtc_open(struct device *dev)
- {
- /*获得平台设备,从平台设备pdev->dev->driver_data获取rtc_device*/
- struct platform_device *pdev = to_platform_device(dev);
- struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
- int ret;
- /*注册RTC报警中断的中断处理函数*/
- ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
- IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
- if (ret) {
- dev_err(dev, "IRQ%d error %d ", s3c_rtc_alarmno, ret);
- return ret;
- }
- /*注册TICK节拍时间中断的中断处理函数*/
- ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
- IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
- if (ret) {
- dev_err(dev, "IRQ%d error %d ", s3c_rtc_tickno, ret);
- goto tick_err;
- }
- return ret;
- tick_err:
- free_irq(s3c_rtc_alarmno, rtc_dev);
- return ret;
- }
RTC设备类关闭接口函数:
- static void s3c_rtc_release(struct device *dev)
- {
- struct platform_device *pdev = to_platform_device(dev);
- struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
- /* do not clear AIE here, it may be needed for wake */
- s3c_rtc_setpie(dev, 0);
- free_irq(s3c_rtc_alarmno, rtc_dev);
- free_irq(s3c_rtc_tickno, rtc_dev);
- }
更新RTCALM寄存器的状态,是否使能:
- static void s3c_rtc_setaie(int to)
- {
- unsigned int tmp;
- pr_debug("%s: aie=%d ", __func__, to);
- tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
- if (to)
- tmp |= S3C2410_RTCALM_ALMEN;
- writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
- }
更新TICNT寄存器的状态,是否使能:
- static int s3c_rtc_setpie(struct device *dev, int enabled)
- {
- unsigned int tmp;
- pr_debug("%s: pie=%d ", __func__, enabled);
- spin_lock_irq(&s3c_rtc_pie_lock);
- tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
- if (enabled)
- tmp |= S3C2410_TICNT_ENABLE;
- writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
- spin_unlock_irq(&s3c_rtc_pie_lock);
- return 0;
- }
更新TICNT节拍时间计数的值:
- static int s3c_rtc_setfreq(struct device *dev, int freq)
- {
- unsigned int tmp;
- if (!is_power_of_2(freq))
- return -EINVAL;
- spin_lock_irq(&s3c_rtc_pie_lock);
- tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
- tmp |= (128 / freq)-1;
- writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
- spin_unlock_irq(&s3c_rtc_pie_lock);
- return 0;
- }
- /* Time read/write */
- static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
- {
- unsigned int have_retried = 0;
- /*获得rtc IO端口寄存器的虚拟地址的起始地址*/
- void __iomem *base = s3c_rtc_base;
- retry_get_time:
- /*读取RTC中BCD数中的:分、时、日期、月、年、秒,放到rtc_time rtc_tm中*/
- rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
- rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
- rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
- rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
- rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
- rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
- /* the only way to work out wether the system was mid-update
- * when we read it is to check the second counter, and if it
- * is zero, then we re-try the entire read
- */
- /*如果到达0秒就检查一下,因为年月日时分可能会有加1操作,比如此时是一年的最后天的最后一分一秒,则年月日时分秒都会改变*/
- if (rtc_tm->tm_sec == 0 && !have_retried) {
- have_retried = 1;
- goto retry_get_time;
- }
- pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x ",
- rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
- rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
- /*使用readb读取寄存器的值得到的是bcd格式,必须转换成bin格式再保存*/
- rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
- rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
- rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
- rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
- rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
- rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
- rtc_tm->tm_year += 100;
- rtc_tm->tm_mon -= 1;
- return 0;
- }
- static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
- {
- void __iomem *base = s3c_rtc_base;
- int year = tm->tm_year - 100;
- pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d ",
- tm->tm_year, tm->tm_mon, tm->tm_mday,
- tm->tm_hour, tm->tm_min, tm->tm_sec);
- /* we get around y2k by simply not supporting it */
- /*RTC时钟的范围是00~99,由BCDYEAR寄存器的0~7位存储*/
- if (year < 0 || year >= 100) {
- dev_err(dev, "rtc only supports 100 years ");
- return -EINVAL;
- }
- /*将上面保存到RTC核心定义的时间结构体中的时间日期值写入对应的寄存器中*/
- writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
- writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
- writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
- writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
- writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
- writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
- return 0;
- }
获取报警时间的值:
- static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
- {
- struct rtc_time *alm_tm = &alrm->time;
- void __iomem *base = s3c_rtc_base;
- unsigned int alm_en;
- /*从RTC的报警寄存器中读取*/
- alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
- alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
- alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
- alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
- alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
- alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
- alm_en = readb(base + S3C2410_RTCALM);
- /*根据RTCALM寄存器的报警全局使能位来设置报警状态结构rtc_wkalrm*/
- alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
- pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x ",
- alm_en,
- alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
- alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
- /* decode the alarm enable field */
- /*如果RTCALM寄存器的秒使能,则将rtc_wkalrm中存放的秒数据由BCD格式转换为BIN格式,否则设置为0xff*/
- if (alm_en & S3C2410_RTCALM_SECEN)
- alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
- else
- alm_tm->tm_sec = 0xff;
- /*如果RTCALM寄存器的分钟使能,则将rtc_wkalrm中存放的分钟数据由BCD格式转换为BIN格式,否则设置为0xff*/
- if (alm_en & S3C2410_RTCALM_MINEN)
- alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
- else
- alm_tm->tm_min = 0xff;
- /*如果RTCALM寄存器的小时使能,则将rtc_wkalrm中存放的小时数据由BCD格式转换为BIN格式,否则设置为0xff*/
- if (alm_en & S3C2410_RTCALM_HOUREN)
- alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
- else
- alm_tm->tm_hour = 0xff;
- /*如果RTCALM寄存器的日使能,则将rtc_wkalrm中存放的日数据由BCD格式转换为BIN格式,否则设置为0xff*/
- if (alm_en & S3C2410_RTCALM_DAYEN)
- alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
- else
- alm_tm->tm_mday = 0xff;
- /*如果RTCALM寄存器的月使能,则将rtc_wkalrm中存放的月数据由BCD格式转换为BIN格式,否则设置为0xff*/
- if (alm_en & S3C2410_RTCALM_MONEN) {
- alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
- alm_tm->tm_mon -= 1;
- } else {
- alm_tm->tm_mon = 0xff;
- }
- /*如果RTCALM寄存器的年使能,则将rtc_wkalrm中存放的年数据由BCD格式转换为BIN格式,否则设置为0xff*/
- if (alm_en & S3C2410_RTCALM_YEAREN)
- alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
- else
- alm_tm->tm_year = 0xffff;
- return 0;
- }
设置报警时间的值:
- static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
- {
- struct rtc_time *tm = &alrm->time;
- void __iomem *base = s3c_rtc_base;
- unsigned int alrm_en;
- pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x ",
- alrm->enabled,
- tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
- tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
- /*读取RTCALM寄存器的全局使能位,关闭所有报警使能*/
- alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
- writeb(0x00, base + S3C2410_RTCALM);
- /*如果秒时间在合理范围内,则使能秒报警位,将报警状态寄存器中封装的time的秒位由BIN格式转换为BCD,写入秒报警寄存器中*/
- if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
- alrm_en |= S3C2410_RTCALM_SECEN;
- writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
- }
- /*如果分钟时间在合理范围内,则使能分钟报警位,将报警状态寄存器中封装的time的分钟位由BIN格式转换为BCD,写入分钟报警寄存器中*/
- if (tm->tm_min < 60 && tm->tm_min >= 0) {
- alrm_en |= S3C2410_RTCALM_MINEN;
- writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
- }
- /*如果小时时间在合理范围内,则使能小时报警位,将报警状态寄存器中封装的time的小时位由BIN格式转换为BCD,写入小时报警寄存器中*/
- if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
- alrm_en |= S3C2410_RTCALM_HOUREN;
- writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
- }
- pr_debug("setting S3C2410_RTCALM to %08x ", alrm_en);
- /*使能RTCALM寄存器全局报警位*/
- writeb(alrm_en, base + S3C2410_RTCALM);
- /**/
- s3c_rtc_setaie(alrm->enabled);
- /*根据全局报警使能的状态来决定是唤醒RTC报警中断还是睡眠RTC报警中断*/
- if (alrm->enabled)
- enable_irq_wake(s3c_rtc_alarmno);
- else
- disable_irq_wake(s3c_rtc_alarmno);
- return 0;
- }
下面来分析一下是怎样获取和设置时间的:
通过用户空间的ioctl,在rtc-dev.c中实现了rtc_dev_ioctl,其中获取和设置时间如下:
- case RTC_RD_TIME:
- mutex_unlock(&rtc->ops_lock);
- err = rtc_read_time(rtc, &tm);
- if (err < 0)
- return err;
- if (copy_to_user(uarg, &tm, sizeof(tm)))
- err = -EFAULT;
- return err;
- case RTC_SET_TIME:
- mutex_unlock(&rtc->ops_lock);
- if (copy_from_user(&tm, uarg, sizeof(tm)))
- return -EFAULT;
- return rtc_set_time(rtc, &tm);
通过copy_to_user和copy_from_user实现时间在内核空间与用户空间的传递。这里调用到的rtc_read_time和rtc_set_time在interface.c中实现:
- int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
- {
- int err;
- err = mutex_lock_interruptible(&rtc->ops_lock);
- if (err)
- return err;
- if (!rtc->ops)
- err = -ENODEV;
- else if (!rtc->ops->read_time)
- err = -EINVAL;
- else {
- memset(tm, 0, sizeof(struct rtc_time));
- err = rtc->ops->read_time(rtc->dev.parent, tm);
- }
- mutex_unlock(&rtc->ops_lock);
- return err;
- }
- int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
- {
- int err;
- err = rtc_valid_tm(tm);
- if (err != 0)
- return err;
- err = mutex_lock_interruptible(&rtc->ops_lock);
- if (err)
- return err;
- if (!rtc->ops)
- err = -ENODEV;
- else if (rtc->ops->set_time)
- err = rtc->ops->set_time(rtc->dev.parent, tm);
- else if (rtc->ops->set_mmss) {
- unsigned long secs;
- err = rtc_tm_to_time(tm, &secs);
- if (err == 0)
- err = rtc->ops->set_mmss(rtc->dev.parent, secs);
- } else
- err = -EINVAL;
- mutex_unlock(&rtc->ops_lock);
- return err;
- }
可以看出他们调用了具体RTC设备驱动中的read_time和set_time函数,对应了s3c2410中的s3c_rtc_gettime和s3c_rtc_settime,这里使用的rtc_tm_to_time函数实现在rtclib.c中,/drivers/rtc/interface.c定义了可供其它模块访问的接口。