转自: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;
- 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;
-
- 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;
- unsigned char pending;
- struct rtc_time time;
- };
- struct rtc_class_ops {
- int (*open)(struct device *);
- void (*release)(struct device *);
- 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_mmss)(struct device *, unsigned long secs);
- 前提是定义好了read_time/set_time,因为RTC框架需要用这两个函数来实现这个功能。
- int (*irq_set_state)(struct device *, int 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);
- int (*update_irq_enable)(struct device *, unsigned int 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->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\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(&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,
- .remove = __devexit_p(s3c_rtc_remove),
- .suspend = s3c_rtc_suspend,
- .resume = s3c_rtc_resume,
- .driver = {
- .name = "s3c2410-rtc",
- .owner = THIS_MODULE,
- },
- };
在arch/arm/plat-s3c24xx/devs.c中定义了rtc的platform_device:
- static struct resource s3c_rtc_resource[] = {
- [0] = {
- .start = S3C24XX_PA_RTC,
- .end = S3C24XX_PA_RTC + 0xff,
- .flags = IORESOURCE_MEM,
- },
- [1] = {
- .start = IRQ_RTC,
- .end = IRQ_RTC,
- .flags = IORESOURCE_IRQ,
- },
- [2] = {
- .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);
-
-
- s3c_rtc_tickno = platform_get_irq(pdev, 1);
- if (s3c_rtc_tickno < 0) {
- dev_err(&pdev->dev, "no irq for rtc tick\n");
- return -ENOENT;
- }
-
- s3c_rtc_alarmno = platform_get_irq(pdev, 0);
- if (s3c_rtc_alarmno < 0) {
- dev_err(&pdev->dev, "no irq for alarm\n");
- return -ENOENT;
- }
- pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
- s3c_rtc_tickno, s3c_rtc_alarmno);
-
-
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (res == NULL) {
- dev_err(&pdev->dev, "failed to get memory region resource\n");
- return -ENOENT;
- }
-
- 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\n");
- ret = -ENOENT;
- goto err_nores;
- }
-
- s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
- if (s3c_rtc_base == NULL) {
- dev_err(&pdev->dev, "failed ioremap()\n");
- ret = -EINVAL;
- goto err_nomap;
- }
-
-
- s3c_rtc_enable(pdev, 1);
- pr_debug("s3c2410_rtc: RTCCON=%02x\n",
- readb(s3c_rtc_base + S3C2410_RTCCON));
-
- s3c_rtc_setfreq(&pdev->dev, 1);
-
- device_init_wakeup(&pdev->dev, 1);
-
-
- rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
- THIS_MODULE);
- if (IS_ERR(rtc)) {
- dev_err(&pdev->dev, "cannot attach rtc\n");
- ret = PTR_ERR(rtc);
- goto err_nortc;
- }
-
- rtc->max_user_freq = 128;
-
- 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;
-
- if (idr_pre_get(&rtc_idr, GFP_KERNEL) == 0) {
- err = -ENOMEM;
- goto exit;
- }
- mutex_lock(&idr_lock);
-
- err = idr_get_new(&rtc_idr, NULL, &id);
- mutex_unlock(&idr_lock);
- if (err < 0)
- goto exit;
- id = id & MAX_ID_MASK;
-
- 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_prepare(rtc);
-
- err = device_register(&rtc->dev);
- if (err)
- goto exit_kfree;
-
- rtc_dev_add_device(rtc);
-
- rtc_sysfs_add_device(rtc);
-
- rtc_proc_add_device(rtc);
- dev_info(dev, "rtc core: registered %s as %s\n",
- 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\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;
-
- 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 {
-
-
- if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
- dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
-
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
- }
-
- if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
- dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
- }
-
- if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
- dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
- tmp = readb(base + S3C2410_RTCCON);
- writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
- }
- }
- }
- static int __devexit s3c_rtc_remove(struct platform_device *dev)
- {
-
- struct rtc_device *rtc = platform_get_drvdata(dev);
-
- platform_set_drvdata(dev, NULL);
-
- rtc_device_unregister(rtc);
-
- s3c_rtc_setpie(&dev->dev, 0);
-
- s3c_rtc_setaie(0);
-
- iounmap(s3c_rtc_base);
-
- release_resource(s3c_rtc_mem);
-
- kfree(s3c_rtc_mem);
- return 0;
- }
这里是电源管理部分,在挂起时保存TICNT的值,并禁止RTCCON,TICNT;在休眠的时候开启RTCCON,并恢复TICNT的值。
- #ifdef CONFIG_PM
- static int ticnt_save;
- static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
- {
-
- 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)
- {
-
- struct platform_device *pdev = to_platform_device(dev);
- struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
- int ret;
-
- 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;
- }
-
- 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\n", 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);
-
- 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\n", __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\n", __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;
- }
- static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
- {
- unsigned int have_retried = 0;
-
- void __iomem *base = s3c_rtc_base;
- retry_get_time:
-
- 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);
-
-
- 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\n",
- rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
- rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
-
- 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\n",
- tm->tm_year, tm->tm_mon, tm->tm_mday,
- tm->tm_hour, tm->tm_min, tm->tm_sec);
-
-
- if (year < 0 || year >= 100) {
- dev_err(dev, "rtc only supports 100 years\n");
- return -EINVAL;
- }
-
- 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;
-
- 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);
-
- alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
- pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
- 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);
-
-
-
- if (alm_en & S3C2410_RTCALM_SECEN)
- alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
- else
- alm_tm->tm_sec = 0xff;
-
- if (alm_en & S3C2410_RTCALM_MINEN)
- alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
- else
- alm_tm->tm_min = 0xff;
-
- if (alm_en & S3C2410_RTCALM_HOUREN)
- alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
- else
- alm_tm->tm_hour = 0xff;
-
- if (alm_en & S3C2410_RTCALM_DAYEN)
- alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
- else
- alm_tm->tm_mday = 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;
- }
-
- 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\n",
- 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);
-
- alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
- writeb(0x00, base + S3C2410_RTCALM);
-
- if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
- alrm_en |= S3C2410_RTCALM_SECEN;
- writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
- }
-
- if (tm->tm_min < 60 && tm->tm_min >= 0) {
- alrm_en |= S3C2410_RTCALM_MINEN;
- writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
- }
-
- 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\n", alrm_en);
-
- writeb(alrm_en, base + S3C2410_RTCALM);
-
- s3c_rtc_setaie(alrm->enabled);
-
- 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定义了可供其它模块访问的接口。