blob: ad66c6ecf36533d339573106f4bd17db05305e26 [file] [log] [blame]
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08001/*
2 * RTC subsystem, interface functions
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * based on arch/arm/common/rtctime.c
8 *
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.
12*/
13
14#include <linux/rtc.h>
15
David Brownellab6a2d72007-05-08 00:33:30 -070016int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080017{
18 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080019
20 err = mutex_lock_interruptible(&rtc->ops_lock);
21 if (err)
22 return -EBUSY;
23
24 if (!rtc->ops)
25 err = -ENODEV;
26 else if (!rtc->ops->read_time)
27 err = -EINVAL;
28 else {
29 memset(tm, 0, sizeof(struct rtc_time));
David Brownellcd966202007-05-08 00:33:40 -070030 err = rtc->ops->read_time(rtc->dev.parent, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080031 }
32
33 mutex_unlock(&rtc->ops_lock);
34 return err;
35}
36EXPORT_SYMBOL_GPL(rtc_read_time);
37
David Brownellab6a2d72007-05-08 00:33:30 -070038int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080039{
40 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080041
42 err = rtc_valid_tm(tm);
43 if (err != 0)
44 return err;
45
46 err = mutex_lock_interruptible(&rtc->ops_lock);
47 if (err)
48 return -EBUSY;
49
50 if (!rtc->ops)
51 err = -ENODEV;
52 else if (!rtc->ops->set_time)
53 err = -EINVAL;
54 else
David Brownellcd966202007-05-08 00:33:40 -070055 err = rtc->ops->set_time(rtc->dev.parent, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080056
57 mutex_unlock(&rtc->ops_lock);
58 return err;
59}
60EXPORT_SYMBOL_GPL(rtc_set_time);
61
David Brownellab6a2d72007-05-08 00:33:30 -070062int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080063{
64 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080065
66 err = mutex_lock_interruptible(&rtc->ops_lock);
67 if (err)
68 return -EBUSY;
69
70 if (!rtc->ops)
71 err = -ENODEV;
72 else if (rtc->ops->set_mmss)
David Brownellcd966202007-05-08 00:33:40 -070073 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080074 else if (rtc->ops->read_time && rtc->ops->set_time) {
75 struct rtc_time new, old;
76
David Brownellcd966202007-05-08 00:33:40 -070077 err = rtc->ops->read_time(rtc->dev.parent, &old);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080078 if (err == 0) {
79 rtc_time_to_tm(secs, &new);
80
81 /*
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.
86 */
87 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
88 (new.tm_hour == 23 && new.tm_min == 59)))
David Brownellcd966202007-05-08 00:33:40 -070089 err = rtc->ops->set_time(rtc->dev.parent,
David Brownellab6a2d72007-05-08 00:33:30 -070090 &new);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080091 }
92 }
93 else
94 err = -EINVAL;
95
96 mutex_unlock(&rtc->ops_lock);
97
98 return err;
99}
100EXPORT_SYMBOL_GPL(rtc_set_mmss);
101
David Brownellab6a2d72007-05-08 00:33:30 -0700102int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800103{
104 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800105
106 err = mutex_lock_interruptible(&rtc->ops_lock);
107 if (err)
108 return -EBUSY;
109
110 if (rtc->ops == NULL)
111 err = -ENODEV;
112 else if (!rtc->ops->read_alarm)
113 err = -EINVAL;
114 else {
115 memset(alarm, 0, sizeof(struct rtc_wkalrm));
David Brownellcd966202007-05-08 00:33:40 -0700116 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800117 }
118
119 mutex_unlock(&rtc->ops_lock);
120 return err;
121}
122EXPORT_SYMBOL_GPL(rtc_read_alarm);
123
David Brownellab6a2d72007-05-08 00:33:30 -0700124int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800125{
126 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800127
David Brownellf8245c22007-05-08 00:34:07 -0700128 err = rtc_valid_tm(&alarm->time);
129 if (err != 0)
130 return err;
131
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800132 err = mutex_lock_interruptible(&rtc->ops_lock);
133 if (err)
134 return -EBUSY;
135
136 if (!rtc->ops)
137 err = -ENODEV;
138 else if (!rtc->ops->set_alarm)
139 err = -EINVAL;
140 else
David Brownellcd966202007-05-08 00:33:40 -0700141 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800142
143 mutex_unlock(&rtc->ops_lock);
144 return err;
145}
146EXPORT_SYMBOL_GPL(rtc_set_alarm);
147
David Brownelld728b1e2006-11-25 11:09:28 -0800148/**
149 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
David Brownellab6a2d72007-05-08 00:33:30 -0700150 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800151 * @num: how many irqs are being reported (usually one)
152 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
153 * Context: in_interrupt(), irqs blocked
154 */
David Brownellab6a2d72007-05-08 00:33:30 -0700155void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800156 unsigned long num, unsigned long events)
157{
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800158 spin_lock(&rtc->irq_lock);
159 rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
160 spin_unlock(&rtc->irq_lock);
161
162 spin_lock(&rtc->irq_task_lock);
163 if (rtc->irq_task)
164 rtc->irq_task->func(rtc->irq_task->private_data);
165 spin_unlock(&rtc->irq_task_lock);
166
167 wake_up_interruptible(&rtc->irq_queue);
168 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
169}
170EXPORT_SYMBOL_GPL(rtc_update_irq);
171
David Brownellab6a2d72007-05-08 00:33:30 -0700172struct rtc_device *rtc_class_open(char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800173{
David Brownellcd966202007-05-08 00:33:40 -0700174 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700175 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800176
177 down(&rtc_class->sem);
David Brownellcd966202007-05-08 00:33:40 -0700178 list_for_each_entry(dev, &rtc_class->devices, node) {
179 if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0) {
180 dev = get_device(dev);
181 if (dev)
182 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800183 break;
184 }
185 }
186
David Brownellab6a2d72007-05-08 00:33:30 -0700187 if (rtc) {
188 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700189 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700190 rtc = NULL;
191 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800192 }
193 up(&rtc_class->sem);
194
David Brownellab6a2d72007-05-08 00:33:30 -0700195 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800196}
197EXPORT_SYMBOL_GPL(rtc_class_open);
198
David Brownellab6a2d72007-05-08 00:33:30 -0700199void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800200{
David Brownellab6a2d72007-05-08 00:33:30 -0700201 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700202 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800203}
204EXPORT_SYMBOL_GPL(rtc_class_close);
205
David Brownellab6a2d72007-05-08 00:33:30 -0700206int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800207{
208 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800209
210 if (task == NULL || task->func == NULL)
211 return -EINVAL;
212
David Brownelld728b1e2006-11-25 11:09:28 -0800213 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800214 if (rtc->irq_task == NULL) {
215 rtc->irq_task = task;
216 retval = 0;
217 }
David Brownelld728b1e2006-11-25 11:09:28 -0800218 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800219
220 return retval;
221}
222EXPORT_SYMBOL_GPL(rtc_irq_register);
223
David Brownellab6a2d72007-05-08 00:33:30 -0700224void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800225{
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800226
David Brownelld728b1e2006-11-25 11:09:28 -0800227 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800228 if (rtc->irq_task == task)
229 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800230 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800231}
232EXPORT_SYMBOL_GPL(rtc_irq_unregister);
233
David Brownellab6a2d72007-05-08 00:33:30 -0700234int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800235{
236 int err = 0;
237 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800238
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700239 if (rtc->ops->irq_set_state == NULL)
240 return -ENXIO;
241
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800242 spin_lock_irqsave(&rtc->irq_task_lock, flags);
243 if (rtc->irq_task != task)
244 err = -ENXIO;
245 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
246
247 if (err == 0)
David Brownellcd966202007-05-08 00:33:40 -0700248 err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800249
250 return err;
251}
252EXPORT_SYMBOL_GPL(rtc_irq_set_state);
253
David Brownellab6a2d72007-05-08 00:33:30 -0700254int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800255{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700256 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800257 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800258
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700259 if (rtc->ops->irq_set_freq == NULL)
260 return -ENXIO;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800261
262 spin_lock_irqsave(&rtc->irq_task_lock, flags);
263 if (rtc->irq_task != task)
264 err = -ENXIO;
265 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
266
267 if (err == 0) {
David Brownellcd966202007-05-08 00:33:40 -0700268 err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800269 if (err == 0)
270 rtc->irq_freq = freq;
271 }
272 return err;
273}
David Brownell2601a462006-11-25 11:09:27 -0800274EXPORT_SYMBOL_GPL(rtc_irq_set_freq);