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) |
| 23 | return -EBUSY; |
| 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) |
| 49 | return -EBUSY; |
| 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) |
| 69 | return -EBUSY; |
| 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) |
| 109 | return -EBUSY; |
| 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; |
| 129 | |
| 130 | /* The lower level RTC driver may not be capable of filling |
| 131 | * in all fields of the rtc_time struct (eg. rtc-cmos), |
| 132 | * and so might instead return -1 in some fields. |
| 133 | * We deal with that here by grabbing a current RTC timestamp |
| 134 | * and using values from that for any missing (-1) values. |
| 135 | * |
| 136 | * But this can be racey, because some fields of the RTC timestamp |
| 137 | * may have wrapped in the interval since we read the RTC alarm, |
| 138 | * which would lead to us inserting inconsistent values in place |
| 139 | * of the -1 fields. |
| 140 | * |
| 141 | * Reading the alarm and timestamp in the reverse sequence |
| 142 | * would have the same race condition, and not solve the issue. |
| 143 | * |
| 144 | * So, we must first read the RTC timestamp, |
| 145 | * then read the RTC alarm value, |
| 146 | * and then read a second RTC timestamp. |
| 147 | * |
| 148 | * If any fields of the second timestamp have changed |
| 149 | * when compared with the first timestamp, then we know |
| 150 | * our timestamp may be inconsistent with that used by |
| 151 | * the low-level rtc_read_alarm_internal() function. |
| 152 | * |
| 153 | * So, when the two timestamps disagree, we just loop and do |
| 154 | * the process again to get a fully consistent set of values. |
| 155 | * |
| 156 | * This could all instead be done in the lower level driver, |
| 157 | * but since more than one lower level RTC implementation needs it, |
| 158 | * then it's probably best best to do it here instead of there.. |
| 159 | */ |
| 160 | |
| 161 | /* Get the "before" timestamp */ |
| 162 | err = rtc_read_time(rtc, &before); |
| 163 | if (err < 0) |
| 164 | return err; |
| 165 | do { |
| 166 | if (!first_time) |
| 167 | memcpy(&before, &now, sizeof(struct rtc_time)); |
| 168 | first_time = 0; |
| 169 | |
| 170 | /* get the RTC alarm values, which may be incomplete */ |
| 171 | err = rtc_read_alarm_internal(rtc, alarm); |
| 172 | if (err) |
| 173 | return err; |
| 174 | if (!alarm->enabled) |
| 175 | return 0; |
| 176 | |
| 177 | /* get the "after" timestamp, to detect wrapped fields */ |
| 178 | err = rtc_read_time(rtc, &now); |
| 179 | if (err < 0) |
| 180 | return err; |
| 181 | |
| 182 | /* note that tm_sec is a "don't care" value here: */ |
| 183 | } while ( before.tm_min != now.tm_min |
| 184 | || before.tm_hour != now.tm_hour |
| 185 | || before.tm_mon != now.tm_mon |
| 186 | || before.tm_year != now.tm_year |
| 187 | || before.tm_isdst != now.tm_isdst); |
| 188 | |
| 189 | /* Fill in any missing alarm fields using the timestamp */ |
| 190 | if (alarm->time.tm_sec == -1) |
| 191 | alarm->time.tm_sec = now.tm_sec; |
| 192 | if (alarm->time.tm_min == -1) |
| 193 | alarm->time.tm_min = now.tm_min; |
| 194 | if (alarm->time.tm_hour == -1) |
| 195 | alarm->time.tm_hour = now.tm_hour; |
| 196 | if (alarm->time.tm_mday == -1) |
| 197 | alarm->time.tm_mday = now.tm_mday; |
| 198 | if (alarm->time.tm_mon == -1) |
| 199 | alarm->time.tm_mon = now.tm_mon; |
| 200 | if (alarm->time.tm_year == -1) |
| 201 | alarm->time.tm_year = now.tm_year; |
| 202 | return 0; |
| 203 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 204 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
| 205 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 206 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 207 | { |
| 208 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 209 | |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 210 | err = rtc_valid_tm(&alarm->time); |
| 211 | if (err != 0) |
| 212 | return err; |
| 213 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 214 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 215 | if (err) |
| 216 | return -EBUSY; |
| 217 | |
| 218 | if (!rtc->ops) |
| 219 | err = -ENODEV; |
| 220 | else if (!rtc->ops->set_alarm) |
| 221 | err = -EINVAL; |
| 222 | else |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 223 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 224 | |
| 225 | mutex_unlock(&rtc->ops_lock); |
| 226 | return err; |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(rtc_set_alarm); |
| 229 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 230 | /** |
| 231 | * rtc_update_irq - report RTC periodic, alarm, and/or update irqs |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 232 | * @rtc: the rtc device |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 233 | * @num: how many irqs are being reported (usually one) |
| 234 | * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF |
| 235 | * Context: in_interrupt(), irqs blocked |
| 236 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 237 | void rtc_update_irq(struct rtc_device *rtc, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 238 | unsigned long num, unsigned long events) |
| 239 | { |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 240 | spin_lock(&rtc->irq_lock); |
| 241 | rtc->irq_data = (rtc->irq_data + (num << 8)) | events; |
| 242 | spin_unlock(&rtc->irq_lock); |
| 243 | |
| 244 | spin_lock(&rtc->irq_task_lock); |
| 245 | if (rtc->irq_task) |
| 246 | rtc->irq_task->func(rtc->irq_task->private_data); |
| 247 | spin_unlock(&rtc->irq_task_lock); |
| 248 | |
| 249 | wake_up_interruptible(&rtc->irq_queue); |
| 250 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
| 253 | |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 254 | static int __rtc_match(struct device *dev, void *data) |
| 255 | { |
| 256 | char *name = (char *)data; |
| 257 | |
| 258 | if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0) |
| 259 | return 1; |
| 260 | return 0; |
| 261 | } |
| 262 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 263 | struct rtc_device *rtc_class_open(char *name) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 264 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 265 | struct device *dev; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 266 | struct rtc_device *rtc = NULL; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 267 | |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 268 | dev = class_find_device(rtc_class, name, __rtc_match); |
| 269 | if (dev) |
| 270 | rtc = to_rtc_device(dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 271 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 272 | if (rtc) { |
| 273 | if (!try_module_get(rtc->owner)) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 274 | put_device(dev); |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 275 | rtc = NULL; |
| 276 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 277 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 278 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 279 | return rtc; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 280 | } |
| 281 | EXPORT_SYMBOL_GPL(rtc_class_open); |
| 282 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 283 | void rtc_class_close(struct rtc_device *rtc) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 284 | { |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 285 | module_put(rtc->owner); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 286 | put_device(&rtc->dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 287 | } |
| 288 | EXPORT_SYMBOL_GPL(rtc_class_close); |
| 289 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 290 | int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 291 | { |
| 292 | int retval = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 293 | |
| 294 | if (task == NULL || task->func == NULL) |
| 295 | return -EINVAL; |
| 296 | |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 297 | /* Cannot register while the char dev is in use */ |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 298 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 299 | return -EBUSY; |
| 300 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 301 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 302 | if (rtc->irq_task == NULL) { |
| 303 | rtc->irq_task = task; |
| 304 | retval = 0; |
| 305 | } |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 306 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 307 | |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 308 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 309 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 310 | return retval; |
| 311 | } |
| 312 | EXPORT_SYMBOL_GPL(rtc_irq_register); |
| 313 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 314 | void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 315 | { |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 316 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 317 | if (rtc->irq_task == task) |
| 318 | rtc->irq_task = NULL; |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 319 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 320 | } |
| 321 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); |
| 322 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 323 | /** |
| 324 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs |
| 325 | * @rtc: the rtc device |
| 326 | * @task: currently registered with rtc_irq_register() |
| 327 | * @enabled: true to enable periodic IRQs |
| 328 | * Context: any |
| 329 | * |
| 330 | * Note that rtc_irq_set_freq() should previously have been used to |
| 331 | * specify the desired frequency of periodic IRQ task->func() callbacks. |
| 332 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 333 | 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] | 334 | { |
| 335 | int err = 0; |
| 336 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 337 | |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 338 | if (rtc->ops->irq_set_state == NULL) |
| 339 | return -ENXIO; |
| 340 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 341 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 342 | if (rtc->irq_task != NULL && task == NULL) |
| 343 | err = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 344 | if (rtc->irq_task != task) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 345 | err = -EACCES; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 346 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 347 | |
| 348 | if (err == 0) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 349 | err = rtc->ops->irq_set_state(rtc->dev.parent, enabled); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 350 | |
| 351 | return err; |
| 352 | } |
| 353 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); |
| 354 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 355 | /** |
| 356 | * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ |
| 357 | * @rtc: the rtc device |
| 358 | * @task: currently registered with rtc_irq_register() |
| 359 | * @freq: positive frequency with which task->func() will be called |
| 360 | * Context: any |
| 361 | * |
| 362 | * Note that rtc_irq_set_state() is used to enable or disable the |
| 363 | * periodic IRQs. |
| 364 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 365 | 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] | 366 | { |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 367 | int err = 0; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 368 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 369 | |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 370 | if (rtc->ops->irq_set_freq == NULL) |
| 371 | return -ENXIO; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 372 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 373 | if (!is_power_of_2(freq)) |
| 374 | return -EINVAL; |
| 375 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 376 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 377 | if (rtc->irq_task != NULL && task == NULL) |
| 378 | err = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 379 | if (rtc->irq_task != task) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 380 | err = -EACCES; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 381 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 382 | |
| 383 | if (err == 0) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 384 | err = rtc->ops->irq_set_freq(rtc->dev.parent, freq); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 385 | if (err == 0) |
| 386 | rtc->irq_freq = freq; |
| 387 | } |
| 388 | return err; |
| 389 | } |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 390 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |