天天看点

Linux驱动修炼之道-RTC子系统框架与源码分析【转】

努力成为linux kernel hacker的人李万鹏原创作品,为梦而战。转载请标明出处

<a href="http://blog.csdn.net/woshixingaaa/archive/2011/05/21/6436215.aspx">http://blog.csdn.net/woshixingaaa/archive/2011/05/21/6436215.aspx</a>

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子系统的框架。

Linux驱动修炼之道-RTC子系统框架与源码分析【转】

下面介绍几个重要的数据结构:

rtc_device用来描述rtc设备:

C-sharp代码 

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-&gt;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\n", __FILE__);  

        return PTR_ERR(rtc_class);  

    }  

    rtc_class-&gt;suspend = rtc_suspend;  

    rtc_class-&gt;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(&amp;rtc_devt, 0, RTC_DEV_MAX, "rtc");  

    if (err &lt; 0)  

        printk(KERN_ERR "%s: failed to allocate char dev region\n",  

            __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(&amp;s3c2410_rtc_driver);  

static void __exit s3c_rtc_exit(void)  

    platform_driver_unregister(&amp;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\n", __func__, pdev);  

    /* find the IRQs */  

    /*获得IRQ资源中的第二个,即TICK节拍时间中断号*/  

    s3c_rtc_tickno = platform_get_irq(pdev, 1);  

    if (s3c_rtc_tickno &lt; 0) {  

        dev_err(&amp;pdev-&gt;dev, "no irq for rtc tick\n");  

        return -ENOENT;  

    /*获取IRQ资源中的第一个,即RTC报警中断*/  

    s3c_rtc_alarmno = platform_get_irq(pdev, 0);  

    if (s3c_rtc_alarmno &lt; 0) {  

        dev_err(&amp;pdev-&gt;dev, "no irq for alarm\n");  

    pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",  

         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(&amp;pdev-&gt;dev, "failed to get memory region resource\n");  

    /*申请IO端口资源所占用的IO空间*/  

    s3c_rtc_mem = request_mem_region(res-&gt;start,  

                     res-&gt;end-res-&gt;start+1,  

                     pdev-&gt;name);  

    if (s3c_rtc_mem == NULL) {  

        dev_err(&amp;pdev-&gt;dev, "failed to reserve memory region\n");  

        ret = -ENOENT;  

        goto err_nores;  

    /*将IO端口占用的IO空间映射到虚拟地址,s3c_rtc_base是这段虚拟地址的起始地址*/  

    s3c_rtc_base = ioremap(res-&gt;start, res-&gt;end - res-&gt;start + 1);  

    if (s3c_rtc_base == NULL) {  

        dev_err(&amp;pdev-&gt;dev, "failed ioremap()\n");  

        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\n",  

         readb(s3c_rtc_base + S3C2410_RTCCON));  

    /*对TICNT第7位进行操作,使能节拍时间计数寄存器*/  

    s3c_rtc_setfreq(&amp;pdev-&gt;dev, 1);  

    /*让电源管理支持唤醒功能*/  

    device_init_wakeup(&amp;pdev-&gt;dev, 1);  

    /* register RTC and exit */  

    /*注册rtc设备,名为"s3c",与s3c_rtcops这个rtc_class_ops进行关联*/  

    rtc = rtc_device_register("s3c", &amp;pdev-&gt;dev, &amp;s3c_rtcops,  

                  THIS_MODULE);  

    if (IS_ERR(rtc)) {  

        dev_err(&amp;pdev-&gt;dev, "cannot attach rtc\n");  

        ret = PTR_ERR(rtc);  

        goto err_nortc;  

    /**/  

    rtc-&gt;max_user_freq = 128;  

    /*将rtc这个rtc_device存放在&amp;pdev-&gt;dev-&gt;driver_data*/  

    platform_set_drvdata(pdev, rtc);  

 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)  

    int id, err;  

    /*为idr(rtc_idr)分配内存*/  

    if (idr_pre_get(&amp;rtc_idr, GFP_KERNEL) == 0) {  

        err = -ENOMEM;  

        goto exit;  

    mutex_lock(&amp;idr_lock);  

    /*分配ID号存于id中,该ID号最终将作为该RTC设备的次设备号*/  

    err = idr_get_new(&amp;rtc_idr, NULL, &amp;id);  

    mutex_unlock(&amp;idr_lock);  

    id = id &amp; MAX_ID_MASK;  

    /*为RTC结构分配内存*/  

    rtc = kzalloc(sizeof(struct rtc_device), GFP_KERNEL);  

    if (rtc == NULL) {  

        goto exit_idr;  

    rtc-&gt;id = id;  

    /*指向原始操作函数集*/  

    rtc-&gt;ops = ops;    

    rtc-&gt;owner = owner;  

    rtc-&gt;max_user_freq = 64;  

    rtc-&gt;dev.parent = dev;  

    rtc-&gt;dev.class = rtc_class;  

    rtc-&gt;dev.release = rtc_device_release;  

    mutex_init(&amp;rtc-&gt;ops_lock);  

    spin_lock_init(&amp;rtc-&gt;irq_lock);  

    spin_lock_init(&amp;rtc-&gt;irq_task_lock);  

    init_waitqueue_head(&amp;rtc-&gt;irq_queue);  

    strlcpy(rtc-&gt;name, name, RTC_DEVICE_NAME_SIZE);  

    dev_set_name(&amp;rtc-&gt;dev, "rtc%d", id);  

    /*rtc-&gt;dev.devt = MKDEV(MAJOR(rtc_devt),rtc-&gt;id); cdev_init(&amp;rtc-&gt;char_dev,&amp;rtc_dev_fops);其中rtc_devt是从调用alloc_chrdev_region时获得的*/  

    rtc_dev_prepare(rtc);  

    /*注册该RTC设备rtc-&gt;dev*/  

    err = device_register(&amp;rtc-&gt;dev);  

    if (err)  

        goto exit_kfree;  

    /*cdev_add(&amp;rtc-&gt;chr_dev,rtc-&gt;dev.devt,1);将rtc-&gt;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\n",  

            rtc-&gt;name, dev_name(&amp;rtc-&gt;dev));  

    return rtc;  

exit_kfree:  

    kfree(rtc);  

exit_idr:  

    idr_remove(&amp;rtc_idr, id);  

exit:  

    dev_err(dev, "rtc core: unable to register %s, err = %d\n",  

            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 &amp; ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);  

        tmp = readb(base + S3C2410_TICNT);  

        writeb(tmp &amp; ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);  

    } else {  

        /* re-enable the device, and check it is ok */  

        /*如果RTCCON没有使能,则使能之*/  

        if ((readb(base+S3C2410_RTCCON) &amp; S3C2410_RTCCON_RTCEN) == 0){  

            dev_info(&amp;pdev-&gt;dev, "rtc disabled, re-enabling\n");  

            tmp = readb(base + S3C2410_RTCCON);  

            writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);  

        }  

        /*如果BCD的计数选择位为1,则置位0,即Merge BCD counts*/  

        if ((readb(base + S3C2410_RTCCON) &amp; S3C2410_RTCCON_CNTSEL)){  

            dev_info(&amp;pdev-&gt;dev, "removing RTCCON_CNTSEL\n");  

            writeb(tmp&amp; ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);  

        /*如果BCD的时钟选择为1,则置位0,即XTAL 1/215 divided clock*/  

        if ((readb(base + S3C2410_RTCCON) &amp; S3C2410_RTCCON_CLKRST)){  

            dev_info(&amp;pdev-&gt;dev, "removing RTCCON_CLKRST\n");  

            writeb(tmp &amp; ~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(&amp;dev-&gt;dev, 0);  

    /*禁止RTC报警控制寄存器RTCALM的全局报警使能功能*/  

    s3c_rtc_setaie(0);  

    /*释放RTC虚拟地址映射空间*/  

    /*释放获取的RTC平台设备的资源*/  

    /*销毁保存RTC平台设备的资源内存空间*/  

    kfree(s3c_rtc_mem);  

这里是电源管理部分,在挂起时保存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);  

static int s3c_rtc_resume(struct platform_device *pdev)  

    writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);  

#else  

#define s3c_rtc_suspend NULL  

#define s3c_rtc_resume  NULL  

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)  

    rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);  

首先来看打开和关闭函数:

static int s3c_rtc_open(struct device *dev)  

    /*获得平台设备,从平台设备pdev-&gt;dev-&gt;driver_data获取rtc_device*/  

    struct platform_device *pdev = to_platform_device(dev);  

    struct rtc_device *rtc_dev = platform_get_drvdata(pdev);  

    /*注册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\n", s3c_rtc_alarmno, ret);  

        return ret;  

    /*注册TICK节拍时间中断的中断处理函数*/  

    ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,  

              IRQF_DISABLED,  "s3c2410-rtc tick", rtc_dev);  

        dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);  

        goto tick_err;  

 tick_err:  

    free_irq(s3c_rtc_alarmno, rtc_dev);  

RTC设备类关闭接口函数:

static void s3c_rtc_release(struct device *dev)  

{     

    /* do not clear AIE here, it may be needed for wake */  

    s3c_rtc_setpie(dev, 0);  

    free_irq(s3c_rtc_tickno, rtc_dev);  

更新RTCALM寄存器的状态,是否使能:

static void s3c_rtc_setaie(int to)  

    pr_debug("%s: aie=%d\n", __func__, to);  

    tmp = readb(s3c_rtc_base + S3C2410_RTCALM) &amp; ~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)  

    pr_debug("%s: pie=%d\n", __func__, enabled);  

    spin_lock_irq(&amp;s3c_rtc_pie_lock);  

    tmp = readb(s3c_rtc_base + S3C2410_TICNT) &amp; ~S3C2410_TICNT_ENABLE;  

    if (enabled)  

        tmp |= S3C2410_TICNT_ENABLE;  

    writeb(tmp, s3c_rtc_base + S3C2410_TICNT);  

    spin_unlock_irq(&amp;s3c_rtc_pie_lock);  

更新TICNT节拍时间计数的值:

static int s3c_rtc_setfreq(struct device *dev, int freq)  

    if (!is_power_of_2(freq))  

        return -EINVAL;  

    tmp = readb(s3c_rtc_base + S3C2410_TICNT) &amp; S3C2410_TICNT_ENABLE;  

    tmp |= (128 / freq)-1;  

/* Time read/write */  

static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)  

    unsigned int have_retried = 0;  

    /*获得rtc IO端口寄存器的虚拟地址的起始地址*/  

 retry_get_time:  

    /*读取RTC中BCD数中的:分、时、日期、月、年、秒,放到rtc_time rtc_tm中*/  

    rtc_tm-&gt;tm_min  = readb(base + S3C2410_RTCMIN);  

    rtc_tm-&gt;tm_hour = readb(base + S3C2410_RTCHOUR);  

    rtc_tm-&gt;tm_mday = readb(base + S3C2410_RTCDATE);  

    rtc_tm-&gt;tm_mon  = readb(base + S3C2410_RTCMON);  

    rtc_tm-&gt;tm_year = readb(base + S3C2410_RTCYEAR);  

    rtc_tm-&gt;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-&gt;tm_sec == 0 &amp;&amp; !have_retried) {  

        have_retried = 1;  

        goto retry_get_time;  

    pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",  

         rtc_tm-&gt;tm_year, rtc_tm-&gt;tm_mon, rtc_tm-&gt;tm_mday,  

         rtc_tm-&gt;tm_hour, rtc_tm-&gt;tm_min, rtc_tm-&gt;tm_sec);  

    /*使用readb读取寄存器的值得到的是bcd格式,必须转换成bin格式再保存*/  

    rtc_tm-&gt;tm_sec = bcd2bin(rtc_tm-&gt;tm_sec);  

    rtc_tm-&gt;tm_min = bcd2bin(rtc_tm-&gt;tm_min);  

    rtc_tm-&gt;tm_hour = bcd2bin(rtc_tm-&gt;tm_hour);  

    rtc_tm-&gt;tm_mday = bcd2bin(rtc_tm-&gt;tm_mday);  

    rtc_tm-&gt;tm_mon = bcd2bin(rtc_tm-&gt;tm_mon);  

    rtc_tm-&gt;tm_year = bcd2bin(rtc_tm-&gt;tm_year);  

    rtc_tm-&gt;tm_year += 100;  

    rtc_tm-&gt;tm_mon -= 1;  

static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)  

    int year = tm-&gt;tm_year - 100;  

    pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",  

         tm-&gt;tm_year, tm-&gt;tm_mon, tm-&gt;tm_mday,  

         tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec);  

    /* we get around y2k by simply not supporting it */  

    /*RTC时钟的范围是00~99,由BCDYEAR寄存器的0~7位存储*/  

    if (year &lt; 0 || year &gt;= 100) {  

        dev_err(dev, "rtc only supports 100 years\n");  

    /*将上面保存到RTC核心定义的时间结构体中的时间日期值写入对应的寄存器中*/  

    writeb(bin2bcd(tm-&gt;tm_sec),  base + S3C2410_RTCSEC);  

    writeb(bin2bcd(tm-&gt;tm_min),  base + S3C2410_RTCMIN);  

    writeb(bin2bcd(tm-&gt;tm_hour), base + S3C2410_RTCHOUR);  

    writeb(bin2bcd(tm-&gt;tm_mday), base + S3C2410_RTCDATE);  

    writeb(bin2bcd(tm-&gt;tm_mon + 1), base + S3C2410_RTCMON);  

    writeb(bin2bcd(year), base + S3C2410_RTCYEAR);  

获取报警时间的值:

static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)  

    struct rtc_time *alm_tm = &amp;alrm-&gt;time;  

    unsigned int alm_en;  

    /*从RTC的报警寄存器中读取*/  

    alm_tm-&gt;tm_sec  = readb(base + S3C2410_ALMSEC);  

    alm_tm-&gt;tm_min  = readb(base + S3C2410_ALMMIN);  

    alm_tm-&gt;tm_hour = readb(base + S3C2410_ALMHOUR);  

    alm_tm-&gt;tm_mon  = readb(base + S3C2410_ALMMON);  

    alm_tm-&gt;tm_mday = readb(base + S3C2410_ALMDATE);  

    alm_tm-&gt;tm_year = readb(base + S3C2410_ALMYEAR);  

    alm_en = readb(base + S3C2410_RTCALM);  

    /*根据RTCALM寄存器的报警全局使能位来设置报警状态结构rtc_wkalrm*/  

    alrm-&gt;enabled = (alm_en &amp; S3C2410_RTCALM_ALMEN) ? 1 : 0;  

    pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",  

         alm_en,  

         alm_tm-&gt;tm_year, alm_tm-&gt;tm_mon, alm_tm-&gt;tm_mday,  

         alm_tm-&gt;tm_hour, alm_tm-&gt;tm_min, alm_tm-&gt;tm_sec);  

    /* decode the alarm enable field */  

    /*如果RTCALM寄存器的秒使能,则将rtc_wkalrm中存放的秒数据由BCD格式转换为BIN格式,否则设置为0xff*/  

    if (alm_en &amp; S3C2410_RTCALM_SECEN)  

        alm_tm-&gt;tm_sec = bcd2bin(alm_tm-&gt;tm_sec);  

    else  

        alm_tm-&gt;tm_sec = 0xff;  

    /*如果RTCALM寄存器的分钟使能,则将rtc_wkalrm中存放的分钟数据由BCD格式转换为BIN格式,否则设置为0xff*/  

    if (alm_en &amp; S3C2410_RTCALM_MINEN)  

        alm_tm-&gt;tm_min = bcd2bin(alm_tm-&gt;tm_min);  

        alm_tm-&gt;tm_min = 0xff;  

    /*如果RTCALM寄存器的小时使能,则将rtc_wkalrm中存放的小时数据由BCD格式转换为BIN格式,否则设置为0xff*/  

    if (alm_en &amp; S3C2410_RTCALM_HOUREN)  

        alm_tm-&gt;tm_hour = bcd2bin(alm_tm-&gt;tm_hour);  

        alm_tm-&gt;tm_hour = 0xff;  

    /*如果RTCALM寄存器的日使能,则将rtc_wkalrm中存放的日数据由BCD格式转换为BIN格式,否则设置为0xff*/  

    if (alm_en &amp; S3C2410_RTCALM_DAYEN)  

        alm_tm-&gt;tm_mday = bcd2bin(alm_tm-&gt;tm_mday);  

        alm_tm-&gt;tm_mday = 0xff;  

    /*如果RTCALM寄存器的月使能,则将rtc_wkalrm中存放的月数据由BCD格式转换为BIN格式,否则设置为0xff*/  

    if (alm_en &amp; S3C2410_RTCALM_MONEN) {  

        alm_tm-&gt;tm_mon = bcd2bin(alm_tm-&gt;tm_mon);  

        alm_tm-&gt;tm_mon -= 1;  

        alm_tm-&gt;tm_mon = 0xff;  

    /*如果RTCALM寄存器的年使能,则将rtc_wkalrm中存放的年数据由BCD格式转换为BIN格式,否则设置为0xff*/  

    if (alm_en &amp; S3C2410_RTCALM_YEAREN)  

        alm_tm-&gt;tm_year = bcd2bin(alm_tm-&gt;tm_year);  

        alm_tm-&gt;tm_year = 0xffff;  

设置报警时间的值:

static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)  

    struct rtc_time *tm = &amp;alrm-&gt;time;  

    unsigned int alrm_en;  

    pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",  

         alrm-&gt;enabled,  

         tm-&gt;tm_mday &amp; 0xff, tm-&gt;tm_mon &amp; 0xff, tm-&gt;tm_year &amp; 0xff,  

         tm-&gt;tm_hour &amp; 0xff, tm-&gt;tm_min &amp; 0xff, tm-&gt;tm_sec);  

    /*读取RTCALM寄存器的全局使能位,关闭所有报警使能*/  

    alrm_en = readb(base + S3C2410_RTCALM) &amp; S3C2410_RTCALM_ALMEN;  

    writeb(0x00, base + S3C2410_RTCALM);  

    /*如果秒时间在合理范围内,则使能秒报警位,将报警状态寄存器中封装的time的秒位由BIN格式转换为BCD,写入秒报警寄存器中*/  

    if (tm-&gt;tm_sec &lt; 60 &amp;&amp; tm-&gt;tm_sec &gt;= 0) {  

        alrm_en |= S3C2410_RTCALM_SECEN;  

        writeb(bin2bcd(tm-&gt;tm_sec), base + S3C2410_ALMSEC);  

    /*如果分钟时间在合理范围内,则使能分钟报警位,将报警状态寄存器中封装的time的分钟位由BIN格式转换为BCD,写入分钟报警寄存器中*/  

    if (tm-&gt;tm_min &lt; 60 &amp;&amp; tm-&gt;tm_min &gt;= 0) {  

        alrm_en |= S3C2410_RTCALM_MINEN;  

        writeb(bin2bcd(tm-&gt;tm_min), base + S3C2410_ALMMIN);  

    /*如果小时时间在合理范围内,则使能小时报警位,将报警状态寄存器中封装的time的小时位由BIN格式转换为BCD,写入小时报警寄存器中*/  

    if (tm-&gt;tm_hour &lt; 24 &amp;&amp; tm-&gt;tm_hour &gt;= 0) {  

        alrm_en |= S3C2410_RTCALM_HOUREN;  

        writeb(bin2bcd(tm-&gt;tm_hour), base + S3C2410_ALMHOUR);  

    pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);  

    /*使能RTCALM寄存器全局报警位*/  

    writeb(alrm_en, base + S3C2410_RTCALM);  

    s3c_rtc_setaie(alrm-&gt;enabled);  

    /*根据全局报警使能的状态来决定是唤醒RTC报警中断还是睡眠RTC报警中断*/  

    if (alrm-&gt;enabled)  

        enable_irq_wake(s3c_rtc_alarmno);  

        disable_irq_wake(s3c_rtc_alarmno);  

下面来分析一下是怎样获取和设置时间的:

通过用户空间的ioctl,在rtc-dev.c中实现了rtc_dev_ioctl,其中获取和设置时间如下:

case RTC_RD_TIME:  

        mutex_unlock(&amp;rtc-&gt;ops_lock);  

        err = rtc_read_time(rtc, &amp;tm);  

        if (err &lt; 0)  

            return err;  

        if (copy_to_user(uarg, &amp;tm, sizeof(tm)))  

            err = -EFAULT;  

        return err;  

    case RTC_SET_TIME:  

        if (copy_from_user(&amp;tm, uarg, sizeof(tm)))  

            return -EFAULT;  

        return rtc_set_time(rtc, &amp;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)  

    err = mutex_lock_interruptible(&amp;rtc-&gt;ops_lock);  

    if (!rtc-&gt;ops)  

        err = -ENODEV;  

    else if (!rtc-&gt;ops-&gt;read_time)  

        err = -EINVAL;  

    else {  

        memset(tm, 0, sizeof(struct rtc_time));  

        err = rtc-&gt;ops-&gt;read_time(rtc-&gt;dev.parent, tm);  

    mutex_unlock(&amp;rtc-&gt;ops_lock);  

    return err;  

int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)  

    err = rtc_valid_tm(tm);  

    if (err != 0)  

    else if (rtc-&gt;ops-&gt;set_time)  

        err = rtc-&gt;ops-&gt;set_time(rtc-&gt;dev.parent, tm);  

    else if (rtc-&gt;ops-&gt;set_mmss) {  

        unsigned long secs;  

        err = rtc_tm_to_time(tm, &amp;secs);  

        if (err == 0)  

            err = rtc-&gt;ops-&gt;set_mmss(rtc-&gt;dev.parent, secs);  

    } else  

可以看出他们调用了具体RTC设备驱动中的read_time和set_time函数,对应了s3c2410中的s3c_rtc_gettime和s3c_rtc_settime,这里使用的rtc_tm_to_time函数实现在rtclib.c中,/drivers/rtc/interface.c定义了可供其它模块访问的接口。

本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/5294484.html,如需转载请自行联系原作者

继续阅读