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