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