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