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> |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 17 | #include <linux/workqueue.h> |
| 18 | |
| 19 | static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
| 20 | { |
| 21 | int err; |
| 22 | if (!rtc->ops) |
| 23 | err = -ENODEV; |
| 24 | else if (!rtc->ops->read_time) |
| 25 | err = -EINVAL; |
| 26 | else { |
| 27 | memset(tm, 0, sizeof(struct rtc_time)); |
| 28 | err = rtc->ops->read_time(rtc->dev.parent, tm); |
| 29 | } |
| 30 | return err; |
| 31 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 32 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 33 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 34 | { |
| 35 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 36 | |
| 37 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 38 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 39 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 40 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 41 | err = __rtc_read_time(rtc, tm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 42 | mutex_unlock(&rtc->ops_lock); |
| 43 | return err; |
| 44 | } |
| 45 | EXPORT_SYMBOL_GPL(rtc_read_time); |
| 46 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 47 | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 48 | { |
| 49 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 50 | |
| 51 | err = rtc_valid_tm(tm); |
| 52 | if (err != 0) |
| 53 | return err; |
| 54 | |
| 55 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 56 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 57 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 58 | |
| 59 | if (!rtc->ops) |
| 60 | err = -ENODEV; |
Alessandro Zummo | bbccf83 | 2009-01-06 14:42:21 -0800 | [diff] [blame] | 61 | else if (rtc->ops->set_time) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 62 | err = rtc->ops->set_time(rtc->dev.parent, tm); |
Alessandro Zummo | bbccf83 | 2009-01-06 14:42:21 -0800 | [diff] [blame] | 63 | else if (rtc->ops->set_mmss) { |
| 64 | unsigned long secs; |
| 65 | err = rtc_tm_to_time(tm, &secs); |
| 66 | if (err == 0) |
| 67 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
| 68 | } else |
| 69 | err = -EINVAL; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 70 | |
| 71 | mutex_unlock(&rtc->ops_lock); |
| 72 | return err; |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(rtc_set_time); |
| 75 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 76 | int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 77 | { |
| 78 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 79 | |
| 80 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 81 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 82 | return err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 83 | |
| 84 | if (!rtc->ops) |
| 85 | err = -ENODEV; |
| 86 | else if (rtc->ops->set_mmss) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 87 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 88 | else if (rtc->ops->read_time && rtc->ops->set_time) { |
| 89 | struct rtc_time new, old; |
| 90 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 91 | err = rtc->ops->read_time(rtc->dev.parent, &old); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 92 | if (err == 0) { |
| 93 | rtc_time_to_tm(secs, &new); |
| 94 | |
| 95 | /* |
| 96 | * avoid writing when we're going to change the day of |
| 97 | * the month. We will retry in the next minute. This |
| 98 | * basically means that if the RTC must not drift |
| 99 | * by more than 1 minute in 11 minutes. |
| 100 | */ |
| 101 | if (!((old.tm_hour == 23 && old.tm_min == 59) || |
| 102 | (new.tm_hour == 23 && new.tm_min == 59))) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 103 | err = rtc->ops->set_time(rtc->dev.parent, |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 104 | &new); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | else |
| 108 | err = -EINVAL; |
| 109 | |
| 110 | mutex_unlock(&rtc->ops_lock); |
| 111 | |
| 112 | return err; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(rtc_set_mmss); |
| 115 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 116 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 117 | { |
| 118 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 119 | |
| 120 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 121 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 122 | return err; |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 123 | alarm->enabled = rtc->aie_timer.enabled; |
| 124 | if (alarm->enabled) |
| 125 | alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 126 | mutex_unlock(&rtc->ops_lock); |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 127 | |
Mark Lord | 0e36a9a | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
| 131 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 132 | int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
| 133 | { |
| 134 | struct rtc_time tm; |
| 135 | long now, scheduled; |
| 136 | int err; |
| 137 | |
| 138 | err = rtc_valid_tm(&alarm->time); |
| 139 | if (err) |
| 140 | return err; |
| 141 | rtc_tm_to_time(&alarm->time, &scheduled); |
| 142 | |
| 143 | /* Make sure we're not setting alarms in the past */ |
| 144 | err = __rtc_read_time(rtc, &tm); |
| 145 | rtc_tm_to_time(&tm, &now); |
| 146 | if (scheduled <= now) |
| 147 | return -ETIME; |
| 148 | /* |
| 149 | * XXX - We just checked to make sure the alarm time is not |
| 150 | * in the past, but there is still a race window where if |
| 151 | * the is alarm set for the next second and the second ticks |
| 152 | * over right here, before we set the alarm. |
| 153 | */ |
| 154 | |
| 155 | if (!rtc->ops) |
| 156 | err = -ENODEV; |
| 157 | else if (!rtc->ops->set_alarm) |
| 158 | err = -EINVAL; |
| 159 | else |
| 160 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 165 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 166 | { |
| 167 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 168 | |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 169 | err = rtc_valid_tm(&alarm->time); |
| 170 | if (err != 0) |
| 171 | return err; |
| 172 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 173 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 174 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 175 | return err; |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 176 | if (rtc->aie_timer.enabled) { |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 177 | rtc_timer_remove(rtc, &rtc->aie_timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 178 | rtc->aie_timer.enabled = 0; |
| 179 | } |
| 180 | rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); |
| 181 | rtc->aie_timer.period = ktime_set(0, 0); |
| 182 | if (alarm->enabled) { |
| 183 | rtc->aie_timer.enabled = 1; |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 184 | rtc_timer_enqueue(rtc, &rtc->aie_timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 185 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 186 | mutex_unlock(&rtc->ops_lock); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 187 | return 0; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 188 | } |
| 189 | EXPORT_SYMBOL_GPL(rtc_set_alarm); |
| 190 | |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 191 | int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) |
| 192 | { |
| 193 | int err = mutex_lock_interruptible(&rtc->ops_lock); |
| 194 | if (err) |
| 195 | return err; |
| 196 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 197 | if (rtc->aie_timer.enabled != enabled) { |
| 198 | if (enabled) { |
| 199 | rtc->aie_timer.enabled = 1; |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 200 | rtc_timer_enqueue(rtc, &rtc->aie_timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 201 | } else { |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 202 | rtc_timer_remove(rtc, &rtc->aie_timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 203 | rtc->aie_timer.enabled = 0; |
| 204 | } |
| 205 | } |
| 206 | |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 207 | if (!rtc->ops) |
| 208 | err = -ENODEV; |
| 209 | else if (!rtc->ops->alarm_irq_enable) |
| 210 | err = -EINVAL; |
| 211 | else |
| 212 | err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); |
| 213 | |
| 214 | mutex_unlock(&rtc->ops_lock); |
| 215 | return err; |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable); |
| 218 | |
| 219 | int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) |
| 220 | { |
| 221 | int err = mutex_lock_interruptible(&rtc->ops_lock); |
| 222 | if (err) |
| 223 | return err; |
| 224 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 225 | /* make sure we're changing state */ |
| 226 | if (rtc->uie_rtctimer.enabled == enabled) |
| 227 | goto out; |
| 228 | |
| 229 | if (enabled) { |
| 230 | struct rtc_time tm; |
| 231 | ktime_t now, onesec; |
| 232 | |
| 233 | __rtc_read_time(rtc, &tm); |
| 234 | onesec = ktime_set(1, 0); |
| 235 | now = rtc_tm_to_ktime(tm); |
| 236 | rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); |
| 237 | rtc->uie_rtctimer.period = ktime_set(1, 0); |
| 238 | rtc->uie_rtctimer.enabled = 1; |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 239 | rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 240 | } else { |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 241 | rtc_timer_remove(rtc, &rtc->uie_rtctimer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 242 | rtc->uie_rtctimer.enabled = 0; |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 243 | } |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 244 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 245 | out: |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 246 | mutex_unlock(&rtc->ops_lock); |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 247 | return err; |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 248 | |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 249 | } |
| 250 | EXPORT_SYMBOL_GPL(rtc_update_irq_enable); |
| 251 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 252 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 253 | /** |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 254 | * rtc_handle_legacy_irq - AIE, UIE and PIE event hook |
| 255 | * @rtc: pointer to the rtc device |
| 256 | * |
| 257 | * This function is called when an AIE, UIE or PIE mode interrupt |
| 258 | * has occured (or been emulated). |
| 259 | * |
| 260 | * Triggers the registered irq_task function callback. |
| 261 | */ |
| 262 | static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) |
| 263 | { |
| 264 | unsigned long flags; |
| 265 | |
| 266 | /* mark one irq of the appropriate mode */ |
| 267 | spin_lock_irqsave(&rtc->irq_lock, flags); |
| 268 | rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode); |
| 269 | spin_unlock_irqrestore(&rtc->irq_lock, flags); |
| 270 | |
| 271 | /* call the task func */ |
| 272 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
| 273 | if (rtc->irq_task) |
| 274 | rtc->irq_task->func(rtc->irq_task->private_data); |
| 275 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 276 | |
| 277 | wake_up_interruptible(&rtc->irq_queue); |
| 278 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * rtc_aie_update_irq - AIE mode rtctimer hook |
| 284 | * @private: pointer to the rtc_device |
| 285 | * |
| 286 | * This functions is called when the aie_timer expires. |
| 287 | */ |
| 288 | void rtc_aie_update_irq(void *private) |
| 289 | { |
| 290 | struct rtc_device *rtc = (struct rtc_device *)private; |
| 291 | rtc_handle_legacy_irq(rtc, 1, RTC_AF); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /** |
| 296 | * rtc_uie_update_irq - UIE mode rtctimer hook |
| 297 | * @private: pointer to the rtc_device |
| 298 | * |
| 299 | * This functions is called when the uie_timer expires. |
| 300 | */ |
| 301 | void rtc_uie_update_irq(void *private) |
| 302 | { |
| 303 | struct rtc_device *rtc = (struct rtc_device *)private; |
| 304 | rtc_handle_legacy_irq(rtc, 1, RTC_UF); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /** |
| 309 | * rtc_pie_update_irq - PIE mode hrtimer hook |
| 310 | * @timer: pointer to the pie mode hrtimer |
| 311 | * |
| 312 | * This function is used to emulate PIE mode interrupts |
| 313 | * using an hrtimer. This function is called when the periodic |
| 314 | * hrtimer expires. |
| 315 | */ |
| 316 | enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer) |
| 317 | { |
| 318 | struct rtc_device *rtc; |
| 319 | ktime_t period; |
| 320 | int count; |
| 321 | rtc = container_of(timer, struct rtc_device, pie_timer); |
| 322 | |
| 323 | period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); |
| 324 | count = hrtimer_forward_now(timer, period); |
| 325 | |
| 326 | rtc_handle_legacy_irq(rtc, count, RTC_PF); |
| 327 | |
| 328 | return HRTIMER_RESTART; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * rtc_update_irq - Triggered when a RTC interrupt occurs. |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 333 | * @rtc: the rtc device |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 334 | * @num: how many irqs are being reported (usually one) |
| 335 | * @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] | 336 | * Context: any |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 337 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 338 | void rtc_update_irq(struct rtc_device *rtc, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 339 | unsigned long num, unsigned long events) |
| 340 | { |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 341 | schedule_work(&rtc->irqwork); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 342 | } |
| 343 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
| 344 | |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 345 | static int __rtc_match(struct device *dev, void *data) |
| 346 | { |
| 347 | char *name = (char *)data; |
| 348 | |
Kay Sievers | d4afc76 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 349 | if (strcmp(dev_name(dev), name) == 0) |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 350 | return 1; |
| 351 | return 0; |
| 352 | } |
| 353 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 354 | struct rtc_device *rtc_class_open(char *name) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 355 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 356 | struct device *dev; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 357 | struct rtc_device *rtc = NULL; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 358 | |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 359 | dev = class_find_device(rtc_class, NULL, name, __rtc_match); |
Dave Young | 71da890 | 2008-01-22 14:00:34 +0800 | [diff] [blame] | 360 | if (dev) |
| 361 | rtc = to_rtc_device(dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 362 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 363 | if (rtc) { |
| 364 | if (!try_module_get(rtc->owner)) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 365 | put_device(dev); |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 366 | rtc = NULL; |
| 367 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 368 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 369 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 370 | return rtc; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 371 | } |
| 372 | EXPORT_SYMBOL_GPL(rtc_class_open); |
| 373 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 374 | void rtc_class_close(struct rtc_device *rtc) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 375 | { |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 376 | module_put(rtc->owner); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 377 | put_device(&rtc->dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 378 | } |
| 379 | EXPORT_SYMBOL_GPL(rtc_class_close); |
| 380 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 381 | int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 382 | { |
| 383 | int retval = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 384 | |
| 385 | if (task == NULL || task->func == NULL) |
| 386 | return -EINVAL; |
| 387 | |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 388 | /* Cannot register while the char dev is in use */ |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 389 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 390 | return -EBUSY; |
| 391 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 392 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 393 | if (rtc->irq_task == NULL) { |
| 394 | rtc->irq_task = task; |
| 395 | retval = 0; |
| 396 | } |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 397 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 398 | |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 399 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 400 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 401 | return retval; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(rtc_irq_register); |
| 404 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 405 | void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 406 | { |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 407 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 408 | if (rtc->irq_task == task) |
| 409 | rtc->irq_task = NULL; |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 410 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 411 | } |
| 412 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); |
| 413 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 414 | /** |
| 415 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs |
| 416 | * @rtc: the rtc device |
| 417 | * @task: currently registered with rtc_irq_register() |
| 418 | * @enabled: true to enable periodic IRQs |
| 419 | * Context: any |
| 420 | * |
| 421 | * Note that rtc_irq_set_freq() should previously have been used to |
| 422 | * specify the desired frequency of periodic IRQ task->func() callbacks. |
| 423 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 424 | 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] | 425 | { |
| 426 | int err = 0; |
| 427 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 428 | |
| 429 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 430 | if (rtc->irq_task != NULL && task == NULL) |
| 431 | err = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 432 | if (rtc->irq_task != task) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 433 | err = -EACCES; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 434 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 435 | if (enabled) { |
| 436 | ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); |
| 437 | hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); |
| 438 | } else { |
| 439 | hrtimer_cancel(&rtc->pie_timer); |
| 440 | } |
| 441 | rtc->pie_enabled = enabled; |
| 442 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 443 | |
| 444 | return err; |
| 445 | } |
| 446 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); |
| 447 | |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 448 | /** |
| 449 | * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ |
| 450 | * @rtc: the rtc device |
| 451 | * @task: currently registered with rtc_irq_register() |
| 452 | * @freq: positive frequency with which task->func() will be called |
| 453 | * Context: any |
| 454 | * |
| 455 | * Note that rtc_irq_set_state() is used to enable or disable the |
| 456 | * periodic IRQs. |
| 457 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 458 | 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] | 459 | { |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 460 | int err = 0; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 461 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 462 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 463 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 464 | if (rtc->irq_task != NULL && task == NULL) |
| 465 | err = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 466 | if (rtc->irq_task != task) |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 467 | err = -EACCES; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 468 | if (err == 0) { |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 469 | rtc->irq_freq = freq; |
| 470 | if (rtc->pie_enabled) { |
| 471 | ktime_t period; |
| 472 | hrtimer_cancel(&rtc->pie_timer); |
| 473 | period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); |
| 474 | hrtimer_start(&rtc->pie_timer, period, |
| 475 | HRTIMER_MODE_REL); |
| 476 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 477 | } |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 478 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 479 | return err; |
| 480 | } |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 481 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 482 | |
| 483 | /** |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 484 | * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 485 | * @rtc rtc device |
| 486 | * @timer timer being added. |
| 487 | * |
| 488 | * Enqueues a timer onto the rtc devices timerqueue and sets |
| 489 | * the next alarm event appropriately. |
| 490 | * |
| 491 | * Must hold ops_lock for proper serialization of timerqueue |
| 492 | */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 493 | void rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 494 | { |
| 495 | timerqueue_add(&rtc->timerqueue, &timer->node); |
| 496 | if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) { |
| 497 | struct rtc_wkalrm alarm; |
| 498 | int err; |
| 499 | alarm.time = rtc_ktime_to_tm(timer->node.expires); |
| 500 | alarm.enabled = 1; |
| 501 | err = __rtc_set_alarm(rtc, &alarm); |
| 502 | if (err == -ETIME) |
| 503 | schedule_work(&rtc->irqwork); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 508 | * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 509 | * @rtc rtc device |
| 510 | * @timer timer being removed. |
| 511 | * |
| 512 | * Removes a timer onto the rtc devices timerqueue and sets |
| 513 | * the next alarm event appropriately. |
| 514 | * |
| 515 | * Must hold ops_lock for proper serialization of timerqueue |
| 516 | */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 517 | void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 518 | { |
| 519 | struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); |
| 520 | timerqueue_del(&rtc->timerqueue, &timer->node); |
| 521 | |
| 522 | if (next == &timer->node) { |
| 523 | struct rtc_wkalrm alarm; |
| 524 | int err; |
| 525 | next = timerqueue_getnext(&rtc->timerqueue); |
| 526 | if (!next) |
| 527 | return; |
| 528 | alarm.time = rtc_ktime_to_tm(next->expires); |
| 529 | alarm.enabled = 1; |
| 530 | err = __rtc_set_alarm(rtc, &alarm); |
| 531 | if (err == -ETIME) |
| 532 | schedule_work(&rtc->irqwork); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 537 | * rtc_timer_do_work - Expires rtc timers |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 538 | * @rtc rtc device |
| 539 | * @timer timer being removed. |
| 540 | * |
| 541 | * Expires rtc timers. Reprograms next alarm event if needed. |
| 542 | * Called via worktask. |
| 543 | * |
| 544 | * Serializes access to timerqueue via ops_lock mutex |
| 545 | */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 546 | void rtc_timer_do_work(struct work_struct *work) |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 547 | { |
| 548 | struct rtc_timer *timer; |
| 549 | struct timerqueue_node *next; |
| 550 | ktime_t now; |
| 551 | struct rtc_time tm; |
| 552 | |
| 553 | struct rtc_device *rtc = |
| 554 | container_of(work, struct rtc_device, irqwork); |
| 555 | |
| 556 | mutex_lock(&rtc->ops_lock); |
| 557 | again: |
| 558 | __rtc_read_time(rtc, &tm); |
| 559 | now = rtc_tm_to_ktime(tm); |
| 560 | while ((next = timerqueue_getnext(&rtc->timerqueue))) { |
| 561 | if (next->expires.tv64 > now.tv64) |
| 562 | break; |
| 563 | |
| 564 | /* expire timer */ |
| 565 | timer = container_of(next, struct rtc_timer, node); |
| 566 | timerqueue_del(&rtc->timerqueue, &timer->node); |
| 567 | timer->enabled = 0; |
| 568 | if (timer->task.func) |
| 569 | timer->task.func(timer->task.private_data); |
| 570 | |
| 571 | /* Re-add/fwd periodic timers */ |
| 572 | if (ktime_to_ns(timer->period)) { |
| 573 | timer->node.expires = ktime_add(timer->node.expires, |
| 574 | timer->period); |
| 575 | timer->enabled = 1; |
| 576 | timerqueue_add(&rtc->timerqueue, &timer->node); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Set next alarm */ |
| 581 | if (next) { |
| 582 | struct rtc_wkalrm alarm; |
| 583 | int err; |
| 584 | alarm.time = rtc_ktime_to_tm(next->expires); |
| 585 | alarm.enabled = 1; |
| 586 | err = __rtc_set_alarm(rtc, &alarm); |
| 587 | if (err == -ETIME) |
| 588 | goto again; |
| 589 | } |
| 590 | |
| 591 | mutex_unlock(&rtc->ops_lock); |
| 592 | } |
| 593 | |
| 594 | |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 595 | /* rtc_timer_init - Initializes an rtc_timer |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 596 | * @timer: timer to be intiialized |
| 597 | * @f: function pointer to be called when timer fires |
| 598 | * @data: private data passed to function pointer |
| 599 | * |
| 600 | * Kernel interface to initializing an rtc_timer. |
| 601 | */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 602 | void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data) |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 603 | { |
| 604 | timerqueue_init(&timer->node); |
| 605 | timer->enabled = 0; |
| 606 | timer->task.func = f; |
| 607 | timer->task.private_data = data; |
| 608 | } |
| 609 | |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 610 | /* rtc_timer_start - Sets an rtc_timer to fire in the future |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 611 | * @ rtc: rtc device to be used |
| 612 | * @ timer: timer being set |
| 613 | * @ expires: time at which to expire the timer |
| 614 | * @ period: period that the timer will recur |
| 615 | * |
| 616 | * Kernel interface to set an rtc_timer |
| 617 | */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 618 | int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer, |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 619 | ktime_t expires, ktime_t period) |
| 620 | { |
| 621 | int ret = 0; |
| 622 | mutex_lock(&rtc->ops_lock); |
| 623 | if (timer->enabled) |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 624 | rtc_timer_remove(rtc, timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 625 | |
| 626 | timer->node.expires = expires; |
| 627 | timer->period = period; |
| 628 | |
| 629 | timer->enabled = 1; |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 630 | rtc_timer_enqueue(rtc, timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 631 | |
| 632 | mutex_unlock(&rtc->ops_lock); |
| 633 | return ret; |
| 634 | } |
| 635 | |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 636 | /* rtc_timer_cancel - Stops an rtc_timer |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 637 | * @ rtc: rtc device to be used |
| 638 | * @ timer: timer being set |
| 639 | * |
| 640 | * Kernel interface to cancel an rtc_timer |
| 641 | */ |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 642 | int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer) |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 643 | { |
| 644 | int ret = 0; |
| 645 | mutex_lock(&rtc->ops_lock); |
| 646 | if (timer->enabled) |
Thomas Gleixner | 96c8f06 | 2010-12-13 22:45:48 +0100 | [diff] [blame^] | 647 | rtc_timer_remove(rtc, timer); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 648 | timer->enabled = 0; |
| 649 | mutex_unlock(&rtc->ops_lock); |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | |