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> |
| 15 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 16 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 17 | { |
| 18 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 19 | |
| 20 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 21 | if (err) |
| 22 | return -EBUSY; |
| 23 | |
| 24 | if (!rtc->ops) |
| 25 | err = -ENODEV; |
| 26 | else if (!rtc->ops->read_time) |
| 27 | err = -EINVAL; |
| 28 | else { |
| 29 | memset(tm, 0, sizeof(struct rtc_time)); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 30 | err = rtc->ops->read_time(rtc->dev.parent, tm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | mutex_unlock(&rtc->ops_lock); |
| 34 | return err; |
| 35 | } |
| 36 | EXPORT_SYMBOL_GPL(rtc_read_time); |
| 37 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 38 | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 39 | { |
| 40 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 41 | |
| 42 | err = rtc_valid_tm(tm); |
| 43 | if (err != 0) |
| 44 | return err; |
| 45 | |
| 46 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 47 | if (err) |
| 48 | return -EBUSY; |
| 49 | |
| 50 | if (!rtc->ops) |
| 51 | err = -ENODEV; |
| 52 | else if (!rtc->ops->set_time) |
| 53 | err = -EINVAL; |
| 54 | else |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 55 | err = rtc->ops->set_time(rtc->dev.parent, tm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 56 | |
| 57 | mutex_unlock(&rtc->ops_lock); |
| 58 | return err; |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(rtc_set_time); |
| 61 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 62 | int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 63 | { |
| 64 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 65 | |
| 66 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 67 | if (err) |
| 68 | return -EBUSY; |
| 69 | |
| 70 | if (!rtc->ops) |
| 71 | err = -ENODEV; |
| 72 | else if (rtc->ops->set_mmss) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 73 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 74 | else if (rtc->ops->read_time && rtc->ops->set_time) { |
| 75 | struct rtc_time new, old; |
| 76 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 77 | err = rtc->ops->read_time(rtc->dev.parent, &old); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 78 | if (err == 0) { |
| 79 | rtc_time_to_tm(secs, &new); |
| 80 | |
| 81 | /* |
| 82 | * avoid writing when we're going to change the day of |
| 83 | * the month. We will retry in the next minute. This |
| 84 | * basically means that if the RTC must not drift |
| 85 | * by more than 1 minute in 11 minutes. |
| 86 | */ |
| 87 | if (!((old.tm_hour == 23 && old.tm_min == 59) || |
| 88 | (new.tm_hour == 23 && new.tm_min == 59))) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 89 | err = rtc->ops->set_time(rtc->dev.parent, |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 90 | &new); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | else |
| 94 | err = -EINVAL; |
| 95 | |
| 96 | mutex_unlock(&rtc->ops_lock); |
| 97 | |
| 98 | return err; |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(rtc_set_mmss); |
| 101 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 102 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 103 | { |
| 104 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 105 | |
| 106 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 107 | if (err) |
| 108 | return -EBUSY; |
| 109 | |
| 110 | if (rtc->ops == NULL) |
| 111 | err = -ENODEV; |
| 112 | else if (!rtc->ops->read_alarm) |
| 113 | err = -EINVAL; |
| 114 | else { |
| 115 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 116 | err = rtc->ops->read_alarm(rtc->dev.parent, alarm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | mutex_unlock(&rtc->ops_lock); |
| 120 | return err; |
| 121 | } |
| 122 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
| 123 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 124 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 125 | { |
| 126 | int err; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 127 | |
| 128 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 129 | if (err) |
| 130 | return -EBUSY; |
| 131 | |
| 132 | if (!rtc->ops) |
| 133 | err = -ENODEV; |
| 134 | else if (!rtc->ops->set_alarm) |
| 135 | err = -EINVAL; |
| 136 | else |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 137 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 138 | |
| 139 | mutex_unlock(&rtc->ops_lock); |
| 140 | return err; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(rtc_set_alarm); |
| 143 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 144 | /** |
| 145 | * rtc_update_irq - report RTC periodic, alarm, and/or update irqs |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 146 | * @rtc: the rtc device |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 147 | * @num: how many irqs are being reported (usually one) |
| 148 | * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF |
| 149 | * Context: in_interrupt(), irqs blocked |
| 150 | */ |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 151 | void rtc_update_irq(struct rtc_device *rtc, |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 152 | unsigned long num, unsigned long events) |
| 153 | { |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 154 | spin_lock(&rtc->irq_lock); |
| 155 | rtc->irq_data = (rtc->irq_data + (num << 8)) | events; |
| 156 | spin_unlock(&rtc->irq_lock); |
| 157 | |
| 158 | spin_lock(&rtc->irq_task_lock); |
| 159 | if (rtc->irq_task) |
| 160 | rtc->irq_task->func(rtc->irq_task->private_data); |
| 161 | spin_unlock(&rtc->irq_task_lock); |
| 162 | |
| 163 | wake_up_interruptible(&rtc->irq_queue); |
| 164 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
| 167 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 168 | struct rtc_device *rtc_class_open(char *name) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 169 | { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 170 | struct device *dev; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 171 | struct rtc_device *rtc = NULL; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 172 | |
| 173 | down(&rtc_class->sem); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 174 | list_for_each_entry(dev, &rtc_class->devices, node) { |
| 175 | if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0) { |
| 176 | dev = get_device(dev); |
| 177 | if (dev) |
| 178 | rtc = to_rtc_device(dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 183 | if (rtc) { |
| 184 | if (!try_module_get(rtc->owner)) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 185 | put_device(dev); |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 186 | rtc = NULL; |
| 187 | } |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 188 | } |
| 189 | up(&rtc_class->sem); |
| 190 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 191 | return rtc; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 192 | } |
| 193 | EXPORT_SYMBOL_GPL(rtc_class_open); |
| 194 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 195 | void rtc_class_close(struct rtc_device *rtc) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 196 | { |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 197 | module_put(rtc->owner); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 198 | put_device(&rtc->dev); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 199 | } |
| 200 | EXPORT_SYMBOL_GPL(rtc_class_close); |
| 201 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 202 | int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 203 | { |
| 204 | int retval = -EBUSY; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 205 | |
| 206 | if (task == NULL || task->func == NULL) |
| 207 | return -EINVAL; |
| 208 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 209 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 210 | if (rtc->irq_task == NULL) { |
| 211 | rtc->irq_task = task; |
| 212 | retval = 0; |
| 213 | } |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 214 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 215 | |
| 216 | return retval; |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(rtc_irq_register); |
| 219 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 220 | void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 221 | { |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 222 | |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 223 | spin_lock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 224 | if (rtc->irq_task == task) |
| 225 | rtc->irq_task = NULL; |
David Brownell | d728b1e | 2006-11-25 11:09:28 -0800 | [diff] [blame] | 226 | spin_unlock_irq(&rtc->irq_task_lock); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 227 | } |
| 228 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); |
| 229 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 230 | 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] | 231 | { |
| 232 | int err = 0; |
| 233 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 234 | |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 235 | if (rtc->ops->irq_set_state == NULL) |
| 236 | return -ENXIO; |
| 237 | |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 238 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
| 239 | if (rtc->irq_task != task) |
| 240 | err = -ENXIO; |
| 241 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 242 | |
| 243 | if (err == 0) |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 244 | err = rtc->ops->irq_set_state(rtc->dev.parent, enabled); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 245 | |
| 246 | return err; |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); |
| 249 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 250 | 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] | 251 | { |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 252 | int err = 0; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 253 | unsigned long flags; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 254 | |
Alessandro Zummo | 56f10c6 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 255 | if (rtc->ops->irq_set_freq == NULL) |
| 256 | return -ENXIO; |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 257 | |
| 258 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
| 259 | if (rtc->irq_task != task) |
| 260 | err = -ENXIO; |
| 261 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
| 262 | |
| 263 | if (err == 0) { |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame^] | 264 | err = rtc->ops->irq_set_freq(rtc->dev.parent, freq); |
Alessandro Zummo | 0c86edc | 2006-03-27 01:16:37 -0800 | [diff] [blame] | 265 | if (err == 0) |
| 266 | rtc->irq_freq = freq; |
| 267 | } |
| 268 | return err; |
| 269 | } |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |