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