diff options
author | Paul Mundt <lethal@linux-sh.org> | 2011-01-13 01:06:28 -0500 |
---|---|---|
committer | Paul Mundt <lethal@linux-sh.org> | 2011-01-13 01:06:28 -0500 |
commit | f43dc23d5ea91fca257be02138a255f02d98e806 (patch) | |
tree | b29722f6e965316e90ac97abf79923ced250dc21 /drivers/rtc/interface.c | |
parent | f8e53553f452dcbf67cb89c8cba63a1cd6eb4cc0 (diff) | |
parent | 4162cf64973df51fc885825bc9ca4d055891c49f (diff) |
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6 into common/serial-rework
Conflicts:
arch/sh/kernel/cpu/sh2/setup-sh7619.c
arch/sh/kernel/cpu/sh2a/setup-mxg.c
arch/sh/kernel/cpu/sh2a/setup-sh7201.c
arch/sh/kernel/cpu/sh2a/setup-sh7203.c
arch/sh/kernel/cpu/sh2a/setup-sh7206.c
arch/sh/kernel/cpu/sh3/setup-sh7705.c
arch/sh/kernel/cpu/sh3/setup-sh770x.c
arch/sh/kernel/cpu/sh3/setup-sh7710.c
arch/sh/kernel/cpu/sh3/setup-sh7720.c
arch/sh/kernel/cpu/sh4/setup-sh4-202.c
arch/sh/kernel/cpu/sh4/setup-sh7750.c
arch/sh/kernel/cpu/sh4/setup-sh7760.c
arch/sh/kernel/cpu/sh4a/setup-sh7343.c
arch/sh/kernel/cpu/sh4a/setup-sh7366.c
arch/sh/kernel/cpu/sh4a/setup-sh7722.c
arch/sh/kernel/cpu/sh4a/setup-sh7723.c
arch/sh/kernel/cpu/sh4a/setup-sh7724.c
arch/sh/kernel/cpu/sh4a/setup-sh7763.c
arch/sh/kernel/cpu/sh4a/setup-sh7770.c
arch/sh/kernel/cpu/sh4a/setup-sh7780.c
arch/sh/kernel/cpu/sh4a/setup-sh7785.c
arch/sh/kernel/cpu/sh4a/setup-sh7786.c
arch/sh/kernel/cpu/sh4a/setup-shx3.c
arch/sh/kernel/cpu/sh5/setup-sh5.c
drivers/serial/sh-sci.c
drivers/serial/sh-sci.h
include/linux/serial_sci.h
Diffstat (limited to 'drivers/rtc/interface.c')
-rw-r--r-- | drivers/rtc/interface.c | 575 |
1 files changed, 350 insertions, 225 deletions
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 4cdb31a362ca..90384b9f6b2c 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c | |||
@@ -12,16 +12,13 @@ | |||
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include <linux/rtc.h> | 14 | #include <linux/rtc.h> |
15 | #include <linux/sched.h> | ||
15 | #include <linux/log2.h> | 16 | #include <linux/log2.h> |
17 | #include <linux/workqueue.h> | ||
16 | 18 | ||
17 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) | 19 | static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
18 | { | 20 | { |
19 | int err; | 21 | int err; |
20 | |||
21 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
22 | if (err) | ||
23 | return err; | ||
24 | |||
25 | if (!rtc->ops) | 22 | if (!rtc->ops) |
26 | err = -ENODEV; | 23 | err = -ENODEV; |
27 | else if (!rtc->ops->read_time) | 24 | else if (!rtc->ops->read_time) |
@@ -30,7 +27,18 @@ int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) | |||
30 | memset(tm, 0, sizeof(struct rtc_time)); | 27 | memset(tm, 0, sizeof(struct rtc_time)); |
31 | err = rtc->ops->read_time(rtc->dev.parent, tm); | 28 | err = rtc->ops->read_time(rtc->dev.parent, tm); |
32 | } | 29 | } |
30 | return err; | ||
31 | } | ||
32 | |||
33 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) | ||
34 | { | ||
35 | int err; | ||
33 | 36 | ||
37 | err = mutex_lock_interruptible(&rtc->ops_lock); | ||
38 | if (err) | ||
39 | return err; | ||
40 | |||
41 | err = __rtc_read_time(rtc, tm); | ||
34 | mutex_unlock(&rtc->ops_lock); | 42 | mutex_unlock(&rtc->ops_lock); |
35 | return err; | 43 | return err; |
36 | } | 44 | } |
@@ -105,188 +113,54 @@ int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs) | |||
105 | } | 113 | } |
106 | EXPORT_SYMBOL_GPL(rtc_set_mmss); | 114 | EXPORT_SYMBOL_GPL(rtc_set_mmss); |
107 | 115 | ||
108 | static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | 116 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
109 | { | 117 | { |
110 | int err; | 118 | int err; |
111 | 119 | ||
112 | err = mutex_lock_interruptible(&rtc->ops_lock); | 120 | err = mutex_lock_interruptible(&rtc->ops_lock); |
113 | if (err) | 121 | if (err) |
114 | return err; | 122 | return err; |
115 | 123 | alarm->enabled = rtc->aie_timer.enabled; | |
116 | if (rtc->ops == NULL) | 124 | if (alarm->enabled) |
117 | err = -ENODEV; | 125 | alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); |
118 | else if (!rtc->ops->read_alarm) | ||
119 | err = -EINVAL; | ||
120 | else { | ||
121 | memset(alarm, 0, sizeof(struct rtc_wkalrm)); | ||
122 | err = rtc->ops->read_alarm(rtc->dev.parent, alarm); | ||
123 | } | ||
124 | |||
125 | mutex_unlock(&rtc->ops_lock); | 126 | mutex_unlock(&rtc->ops_lock); |
126 | return err; | 127 | |
128 | return 0; | ||
127 | } | 129 | } |
130 | EXPORT_SYMBOL_GPL(rtc_read_alarm); | ||
128 | 131 | ||
129 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | 132 | int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
130 | { | 133 | { |
134 | struct rtc_time tm; | ||
135 | long now, scheduled; | ||
131 | int err; | 136 | int err; |
132 | struct rtc_time before, now; | ||
133 | int first_time = 1; | ||
134 | unsigned long t_now, t_alm; | ||
135 | enum { none, day, month, year } missing = none; | ||
136 | unsigned days; | ||
137 | |||
138 | /* The lower level RTC driver may return -1 in some fields, | ||
139 | * creating invalid alarm->time values, for reasons like: | ||
140 | * | ||
141 | * - The hardware may not be capable of filling them in; | ||
142 | * many alarms match only on time-of-day fields, not | ||
143 | * day/month/year calendar data. | ||
144 | * | ||
145 | * - Some hardware uses illegal values as "wildcard" match | ||
146 | * values, which non-Linux firmware (like a BIOS) may try | ||
147 | * to set up as e.g. "alarm 15 minutes after each hour". | ||
148 | * Linux uses only oneshot alarms. | ||
149 | * | ||
150 | * When we see that here, we deal with it by using values from | ||
151 | * a current RTC timestamp for any missing (-1) values. The | ||
152 | * RTC driver prevents "periodic alarm" modes. | ||
153 | * | ||
154 | * But this can be racey, because some fields of the RTC timestamp | ||
155 | * may have wrapped in the interval since we read the RTC alarm, | ||
156 | * which would lead to us inserting inconsistent values in place | ||
157 | * of the -1 fields. | ||
158 | * | ||
159 | * Reading the alarm and timestamp in the reverse sequence | ||
160 | * would have the same race condition, and not solve the issue. | ||
161 | * | ||
162 | * So, we must first read the RTC timestamp, | ||
163 | * then read the RTC alarm value, | ||
164 | * and then read a second RTC timestamp. | ||
165 | * | ||
166 | * If any fields of the second timestamp have changed | ||
167 | * when compared with the first timestamp, then we know | ||
168 | * our timestamp may be inconsistent with that used by | ||
169 | * the low-level rtc_read_alarm_internal() function. | ||
170 | * | ||
171 | * So, when the two timestamps disagree, we just loop and do | ||
172 | * the process again to get a fully consistent set of values. | ||
173 | * | ||
174 | * This could all instead be done in the lower level driver, | ||
175 | * but since more than one lower level RTC implementation needs it, | ||
176 | * then it's probably best best to do it here instead of there.. | ||
177 | */ | ||
178 | 137 | ||
179 | /* Get the "before" timestamp */ | 138 | err = rtc_valid_tm(&alarm->time); |
180 | err = rtc_read_time(rtc, &before); | 139 | if (err) |
181 | if (err < 0) | ||
182 | return err; | 140 | return err; |
183 | do { | 141 | rtc_tm_to_time(&alarm->time, &scheduled); |
184 | if (!first_time) | ||
185 | memcpy(&before, &now, sizeof(struct rtc_time)); | ||
186 | first_time = 0; | ||
187 | |||
188 | /* get the RTC alarm values, which may be incomplete */ | ||
189 | err = rtc_read_alarm_internal(rtc, alarm); | ||
190 | if (err) | ||
191 | return err; | ||
192 | if (!alarm->enabled) | ||
193 | return 0; | ||
194 | |||
195 | /* full-function RTCs won't have such missing fields */ | ||
196 | if (rtc_valid_tm(&alarm->time) == 0) | ||
197 | return 0; | ||
198 | |||
199 | /* get the "after" timestamp, to detect wrapped fields */ | ||
200 | err = rtc_read_time(rtc, &now); | ||
201 | if (err < 0) | ||
202 | return err; | ||
203 | |||
204 | /* note that tm_sec is a "don't care" value here: */ | ||
205 | } while ( before.tm_min != now.tm_min | ||
206 | || before.tm_hour != now.tm_hour | ||
207 | || before.tm_mon != now.tm_mon | ||
208 | || before.tm_year != now.tm_year); | ||
209 | |||
210 | /* Fill in the missing alarm fields using the timestamp; we | ||
211 | * know there's at least one since alarm->time is invalid. | ||
212 | */ | ||
213 | if (alarm->time.tm_sec == -1) | ||
214 | alarm->time.tm_sec = now.tm_sec; | ||
215 | if (alarm->time.tm_min == -1) | ||
216 | alarm->time.tm_min = now.tm_min; | ||
217 | if (alarm->time.tm_hour == -1) | ||
218 | alarm->time.tm_hour = now.tm_hour; | ||
219 | |||
220 | /* For simplicity, only support date rollover for now */ | ||
221 | if (alarm->time.tm_mday == -1) { | ||
222 | alarm->time.tm_mday = now.tm_mday; | ||
223 | missing = day; | ||
224 | } | ||
225 | if (alarm->time.tm_mon == -1) { | ||
226 | alarm->time.tm_mon = now.tm_mon; | ||
227 | if (missing == none) | ||
228 | missing = month; | ||
229 | } | ||
230 | if (alarm->time.tm_year == -1) { | ||
231 | alarm->time.tm_year = now.tm_year; | ||
232 | if (missing == none) | ||
233 | missing = year; | ||
234 | } | ||
235 | |||
236 | /* with luck, no rollover is needed */ | ||
237 | rtc_tm_to_time(&now, &t_now); | ||
238 | rtc_tm_to_time(&alarm->time, &t_alm); | ||
239 | if (t_now < t_alm) | ||
240 | goto done; | ||
241 | |||
242 | switch (missing) { | ||
243 | 142 | ||
244 | /* 24 hour rollover ... if it's now 10am Monday, an alarm that | 143 | /* Make sure we're not setting alarms in the past */ |
245 | * that will trigger at 5am will do so at 5am Tuesday, which | 144 | err = __rtc_read_time(rtc, &tm); |
246 | * could also be in the next month or year. This is a common | 145 | rtc_tm_to_time(&tm, &now); |
247 | * case, especially for PCs. | 146 | if (scheduled <= now) |
248 | */ | 147 | return -ETIME; |
249 | case day: | 148 | /* |
250 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); | 149 | * XXX - We just checked to make sure the alarm time is not |
251 | t_alm += 24 * 60 * 60; | 150 | * in the past, but there is still a race window where if |
252 | rtc_time_to_tm(t_alm, &alarm->time); | 151 | * the is alarm set for the next second and the second ticks |
253 | break; | 152 | * over right here, before we set the alarm. |
254 | |||
255 | /* Month rollover ... if it's the 31th, an alarm on the 3rd will | ||
256 | * be next month. An alarm matching on the 30th, 29th, or 28th | ||
257 | * may end up in the month after that! Many newer PCs support | ||
258 | * this type of alarm. | ||
259 | */ | 153 | */ |
260 | case month: | ||
261 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); | ||
262 | do { | ||
263 | if (alarm->time.tm_mon < 11) | ||
264 | alarm->time.tm_mon++; | ||
265 | else { | ||
266 | alarm->time.tm_mon = 0; | ||
267 | alarm->time.tm_year++; | ||
268 | } | ||
269 | days = rtc_month_days(alarm->time.tm_mon, | ||
270 | alarm->time.tm_year); | ||
271 | } while (days < alarm->time.tm_mday); | ||
272 | break; | ||
273 | |||
274 | /* Year rollover ... easy except for leap years! */ | ||
275 | case year: | ||
276 | dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); | ||
277 | do { | ||
278 | alarm->time.tm_year++; | ||
279 | } while (rtc_valid_tm(&alarm->time) != 0); | ||
280 | break; | ||
281 | |||
282 | default: | ||
283 | dev_warn(&rtc->dev, "alarm rollover not handled\n"); | ||
284 | } | ||
285 | 154 | ||
286 | done: | 155 | if (!rtc->ops) |
287 | return 0; | 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; | ||
288 | } | 163 | } |
289 | EXPORT_SYMBOL_GPL(rtc_read_alarm); | ||
290 | 164 | ||
291 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | 165 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
292 | { | 166 | { |
@@ -299,16 +173,18 @@ int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) | |||
299 | err = mutex_lock_interruptible(&rtc->ops_lock); | 173 | err = mutex_lock_interruptible(&rtc->ops_lock); |
300 | if (err) | 174 | if (err) |
301 | return err; | 175 | return err; |
302 | 176 | if (rtc->aie_timer.enabled) { | |
303 | if (!rtc->ops) | 177 | rtc_timer_remove(rtc, &rtc->aie_timer); |
304 | err = -ENODEV; | 178 | rtc->aie_timer.enabled = 0; |
305 | else if (!rtc->ops->set_alarm) | 179 | } |
306 | err = -EINVAL; | 180 | rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); |
307 | else | 181 | rtc->aie_timer.period = ktime_set(0, 0); |
308 | err = rtc->ops->set_alarm(rtc->dev.parent, alarm); | 182 | if (alarm->enabled) { |
309 | 183 | rtc->aie_timer.enabled = 1; | |
184 | rtc_timer_enqueue(rtc, &rtc->aie_timer); | ||
185 | } | ||
310 | mutex_unlock(&rtc->ops_lock); | 186 | mutex_unlock(&rtc->ops_lock); |
311 | return err; | 187 | return 0; |
312 | } | 188 | } |
313 | EXPORT_SYMBOL_GPL(rtc_set_alarm); | 189 | EXPORT_SYMBOL_GPL(rtc_set_alarm); |
314 | 190 | ||
@@ -318,6 +194,16 @@ int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) | |||
318 | if (err) | 194 | if (err) |
319 | return err; | 195 | return err; |
320 | 196 | ||
197 | if (rtc->aie_timer.enabled != enabled) { | ||
198 | if (enabled) { | ||
199 | rtc->aie_timer.enabled = 1; | ||
200 | rtc_timer_enqueue(rtc, &rtc->aie_timer); | ||
201 | } else { | ||
202 | rtc_timer_remove(rtc, &rtc->aie_timer); | ||
203 | rtc->aie_timer.enabled = 0; | ||
204 | } | ||
205 | } | ||
206 | |||
321 | if (!rtc->ops) | 207 | if (!rtc->ops) |
322 | err = -ENODEV; | 208 | err = -ENODEV; |
323 | else if (!rtc->ops->alarm_irq_enable) | 209 | else if (!rtc->ops->alarm_irq_enable) |
@@ -336,52 +222,53 @@ int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) | |||
336 | if (err) | 222 | if (err) |
337 | return err; | 223 | return err; |
338 | 224 | ||
339 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL | 225 | /* make sure we're changing state */ |
340 | if (enabled == 0 && rtc->uie_irq_active) { | 226 | if (rtc->uie_rtctimer.enabled == enabled) |
341 | mutex_unlock(&rtc->ops_lock); | 227 | goto out; |
342 | return rtc_dev_update_irq_enable_emul(rtc, enabled); | 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; | ||
239 | rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); | ||
240 | } else { | ||
241 | rtc_timer_remove(rtc, &rtc->uie_rtctimer); | ||
242 | rtc->uie_rtctimer.enabled = 0; | ||
343 | } | 243 | } |
344 | #endif | ||
345 | |||
346 | if (!rtc->ops) | ||
347 | err = -ENODEV; | ||
348 | else if (!rtc->ops->update_irq_enable) | ||
349 | err = -EINVAL; | ||
350 | else | ||
351 | err = rtc->ops->update_irq_enable(rtc->dev.parent, enabled); | ||
352 | 244 | ||
245 | out: | ||
353 | mutex_unlock(&rtc->ops_lock); | 246 | mutex_unlock(&rtc->ops_lock); |
354 | |||
355 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL | ||
356 | /* | ||
357 | * Enable emulation if the driver did not provide | ||
358 | * the update_irq_enable function pointer or if returned | ||
359 | * -EINVAL to signal that it has been configured without | ||
360 | * interrupts or that are not available at the moment. | ||
361 | */ | ||
362 | if (err == -EINVAL) | ||
363 | err = rtc_dev_update_irq_enable_emul(rtc, enabled); | ||
364 | #endif | ||
365 | return err; | 247 | return err; |
248 | |||
366 | } | 249 | } |
367 | EXPORT_SYMBOL_GPL(rtc_update_irq_enable); | 250 | EXPORT_SYMBOL_GPL(rtc_update_irq_enable); |
368 | 251 | ||
252 | |||
369 | /** | 253 | /** |
370 | * rtc_update_irq - report RTC periodic, alarm, and/or update irqs | 254 | * rtc_handle_legacy_irq - AIE, UIE and PIE event hook |
371 | * @rtc: the rtc device | 255 | * @rtc: pointer to the rtc device |
372 | * @num: how many irqs are being reported (usually one) | 256 | * |
373 | * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF | 257 | * This function is called when an AIE, UIE or PIE mode interrupt |
374 | * Context: any | 258 | * has occured (or been emulated). |
259 | * | ||
260 | * Triggers the registered irq_task function callback. | ||
375 | */ | 261 | */ |
376 | void rtc_update_irq(struct rtc_device *rtc, | 262 | static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) |
377 | unsigned long num, unsigned long events) | ||
378 | { | 263 | { |
379 | unsigned long flags; | 264 | unsigned long flags; |
380 | 265 | ||
266 | /* mark one irq of the appropriate mode */ | ||
381 | spin_lock_irqsave(&rtc->irq_lock, flags); | 267 | spin_lock_irqsave(&rtc->irq_lock, flags); |
382 | rtc->irq_data = (rtc->irq_data + (num << 8)) | events; | 268 | rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode); |
383 | spin_unlock_irqrestore(&rtc->irq_lock, flags); | 269 | spin_unlock_irqrestore(&rtc->irq_lock, flags); |
384 | 270 | ||
271 | /* call the task func */ | ||
385 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | 272 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
386 | if (rtc->irq_task) | 273 | if (rtc->irq_task) |
387 | rtc->irq_task->func(rtc->irq_task->private_data); | 274 | rtc->irq_task->func(rtc->irq_task->private_data); |
@@ -390,6 +277,69 @@ void rtc_update_irq(struct rtc_device *rtc, | |||
390 | wake_up_interruptible(&rtc->irq_queue); | 277 | wake_up_interruptible(&rtc->irq_queue); |
391 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); | 278 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); |
392 | } | 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. | ||
333 | * @rtc: the rtc device | ||
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 | ||
336 | * Context: any | ||
337 | */ | ||
338 | void rtc_update_irq(struct rtc_device *rtc, | ||
339 | unsigned long num, unsigned long events) | ||
340 | { | ||
341 | schedule_work(&rtc->irqwork); | ||
342 | } | ||
393 | EXPORT_SYMBOL_GPL(rtc_update_irq); | 343 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
394 | 344 | ||
395 | static int __rtc_match(struct device *dev, void *data) | 345 | static int __rtc_match(struct device *dev, void *data) |
@@ -476,18 +426,20 @@ int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled | |||
476 | int err = 0; | 426 | int err = 0; |
477 | unsigned long flags; | 427 | unsigned long flags; |
478 | 428 | ||
479 | if (rtc->ops->irq_set_state == NULL) | ||
480 | return -ENXIO; | ||
481 | |||
482 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | 429 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
483 | if (rtc->irq_task != NULL && task == NULL) | 430 | if (rtc->irq_task != NULL && task == NULL) |
484 | err = -EBUSY; | 431 | err = -EBUSY; |
485 | if (rtc->irq_task != task) | 432 | if (rtc->irq_task != task) |
486 | err = -EACCES; | 433 | err = -EACCES; |
487 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | ||
488 | 434 | ||
489 | if (err == 0) | 435 | if (enabled) { |
490 | err = rtc->ops->irq_set_state(rtc->dev.parent, 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); | ||
491 | 443 | ||
492 | return err; | 444 | return err; |
493 | } | 445 | } |
@@ -508,21 +460,194 @@ int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq) | |||
508 | int err = 0; | 460 | int err = 0; |
509 | unsigned long flags; | 461 | unsigned long flags; |
510 | 462 | ||
511 | if (rtc->ops->irq_set_freq == NULL) | ||
512 | return -ENXIO; | ||
513 | |||
514 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | 463 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
515 | if (rtc->irq_task != NULL && task == NULL) | 464 | if (rtc->irq_task != NULL && task == NULL) |
516 | err = -EBUSY; | 465 | err = -EBUSY; |
517 | if (rtc->irq_task != task) | 466 | if (rtc->irq_task != task) |
518 | err = -EACCES; | 467 | err = -EACCES; |
519 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | ||
520 | |||
521 | if (err == 0) { | 468 | if (err == 0) { |
522 | err = rtc->ops->irq_set_freq(rtc->dev.parent, freq); | 469 | rtc->irq_freq = freq; |
523 | if (err == 0) | 470 | if (rtc->pie_enabled) { |
524 | rtc->irq_freq = freq; | 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 | } | ||
525 | } | 477 | } |
478 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | ||
526 | return err; | 479 | return err; |
527 | } | 480 | } |
528 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); | 481 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |
482 | |||
483 | /** | ||
484 | * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue | ||
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 | */ | ||
493 | void rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) | ||
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 | /** | ||
508 | * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue | ||
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 | */ | ||
517 | void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) | ||
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 | /** | ||
537 | * rtc_timer_do_work - Expires rtc timers | ||
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 | */ | ||
546 | void rtc_timer_do_work(struct work_struct *work) | ||
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 | |||
595 | /* rtc_timer_init - Initializes an rtc_timer | ||
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 | */ | ||
602 | void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data) | ||
603 | { | ||
604 | timerqueue_init(&timer->node); | ||
605 | timer->enabled = 0; | ||
606 | timer->task.func = f; | ||
607 | timer->task.private_data = data; | ||
608 | } | ||
609 | |||
610 | /* rtc_timer_start - Sets an rtc_timer to fire in the future | ||
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 | */ | ||
618 | int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer, | ||
619 | ktime_t expires, ktime_t period) | ||
620 | { | ||
621 | int ret = 0; | ||
622 | mutex_lock(&rtc->ops_lock); | ||
623 | if (timer->enabled) | ||
624 | rtc_timer_remove(rtc, timer); | ||
625 | |||
626 | timer->node.expires = expires; | ||
627 | timer->period = period; | ||
628 | |||
629 | timer->enabled = 1; | ||
630 | rtc_timer_enqueue(rtc, timer); | ||
631 | |||
632 | mutex_unlock(&rtc->ops_lock); | ||
633 | return ret; | ||
634 | } | ||
635 | |||
636 | /* rtc_timer_cancel - Stops an rtc_timer | ||
637 | * @ rtc: rtc device to be used | ||
638 | * @ timer: timer being set | ||
639 | * | ||
640 | * Kernel interface to cancel an rtc_timer | ||
641 | */ | ||
642 | int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer) | ||
643 | { | ||
644 | int ret = 0; | ||
645 | mutex_lock(&rtc->ops_lock); | ||
646 | if (timer->enabled) | ||
647 | rtc_timer_remove(rtc, timer); | ||
648 | timer->enabled = 0; | ||
649 | mutex_unlock(&rtc->ops_lock); | ||
650 | return ret; | ||
651 | } | ||
652 | |||
653 | |||