]>
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 | 33 | if (err < 0) { |
d0bddb51 AK |
34 | dev_dbg(&rtc->dev, "read_time: fail to read: %d\n", |
35 | err); | |
16682c86 HG |
36 | return err; |
37 | } | |
38 | ||
39 | err = rtc_valid_tm(tm); | |
40 | if (err < 0) | |
d0bddb51 | 41 | dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n"); |
0c86edc0 | 42 | } |
6610e089 JS |
43 | return err; |
44 | } | |
45 | ||
46 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) | |
47 | { | |
48 | int err; | |
0c86edc0 | 49 | |
6610e089 JS |
50 | err = mutex_lock_interruptible(&rtc->ops_lock); |
51 | if (err) | |
52 | return err; | |
53 | ||
54 | err = __rtc_read_time(rtc, tm); | |
0c86edc0 AZ |
55 | mutex_unlock(&rtc->ops_lock); |
56 | return err; | |
57 | } | |
58 | EXPORT_SYMBOL_GPL(rtc_read_time); | |
59 | ||
ab6a2d70 | 60 | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
0c86edc0 | 61 | { |
be206328 | 62 | int err, uie; |
0c86edc0 AZ |
63 | |
64 | err = rtc_valid_tm(tm); | |
65 | if (err != 0) | |
66 | return err; | |
67 | ||
be206328 AB |
68 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
69 | uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active; | |
70 | #else | |
71 | uie = rtc->uie_rtctimer.enabled; | |
72 | #endif | |
73 | if (uie) { | |
74 | err = rtc_update_irq_enable(rtc, 0); | |
75 | if (err) | |
76 | return err; | |
77 | } | |
78 | ||
0c86edc0 AZ |
79 | err = mutex_lock_interruptible(&rtc->ops_lock); |
80 | if (err) | |
b68bb263 | 81 | return err; |
0c86edc0 AZ |
82 | |
83 | if (!rtc->ops) | |
84 | err = -ENODEV; | |
bbccf83f | 85 | else if (rtc->ops->set_time) |
cd966209 | 86 | err = rtc->ops->set_time(rtc->dev.parent, tm); |
8e4ff1a8 XP |
87 | else if (rtc->ops->set_mmss64) { |
88 | time64_t secs64 = rtc_tm_to_time64(tm); | |
89 | ||
90 | err = rtc->ops->set_mmss64(rtc->dev.parent, secs64); | |
91 | } else if (rtc->ops->set_mmss) { | |
bc10aa93 XP |
92 | time64_t secs64 = rtc_tm_to_time64(tm); |
93 | err = rtc->ops->set_mmss(rtc->dev.parent, secs64); | |
bbccf83f AZ |
94 | } else |
95 | err = -EINVAL; | |
0c86edc0 | 96 | |
14d0e347 | 97 | pm_stay_awake(rtc->dev.parent); |
0c86edc0 | 98 | mutex_unlock(&rtc->ops_lock); |
5f9679d2 N |
99 | /* A timer might have just expired */ |
100 | schedule_work(&rtc->irqwork); | |
be206328 AB |
101 | |
102 | if (uie) { | |
103 | err = rtc_update_irq_enable(rtc, 1); | |
104 | if (err) | |
105 | return err; | |
106 | } | |
107 | ||
0c86edc0 AZ |
108 | return err; |
109 | } | |
110 | EXPORT_SYMBOL_GPL(rtc_set_time); | |
111 | ||
f44f7f96 JS |
112 | static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
113 | { | |
114 | int err; | |
115 | ||
116 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
117 | if (err) | |
118 | return err; | |
119 | ||
120 | if (rtc->ops == NULL) | |
121 | err = -ENODEV; | |
122 | else if (!rtc->ops->read_alarm) | |
123 | err = -EINVAL; | |
124 | else { | |
d68778b8 UKK |
125 | alarm->enabled = 0; |
126 | alarm->pending = 0; | |
127 | alarm->time.tm_sec = -1; | |
128 | alarm->time.tm_min = -1; | |
129 | alarm->time.tm_hour = -1; | |
130 | alarm->time.tm_mday = -1; | |
131 | alarm->time.tm_mon = -1; | |
132 | alarm->time.tm_year = -1; | |
133 | alarm->time.tm_wday = -1; | |
134 | alarm->time.tm_yday = -1; | |
135 | alarm->time.tm_isdst = -1; | |
f44f7f96 JS |
136 | err = rtc->ops->read_alarm(rtc->dev.parent, alarm); |
137 | } | |
138 | ||
139 | mutex_unlock(&rtc->ops_lock); | |
140 | return err; | |
141 | } | |
142 | ||
143 | int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |
144 | { | |
145 | int err; | |
146 | struct rtc_time before, now; | |
147 | int first_time = 1; | |
bc10aa93 | 148 | time64_t t_now, t_alm; |
f44f7f96 JS |
149 | enum { none, day, month, year } missing = none; |
150 | unsigned days; | |
151 | ||
152 | /* The lower level RTC driver may return -1 in some fields, | |
153 | * creating invalid alarm->time values, for reasons like: | |
154 | * | |
155 | * - The hardware may not be capable of filling them in; | |
156 | * many alarms match only on time-of-day fields, not | |
157 | * day/month/year calendar data. | |
158 | * | |
159 | * - Some hardware uses illegal values as "wildcard" match | |
160 | * values, which non-Linux firmware (like a BIOS) may try | |
161 | * to set up as e.g. "alarm 15 minutes after each hour". | |
162 | * Linux uses only oneshot alarms. | |
163 | * | |
164 | * When we see that here, we deal with it by using values from | |
165 | * a current RTC timestamp for any missing (-1) values. The | |
166 | * RTC driver prevents "periodic alarm" modes. | |
167 | * | |
168 | * But this can be racey, because some fields of the RTC timestamp | |
169 | * may have wrapped in the interval since we read the RTC alarm, | |
170 | * which would lead to us inserting inconsistent values in place | |
171 | * of the -1 fields. | |
172 | * | |
173 | * Reading the alarm and timestamp in the reverse sequence | |
174 | * would have the same race condition, and not solve the issue. | |
175 | * | |
176 | * So, we must first read the RTC timestamp, | |
177 | * then read the RTC alarm value, | |
178 | * and then read a second RTC timestamp. | |
179 | * | |
180 | * If any fields of the second timestamp have changed | |
181 | * when compared with the first timestamp, then we know | |
182 | * our timestamp may be inconsistent with that used by | |
183 | * the low-level rtc_read_alarm_internal() function. | |
184 | * | |
185 | * So, when the two timestamps disagree, we just loop and do | |
186 | * the process again to get a fully consistent set of values. | |
187 | * | |
188 | * This could all instead be done in the lower level driver, | |
189 | * but since more than one lower level RTC implementation needs it, | |
190 | * then it's probably best best to do it here instead of there.. | |
191 | */ | |
192 | ||
193 | /* Get the "before" timestamp */ | |
194 | err = rtc_read_time(rtc, &before); | |
195 | if (err < 0) | |
196 | return err; | |
197 | do { | |
198 | if (!first_time) | |
199 | memcpy(&before, &now, sizeof(struct rtc_time)); | |
200 | first_time = 0; | |
201 | ||
202 | /* get the RTC alarm values, which may be incomplete */ | |
203 | err = rtc_read_alarm_internal(rtc, alarm); | |
204 | if (err) | |
205 | return err; | |
206 | ||
207 | /* full-function RTCs won't have such missing fields */ | |
208 | if (rtc_valid_tm(&alarm->time) == 0) | |
209 | return 0; | |
210 | ||
211 | /* get the "after" timestamp, to detect wrapped fields */ | |
212 | err = rtc_read_time(rtc, &now); | |
213 | if (err < 0) | |
214 | return err; | |
215 | ||
216 | /* note that tm_sec is a "don't care" value here: */ | |
217 | } while ( before.tm_min != now.tm_min | |
218 | || before.tm_hour != now.tm_hour | |
219 | || before.tm_mon != now.tm_mon | |
220 | || before.tm_year != now.tm_year); | |
221 | ||
222 | /* Fill in the missing alarm fields using the timestamp; we | |
223 | * know there's at least one since alarm->time is invalid. | |
224 | */ | |
225 | if (alarm->time.tm_sec == -1) | |
226 | alarm->time.tm_sec = now.tm_sec; | |
227 | if (alarm->time.tm_min == -1) | |
228 | alarm->time.tm_min = now.tm_min; | |
229 | if (alarm->time.tm_hour == -1) | |
230 | alarm->time.tm_hour = now.tm_hour; | |
231 | ||
232 | /* For simplicity, only support date rollover for now */ | |
e74a8f2e | 233 | if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) { |
f44f7f96 JS |
234 | alarm->time.tm_mday = now.tm_mday; |
235 | missing = day; | |
236 | } | |
e74a8f2e | 237 | if ((unsigned)alarm->time.tm_mon >= 12) { |
f44f7f96 JS |
238 | alarm->time.tm_mon = now.tm_mon; |
239 | if (missing == none) | |
240 | missing = month; | |
241 | } | |
242 | if (alarm->time.tm_year == -1) { | |
243 | alarm->time.tm_year = now.tm_year; | |
244 | if (missing == none) | |
245 | missing = year; | |
246 | } | |
247 | ||
da96aea0 VJ |
248 | /* Can't proceed if alarm is still invalid after replacing |
249 | * missing fields. | |
250 | */ | |
251 | err = rtc_valid_tm(&alarm->time); | |
252 | if (err) | |
253 | goto done; | |
254 | ||
f44f7f96 | 255 | /* with luck, no rollover is needed */ |
bc10aa93 XP |
256 | t_now = rtc_tm_to_time64(&now); |
257 | t_alm = rtc_tm_to_time64(&alarm->time); | |
f44f7f96 JS |
258 | if (t_now < t_alm) |
259 | goto done; | |
260 | ||
261 | switch (missing) { | |
262 | ||
263 | /* 24 hour rollover ... if it's now 10am Monday, an alarm that | |
264 | * that will trigger at 5am will do so at 5am Tuesday, which | |
265 | * could also be in the next month or year. This is a common | |
266 | * case, especially for PCs. | |
267 | */ | |
268 | case day: | |
269 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); | |
270 | t_alm += 24 * 60 * 60; | |
bc10aa93 | 271 | rtc_time64_to_tm(t_alm, &alarm->time); |
f44f7f96 JS |
272 | break; |
273 | ||
274 | /* Month rollover ... if it's the 31th, an alarm on the 3rd will | |
275 | * be next month. An alarm matching on the 30th, 29th, or 28th | |
276 | * may end up in the month after that! Many newer PCs support | |
277 | * this type of alarm. | |
278 | */ | |
279 | case month: | |
280 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); | |
281 | do { | |
282 | if (alarm->time.tm_mon < 11) | |
283 | alarm->time.tm_mon++; | |
284 | else { | |
285 | alarm->time.tm_mon = 0; | |
286 | alarm->time.tm_year++; | |
287 | } | |
288 | days = rtc_month_days(alarm->time.tm_mon, | |
289 | alarm->time.tm_year); | |
290 | } while (days < alarm->time.tm_mday); | |
291 | break; | |
292 | ||
293 | /* Year rollover ... easy except for leap years! */ | |
294 | case year: | |
295 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); | |
296 | do { | |
297 | alarm->time.tm_year++; | |
ee1d9014 AN |
298 | } while (!is_leap_year(alarm->time.tm_year + 1900) |
299 | && rtc_valid_tm(&alarm->time) != 0); | |
f44f7f96 JS |
300 | break; |
301 | ||
302 | default: | |
303 | dev_warn(&rtc->dev, "alarm rollover not handled\n"); | |
304 | } | |
305 | ||
ee1d9014 AN |
306 | err = rtc_valid_tm(&alarm->time); |
307 | ||
da96aea0 | 308 | done: |
ee1d9014 AN |
309 | if (err) { |
310 | dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n", | |
311 | alarm->time.tm_year + 1900, alarm->time.tm_mon + 1, | |
312 | alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min, | |
313 | alarm->time.tm_sec); | |
314 | } | |
315 | ||
316 | return err; | |
f44f7f96 JS |
317 | } |
318 | ||
6610e089 | 319 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0c86edc0 AZ |
320 | { |
321 | int err; | |
0c86edc0 AZ |
322 | |
323 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
324 | if (err) | |
b68bb263 | 325 | return err; |
d5553a55 JS |
326 | if (rtc->ops == NULL) |
327 | err = -ENODEV; | |
328 | else if (!rtc->ops->read_alarm) | |
329 | err = -EINVAL; | |
330 | else { | |
331 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); | |
332 | alarm->enabled = rtc->aie_timer.enabled; | |
6610e089 | 333 | alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); |
d5553a55 | 334 | } |
0c86edc0 | 335 | mutex_unlock(&rtc->ops_lock); |
6610e089 | 336 | |
d5553a55 | 337 | return err; |
0c86edc0 | 338 | } |
6610e089 | 339 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
0e36a9a4 | 340 | |
d576fe49 | 341 | static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0e36a9a4 | 342 | { |
6610e089 | 343 | struct rtc_time tm; |
bc10aa93 | 344 | time64_t now, scheduled; |
0e36a9a4 | 345 | int err; |
0e36a9a4 | 346 | |
6610e089 JS |
347 | err = rtc_valid_tm(&alarm->time); |
348 | if (err) | |
0e36a9a4 | 349 | return err; |
bc10aa93 | 350 | scheduled = rtc_tm_to_time64(&alarm->time); |
a01cc657 | 351 | |
6610e089 JS |
352 | /* Make sure we're not setting alarms in the past */ |
353 | err = __rtc_read_time(rtc, &tm); | |
ca6dc2da HG |
354 | if (err) |
355 | return err; | |
bc10aa93 | 356 | now = rtc_tm_to_time64(&tm); |
6610e089 JS |
357 | if (scheduled <= now) |
358 | return -ETIME; | |
359 | /* | |
360 | * XXX - We just checked to make sure the alarm time is not | |
361 | * in the past, but there is still a race window where if | |
362 | * the is alarm set for the next second and the second ticks | |
363 | * over right here, before we set the alarm. | |
a01cc657 | 364 | */ |
a01cc657 | 365 | |
157e8bf8 LT |
366 | if (!rtc->ops) |
367 | err = -ENODEV; | |
368 | else if (!rtc->ops->set_alarm) | |
369 | err = -EINVAL; | |
370 | else | |
371 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); | |
372 | ||
373 | return err; | |
0e36a9a4 | 374 | } |
0c86edc0 | 375 | |
ab6a2d70 | 376 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0c86edc0 AZ |
377 | { |
378 | int err; | |
0c86edc0 | 379 | |
03b2c662 AB |
380 | if (!rtc->ops) |
381 | return -ENODEV; | |
382 | else if (!rtc->ops->set_alarm) | |
383 | return -EINVAL; | |
384 | ||
f8245c26 DB |
385 | err = rtc_valid_tm(&alarm->time); |
386 | if (err != 0) | |
387 | return err; | |
388 | ||
0c86edc0 AZ |
389 | err = mutex_lock_interruptible(&rtc->ops_lock); |
390 | if (err) | |
b68bb263 | 391 | return err; |
3ff2e13c | 392 | if (rtc->aie_timer.enabled) |
96c8f06a | 393 | rtc_timer_remove(rtc, &rtc->aie_timer); |
3ff2e13c | 394 | |
6610e089 | 395 | rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); |
8b0e1953 | 396 | rtc->aie_timer.period = 0; |
3ff2e13c | 397 | if (alarm->enabled) |
aa0be0f4 | 398 | err = rtc_timer_enqueue(rtc, &rtc->aie_timer); |
3ff2e13c | 399 | |
0c86edc0 | 400 | mutex_unlock(&rtc->ops_lock); |
aa0be0f4 | 401 | return err; |
0c86edc0 AZ |
402 | } |
403 | EXPORT_SYMBOL_GPL(rtc_set_alarm); | |
404 | ||
f6d5b331 JS |
405 | /* Called once per device from rtc_device_register */ |
406 | int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |
407 | { | |
408 | int err; | |
bd729d72 | 409 | struct rtc_time now; |
f6d5b331 JS |
410 | |
411 | err = rtc_valid_tm(&alarm->time); | |
412 | if (err != 0) | |
413 | return err; | |
414 | ||
bd729d72 JS |
415 | err = rtc_read_time(rtc, &now); |
416 | if (err) | |
417 | return err; | |
418 | ||
f6d5b331 JS |
419 | err = mutex_lock_interruptible(&rtc->ops_lock); |
420 | if (err) | |
421 | return err; | |
422 | ||
423 | rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); | |
8b0e1953 | 424 | rtc->aie_timer.period = 0; |
bd729d72 | 425 | |
6785b3b6 | 426 | /* Alarm has to be enabled & in the future for us to enqueue it */ |
2456e855 TG |
427 | if (alarm->enabled && (rtc_tm_to_ktime(now) < |
428 | rtc->aie_timer.node.expires)) { | |
bd729d72 | 429 | |
f6d5b331 JS |
430 | rtc->aie_timer.enabled = 1; |
431 | timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); | |
432 | } | |
433 | mutex_unlock(&rtc->ops_lock); | |
434 | return err; | |
435 | } | |
436 | EXPORT_SYMBOL_GPL(rtc_initialize_alarm); | |
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 | ||
8b0e1953 | 587 | period = NSEC_PER_SEC / rtc->irq_freq; |
6610e089 JS |
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 | { | |
e7cba884 | 605 | if (IS_ERR_OR_NULL(rtc)) |
131c9cc8 AZ |
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) { | |
8b0e1953 | 698 | ktime_t period = NSEC_PER_SEC / rtc->irq_freq; |
3c8bb90e TG |
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 | { |
2b2f5ff0 CIK |
789 | struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); |
790 | struct rtc_time tm; | |
791 | ktime_t now; | |
792 | ||
aa0be0f4 | 793 | timer->enabled = 1; |
2b2f5ff0 CIK |
794 | __rtc_read_time(rtc, &tm); |
795 | now = rtc_tm_to_ktime(tm); | |
796 | ||
797 | /* Skip over expired timers */ | |
798 | while (next) { | |
2456e855 | 799 | if (next->expires >= now) |
2b2f5ff0 CIK |
800 | break; |
801 | next = timerqueue_iterate_next(next); | |
802 | } | |
803 | ||
6610e089 | 804 | timerqueue_add(&rtc->timerqueue, &timer->node); |
74717b28 | 805 | if (!next || ktime_before(timer->node.expires, next->expires)) { |
6610e089 JS |
806 | struct rtc_wkalrm alarm; |
807 | int err; | |
808 | alarm.time = rtc_ktime_to_tm(timer->node.expires); | |
809 | alarm.enabled = 1; | |
810 | err = __rtc_set_alarm(rtc, &alarm); | |
14d0e347 ZM |
811 | if (err == -ETIME) { |
812 | pm_stay_awake(rtc->dev.parent); | |
6610e089 | 813 | schedule_work(&rtc->irqwork); |
14d0e347 | 814 | } else if (err) { |
aa0be0f4 JS |
815 | timerqueue_del(&rtc->timerqueue, &timer->node); |
816 | timer->enabled = 0; | |
817 | return err; | |
818 | } | |
6610e089 | 819 | } |
aa0be0f4 | 820 | return 0; |
6610e089 JS |
821 | } |
822 | ||
41c7f742 RV |
823 | static void rtc_alarm_disable(struct rtc_device *rtc) |
824 | { | |
825 | if (!rtc->ops || !rtc->ops->alarm_irq_enable) | |
826 | return; | |
827 | ||
828 | rtc->ops->alarm_irq_enable(rtc->dev.parent, false); | |
829 | } | |
830 | ||
6610e089 | 831 | /** |
96c8f06a | 832 | * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue |
6610e089 JS |
833 | * @rtc rtc device |
834 | * @timer timer being removed. | |
835 | * | |
836 | * Removes a timer onto the rtc devices timerqueue and sets | |
837 | * the next alarm event appropriately. | |
838 | * | |
aa0be0f4 JS |
839 | * Clears the enabled bit on the removed timer. |
840 | * | |
6610e089 JS |
841 | * Must hold ops_lock for proper serialization of timerqueue |
842 | */ | |
aa0be0f4 | 843 | static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 JS |
844 | { |
845 | struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); | |
846 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
aa0be0f4 | 847 | timer->enabled = 0; |
6610e089 JS |
848 | if (next == &timer->node) { |
849 | struct rtc_wkalrm alarm; | |
850 | int err; | |
851 | next = timerqueue_getnext(&rtc->timerqueue); | |
41c7f742 RV |
852 | if (!next) { |
853 | rtc_alarm_disable(rtc); | |
6610e089 | 854 | return; |
41c7f742 | 855 | } |
6610e089 JS |
856 | alarm.time = rtc_ktime_to_tm(next->expires); |
857 | alarm.enabled = 1; | |
858 | err = __rtc_set_alarm(rtc, &alarm); | |
14d0e347 ZM |
859 | if (err == -ETIME) { |
860 | pm_stay_awake(rtc->dev.parent); | |
6610e089 | 861 | schedule_work(&rtc->irqwork); |
14d0e347 | 862 | } |
6610e089 JS |
863 | } |
864 | } | |
865 | ||
866 | /** | |
96c8f06a | 867 | * rtc_timer_do_work - Expires rtc timers |
6610e089 JS |
868 | * @rtc rtc device |
869 | * @timer timer being removed. | |
870 | * | |
871 | * Expires rtc timers. Reprograms next alarm event if needed. | |
872 | * Called via worktask. | |
873 | * | |
874 | * Serializes access to timerqueue via ops_lock mutex | |
875 | */ | |
96c8f06a | 876 | void rtc_timer_do_work(struct work_struct *work) |
6610e089 JS |
877 | { |
878 | struct rtc_timer *timer; | |
879 | struct timerqueue_node *next; | |
880 | ktime_t now; | |
881 | struct rtc_time tm; | |
882 | ||
883 | struct rtc_device *rtc = | |
884 | container_of(work, struct rtc_device, irqwork); | |
885 | ||
886 | mutex_lock(&rtc->ops_lock); | |
887 | again: | |
888 | __rtc_read_time(rtc, &tm); | |
889 | now = rtc_tm_to_ktime(tm); | |
890 | while ((next = timerqueue_getnext(&rtc->timerqueue))) { | |
2456e855 | 891 | if (next->expires > now) |
6610e089 JS |
892 | break; |
893 | ||
894 | /* expire timer */ | |
895 | timer = container_of(next, struct rtc_timer, node); | |
896 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
897 | timer->enabled = 0; | |
898 | if (timer->task.func) | |
899 | timer->task.func(timer->task.private_data); | |
900 | ||
901 | /* Re-add/fwd periodic timers */ | |
902 | if (ktime_to_ns(timer->period)) { | |
903 | timer->node.expires = ktime_add(timer->node.expires, | |
904 | timer->period); | |
905 | timer->enabled = 1; | |
906 | timerqueue_add(&rtc->timerqueue, &timer->node); | |
907 | } | |
908 | } | |
909 | ||
910 | /* Set next alarm */ | |
911 | if (next) { | |
912 | struct rtc_wkalrm alarm; | |
913 | int err; | |
6528b889 XP |
914 | int retry = 3; |
915 | ||
6610e089 JS |
916 | alarm.time = rtc_ktime_to_tm(next->expires); |
917 | alarm.enabled = 1; | |
6528b889 | 918 | reprogram: |
6610e089 JS |
919 | err = __rtc_set_alarm(rtc, &alarm); |
920 | if (err == -ETIME) | |
921 | goto again; | |
6528b889 XP |
922 | else if (err) { |
923 | if (retry-- > 0) | |
924 | goto reprogram; | |
925 | ||
926 | timer = container_of(next, struct rtc_timer, node); | |
927 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
928 | timer->enabled = 0; | |
929 | dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); | |
930 | goto again; | |
931 | } | |
41c7f742 RV |
932 | } else |
933 | rtc_alarm_disable(rtc); | |
6610e089 | 934 | |
14d0e347 | 935 | pm_relax(rtc->dev.parent); |
6610e089 JS |
936 | mutex_unlock(&rtc->ops_lock); |
937 | } | |
938 | ||
939 | ||
96c8f06a | 940 | /* rtc_timer_init - Initializes an rtc_timer |
6610e089 JS |
941 | * @timer: timer to be intiialized |
942 | * @f: function pointer to be called when timer fires | |
943 | * @data: private data passed to function pointer | |
944 | * | |
945 | * Kernel interface to initializing an rtc_timer. | |
946 | */ | |
3ff2e13c | 947 | void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data) |
6610e089 JS |
948 | { |
949 | timerqueue_init(&timer->node); | |
950 | timer->enabled = 0; | |
951 | timer->task.func = f; | |
952 | timer->task.private_data = data; | |
953 | } | |
954 | ||
96c8f06a | 955 | /* rtc_timer_start - Sets an rtc_timer to fire in the future |
6610e089 JS |
956 | * @ rtc: rtc device to be used |
957 | * @ timer: timer being set | |
958 | * @ expires: time at which to expire the timer | |
959 | * @ period: period that the timer will recur | |
960 | * | |
961 | * Kernel interface to set an rtc_timer | |
962 | */ | |
3ff2e13c | 963 | int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, |
6610e089 JS |
964 | ktime_t expires, ktime_t period) |
965 | { | |
966 | int ret = 0; | |
967 | mutex_lock(&rtc->ops_lock); | |
968 | if (timer->enabled) | |
96c8f06a | 969 | rtc_timer_remove(rtc, timer); |
6610e089 JS |
970 | |
971 | timer->node.expires = expires; | |
972 | timer->period = period; | |
973 | ||
aa0be0f4 | 974 | ret = rtc_timer_enqueue(rtc, timer); |
6610e089 JS |
975 | |
976 | mutex_unlock(&rtc->ops_lock); | |
977 | return ret; | |
978 | } | |
979 | ||
96c8f06a | 980 | /* rtc_timer_cancel - Stops an rtc_timer |
6610e089 JS |
981 | * @ rtc: rtc device to be used |
982 | * @ timer: timer being set | |
983 | * | |
984 | * Kernel interface to cancel an rtc_timer | |
985 | */ | |
73744a64 | 986 | void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 | 987 | { |
6610e089 JS |
988 | mutex_lock(&rtc->ops_lock); |
989 | if (timer->enabled) | |
96c8f06a | 990 | rtc_timer_remove(rtc, timer); |
6610e089 | 991 | mutex_unlock(&rtc->ops_lock); |
6610e089 JS |
992 | } |
993 | ||
b3967067 JC |
994 | /** |
995 | * rtc_read_offset - Read the amount of rtc offset in parts per billion | |
996 | * @ rtc: rtc device to be used | |
997 | * @ offset: the offset in parts per billion | |
998 | * | |
999 | * see below for details. | |
1000 | * | |
1001 | * Kernel interface to read rtc clock offset | |
1002 | * Returns 0 on success, or a negative number on error. | |
1003 | * If read_offset() is not implemented for the rtc, return -EINVAL | |
1004 | */ | |
1005 | int rtc_read_offset(struct rtc_device *rtc, long *offset) | |
1006 | { | |
1007 | int ret; | |
1008 | ||
1009 | if (!rtc->ops) | |
1010 | return -ENODEV; | |
1011 | ||
1012 | if (!rtc->ops->read_offset) | |
1013 | return -EINVAL; | |
1014 | ||
1015 | mutex_lock(&rtc->ops_lock); | |
1016 | ret = rtc->ops->read_offset(rtc->dev.parent, offset); | |
1017 | mutex_unlock(&rtc->ops_lock); | |
1018 | return ret; | |
1019 | } | |
6610e089 | 1020 | |
b3967067 JC |
1021 | /** |
1022 | * rtc_set_offset - Adjusts the duration of the average second | |
1023 | * @ rtc: rtc device to be used | |
1024 | * @ offset: the offset in parts per billion | |
1025 | * | |
1026 | * Some rtc's allow an adjustment to the average duration of a second | |
1027 | * to compensate for differences in the actual clock rate due to temperature, | |
1028 | * the crystal, capacitor, etc. | |
1029 | * | |
8a25c8f6 RK |
1030 | * The adjustment applied is as follows: |
1031 | * t = t0 * (1 + offset * 1e-9) | |
1032 | * where t0 is the measured length of 1 RTC second with offset = 0 | |
1033 | * | |
b3967067 JC |
1034 | * Kernel interface to adjust an rtc clock offset. |
1035 | * Return 0 on success, or a negative number on error. | |
1036 | * If the rtc offset is not setable (or not implemented), return -EINVAL | |
1037 | */ | |
1038 | int rtc_set_offset(struct rtc_device *rtc, long offset) | |
1039 | { | |
1040 | int ret; | |
1041 | ||
1042 | if (!rtc->ops) | |
1043 | return -ENODEV; | |
1044 | ||
1045 | if (!rtc->ops->set_offset) | |
1046 | return -EINVAL; | |
1047 | ||
1048 | mutex_lock(&rtc->ops_lock); | |
1049 | ret = rtc->ops->set_offset(rtc->dev.parent, offset); | |
1050 | mutex_unlock(&rtc->ops_lock); | |
1051 | return ret; | |
1052 | } |