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