]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/rtc/interface.c
RTC: periodic irq fix
[mirror_ubuntu-bionic-kernel.git] / drivers / rtc / interface.c
CommitLineData
0c86edc0
AZ
1/*
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
ab6a2d70 16int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
0c86edc0
AZ
17{
18 int err;
0c86edc0
AZ
19
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));
cd966209 30 err = rtc->ops->read_time(rtc->dev.parent, tm);
0c86edc0
AZ
31 }
32
33 mutex_unlock(&rtc->ops_lock);
34 return err;
35}
36EXPORT_SYMBOL_GPL(rtc_read_time);
37
ab6a2d70 38int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
0c86edc0
AZ
39{
40 int err;
0c86edc0
AZ
41
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
cd966209 55 err = rtc->ops->set_time(rtc->dev.parent, tm);
0c86edc0
AZ
56
57 mutex_unlock(&rtc->ops_lock);
58 return err;
59}
60EXPORT_SYMBOL_GPL(rtc_set_time);
61
ab6a2d70 62int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
0c86edc0
AZ
63{
64 int err;
0c86edc0
AZ
65
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)
cd966209 73 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
0c86edc0
AZ
74 else if (rtc->ops->read_time && rtc->ops->set_time) {
75 struct rtc_time new, old;
76
cd966209 77 err = rtc->ops->read_time(rtc->dev.parent, &old);
0c86edc0
AZ
78 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)))
cd966209 89 err = rtc->ops->set_time(rtc->dev.parent,
ab6a2d70 90 &new);
0c86edc0
AZ
91 }
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
ab6a2d70 102int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0c86edc0
AZ
103{
104 int err;
0c86edc0
AZ
105
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));
cd966209 116 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
0c86edc0
AZ
117 }
118
119 mutex_unlock(&rtc->ops_lock);
120 return err;
121}
122EXPORT_SYMBOL_GPL(rtc_read_alarm);
123
ab6a2d70 124int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
0c86edc0
AZ
125{
126 int err;
0c86edc0 127
f8245c26
DB
128 err = rtc_valid_tm(&alarm->time);
129 if (err != 0)
130 return err;
131
0c86edc0
AZ
132 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
cd966209 141 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
0c86edc0
AZ
142
143 mutex_unlock(&rtc->ops_lock);
144 return err;
145}
146EXPORT_SYMBOL_GPL(rtc_set_alarm);
147
d728b1e6
DB
148/**
149 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
ab6a2d70 150 * @rtc: the rtc device
d728b1e6
DB
151 * @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 */
ab6a2d70 155void rtc_update_irq(struct rtc_device *rtc,
0c86edc0
AZ
156 unsigned long num, unsigned long events)
157{
0c86edc0
AZ
158 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
ab6a2d70 172struct rtc_device *rtc_class_open(char *name)
0c86edc0 173{
cd966209 174 struct device *dev;
ab6a2d70 175 struct rtc_device *rtc = NULL;
0c86edc0
AZ
176
177 down(&rtc_class->sem);
cd966209
DB
178 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);
0c86edc0
AZ
183 break;
184 }
185 }
186
ab6a2d70
DB
187 if (rtc) {
188 if (!try_module_get(rtc->owner)) {
cd966209 189 put_device(dev);
ab6a2d70
DB
190 rtc = NULL;
191 }
0c86edc0
AZ
192 }
193 up(&rtc_class->sem);
194
ab6a2d70 195 return rtc;
0c86edc0
AZ
196}
197EXPORT_SYMBOL_GPL(rtc_class_open);
198
ab6a2d70 199void rtc_class_close(struct rtc_device *rtc)
0c86edc0 200{
ab6a2d70 201 module_put(rtc->owner);
cd966209 202 put_device(&rtc->dev);
0c86edc0
AZ
203}
204EXPORT_SYMBOL_GPL(rtc_class_close);
205
ab6a2d70 206int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
0c86edc0
AZ
207{
208 int retval = -EBUSY;
0c86edc0
AZ
209
210 if (task == NULL || task->func == NULL)
211 return -EINVAL;
212
d691eb90
AZ
213 /* Cannot register while the char dev is in use */
214 if (!(mutex_trylock(&rtc->char_lock)))
215 return -EBUSY;
216
d728b1e6 217 spin_lock_irq(&rtc->irq_task_lock);
0c86edc0
AZ
218 if (rtc->irq_task == NULL) {
219 rtc->irq_task = task;
220 retval = 0;
221 }
d728b1e6 222 spin_unlock_irq(&rtc->irq_task_lock);
0c86edc0 223
d691eb90
AZ
224 mutex_unlock(&rtc->char_lock);
225
0c86edc0
AZ
226 return retval;
227}
228EXPORT_SYMBOL_GPL(rtc_irq_register);
229
ab6a2d70 230void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
0c86edc0 231{
d728b1e6 232 spin_lock_irq(&rtc->irq_task_lock);
0c86edc0
AZ
233 if (rtc->irq_task == task)
234 rtc->irq_task = NULL;
d728b1e6 235 spin_unlock_irq(&rtc->irq_task_lock);
0c86edc0
AZ
236}
237EXPORT_SYMBOL_GPL(rtc_irq_unregister);
238
ab6a2d70 239int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
0c86edc0
AZ
240{
241 int err = 0;
242 unsigned long flags;
0c86edc0 243
56f10c63
AZ
244 if (rtc->ops->irq_set_state == NULL)
245 return -ENXIO;
246
0c86edc0 247 spin_lock_irqsave(&rtc->irq_task_lock, flags);
d691eb90
AZ
248 if (rtc->irq_task != NULL && task == NULL)
249 err = -EBUSY;
0c86edc0 250 if (rtc->irq_task != task)
d691eb90 251 err = -EACCES;
0c86edc0
AZ
252 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
253
254 if (err == 0)
cd966209 255 err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
0c86edc0
AZ
256
257 return err;
258}
259EXPORT_SYMBOL_GPL(rtc_irq_set_state);
260
ab6a2d70 261int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
0c86edc0 262{
56f10c63 263 int err = 0;
0c86edc0 264 unsigned long flags;
0c86edc0 265
56f10c63
AZ
266 if (rtc->ops->irq_set_freq == NULL)
267 return -ENXIO;
0c86edc0
AZ
268
269 spin_lock_irqsave(&rtc->irq_task_lock, flags);
d691eb90
AZ
270 if (rtc->irq_task != NULL && task == NULL)
271 err = -EBUSY;
0c86edc0 272 if (rtc->irq_task != task)
d691eb90 273 err = -EACCES;
0c86edc0
AZ
274 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
275
276 if (err == 0) {
cd966209 277 err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
0c86edc0
AZ
278 if (err == 0)
279 rtc->irq_freq = freq;
280 }
281 return err;
282}
2601a464 283EXPORT_SYMBOL_GPL(rtc_irq_set_freq);