2 * RTC subsystem, interface functions
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * based on arch/arm/common/rtctime.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/rtc.h>
16 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
20 err = mutex_lock_interruptible(&rtc->ops_lock);
26 else if (!rtc->ops->read_time)
29 memset(tm, 0, sizeof(struct rtc_time));
30 err = rtc->ops->read_time(rtc->class_dev.dev, tm);
33 mutex_unlock(&rtc->ops_lock);
36 EXPORT_SYMBOL_GPL(rtc_read_time);
38 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
42 err = rtc_valid_tm(tm);
46 err = mutex_lock_interruptible(&rtc->ops_lock);
52 else if (!rtc->ops->set_time)
55 err = rtc->ops->set_time(rtc->class_dev.dev, tm);
57 mutex_unlock(&rtc->ops_lock);
60 EXPORT_SYMBOL_GPL(rtc_set_time);
62 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
66 err = mutex_lock_interruptible(&rtc->ops_lock);
72 else if (rtc->ops->set_mmss)
73 err = rtc->ops->set_mmss(rtc->class_dev.dev, secs);
74 else if (rtc->ops->read_time && rtc->ops->set_time) {
75 struct rtc_time new, old;
77 err = rtc->ops->read_time(rtc->class_dev.dev, &old);
79 rtc_time_to_tm(secs, &new);
82 * avoid writing when we're going to change the day of
83 * the month. We will retry in the next minute. This
84 * basically means that if the RTC must not drift
85 * by more than 1 minute in 11 minutes.
87 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
88 (new.tm_hour == 23 && new.tm_min == 59)))
89 err = rtc->ops->set_time(rtc->class_dev.dev,
96 mutex_unlock(&rtc->ops_lock);
100 EXPORT_SYMBOL_GPL(rtc_set_mmss);
102 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
106 err = mutex_lock_interruptible(&rtc->ops_lock);
110 if (rtc->ops == NULL)
112 else if (!rtc->ops->read_alarm)
115 memset(alarm, 0, sizeof(struct rtc_wkalrm));
116 err = rtc->ops->read_alarm(rtc->class_dev.dev, alarm);
119 mutex_unlock(&rtc->ops_lock);
122 EXPORT_SYMBOL_GPL(rtc_read_alarm);
124 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
128 err = mutex_lock_interruptible(&rtc->ops_lock);
134 else if (!rtc->ops->set_alarm)
137 err = rtc->ops->set_alarm(rtc->class_dev.dev, alarm);
139 mutex_unlock(&rtc->ops_lock);
142 EXPORT_SYMBOL_GPL(rtc_set_alarm);
145 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
146 * @rtc: the rtc device
147 * @num: how many irqs are being reported (usually one)
148 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
149 * Context: in_interrupt(), irqs blocked
151 void rtc_update_irq(struct rtc_device *rtc,
152 unsigned long num, unsigned long events)
154 spin_lock(&rtc->irq_lock);
155 rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
156 spin_unlock(&rtc->irq_lock);
158 spin_lock(&rtc->irq_task_lock);
160 rtc->irq_task->func(rtc->irq_task->private_data);
161 spin_unlock(&rtc->irq_task_lock);
163 wake_up_interruptible(&rtc->irq_queue);
164 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
166 EXPORT_SYMBOL_GPL(rtc_update_irq);
168 struct rtc_device *rtc_class_open(char *name)
170 struct class_device *class_dev_tmp;
171 struct rtc_device *rtc = NULL;
173 down(&rtc_class->sem);
174 list_for_each_entry(class_dev_tmp, &rtc_class->children, node) {
175 if (strncmp(class_dev_tmp->class_id, name, BUS_ID_SIZE) == 0) {
176 class_dev_tmp = class_device_get(class_dev_tmp);
178 rtc = to_rtc_device(class_dev_tmp);
184 if (!try_module_get(rtc->owner)) {
185 class_device_put(class_dev_tmp);
193 EXPORT_SYMBOL_GPL(rtc_class_open);
195 void rtc_class_close(struct rtc_device *rtc)
197 module_put(rtc->owner);
198 class_device_put(&rtc->class_dev);
200 EXPORT_SYMBOL_GPL(rtc_class_close);
202 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
206 if (task == NULL || task->func == NULL)
209 spin_lock_irq(&rtc->irq_task_lock);
210 if (rtc->irq_task == NULL) {
211 rtc->irq_task = task;
214 spin_unlock_irq(&rtc->irq_task_lock);
218 EXPORT_SYMBOL_GPL(rtc_irq_register);
220 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
223 spin_lock_irq(&rtc->irq_task_lock);
224 if (rtc->irq_task == task)
225 rtc->irq_task = NULL;
226 spin_unlock_irq(&rtc->irq_task_lock);
228 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
230 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
235 if (rtc->ops->irq_set_state == NULL)
238 spin_lock_irqsave(&rtc->irq_task_lock, flags);
239 if (rtc->irq_task != task)
241 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
244 err = rtc->ops->irq_set_state(rtc->class_dev.dev, enabled);
248 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
250 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
255 if (rtc->ops->irq_set_freq == NULL)
258 spin_lock_irqsave(&rtc->irq_task_lock, flags);
259 if (rtc->irq_task != task)
261 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
264 err = rtc->ops->irq_set_freq(rtc->class_dev.dev, freq);
266 rtc->irq_freq = freq;
270 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);