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> |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 16 | #include <linux/log2.h> |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 17 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 18 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 19 | { |
| 20 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 21 | |
| 22 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 23 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 24 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 25 | |
| 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)); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 32 | err = rtc->ops->read_time(rtc->dev.parent, tm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | mutex_unlock(&rtc->ops_lock); |
| 36 | return err; |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(rtc_read_time); |
| 39 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 40 | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 41 | { |
| 42 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 43 | |
| 44 | err = rtc_valid_tm(tm); |
| 45 | if (err != 0) |
| 46 | return err; |
| 47 | |
| 48 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 49 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 50 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 51 | |
| 52 | if (!rtc->ops) |
| 53 | err = -ENODEV; |
Alessandro Zummo | bbccf83 | 2009-01-06 14:42:21 -0800 | [diff] [blame] | 54 | else if (rtc->ops->set_time) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 55 | err = rtc->ops->set_time(rtc->dev.parent, tm); |
Alessandro Zummo | bbccf83 | 2009-01-06 14:42:21 -0800 | [diff] [blame] | 56 | else if (rtc->ops->set_mmss) { |
| 57 | unsigned long secs; |
| 58 | err = rtc_tm_to_time(tm, &secs); |
| 59 | if (err == 0) |
| 60 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
| 61 | } else |
| 62 | err = -EINVAL; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 63 | |
| 64 | mutex_unlock(&rtc->ops_lock); |
| 65 | return err; |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(rtc_set_time); |
| 68 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 69 | int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 70 | { |
| 71 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 72 | |
| 73 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 74 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 75 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 76 | |
| 77 | if (!rtc->ops) |
| 78 | err = -ENODEV; |
| 79 | else if (rtc->ops->set_mmss) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 80 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 81 | else if (rtc->ops->read_time && rtc->ops->set_time) { |
| 82 | struct rtc_time new, old; |
| 83 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 84 | err = rtc->ops->read_time(rtc->dev.parent, &old); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 85 | if (err == 0) { |
| 86 | rtc_time_to_tm(secs, &new); |
| 87 | |
| 88 | /* |
| 89 | * avoid writing when we're going to change the day of |
| 90 | * the month. We will retry in the next minute. This |
| 91 | * basically means that if the RTC must not drift |
| 92 | * by more than 1 minute in 11 minutes. |
| 93 | */ |
| 94 | if (!((old.tm_hour == 23 && old.tm_min == 59) || |
| 95 | (new.tm_hour == 23 && new.tm_min == 59))) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 96 | err = rtc->ops->set_time(rtc->dev.parent, |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 97 | &new); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | else |
| 101 | err = -EINVAL; |
| 102 | |
| 103 | mutex_unlock(&rtc->ops_lock); |
| 104 | |
| 105 | return err; |
| 106 | } |
| 107 | EXPORT_SYMBOL_GPL(rtc_set_mmss); |
| 108 | |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 109 | static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 110 | { |
| 111 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 112 | |
| 113 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 114 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 115 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 116 | |
| 117 | if (rtc->ops == NULL) |
| 118 | err = -ENODEV; |
| 119 | else if (!rtc->ops->read_alarm) |
| 120 | err = -EINVAL; |
| 121 | else { |
| 122 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 123 | err = rtc->ops->read_alarm(rtc->dev.parent, alarm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | mutex_unlock(&rtc->ops_lock); |
| 127 | return err; |
| 128 | } |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 129 | |
| 130 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
| 131 | { |
| 132 | int err; |
| 133 | struct rtc_time before, now; |
| 134 | int first_time = 1; |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 135 | unsigned long t_now, t_alm; |
| 136 | enum { none, day, month, year } missing = none; |
| 137 | unsigned days; |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 138 | |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 139 | /* The lower level RTC driver may return -1 in some fields, |
| 140 | * creating invalid alarm->time values, for reasons like: |
| 141 | * |
| 142 | * - The hardware may not be capable of filling them in; |
| 143 | * many alarms match only on time-of-day fields, not |
| 144 | * day/month/year calendar data. |
| 145 | * |
| 146 | * - Some hardware uses illegal values as "wildcard" match |
| 147 | * values, which non-Linux firmware (like a BIOS) may try |
| 148 | * to set up as e.g. "alarm 15 minutes after each hour". |
| 149 | * Linux uses only oneshot alarms. |
| 150 | * |
| 151 | * When we see that here, we deal with it by using values from |
| 152 | * a current RTC timestamp for any missing (-1) values. The |
| 153 | * RTC driver prevents "periodic alarm" modes. |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 154 | * |
| 155 | * But this can be racey, because some fields of the RTC timestamp |
| 156 | * may have wrapped in the interval since we read the RTC alarm, |
| 157 | * which would lead to us inserting inconsistent values in place |
| 158 | * of the -1 fields. |
| 159 | * |
| 160 | * Reading the alarm and timestamp in the reverse sequence |
| 161 | * would have the same race condition, and not solve the issue. |
| 162 | * |
| 163 | * So, we must first read the RTC timestamp, |
| 164 | * then read the RTC alarm value, |
| 165 | * and then read a second RTC timestamp. |
| 166 | * |
| 167 | * If any fields of the second timestamp have changed |
| 168 | * when compared with the first timestamp, then we know |
| 169 | * our timestamp may be inconsistent with that used by |
| 170 | * the low-level rtc_read_alarm_internal() function. |
| 171 | * |
| 172 | * So, when the two timestamps disagree, we just loop and do |
| 173 | * the process again to get a fully consistent set of values. |
| 174 | * |
| 175 | * This could all instead be done in the lower level driver, |
| 176 | * but since more than one lower level RTC implementation needs it, |
| 177 | * then it's probably best best to do it here instead of there.. |
| 178 | */ |
| 179 | |
| 180 | /* Get the "before" timestamp */ |
| 181 | err = rtc_read_time(rtc, &before); |
| 182 | if (err < 0) |
| 183 | return err; |
| 184 | do { |
| 185 | if (!first_time) |
| 186 | memcpy(&before, &now, sizeof(struct rtc_time)); |
| 187 | first_time = 0; |
| 188 | |
| 189 | /* get the RTC alarm values, which may be incomplete */ |
| 190 | err = rtc_read_alarm_internal(rtc, alarm); |
| 191 | if (err) |
| 192 | return err; |
| 193 | if (!alarm->enabled) |
| 194 | return 0; |
| 195 | |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 196 | /* full-function RTCs won't have such missing fields */ |
| 197 | if (rtc_valid_tm(&alarm->time) == 0) |
| 198 | return 0; |
| 199 | |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 200 | /* get the "after" timestamp, to detect wrapped fields */ |
| 201 | err = rtc_read_time(rtc, &now); |
| 202 | if (err < 0) |
| 203 | return err; |
| 204 | |
| 205 | /* note that tm_sec is a "don't care" value here: */ |
| 206 | } while ( before.tm_min != now.tm_min |
| 207 | || before.tm_hour != now.tm_hour |
| 208 | || before.tm_mon != now.tm_mon |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 209 | || before.tm_year != now.tm_year); |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 210 | |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 211 | /* Fill in the missing alarm fields using the timestamp; we |
| 212 | * know there's at least one since alarm->time is invalid. |
| 213 | */ |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 214 | if (alarm->time.tm_sec == -1) |
| 215 | alarm->time.tm_sec = now.tm_sec; |
| 216 | if (alarm->time.tm_min == -1) |
| 217 | alarm->time.tm_min = now.tm_min; |
| 218 | if (alarm->time.tm_hour == -1) |
| 219 | alarm->time.tm_hour = now.tm_hour; |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 220 | |
| 221 | /* For simplicity, only support date rollover for now */ |
| 222 | if (alarm->time.tm_mday == -1) { |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 223 | alarm->time.tm_mday = now.tm_mday; |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 224 | missing = day; |
| 225 | } |
| 226 | if (alarm->time.tm_mon == -1) { |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 227 | alarm->time.tm_mon = now.tm_mon; |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 228 | if (missing == none) |
| 229 | missing = month; |
| 230 | } |
| 231 | if (alarm->time.tm_year == -1) { |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 232 | alarm->time.tm_year = now.tm_year; |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 233 | if (missing == none) |
| 234 | missing = year; |
| 235 | } |
| 236 | |
| 237 | /* with luck, no rollover is needed */ |
| 238 | rtc_tm_to_time(&now, &t_now); |
| 239 | rtc_tm_to_time(&alarm->time, &t_alm); |
| 240 | if (t_now < t_alm) |
| 241 | goto done; |
| 242 | |
| 243 | switch (missing) { |
| 244 | |
| 245 | /* 24 hour rollover ... if it's now 10am Monday, an alarm that |
| 246 | * that will trigger at 5am will do so at 5am Tuesday, which |
| 247 | * could also be in the next month or year. This is a common |
| 248 | * case, especially for PCs. |
| 249 | */ |
| 250 | case day: |
| 251 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); |
| 252 | t_alm += 24 * 60 * 60; |
| 253 | rtc_time_to_tm(t_alm, &alarm->time); |
| 254 | break; |
| 255 | |
| 256 | /* Month rollover ... if it's the 31th, an alarm on the 3rd will |
| 257 | * be next month. An alarm matching on the 30th, 29th, or 28th |
| 258 | * may end up in the month after that! Many newer PCs support |
| 259 | * this type of alarm. |
| 260 | */ |
| 261 | case month: |
| 262 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); |
| 263 | do { |
| 264 | if (alarm->time.tm_mon < 11) |
| 265 | alarm->time.tm_mon++; |
| 266 | else { |
| 267 | alarm->time.tm_mon = 0; |
| 268 | alarm->time.tm_year++; |
| 269 | } |
| 270 | days = rtc_month_days(alarm->time.tm_mon, |
| 271 | alarm->time.tm_year); |
| 272 | } while (days < alarm->time.tm_mday); |
| 273 | break; |
| 274 | |
| 275 | /* Year rollover ... easy except for leap years! */ |
| 276 | case year: |
| 277 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); |
| 278 | do { |
| 279 | alarm->time.tm_year++; |
Mark Brown | 9e3a4af | 2008-11-06 12:53:18 -0800 | [diff] [blame] | 280 | } while (rtc_valid_tm(&alarm->time) != 0); |
David Brownell | a01cc65 | 2008-07-04 09:59:26 -0700 | [diff] [blame] | 281 | break; |
| 282 | |
| 283 | default: |
| 284 | dev_warn(&rtc->dev, "alarm rollover not handled\n"); |
| 285 | } |
| 286 | |
| 287 | done: |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 290 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
| 291 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 292 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 293 | { |
| 294 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 295 | |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 296 | err = rtc_valid_tm(&alarm->time); |
| 297 | if (err != 0) |
| 298 | return err; |
| 299 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 300 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 301 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 302 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 303 | |
| 304 | if (!rtc->ops) |
| 305 | err = -ENODEV; |
| 306 | else if (!rtc->ops->set_alarm) |
| 307 | err = -EINVAL; |
| 308 | else |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 309 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 310 | |
| 311 | mutex_unlock(&rtc->ops_lock); |
| 312 | return err; |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(rtc_set_alarm); |
| 315 | |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 316 | int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) |
| 317 | { |
| 318 | int err = mutex_lock_interruptible(&rtc->ops_lock); |
| 319 | if (err) |
| 320 | return err; |
| 321 | |
| 322 | if (!rtc->ops) |
| 323 | err = -ENODEV; |
| 324 | else if (!rtc->ops->alarm_irq_enable) |
| 325 | err = -EINVAL; |
| 326 | else |
| 327 | err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); |
| 328 | |
| 329 | mutex_unlock(&rtc->ops_lock); |
| 330 | return err; |
| 331 | } |
| 332 | EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable); |
| 333 | |
| 334 | int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) |
| 335 | { |
| 336 | int err = mutex_lock_interruptible(&rtc->ops_lock); |
| 337 | if (err) |
| 338 | return err; |
| 339 | |
| 340 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
| 341 | if (enabled == 0 && rtc->uie_irq_active) { |
| 342 | mutex_unlock(&rtc->ops_lock); |
| 343 | return rtc_dev_update_irq_enable_emul(rtc, enabled); |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | if (!rtc->ops) |
| 348 | err = -ENODEV; |
| 349 | else if (!rtc->ops->update_irq_enable) |
| 350 | err = -EINVAL; |
| 351 | else |
| 352 | err = rtc->ops->update_irq_enable(rtc->dev.parent, enabled); |
| 353 | |
| 354 | mutex_unlock(&rtc->ops_lock); |
| 355 | |
| 356 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
| 357 | /* |
| 358 | * Enable emulation if the driver did not provide |
| 359 | * the update_irq_enable function pointer or if returned |
| 360 | * -EINVAL to signal that it has been configured without |
| 361 | * interrupts or that are not available at the moment. |
| 362 | */ |
| 363 | if (err == -EINVAL) |
| 364 | err = rtc_dev_update_irq_enable_emul(rtc, enabled); |
| 365 | #endif |
| 366 | return err; |
| 367 | } |
| 368 | EXPORT_SYMBOL_GPL(rtc_update_irq_enable); |
| 369 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 370 | /** |
| 371 | * rtc_update_irq - report RTC periodic, alarm, and/or update irqs |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 372 | * @rtc: the rtc device |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 373 | * @num: how many irqs are being reported (usually one) |
| 374 | * @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] | 375 | * Context: any |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 376 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 377 | void rtc_update_irq(struct rtc_device *rtc, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 378 | unsigned long num, unsigned long events) |
| 379 | { |
Atsushi Nemoto | e6229bec | 2009-06-18 16:49:09 -0700 | [diff] [blame] | 380 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 381 | |
Atsushi Nemoto | e6229bec | 2009-06-18 16:49:09 -0700 | [diff] [blame] | 382 | spin_lock_irqsave(&rtc->irq_lock, flags); |
| 383 | rtc->irq_data = (rtc->irq_data + (num << 8)) | events; |
| 384 | spin_unlock_irqrestore(&rtc->irq_lock, flags); |
| 385 | |
| 386 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 387 | if (rtc->irq_task) |
| 388 | rtc->irq_task->func(rtc->irq_task->private_data); |
Atsushi Nemoto | e6229bec | 2009-06-18 16:49:09 -0700 | [diff] [blame] | 389 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 390 | |
| 391 | wake_up_interruptible(&rtc->irq_queue); |
| 392 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
| 395 | |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 396 | static int __rtc_match(struct device *dev, void *data) |
| 397 | { |
| 398 | char *name = (char *)data; |
| 399 | |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 400 | if (strcmp(dev_name(dev), name) == 0) |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 401 | return 1; |
| 402 | return 0; |
| 403 | } |
| 404 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 405 | struct rtc_device *rtc_class_open(char *name) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 406 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 407 | struct device *dev; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 408 | struct rtc_device *rtc = NULL; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 409 | |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 410 | dev = class_find_device(rtc_class, NULL, name, __rtc_match); |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 411 | if (dev) |
| 412 | rtc = to_rtc_device(dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 413 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 414 | if (rtc) { |
| 415 | if (!try_module_get(rtc->owner)) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 416 | put_device(dev); |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 417 | rtc = NULL; |
| 418 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 419 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 420 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 421 | return rtc; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 422 | } |
| 423 | EXPORT_SYMBOL_GPL(rtc_class_open); |
| 424 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 425 | void rtc_class_close(struct rtc_device *rtc) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 426 | { |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 427 | module_put(rtc->owner); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 428 | put_device(&rtc->dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 429 | } |
| 430 | EXPORT_SYMBOL_GPL(rtc_class_close); |
| 431 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 432 | int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 433 | { |
| 434 | int retval = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 435 | |
| 436 | if (task == NULL || task->func == NULL) |
| 437 | return -EINVAL; |
| 438 | |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 439 | /* Cannot register while the char dev is in use */ |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 440 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 441 | return -EBUSY; |
| 442 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 443 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 444 | if (rtc->irq_task == NULL) { |
| 445 | rtc->irq_task = task; |
| 446 | retval = 0; |
| 447 | } |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 448 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 449 | |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 450 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 451 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 452 | return retval; |
| 453 | } |
| 454 | EXPORT_SYMBOL_GPL(rtc_irq_register); |
| 455 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 456 | void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 457 | { |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 458 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 459 | if (rtc->irq_task == task) |
| 460 | rtc->irq_task = NULL; |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 461 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 462 | } |
| 463 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); |
| 464 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 465 | /** |
| 466 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs |
| 467 | * @rtc: the rtc device |
| 468 | * @task: currently registered with rtc_irq_register() |
| 469 | * @enabled: true to enable periodic IRQs |
| 470 | * Context: any |
| 471 | * |
| 472 | * Note that rtc_irq_set_freq() should previously have been used to |
| 473 | * specify the desired frequency of periodic IRQ task->func() callbacks. |
| 474 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 475 | 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] | 476 | { |
| 477 | int err = 0; |
| 478 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 479 | |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 480 | if (rtc->ops->irq_set_state == NULL) |
| 481 | return -ENXIO; |
| 482 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 483 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 484 | if (rtc->irq_task != NULL && task == NULL) |
| 485 | err = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 486 | if (rtc->irq_task != task) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 487 | err = -EACCES; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 488 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 489 | |
| 490 | if (err == 0) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 491 | err = rtc->ops->irq_set_state(rtc->dev.parent, enabled); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 492 | |
| 493 | return err; |
| 494 | } |
| 495 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); |
| 496 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 497 | /** |
| 498 | * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ |
| 499 | * @rtc: the rtc device |
| 500 | * @task: currently registered with rtc_irq_register() |
| 501 | * @freq: positive frequency with which task->func() will be called |
| 502 | * Context: any |
| 503 | * |
| 504 | * Note that rtc_irq_set_state() is used to enable or disable the |
| 505 | * periodic IRQs. |
| 506 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 507 | 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] | 508 | { |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 509 | int err = 0; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 510 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 511 | |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 512 | if (rtc->ops->irq_set_freq == NULL) |
| 513 | return -ENXIO; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 514 | |
| 515 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 516 | if (rtc->irq_task != NULL && task == NULL) |
| 517 | err = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 518 | if (rtc->irq_task != task) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 519 | err = -EACCES; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 520 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 521 | |
| 522 | if (err == 0) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 523 | err = rtc->ops->irq_set_freq(rtc->dev.parent, freq); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 524 | if (err == 0) |
| 525 | rtc->irq_freq = freq; |
| 526 | } |
| 527 | return err; |
| 528 | } |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 529 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |