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