Commit | Line | Data |
---|---|---|
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> | |
d43c36dc | 15 | #include <linux/sched.h> |
2113852b | 16 | #include <linux/module.h> |
97144c67 | 17 | #include <linux/log2.h> |
6610e089 | 18 | #include <linux/workqueue.h> |
0c86edc0 | 19 | |
aa0be0f4 JS |
20 | static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer); |
21 | static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer); | |
22 | ||
6610e089 | 23 | static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
0c86edc0 AZ |
24 | { |
25 | int err; | |
0c86edc0 AZ |
26 | if (!rtc->ops) |
27 | err = -ENODEV; | |
28 | else if (!rtc->ops->read_time) | |
29 | err = -EINVAL; | |
30 | else { | |
31 | memset(tm, 0, sizeof(struct rtc_time)); | |
cd966209 | 32 | err = rtc->ops->read_time(rtc->dev.parent, tm); |
16682c86 HG |
33 | if (err < 0) { |
34 | dev_err(&rtc->dev, "read_time: fail to read\n"); | |
35 | return err; | |
36 | } | |
37 | ||
38 | err = rtc_valid_tm(tm); | |
39 | if (err < 0) | |
40 | dev_err(&rtc->dev, "read_time: rtc_time isn't valid\n"); | |
0c86edc0 | 41 | } |
6610e089 JS |
42 | return err; |
43 | } | |
44 | ||
45 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) | |
46 | { | |
47 | int err; | |
0c86edc0 | 48 | |
6610e089 JS |
49 | err = mutex_lock_interruptible(&rtc->ops_lock); |
50 | if (err) | |
51 | return err; | |
52 | ||
53 | err = __rtc_read_time(rtc, tm); | |
0c86edc0 AZ |
54 | mutex_unlock(&rtc->ops_lock); |
55 | return err; | |
56 | } | |
57 | EXPORT_SYMBOL_GPL(rtc_read_time); | |
58 | ||
ab6a2d70 | 59 | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
0c86edc0 AZ |
60 | { |
61 | int err; | |
0c86edc0 AZ |
62 | |
63 | err = rtc_valid_tm(tm); | |
64 | if (err != 0) | |
65 | return err; | |
66 | ||
67 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
68 | if (err) | |
b68bb263 | 69 | return err; |
0c86edc0 AZ |
70 | |
71 | if (!rtc->ops) | |
72 | err = -ENODEV; | |
bbccf83f | 73 | else if (rtc->ops->set_time) |
cd966209 | 74 | err = rtc->ops->set_time(rtc->dev.parent, tm); |
8e4ff1a8 XP |
75 | else if (rtc->ops->set_mmss64) { |
76 | time64_t secs64 = rtc_tm_to_time64(tm); | |
77 | ||
78 | err = rtc->ops->set_mmss64(rtc->dev.parent, secs64); | |
79 | } else if (rtc->ops->set_mmss) { | |
bc10aa93 XP |
80 | time64_t secs64 = rtc_tm_to_time64(tm); |
81 | err = rtc->ops->set_mmss(rtc->dev.parent, secs64); | |
bbccf83f AZ |
82 | } else |
83 | err = -EINVAL; | |
0c86edc0 | 84 | |
14d0e347 | 85 | pm_stay_awake(rtc->dev.parent); |
0c86edc0 | 86 | mutex_unlock(&rtc->ops_lock); |
5f9679d2 N |
87 | /* A timer might have just expired */ |
88 | schedule_work(&rtc->irqwork); | |
0c86edc0 AZ |
89 | return err; |
90 | } | |
91 | EXPORT_SYMBOL_GPL(rtc_set_time); | |
92 | ||
ab6a2d70 | 93 | int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs) |
0c86edc0 AZ |
94 | { |
95 | int err; | |
0c86edc0 AZ |
96 | |
97 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
98 | if (err) | |
b68bb263 | 99 | return err; |
0c86edc0 AZ |
100 | |
101 | if (!rtc->ops) | |
102 | err = -ENODEV; | |
8e4ff1a8 XP |
103 | else if (rtc->ops->set_mmss64) |
104 | err = rtc->ops->set_mmss64(rtc->dev.parent, secs); | |
0c86edc0 | 105 | else if (rtc->ops->set_mmss) |
cd966209 | 106 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
0c86edc0 AZ |
107 | else if (rtc->ops->read_time && rtc->ops->set_time) { |
108 | struct rtc_time new, old; | |
109 | ||
cd966209 | 110 | err = rtc->ops->read_time(rtc->dev.parent, &old); |
0c86edc0 | 111 | if (err == 0) { |
bc10aa93 | 112 | rtc_time64_to_tm(secs, &new); |
0c86edc0 AZ |
113 | |
114 | /* | |
115 | * avoid writing when we're going to change the day of | |
116 | * the month. We will retry in the next minute. This | |
117 | * basically means that if the RTC must not drift | |
118 | * by more than 1 minute in 11 minutes. | |
119 | */ | |
120 | if (!((old.tm_hour == 23 && old.tm_min == 59) || | |
121 | (new.tm_hour == 23 && new.tm_min == 59))) | |
cd966209 | 122 | err = rtc->ops->set_time(rtc->dev.parent, |
ab6a2d70 | 123 | &new); |
0c86edc0 | 124 | } |
3ff2e13c | 125 | } else { |
0c86edc0 | 126 | err = -EINVAL; |
3ff2e13c | 127 | } |
0c86edc0 | 128 | |
14d0e347 | 129 | pm_stay_awake(rtc->dev.parent); |
0c86edc0 | 130 | mutex_unlock(&rtc->ops_lock); |
5f9679d2 N |
131 | /* A timer might have just expired */ |
132 | schedule_work(&rtc->irqwork); | |
0c86edc0 AZ |
133 | |
134 | return err; | |
135 | } | |
136 | EXPORT_SYMBOL_GPL(rtc_set_mmss); | |
137 | ||
f44f7f96 JS |
138 | static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
139 | { | |
140 | int err; | |
141 | ||
142 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
143 | if (err) | |
144 | return err; | |
145 | ||
146 | if (rtc->ops == NULL) | |
147 | err = -ENODEV; | |
148 | else if (!rtc->ops->read_alarm) | |
149 | err = -EINVAL; | |
150 | else { | |
151 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); | |
152 | err = rtc->ops->read_alarm(rtc->dev.parent, alarm); | |
153 | } | |
154 | ||
155 | mutex_unlock(&rtc->ops_lock); | |
156 | return err; | |
157 | } | |
158 | ||
159 | int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |
160 | { | |
161 | int err; | |
162 | struct rtc_time before, now; | |
163 | int first_time = 1; | |
bc10aa93 | 164 | time64_t t_now, t_alm; |
f44f7f96 JS |
165 | enum { none, day, month, year } missing = none; |
166 | unsigned days; | |
167 | ||
168 | /* The lower level RTC driver may return -1 in some fields, | |
169 | * creating invalid alarm->time values, for reasons like: | |
170 | * | |
171 | * - The hardware may not be capable of filling them in; | |
172 | * many alarms match only on time-of-day fields, not | |
173 | * day/month/year calendar data. | |
174 | * | |
175 | * - Some hardware uses illegal values as "wildcard" match | |
176 | * values, which non-Linux firmware (like a BIOS) may try | |
177 | * to set up as e.g. "alarm 15 minutes after each hour". | |
178 | * Linux uses only oneshot alarms. | |
179 | * | |
180 | * When we see that here, we deal with it by using values from | |
181 | * a current RTC timestamp for any missing (-1) values. The | |
182 | * RTC driver prevents "periodic alarm" modes. | |
183 | * | |
184 | * But this can be racey, because some fields of the RTC timestamp | |
185 | * may have wrapped in the interval since we read the RTC alarm, | |
186 | * which would lead to us inserting inconsistent values in place | |
187 | * of the -1 fields. | |
188 | * | |
189 | * Reading the alarm and timestamp in the reverse sequence | |
190 | * would have the same race condition, and not solve the issue. | |
191 | * | |
192 | * So, we must first read the RTC timestamp, | |
193 | * then read the RTC alarm value, | |
194 | * and then read a second RTC timestamp. | |
195 | * | |
196 | * If any fields of the second timestamp have changed | |
197 | * when compared with the first timestamp, then we know | |
198 | * our timestamp may be inconsistent with that used by | |
199 | * the low-level rtc_read_alarm_internal() function. | |
200 | * | |
201 | * So, when the two timestamps disagree, we just loop and do | |
202 | * the process again to get a fully consistent set of values. | |
203 | * | |
204 | * This could all instead be done in the lower level driver, | |
205 | * but since more than one lower level RTC implementation needs it, | |
206 | * then it's probably best best to do it here instead of there.. | |
207 | */ | |
208 | ||
209 | /* Get the "before" timestamp */ | |
210 | err = rtc_read_time(rtc, &before); | |
211 | if (err < 0) | |
212 | return err; | |
213 | do { | |
214 | if (!first_time) | |
215 | memcpy(&before, &now, sizeof(struct rtc_time)); | |
216 | first_time = 0; | |
217 | ||
218 | /* get the RTC alarm values, which may be incomplete */ | |
219 | err = rtc_read_alarm_internal(rtc, alarm); | |
220 | if (err) | |
221 | return err; | |
222 | ||
223 | /* full-function RTCs won't have such missing fields */ | |
224 | if (rtc_valid_tm(&alarm->time) == 0) | |
225 | return 0; | |
226 | ||
227 | /* get the "after" timestamp, to detect wrapped fields */ | |
228 | err = rtc_read_time(rtc, &now); | |
229 | if (err < 0) | |
230 | return err; | |
231 | ||
232 | /* note that tm_sec is a "don't care" value here: */ | |
233 | } while ( before.tm_min != now.tm_min | |
234 | || before.tm_hour != now.tm_hour | |
235 | || before.tm_mon != now.tm_mon | |
236 | || before.tm_year != now.tm_year); | |
237 | ||
238 | /* Fill in the missing alarm fields using the timestamp; we | |
239 | * know there's at least one since alarm->time is invalid. | |
240 | */ | |
241 | if (alarm->time.tm_sec == -1) | |
242 | alarm->time.tm_sec = now.tm_sec; | |
243 | if (alarm->time.tm_min == -1) | |
244 | alarm->time.tm_min = now.tm_min; | |
245 | if (alarm->time.tm_hour == -1) | |
246 | alarm->time.tm_hour = now.tm_hour; | |
247 | ||
248 | /* For simplicity, only support date rollover for now */ | |
e74a8f2e | 249 | if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) { |
f44f7f96 JS |
250 | alarm->time.tm_mday = now.tm_mday; |
251 | missing = day; | |
252 | } | |
e74a8f2e | 253 | if ((unsigned)alarm->time.tm_mon >= 12) { |
f44f7f96 JS |
254 | alarm->time.tm_mon = now.tm_mon; |
255 | if (missing == none) | |
256 | missing = month; | |
257 | } | |
258 | if (alarm->time.tm_year == -1) { | |
259 | alarm->time.tm_year = now.tm_year; | |
260 | if (missing == none) | |
261 | missing = year; | |
262 | } | |
263 | ||
264 | /* with luck, no rollover is needed */ | |
bc10aa93 XP |
265 | t_now = rtc_tm_to_time64(&now); |
266 | t_alm = rtc_tm_to_time64(&alarm->time); | |
f44f7f96 JS |
267 | if (t_now < t_alm) |
268 | goto done; | |
269 | ||
270 | switch (missing) { | |
271 | ||
272 | /* 24 hour rollover ... if it's now 10am Monday, an alarm that | |
273 | * that will trigger at 5am will do so at 5am Tuesday, which | |
274 | * could also be in the next month or year. This is a common | |
275 | * case, especially for PCs. | |
276 | */ | |
277 | case day: | |
278 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); | |
279 | t_alm += 24 * 60 * 60; | |
bc10aa93 | 280 | rtc_time64_to_tm(t_alm, &alarm->time); |
f44f7f96 JS |
281 | break; |
282 | ||
283 | /* Month rollover ... if it's the 31th, an alarm on the 3rd will | |
284 | * be next month. An alarm matching on the 30th, 29th, or 28th | |
285 | * may end up in the month after that! Many newer PCs support | |
286 | * this type of alarm. | |
287 | */ | |
288 | case month: | |
289 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); | |
290 | do { | |
291 | if (alarm->time.tm_mon < 11) | |
292 | alarm->time.tm_mon++; | |
293 | else { | |
294 | alarm->time.tm_mon = 0; | |
295 | alarm->time.tm_year++; | |
296 | } | |
297 | days = rtc_month_days(alarm->time.tm_mon, | |
298 | alarm->time.tm_year); | |
299 | } while (days < alarm->time.tm_mday); | |
300 | break; | |
301 | ||
302 | /* Year rollover ... easy except for leap years! */ | |
303 | case year: | |
304 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); | |
305 | do { | |
306 | alarm->time.tm_year++; | |
ee1d9014 AN |
307 | } while (!is_leap_year(alarm->time.tm_year + 1900) |
308 | && rtc_valid_tm(&alarm->time) != 0); | |
f44f7f96 JS |
309 | break; |
310 | ||
311 | default: | |
312 | dev_warn(&rtc->dev, "alarm rollover not handled\n"); | |
313 | } | |
314 | ||
315 | done: | |
ee1d9014 AN |
316 | err = rtc_valid_tm(&alarm->time); |
317 | ||
318 | if (err) { | |
319 | dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n", | |
320 | alarm->time.tm_year + 1900, alarm->time.tm_mon + 1, | |
321 | alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min, | |
322 | alarm->time.tm_sec); | |
323 | } | |
324 | ||
325 | return err; | |
f44f7f96 JS |
326 | } |
327 | ||
6610e089 | 328 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0c86edc0 AZ |
329 | { |
330 | int err; | |
0c86edc0 AZ |
331 | |
332 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
333 | if (err) | |
b68bb263 | 334 | return err; |
d5553a55 JS |
335 | if (rtc->ops == NULL) |
336 | err = -ENODEV; | |
337 | else if (!rtc->ops->read_alarm) | |
338 | err = -EINVAL; | |
339 | else { | |
340 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); | |
341 | alarm->enabled = rtc->aie_timer.enabled; | |
6610e089 | 342 | alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); |
d5553a55 | 343 | } |
0c86edc0 | 344 | mutex_unlock(&rtc->ops_lock); |
6610e089 | 345 | |
d5553a55 | 346 | return err; |
0c86edc0 | 347 | } |
6610e089 | 348 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
0e36a9a4 | 349 | |
d576fe49 | 350 | static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0e36a9a4 | 351 | { |
6610e089 | 352 | struct rtc_time tm; |
bc10aa93 | 353 | time64_t now, scheduled; |
0e36a9a4 | 354 | int err; |
0e36a9a4 | 355 | |
6610e089 JS |
356 | err = rtc_valid_tm(&alarm->time); |
357 | if (err) | |
0e36a9a4 | 358 | return err; |
bc10aa93 | 359 | scheduled = rtc_tm_to_time64(&alarm->time); |
a01cc657 | 360 | |
6610e089 JS |
361 | /* Make sure we're not setting alarms in the past */ |
362 | err = __rtc_read_time(rtc, &tm); | |
ca6dc2da HG |
363 | if (err) |
364 | return err; | |
bc10aa93 | 365 | now = rtc_tm_to_time64(&tm); |
6610e089 JS |
366 | if (scheduled <= now) |
367 | return -ETIME; | |
368 | /* | |
369 | * XXX - We just checked to make sure the alarm time is not | |
370 | * in the past, but there is still a race window where if | |
371 | * the is alarm set for the next second and the second ticks | |
372 | * over right here, before we set the alarm. | |
a01cc657 | 373 | */ |
a01cc657 | 374 | |
157e8bf8 LT |
375 | if (!rtc->ops) |
376 | err = -ENODEV; | |
377 | else if (!rtc->ops->set_alarm) | |
378 | err = -EINVAL; | |
379 | else | |
380 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); | |
381 | ||
382 | return err; | |
0e36a9a4 | 383 | } |
0c86edc0 | 384 | |
ab6a2d70 | 385 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0c86edc0 AZ |
386 | { |
387 | int err; | |
0c86edc0 | 388 | |
f8245c26 DB |
389 | err = rtc_valid_tm(&alarm->time); |
390 | if (err != 0) | |
391 | return err; | |
392 | ||
0c86edc0 AZ |
393 | err = mutex_lock_interruptible(&rtc->ops_lock); |
394 | if (err) | |
b68bb263 | 395 | return err; |
3ff2e13c | 396 | if (rtc->aie_timer.enabled) |
96c8f06a | 397 | rtc_timer_remove(rtc, &rtc->aie_timer); |
3ff2e13c | 398 | |
6610e089 JS |
399 | rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); |
400 | rtc->aie_timer.period = ktime_set(0, 0); | |
3ff2e13c | 401 | if (alarm->enabled) |
aa0be0f4 | 402 | err = rtc_timer_enqueue(rtc, &rtc->aie_timer); |
3ff2e13c | 403 | |
0c86edc0 | 404 | mutex_unlock(&rtc->ops_lock); |
aa0be0f4 | 405 | return err; |
0c86edc0 AZ |
406 | } |
407 | EXPORT_SYMBOL_GPL(rtc_set_alarm); | |
408 | ||
f6d5b331 JS |
409 | /* Called once per device from rtc_device_register */ |
410 | int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |
411 | { | |
412 | int err; | |
bd729d72 | 413 | struct rtc_time now; |
f6d5b331 JS |
414 | |
415 | err = rtc_valid_tm(&alarm->time); | |
416 | if (err != 0) | |
417 | return err; | |
418 | ||
bd729d72 JS |
419 | err = rtc_read_time(rtc, &now); |
420 | if (err) | |
421 | return err; | |
422 | ||
f6d5b331 JS |
423 | err = mutex_lock_interruptible(&rtc->ops_lock); |
424 | if (err) | |
425 | return err; | |
426 | ||
427 | rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); | |
428 | rtc->aie_timer.period = ktime_set(0, 0); | |
bd729d72 JS |
429 | |
430 | /* Alarm has to be enabled & in the futrure for us to enqueue it */ | |
431 | if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 < | |
432 | rtc->aie_timer.node.expires.tv64)) { | |
433 | ||
f6d5b331 JS |
434 | rtc->aie_timer.enabled = 1; |
435 | timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); | |
436 | } | |
437 | mutex_unlock(&rtc->ops_lock); | |
438 | return err; | |
439 | } | |
440 | EXPORT_SYMBOL_GPL(rtc_initialize_alarm); | |
441 | ||
442 | ||
443 | ||
099e6576 AZ |
444 | int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) |
445 | { | |
446 | int err = mutex_lock_interruptible(&rtc->ops_lock); | |
447 | if (err) | |
448 | return err; | |
449 | ||
6610e089 | 450 | if (rtc->aie_timer.enabled != enabled) { |
aa0be0f4 JS |
451 | if (enabled) |
452 | err = rtc_timer_enqueue(rtc, &rtc->aie_timer); | |
453 | else | |
96c8f06a | 454 | rtc_timer_remove(rtc, &rtc->aie_timer); |
6610e089 JS |
455 | } |
456 | ||
aa0be0f4 | 457 | if (err) |
516373b8 UKK |
458 | /* nothing */; |
459 | else if (!rtc->ops) | |
099e6576 AZ |
460 | err = -ENODEV; |
461 | else if (!rtc->ops->alarm_irq_enable) | |
462 | err = -EINVAL; | |
463 | else | |
464 | err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); | |
465 | ||
466 | mutex_unlock(&rtc->ops_lock); | |
467 | return err; | |
468 | } | |
469 | EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable); | |
470 | ||
471 | int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) | |
472 | { | |
473 | int err = mutex_lock_interruptible(&rtc->ops_lock); | |
474 | if (err) | |
475 | return err; | |
476 | ||
456d66ec JS |
477 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
478 | if (enabled == 0 && rtc->uie_irq_active) { | |
479 | mutex_unlock(&rtc->ops_lock); | |
480 | return rtc_dev_update_irq_enable_emul(rtc, 0); | |
481 | } | |
482 | #endif | |
6610e089 JS |
483 | /* make sure we're changing state */ |
484 | if (rtc->uie_rtctimer.enabled == enabled) | |
485 | goto out; | |
486 | ||
4a649903 JS |
487 | if (rtc->uie_unsupported) { |
488 | err = -EINVAL; | |
489 | goto out; | |
490 | } | |
491 | ||
6610e089 JS |
492 | if (enabled) { |
493 | struct rtc_time tm; | |
494 | ktime_t now, onesec; | |
495 | ||
496 | __rtc_read_time(rtc, &tm); | |
497 | onesec = ktime_set(1, 0); | |
498 | now = rtc_tm_to_ktime(tm); | |
499 | rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); | |
500 | rtc->uie_rtctimer.period = ktime_set(1, 0); | |
aa0be0f4 JS |
501 | err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); |
502 | } else | |
96c8f06a | 503 | rtc_timer_remove(rtc, &rtc->uie_rtctimer); |
099e6576 | 504 | |
6610e089 | 505 | out: |
099e6576 | 506 | mutex_unlock(&rtc->ops_lock); |
456d66ec JS |
507 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
508 | /* | |
509 | * Enable emulation if the driver did not provide | |
510 | * the update_irq_enable function pointer or if returned | |
511 | * -EINVAL to signal that it has been configured without | |
512 | * interrupts or that are not available at the moment. | |
513 | */ | |
514 | if (err == -EINVAL) | |
515 | err = rtc_dev_update_irq_enable_emul(rtc, enabled); | |
516 | #endif | |
099e6576 | 517 | return err; |
6610e089 | 518 | |
099e6576 AZ |
519 | } |
520 | EXPORT_SYMBOL_GPL(rtc_update_irq_enable); | |
521 | ||
6610e089 | 522 | |
d728b1e6 | 523 | /** |
6610e089 JS |
524 | * rtc_handle_legacy_irq - AIE, UIE and PIE event hook |
525 | * @rtc: pointer to the rtc device | |
526 | * | |
527 | * This function is called when an AIE, UIE or PIE mode interrupt | |
25985edc | 528 | * has occurred (or been emulated). |
6610e089 JS |
529 | * |
530 | * Triggers the registered irq_task function callback. | |
d728b1e6 | 531 | */ |
456d66ec | 532 | void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) |
0c86edc0 | 533 | { |
e6229bec AN |
534 | unsigned long flags; |
535 | ||
6610e089 | 536 | /* mark one irq of the appropriate mode */ |
e6229bec | 537 | spin_lock_irqsave(&rtc->irq_lock, flags); |
6610e089 | 538 | rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode); |
e6229bec | 539 | spin_unlock_irqrestore(&rtc->irq_lock, flags); |
0c86edc0 | 540 | |
6610e089 | 541 | /* call the task func */ |
e6229bec | 542 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
543 | if (rtc->irq_task) |
544 | rtc->irq_task->func(rtc->irq_task->private_data); | |
e6229bec | 545 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
546 | |
547 | wake_up_interruptible(&rtc->irq_queue); | |
548 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); | |
549 | } | |
6610e089 JS |
550 | |
551 | ||
552 | /** | |
553 | * rtc_aie_update_irq - AIE mode rtctimer hook | |
554 | * @private: pointer to the rtc_device | |
555 | * | |
556 | * This functions is called when the aie_timer expires. | |
557 | */ | |
558 | void rtc_aie_update_irq(void *private) | |
559 | { | |
560 | struct rtc_device *rtc = (struct rtc_device *)private; | |
561 | rtc_handle_legacy_irq(rtc, 1, RTC_AF); | |
562 | } | |
563 | ||
564 | ||
565 | /** | |
566 | * rtc_uie_update_irq - UIE mode rtctimer hook | |
567 | * @private: pointer to the rtc_device | |
568 | * | |
569 | * This functions is called when the uie_timer expires. | |
570 | */ | |
571 | void rtc_uie_update_irq(void *private) | |
572 | { | |
573 | struct rtc_device *rtc = (struct rtc_device *)private; | |
574 | rtc_handle_legacy_irq(rtc, 1, RTC_UF); | |
575 | } | |
576 | ||
577 | ||
578 | /** | |
579 | * rtc_pie_update_irq - PIE mode hrtimer hook | |
580 | * @timer: pointer to the pie mode hrtimer | |
581 | * | |
582 | * This function is used to emulate PIE mode interrupts | |
583 | * using an hrtimer. This function is called when the periodic | |
584 | * hrtimer expires. | |
585 | */ | |
586 | enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer) | |
587 | { | |
588 | struct rtc_device *rtc; | |
589 | ktime_t period; | |
590 | int count; | |
591 | rtc = container_of(timer, struct rtc_device, pie_timer); | |
592 | ||
593 | period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); | |
594 | count = hrtimer_forward_now(timer, period); | |
595 | ||
596 | rtc_handle_legacy_irq(rtc, count, RTC_PF); | |
597 | ||
598 | return HRTIMER_RESTART; | |
599 | } | |
600 | ||
601 | /** | |
602 | * rtc_update_irq - Triggered when a RTC interrupt occurs. | |
603 | * @rtc: the rtc device | |
604 | * @num: how many irqs are being reported (usually one) | |
605 | * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF | |
606 | * Context: any | |
607 | */ | |
608 | void rtc_update_irq(struct rtc_device *rtc, | |
609 | unsigned long num, unsigned long events) | |
610 | { | |
131c9cc8 AZ |
611 | if (unlikely(IS_ERR_OR_NULL(rtc))) |
612 | return; | |
613 | ||
7523ceed | 614 | pm_stay_awake(rtc->dev.parent); |
6610e089 JS |
615 | schedule_work(&rtc->irqwork); |
616 | } | |
0c86edc0 AZ |
617 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
618 | ||
9f3b795a | 619 | static int __rtc_match(struct device *dev, const void *data) |
71da8905 | 620 | { |
9f3b795a | 621 | const char *name = data; |
71da8905 | 622 | |
d4afc76c | 623 | if (strcmp(dev_name(dev), name) == 0) |
71da8905 DY |
624 | return 1; |
625 | return 0; | |
626 | } | |
627 | ||
9f3b795a | 628 | struct rtc_device *rtc_class_open(const char *name) |
0c86edc0 | 629 | { |
cd966209 | 630 | struct device *dev; |
ab6a2d70 | 631 | struct rtc_device *rtc = NULL; |
0c86edc0 | 632 | |
695794ae | 633 | dev = class_find_device(rtc_class, NULL, name, __rtc_match); |
71da8905 DY |
634 | if (dev) |
635 | rtc = to_rtc_device(dev); | |
0c86edc0 | 636 | |
ab6a2d70 DB |
637 | if (rtc) { |
638 | if (!try_module_get(rtc->owner)) { | |
cd966209 | 639 | put_device(dev); |
ab6a2d70 DB |
640 | rtc = NULL; |
641 | } | |
0c86edc0 | 642 | } |
0c86edc0 | 643 | |
ab6a2d70 | 644 | return rtc; |
0c86edc0 AZ |
645 | } |
646 | EXPORT_SYMBOL_GPL(rtc_class_open); | |
647 | ||
ab6a2d70 | 648 | void rtc_class_close(struct rtc_device *rtc) |
0c86edc0 | 649 | { |
ab6a2d70 | 650 | module_put(rtc->owner); |
cd966209 | 651 | put_device(&rtc->dev); |
0c86edc0 AZ |
652 | } |
653 | EXPORT_SYMBOL_GPL(rtc_class_close); | |
654 | ||
ab6a2d70 | 655 | int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) |
0c86edc0 AZ |
656 | { |
657 | int retval = -EBUSY; | |
0c86edc0 AZ |
658 | |
659 | if (task == NULL || task->func == NULL) | |
660 | return -EINVAL; | |
661 | ||
d691eb90 | 662 | /* Cannot register while the char dev is in use */ |
372a302e | 663 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
d691eb90 AZ |
664 | return -EBUSY; |
665 | ||
d728b1e6 | 666 | spin_lock_irq(&rtc->irq_task_lock); |
0c86edc0 AZ |
667 | if (rtc->irq_task == NULL) { |
668 | rtc->irq_task = task; | |
669 | retval = 0; | |
670 | } | |
d728b1e6 | 671 | spin_unlock_irq(&rtc->irq_task_lock); |
0c86edc0 | 672 | |
372a302e | 673 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
d691eb90 | 674 | |
0c86edc0 AZ |
675 | return retval; |
676 | } | |
677 | EXPORT_SYMBOL_GPL(rtc_irq_register); | |
678 | ||
ab6a2d70 | 679 | void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) |
0c86edc0 | 680 | { |
d728b1e6 | 681 | spin_lock_irq(&rtc->irq_task_lock); |
0c86edc0 AZ |
682 | if (rtc->irq_task == task) |
683 | rtc->irq_task = NULL; | |
d728b1e6 | 684 | spin_unlock_irq(&rtc->irq_task_lock); |
0c86edc0 AZ |
685 | } |
686 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); | |
687 | ||
3c8bb90e TG |
688 | static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled) |
689 | { | |
690 | /* | |
691 | * We always cancel the timer here first, because otherwise | |
692 | * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); | |
693 | * when we manage to start the timer before the callback | |
694 | * returns HRTIMER_RESTART. | |
695 | * | |
696 | * We cannot use hrtimer_cancel() here as a running callback | |
697 | * could be blocked on rtc->irq_task_lock and hrtimer_cancel() | |
698 | * would spin forever. | |
699 | */ | |
700 | if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0) | |
701 | return -1; | |
702 | ||
703 | if (enabled) { | |
704 | ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq); | |
705 | ||
706 | hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); | |
707 | } | |
708 | return 0; | |
709 | } | |
710 | ||
97144c67 DB |
711 | /** |
712 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs | |
713 | * @rtc: the rtc device | |
714 | * @task: currently registered with rtc_irq_register() | |
715 | * @enabled: true to enable periodic IRQs | |
716 | * Context: any | |
717 | * | |
718 | * Note that rtc_irq_set_freq() should previously have been used to | |
719 | * specify the desired frequency of periodic IRQ task->func() callbacks. | |
720 | */ | |
ab6a2d70 | 721 | int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled) |
0c86edc0 AZ |
722 | { |
723 | int err = 0; | |
724 | unsigned long flags; | |
0c86edc0 | 725 | |
3c8bb90e | 726 | retry: |
0c86edc0 | 727 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
d691eb90 AZ |
728 | if (rtc->irq_task != NULL && task == NULL) |
729 | err = -EBUSY; | |
0734e27f | 730 | else if (rtc->irq_task != task) |
d691eb90 | 731 | err = -EACCES; |
0734e27f | 732 | else { |
3c8bb90e TG |
733 | if (rtc_update_hrtimer(rtc, enabled) < 0) { |
734 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | |
735 | cpu_relax(); | |
736 | goto retry; | |
737 | } | |
738 | rtc->pie_enabled = enabled; | |
6610e089 | 739 | } |
6610e089 | 740 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
741 | return err; |
742 | } | |
743 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); | |
744 | ||
97144c67 DB |
745 | /** |
746 | * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ | |
747 | * @rtc: the rtc device | |
748 | * @task: currently registered with rtc_irq_register() | |
749 | * @freq: positive frequency with which task->func() will be called | |
750 | * Context: any | |
751 | * | |
752 | * Note that rtc_irq_set_state() is used to enable or disable the | |
753 | * periodic IRQs. | |
754 | */ | |
ab6a2d70 | 755 | int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq) |
0c86edc0 | 756 | { |
56f10c63 | 757 | int err = 0; |
0c86edc0 | 758 | unsigned long flags; |
0c86edc0 | 759 | |
6e7a333e | 760 | if (freq <= 0 || freq > RTC_MAX_FREQ) |
83a06bf5 | 761 | return -EINVAL; |
3c8bb90e | 762 | retry: |
0c86edc0 | 763 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
d691eb90 AZ |
764 | if (rtc->irq_task != NULL && task == NULL) |
765 | err = -EBUSY; | |
0734e27f | 766 | else if (rtc->irq_task != task) |
d691eb90 | 767 | err = -EACCES; |
0734e27f | 768 | else { |
6610e089 | 769 | rtc->irq_freq = freq; |
3c8bb90e TG |
770 | if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) { |
771 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | |
772 | cpu_relax(); | |
773 | goto retry; | |
6610e089 | 774 | } |
0c86edc0 | 775 | } |
6610e089 | 776 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
777 | return err; |
778 | } | |
2601a464 | 779 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |
6610e089 JS |
780 | |
781 | /** | |
96c8f06a | 782 | * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue |
6610e089 JS |
783 | * @rtc rtc device |
784 | * @timer timer being added. | |
785 | * | |
786 | * Enqueues a timer onto the rtc devices timerqueue and sets | |
787 | * the next alarm event appropriately. | |
788 | * | |
aa0be0f4 JS |
789 | * Sets the enabled bit on the added timer. |
790 | * | |
6610e089 JS |
791 | * Must hold ops_lock for proper serialization of timerqueue |
792 | */ | |
aa0be0f4 | 793 | static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 | 794 | { |
aa0be0f4 | 795 | timer->enabled = 1; |
6610e089 JS |
796 | timerqueue_add(&rtc->timerqueue, &timer->node); |
797 | if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) { | |
798 | struct rtc_wkalrm alarm; | |
799 | int err; | |
800 | alarm.time = rtc_ktime_to_tm(timer->node.expires); | |
801 | alarm.enabled = 1; | |
802 | err = __rtc_set_alarm(rtc, &alarm); | |
14d0e347 ZM |
803 | if (err == -ETIME) { |
804 | pm_stay_awake(rtc->dev.parent); | |
6610e089 | 805 | schedule_work(&rtc->irqwork); |
14d0e347 | 806 | } else if (err) { |
aa0be0f4 JS |
807 | timerqueue_del(&rtc->timerqueue, &timer->node); |
808 | timer->enabled = 0; | |
809 | return err; | |
810 | } | |
6610e089 | 811 | } |
aa0be0f4 | 812 | return 0; |
6610e089 JS |
813 | } |
814 | ||
41c7f742 RV |
815 | static void rtc_alarm_disable(struct rtc_device *rtc) |
816 | { | |
817 | if (!rtc->ops || !rtc->ops->alarm_irq_enable) | |
818 | return; | |
819 | ||
820 | rtc->ops->alarm_irq_enable(rtc->dev.parent, false); | |
821 | } | |
822 | ||
6610e089 | 823 | /** |
96c8f06a | 824 | * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue |
6610e089 JS |
825 | * @rtc rtc device |
826 | * @timer timer being removed. | |
827 | * | |
828 | * Removes a timer onto the rtc devices timerqueue and sets | |
829 | * the next alarm event appropriately. | |
830 | * | |
aa0be0f4 JS |
831 | * Clears the enabled bit on the removed timer. |
832 | * | |
6610e089 JS |
833 | * Must hold ops_lock for proper serialization of timerqueue |
834 | */ | |
aa0be0f4 | 835 | static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 JS |
836 | { |
837 | struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); | |
838 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
aa0be0f4 | 839 | timer->enabled = 0; |
6610e089 JS |
840 | if (next == &timer->node) { |
841 | struct rtc_wkalrm alarm; | |
842 | int err; | |
843 | next = timerqueue_getnext(&rtc->timerqueue); | |
41c7f742 RV |
844 | if (!next) { |
845 | rtc_alarm_disable(rtc); | |
6610e089 | 846 | return; |
41c7f742 | 847 | } |
6610e089 JS |
848 | alarm.time = rtc_ktime_to_tm(next->expires); |
849 | alarm.enabled = 1; | |
850 | err = __rtc_set_alarm(rtc, &alarm); | |
14d0e347 ZM |
851 | if (err == -ETIME) { |
852 | pm_stay_awake(rtc->dev.parent); | |
6610e089 | 853 | schedule_work(&rtc->irqwork); |
14d0e347 | 854 | } |
6610e089 JS |
855 | } |
856 | } | |
857 | ||
858 | /** | |
96c8f06a | 859 | * rtc_timer_do_work - Expires rtc timers |
6610e089 JS |
860 | * @rtc rtc device |
861 | * @timer timer being removed. | |
862 | * | |
863 | * Expires rtc timers. Reprograms next alarm event if needed. | |
864 | * Called via worktask. | |
865 | * | |
866 | * Serializes access to timerqueue via ops_lock mutex | |
867 | */ | |
96c8f06a | 868 | void rtc_timer_do_work(struct work_struct *work) |
6610e089 JS |
869 | { |
870 | struct rtc_timer *timer; | |
871 | struct timerqueue_node *next; | |
872 | ktime_t now; | |
873 | struct rtc_time tm; | |
874 | ||
875 | struct rtc_device *rtc = | |
876 | container_of(work, struct rtc_device, irqwork); | |
877 | ||
878 | mutex_lock(&rtc->ops_lock); | |
879 | again: | |
880 | __rtc_read_time(rtc, &tm); | |
881 | now = rtc_tm_to_ktime(tm); | |
882 | while ((next = timerqueue_getnext(&rtc->timerqueue))) { | |
883 | if (next->expires.tv64 > now.tv64) | |
884 | break; | |
885 | ||
886 | /* expire timer */ | |
887 | timer = container_of(next, struct rtc_timer, node); | |
888 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
889 | timer->enabled = 0; | |
890 | if (timer->task.func) | |
891 | timer->task.func(timer->task.private_data); | |
892 | ||
893 | /* Re-add/fwd periodic timers */ | |
894 | if (ktime_to_ns(timer->period)) { | |
895 | timer->node.expires = ktime_add(timer->node.expires, | |
896 | timer->period); | |
897 | timer->enabled = 1; | |
898 | timerqueue_add(&rtc->timerqueue, &timer->node); | |
899 | } | |
900 | } | |
901 | ||
902 | /* Set next alarm */ | |
903 | if (next) { | |
904 | struct rtc_wkalrm alarm; | |
905 | int err; | |
6528b889 XP |
906 | int retry = 3; |
907 | ||
6610e089 JS |
908 | alarm.time = rtc_ktime_to_tm(next->expires); |
909 | alarm.enabled = 1; | |
6528b889 | 910 | reprogram: |
6610e089 JS |
911 | err = __rtc_set_alarm(rtc, &alarm); |
912 | if (err == -ETIME) | |
913 | goto again; | |
6528b889 XP |
914 | else if (err) { |
915 | if (retry-- > 0) | |
916 | goto reprogram; | |
917 | ||
918 | timer = container_of(next, struct rtc_timer, node); | |
919 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
920 | timer->enabled = 0; | |
921 | dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); | |
922 | goto again; | |
923 | } | |
41c7f742 RV |
924 | } else |
925 | rtc_alarm_disable(rtc); | |
6610e089 | 926 | |
14d0e347 | 927 | pm_relax(rtc->dev.parent); |
6610e089 JS |
928 | mutex_unlock(&rtc->ops_lock); |
929 | } | |
930 | ||
931 | ||
96c8f06a | 932 | /* rtc_timer_init - Initializes an rtc_timer |
6610e089 JS |
933 | * @timer: timer to be intiialized |
934 | * @f: function pointer to be called when timer fires | |
935 | * @data: private data passed to function pointer | |
936 | * | |
937 | * Kernel interface to initializing an rtc_timer. | |
938 | */ | |
3ff2e13c | 939 | void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data) |
6610e089 JS |
940 | { |
941 | timerqueue_init(&timer->node); | |
942 | timer->enabled = 0; | |
943 | timer->task.func = f; | |
944 | timer->task.private_data = data; | |
945 | } | |
946 | ||
96c8f06a | 947 | /* rtc_timer_start - Sets an rtc_timer to fire in the future |
6610e089 JS |
948 | * @ rtc: rtc device to be used |
949 | * @ timer: timer being set | |
950 | * @ expires: time at which to expire the timer | |
951 | * @ period: period that the timer will recur | |
952 | * | |
953 | * Kernel interface to set an rtc_timer | |
954 | */ | |
3ff2e13c | 955 | int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, |
6610e089 JS |
956 | ktime_t expires, ktime_t period) |
957 | { | |
958 | int ret = 0; | |
959 | mutex_lock(&rtc->ops_lock); | |
960 | if (timer->enabled) | |
96c8f06a | 961 | rtc_timer_remove(rtc, timer); |
6610e089 JS |
962 | |
963 | timer->node.expires = expires; | |
964 | timer->period = period; | |
965 | ||
aa0be0f4 | 966 | ret = rtc_timer_enqueue(rtc, timer); |
6610e089 JS |
967 | |
968 | mutex_unlock(&rtc->ops_lock); | |
969 | return ret; | |
970 | } | |
971 | ||
96c8f06a | 972 | /* rtc_timer_cancel - Stops an rtc_timer |
6610e089 JS |
973 | * @ rtc: rtc device to be used |
974 | * @ timer: timer being set | |
975 | * | |
976 | * Kernel interface to cancel an rtc_timer | |
977 | */ | |
3ff2e13c | 978 | int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 JS |
979 | { |
980 | int ret = 0; | |
981 | mutex_lock(&rtc->ops_lock); | |
982 | if (timer->enabled) | |
96c8f06a | 983 | rtc_timer_remove(rtc, timer); |
6610e089 JS |
984 | mutex_unlock(&rtc->ops_lock); |
985 | return ret; | |
986 | } | |
987 | ||
988 |