aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorThomas Gleixner <tglx@linutronix.de>2011-02-22 12:24:26 -0500
committerThomas Gleixner <tglx@linutronix.de>2011-02-22 12:41:48 -0500
commit695884fb8acd9857e0e7120ccb2150e30f4b8fef (patch)
tree49aa424c1a021ce432e9fa5ea29d37a23e4e30cc /drivers/rtc
parent5df91509d324d44cfb11e55d9cb02fe18b53b045 (diff)
parent04bea68b2f0eeebb089ecc67b618795925268b4a (diff)
Merge branch 'devicetree/for-x86' of git://git.secretlab.ca/git/linux-2.6 into x86/platform
Reason: x86 devicetree support for ce4100 depends on those device tree changes scheduled for .39. Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig12
-rw-r--r--drivers/rtc/class.c13
-rw-r--r--drivers/rtc/interface.c567
-rw-r--r--drivers/rtc/rtc-cmos.c19
-rw-r--r--drivers/rtc/rtc-dev.c104
-rw-r--r--drivers/rtc/rtc-ds1305.c2
-rw-r--r--drivers/rtc/rtc-ds1307.c12
-rw-r--r--drivers/rtc/rtc-ds1374.c2
-rw-r--r--drivers/rtc/rtc-ds3232.c2
-rw-r--r--drivers/rtc/rtc-lib.c28
-rw-r--r--drivers/rtc/rtc-max6902.c3
-rw-r--r--drivers/rtc/rtc-max8998.c54
-rw-r--r--drivers/rtc/rtc-omap.c6
-rw-r--r--drivers/rtc/rtc-rs5c372.c2
-rw-r--r--drivers/rtc/rtc-rx8025.c2
-rw-r--r--drivers/rtc/rtc-sa1100.c161
-rw-r--r--drivers/rtc/rtc-sh.c4
17 files changed, 599 insertions, 394 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 4941cade319f..cdd97192dc69 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -97,18 +97,6 @@ config RTC_INTF_DEV
97 97
98 If unsure, say Y. 98 If unsure, say Y.
99 99
100config RTC_INTF_DEV_UIE_EMUL
101 bool "RTC UIE emulation on dev interface"
102 depends on RTC_INTF_DEV
103 help
104 Provides an emulation for RTC_UIE if the underlying rtc chip
105 driver does not expose RTC_UIE ioctls. Those requests generate
106 once-per-second update interrupts, used for synchronization.
107
108 The emulation code will read the time from the hardware
109 clock several times per second, please enable this option
110 only if you know that you really need it.
111
112config RTC_DRV_TEST 100config RTC_DRV_TEST
113 tristate "Test driver/device" 101 tristate "Test driver/device"
114 help 102 help
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index e6539cbabb35..9583cbcc6b79 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -16,6 +16,7 @@
16#include <linux/kdev_t.h> 16#include <linux/kdev_t.h>
17#include <linux/idr.h> 17#include <linux/idr.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/workqueue.h>
19 20
20#include "rtc-core.h" 21#include "rtc-core.h"
21 22
@@ -152,6 +153,18 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
152 spin_lock_init(&rtc->irq_task_lock); 153 spin_lock_init(&rtc->irq_task_lock);
153 init_waitqueue_head(&rtc->irq_queue); 154 init_waitqueue_head(&rtc->irq_queue);
154 155
156 /* Init timerqueue */
157 timerqueue_init_head(&rtc->timerqueue);
158 INIT_WORK(&rtc->irqwork, rtc_timer_do_work);
159 /* Init aie timer */
160 rtc_timer_init(&rtc->aie_timer, rtc_aie_update_irq, (void *)rtc);
161 /* Init uie timer */
162 rtc_timer_init(&rtc->uie_rtctimer, rtc_uie_update_irq, (void *)rtc);
163 /* Init pie timer */
164 hrtimer_init(&rtc->pie_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
165 rtc->pie_timer.function = rtc_pie_update_irq;
166 rtc->pie_enabled = 0;
167
155 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE); 168 strlcpy(rtc->name, name, RTC_DEVICE_NAME_SIZE);
156 dev_set_name(&rtc->dev, "rtc%d", id); 169 dev_set_name(&rtc->dev, "rtc%d", id);
157 170
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index a0c816238aa9..925006d33109 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -14,15 +14,14 @@
14#include <linux/rtc.h> 14#include <linux/rtc.h>
15#include <linux/sched.h> 15#include <linux/sched.h>
16#include <linux/log2.h> 16#include <linux/log2.h>
17#include <linux/workqueue.h>
17 18
18int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) 19static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
20static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
21
22static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
19{ 23{
20 int err; 24 int err;
21
22 err = mutex_lock_interruptible(&rtc->ops_lock);
23 if (err)
24 return err;
25
26 if (!rtc->ops) 25 if (!rtc->ops)
27 err = -ENODEV; 26 err = -ENODEV;
28 else if (!rtc->ops->read_time) 27 else if (!rtc->ops->read_time)
@@ -31,7 +30,18 @@ int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
31 memset(tm, 0, sizeof(struct rtc_time)); 30 memset(tm, 0, sizeof(struct rtc_time));
32 err = rtc->ops->read_time(rtc->dev.parent, tm); 31 err = rtc->ops->read_time(rtc->dev.parent, tm);
33 } 32 }
33 return err;
34}
35
36int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
37{
38 int err;
39
40 err = mutex_lock_interruptible(&rtc->ops_lock);
41 if (err)
42 return err;
34 43
44 err = __rtc_read_time(rtc, tm);
35 mutex_unlock(&rtc->ops_lock); 45 mutex_unlock(&rtc->ops_lock);
36 return err; 46 return err;
37} 47}
@@ -106,188 +116,60 @@ int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
106} 116}
107EXPORT_SYMBOL_GPL(rtc_set_mmss); 117EXPORT_SYMBOL_GPL(rtc_set_mmss);
108 118
109static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 119int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
110{ 120{
111 int err; 121 int err;
112 122
113 err = mutex_lock_interruptible(&rtc->ops_lock); 123 err = mutex_lock_interruptible(&rtc->ops_lock);
114 if (err) 124 if (err)
115 return err; 125 return err;
116
117 if (rtc->ops == NULL) 126 if (rtc->ops == NULL)
118 err = -ENODEV; 127 err = -ENODEV;
119 else if (!rtc->ops->read_alarm) 128 else if (!rtc->ops->read_alarm)
120 err = -EINVAL; 129 err = -EINVAL;
121 else { 130 else {
122 memset(alarm, 0, sizeof(struct rtc_wkalrm)); 131 memset(alarm, 0, sizeof(struct rtc_wkalrm));
123 err = rtc->ops->read_alarm(rtc->dev.parent, alarm); 132 alarm->enabled = rtc->aie_timer.enabled;
133 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
124 } 134 }
125
126 mutex_unlock(&rtc->ops_lock); 135 mutex_unlock(&rtc->ops_lock);
136
127 return err; 137 return err;
128} 138}
139EXPORT_SYMBOL_GPL(rtc_read_alarm);
129 140
130int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 141int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
131{ 142{
143 struct rtc_time tm;
144 long now, scheduled;
132 int err; 145 int err;
133 struct rtc_time before, now;
134 int first_time = 1;
135 unsigned long t_now, t_alm;
136 enum { none, day, month, year } missing = none;
137 unsigned days;
138
139 /* The lower level RTC driver may return -1 in some fields,
140 * creating invalid alarm->time values, for reasons like:
141 *
142 * - The hardware may not be capable of filling them in;
143 * many alarms match only on time-of-day fields, not
144 * day/month/year calendar data.
145 *
146 * - Some hardware uses illegal values as "wildcard" match
147 * values, which non-Linux firmware (like a BIOS) may try
148 * to set up as e.g. "alarm 15 minutes after each hour".
149 * Linux uses only oneshot alarms.
150 *
151 * When we see that here, we deal with it by using values from
152 * a current RTC timestamp for any missing (-1) values. The
153 * RTC driver prevents "periodic alarm" modes.
154 *
155 * But this can be racey, because some fields of the RTC timestamp
156 * may have wrapped in the interval since we read the RTC alarm,
157 * which would lead to us inserting inconsistent values in place
158 * of the -1 fields.
159 *
160 * Reading the alarm and timestamp in the reverse sequence
161 * would have the same race condition, and not solve the issue.
162 *
163 * So, we must first read the RTC timestamp,
164 * then read the RTC alarm value,
165 * and then read a second RTC timestamp.
166 *
167 * If any fields of the second timestamp have changed
168 * when compared with the first timestamp, then we know
169 * our timestamp may be inconsistent with that used by
170 * the low-level rtc_read_alarm_internal() function.
171 *
172 * So, when the two timestamps disagree, we just loop and do
173 * the process again to get a fully consistent set of values.
174 *
175 * This could all instead be done in the lower level driver,
176 * but since more than one lower level RTC implementation needs it,
177 * then it's probably best best to do it here instead of there..
178 */
179 146
180 /* Get the "before" timestamp */ 147 err = rtc_valid_tm(&alarm->time);
181 err = rtc_read_time(rtc, &before); 148 if (err)
182 if (err < 0)
183 return err; 149 return err;
184 do { 150 rtc_tm_to_time(&alarm->time, &scheduled);
185 if (!first_time)
186 memcpy(&before, &now, sizeof(struct rtc_time));
187 first_time = 0;
188
189 /* get the RTC alarm values, which may be incomplete */
190 err = rtc_read_alarm_internal(rtc, alarm);
191 if (err)
192 return err;
193 if (!alarm->enabled)
194 return 0;
195
196 /* full-function RTCs won't have such missing fields */
197 if (rtc_valid_tm(&alarm->time) == 0)
198 return 0;
199
200 /* get the "after" timestamp, to detect wrapped fields */
201 err = rtc_read_time(rtc, &now);
202 if (err < 0)
203 return err;
204 151
205 /* note that tm_sec is a "don't care" value here: */ 152 /* Make sure we're not setting alarms in the past */
206 } while ( before.tm_min != now.tm_min 153 err = __rtc_read_time(rtc, &tm);
207 || before.tm_hour != now.tm_hour 154 rtc_tm_to_time(&tm, &now);
208 || before.tm_mon != now.tm_mon 155 if (scheduled <= now)
209 || before.tm_year != now.tm_year); 156 return -ETIME;
210 157 /*
211 /* Fill in the missing alarm fields using the timestamp; we 158 * XXX - We just checked to make sure the alarm time is not
212 * know there's at least one since alarm->time is invalid. 159 * in the past, but there is still a race window where if
160 * the is alarm set for the next second and the second ticks
161 * over right here, before we set the alarm.
213 */ 162 */
214 if (alarm->time.tm_sec == -1)
215 alarm->time.tm_sec = now.tm_sec;
216 if (alarm->time.tm_min == -1)
217 alarm->time.tm_min = now.tm_min;
218 if (alarm->time.tm_hour == -1)
219 alarm->time.tm_hour = now.tm_hour;
220
221 /* For simplicity, only support date rollover for now */
222 if (alarm->time.tm_mday == -1) {
223 alarm->time.tm_mday = now.tm_mday;
224 missing = day;
225 }
226 if (alarm->time.tm_mon == -1) {
227 alarm->time.tm_mon = now.tm_mon;
228 if (missing == none)
229 missing = month;
230 }
231 if (alarm->time.tm_year == -1) {
232 alarm->time.tm_year = now.tm_year;
233 if (missing == none)
234 missing = year;
235 }
236
237 /* with luck, no rollover is needed */
238 rtc_tm_to_time(&now, &t_now);
239 rtc_tm_to_time(&alarm->time, &t_alm);
240 if (t_now < t_alm)
241 goto done;
242 163
243 switch (missing) { 164 if (!rtc->ops)
244 165 err = -ENODEV;
245 /* 24 hour rollover ... if it's now 10am Monday, an alarm that 166 else if (!rtc->ops->set_alarm)
246 * that will trigger at 5am will do so at 5am Tuesday, which 167 err = -EINVAL;
247 * could also be in the next month or year. This is a common 168 else
248 * case, especially for PCs. 169 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
249 */
250 case day:
251 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
252 t_alm += 24 * 60 * 60;
253 rtc_time_to_tm(t_alm, &alarm->time);
254 break;
255
256 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
257 * be next month. An alarm matching on the 30th, 29th, or 28th
258 * may end up in the month after that! Many newer PCs support
259 * this type of alarm.
260 */
261 case month:
262 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
263 do {
264 if (alarm->time.tm_mon < 11)
265 alarm->time.tm_mon++;
266 else {
267 alarm->time.tm_mon = 0;
268 alarm->time.tm_year++;
269 }
270 days = rtc_month_days(alarm->time.tm_mon,
271 alarm->time.tm_year);
272 } while (days < alarm->time.tm_mday);
273 break;
274
275 /* Year rollover ... easy except for leap years! */
276 case year:
277 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
278 do {
279 alarm->time.tm_year++;
280 } while (rtc_valid_tm(&alarm->time) != 0);
281 break;
282
283 default:
284 dev_warn(&rtc->dev, "alarm rollover not handled\n");
285 }
286 170
287done: 171 return err;
288 return 0;
289} 172}
290EXPORT_SYMBOL_GPL(rtc_read_alarm);
291 173
292int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 174int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
293{ 175{
@@ -300,14 +182,14 @@ int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
300 err = mutex_lock_interruptible(&rtc->ops_lock); 182 err = mutex_lock_interruptible(&rtc->ops_lock);
301 if (err) 183 if (err)
302 return err; 184 return err;
303 185 if (rtc->aie_timer.enabled) {
304 if (!rtc->ops) 186 rtc_timer_remove(rtc, &rtc->aie_timer);
305 err = -ENODEV; 187 }
306 else if (!rtc->ops->set_alarm) 188 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
307 err = -EINVAL; 189 rtc->aie_timer.period = ktime_set(0, 0);
308 else 190 if (alarm->enabled) {
309 err = rtc->ops->set_alarm(rtc->dev.parent, alarm); 191 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
310 192 }
311 mutex_unlock(&rtc->ops_lock); 193 mutex_unlock(&rtc->ops_lock);
312 return err; 194 return err;
313} 195}
@@ -319,6 +201,16 @@ int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
319 if (err) 201 if (err)
320 return err; 202 return err;
321 203
204 if (rtc->aie_timer.enabled != enabled) {
205 if (enabled)
206 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
207 else
208 rtc_timer_remove(rtc, &rtc->aie_timer);
209 }
210
211 if (err)
212 return err;
213
322 if (!rtc->ops) 214 if (!rtc->ops)
323 err = -ENODEV; 215 err = -ENODEV;
324 else if (!rtc->ops->alarm_irq_enable) 216 else if (!rtc->ops->alarm_irq_enable)
@@ -337,52 +229,50 @@ int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
337 if (err) 229 if (err)
338 return err; 230 return err;
339 231
340#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 232 /* make sure we're changing state */
341 if (enabled == 0 && rtc->uie_irq_active) { 233 if (rtc->uie_rtctimer.enabled == enabled)
342 mutex_unlock(&rtc->ops_lock); 234 goto out;
343 return rtc_dev_update_irq_enable_emul(rtc, enabled);
344 }
345#endif
346 235
347 if (!rtc->ops) 236 if (enabled) {
348 err = -ENODEV; 237 struct rtc_time tm;
349 else if (!rtc->ops->update_irq_enable) 238 ktime_t now, onesec;
350 err = -EINVAL;
351 else
352 err = rtc->ops->update_irq_enable(rtc->dev.parent, enabled);
353 239
354 mutex_unlock(&rtc->ops_lock); 240 __rtc_read_time(rtc, &tm);
241 onesec = ktime_set(1, 0);
242 now = rtc_tm_to_ktime(tm);
243 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
244 rtc->uie_rtctimer.period = ktime_set(1, 0);
245 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
246 } else
247 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
355 248
356#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 249out:
357 /* 250 mutex_unlock(&rtc->ops_lock);
358 * Enable emulation if the driver did not provide
359 * the update_irq_enable function pointer or if returned
360 * -EINVAL to signal that it has been configured without
361 * interrupts or that are not available at the moment.
362 */
363 if (err == -EINVAL)
364 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
365#endif
366 return err; 251 return err;
252
367} 253}
368EXPORT_SYMBOL_GPL(rtc_update_irq_enable); 254EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
369 255
256
370/** 257/**
371 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs 258 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
372 * @rtc: the rtc device 259 * @rtc: pointer to the rtc device
373 * @num: how many irqs are being reported (usually one) 260 *
374 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF 261 * This function is called when an AIE, UIE or PIE mode interrupt
375 * Context: any 262 * has occured (or been emulated).
263 *
264 * Triggers the registered irq_task function callback.
376 */ 265 */
377void rtc_update_irq(struct rtc_device *rtc, 266static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
378 unsigned long num, unsigned long events)
379{ 267{
380 unsigned long flags; 268 unsigned long flags;
381 269
270 /* mark one irq of the appropriate mode */
382 spin_lock_irqsave(&rtc->irq_lock, flags); 271 spin_lock_irqsave(&rtc->irq_lock, flags);
383 rtc->irq_data = (rtc->irq_data + (num << 8)) | events; 272 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
384 spin_unlock_irqrestore(&rtc->irq_lock, flags); 273 spin_unlock_irqrestore(&rtc->irq_lock, flags);
385 274
275 /* call the task func */
386 spin_lock_irqsave(&rtc->irq_task_lock, flags); 276 spin_lock_irqsave(&rtc->irq_task_lock, flags);
387 if (rtc->irq_task) 277 if (rtc->irq_task)
388 rtc->irq_task->func(rtc->irq_task->private_data); 278 rtc->irq_task->func(rtc->irq_task->private_data);
@@ -391,6 +281,69 @@ void rtc_update_irq(struct rtc_device *rtc,
391 wake_up_interruptible(&rtc->irq_queue); 281 wake_up_interruptible(&rtc->irq_queue);
392 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); 282 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
393} 283}
284
285
286/**
287 * rtc_aie_update_irq - AIE mode rtctimer hook
288 * @private: pointer to the rtc_device
289 *
290 * This functions is called when the aie_timer expires.
291 */
292void rtc_aie_update_irq(void *private)
293{
294 struct rtc_device *rtc = (struct rtc_device *)private;
295 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
296}
297
298
299/**
300 * rtc_uie_update_irq - UIE mode rtctimer hook
301 * @private: pointer to the rtc_device
302 *
303 * This functions is called when the uie_timer expires.
304 */
305void rtc_uie_update_irq(void *private)
306{
307 struct rtc_device *rtc = (struct rtc_device *)private;
308 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
309}
310
311
312/**
313 * rtc_pie_update_irq - PIE mode hrtimer hook
314 * @timer: pointer to the pie mode hrtimer
315 *
316 * This function is used to emulate PIE mode interrupts
317 * using an hrtimer. This function is called when the periodic
318 * hrtimer expires.
319 */
320enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
321{
322 struct rtc_device *rtc;
323 ktime_t period;
324 int count;
325 rtc = container_of(timer, struct rtc_device, pie_timer);
326
327 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
328 count = hrtimer_forward_now(timer, period);
329
330 rtc_handle_legacy_irq(rtc, count, RTC_PF);
331
332 return HRTIMER_RESTART;
333}
334
335/**
336 * rtc_update_irq - Triggered when a RTC interrupt occurs.
337 * @rtc: the rtc device
338 * @num: how many irqs are being reported (usually one)
339 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
340 * Context: any
341 */
342void rtc_update_irq(struct rtc_device *rtc,
343 unsigned long num, unsigned long events)
344{
345 schedule_work(&rtc->irqwork);
346}
394EXPORT_SYMBOL_GPL(rtc_update_irq); 347EXPORT_SYMBOL_GPL(rtc_update_irq);
395 348
396static int __rtc_match(struct device *dev, void *data) 349static int __rtc_match(struct device *dev, void *data)
@@ -477,18 +430,20 @@ int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled
477 int err = 0; 430 int err = 0;
478 unsigned long flags; 431 unsigned long flags;
479 432
480 if (rtc->ops->irq_set_state == NULL)
481 return -ENXIO;
482
483 spin_lock_irqsave(&rtc->irq_task_lock, flags); 433 spin_lock_irqsave(&rtc->irq_task_lock, flags);
484 if (rtc->irq_task != NULL && task == NULL) 434 if (rtc->irq_task != NULL && task == NULL)
485 err = -EBUSY; 435 err = -EBUSY;
486 if (rtc->irq_task != task) 436 if (rtc->irq_task != task)
487 err = -EACCES; 437 err = -EACCES;
488 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
489 438
490 if (err == 0) 439 if (enabled) {
491 err = rtc->ops->irq_set_state(rtc->dev.parent, enabled); 440 ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
441 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
442 } else {
443 hrtimer_cancel(&rtc->pie_timer);
444 }
445 rtc->pie_enabled = enabled;
446 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
492 447
493 return err; 448 return err;
494} 449}
@@ -509,21 +464,203 @@ int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
509 int err = 0; 464 int err = 0;
510 unsigned long flags; 465 unsigned long flags;
511 466
512 if (rtc->ops->irq_set_freq == NULL)
513 return -ENXIO;
514
515 spin_lock_irqsave(&rtc->irq_task_lock, flags); 467 spin_lock_irqsave(&rtc->irq_task_lock, flags);
516 if (rtc->irq_task != NULL && task == NULL) 468 if (rtc->irq_task != NULL && task == NULL)
517 err = -EBUSY; 469 err = -EBUSY;
518 if (rtc->irq_task != task) 470 if (rtc->irq_task != task)
519 err = -EACCES; 471 err = -EACCES;
520 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
521
522 if (err == 0) { 472 if (err == 0) {
523 err = rtc->ops->irq_set_freq(rtc->dev.parent, freq); 473 rtc->irq_freq = freq;
524 if (err == 0) 474 if (rtc->pie_enabled) {
525 rtc->irq_freq = freq; 475 ktime_t period;
476 hrtimer_cancel(&rtc->pie_timer);
477 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
478 hrtimer_start(&rtc->pie_timer, period,
479 HRTIMER_MODE_REL);
480 }
526 } 481 }
482 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
527 return err; 483 return err;
528} 484}
529EXPORT_SYMBOL_GPL(rtc_irq_set_freq); 485EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
486
487/**
488 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
489 * @rtc rtc device
490 * @timer timer being added.
491 *
492 * Enqueues a timer onto the rtc devices timerqueue and sets
493 * the next alarm event appropriately.
494 *
495 * Sets the enabled bit on the added timer.
496 *
497 * Must hold ops_lock for proper serialization of timerqueue
498 */
499static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
500{
501 timer->enabled = 1;
502 timerqueue_add(&rtc->timerqueue, &timer->node);
503 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
504 struct rtc_wkalrm alarm;
505 int err;
506 alarm.time = rtc_ktime_to_tm(timer->node.expires);
507 alarm.enabled = 1;
508 err = __rtc_set_alarm(rtc, &alarm);
509 if (err == -ETIME)
510 schedule_work(&rtc->irqwork);
511 else if (err) {
512 timerqueue_del(&rtc->timerqueue, &timer->node);
513 timer->enabled = 0;
514 return err;
515 }
516 }
517 return 0;
518}
519
520/**
521 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
522 * @rtc rtc device
523 * @timer timer being removed.
524 *
525 * Removes a timer onto the rtc devices timerqueue and sets
526 * the next alarm event appropriately.
527 *
528 * Clears the enabled bit on the removed timer.
529 *
530 * Must hold ops_lock for proper serialization of timerqueue
531 */
532static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
533{
534 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
535 timerqueue_del(&rtc->timerqueue, &timer->node);
536 timer->enabled = 0;
537 if (next == &timer->node) {
538 struct rtc_wkalrm alarm;
539 int err;
540 next = timerqueue_getnext(&rtc->timerqueue);
541 if (!next)
542 return;
543 alarm.time = rtc_ktime_to_tm(next->expires);
544 alarm.enabled = 1;
545 err = __rtc_set_alarm(rtc, &alarm);
546 if (err == -ETIME)
547 schedule_work(&rtc->irqwork);
548 }
549}
550
551/**
552 * rtc_timer_do_work - Expires rtc timers
553 * @rtc rtc device
554 * @timer timer being removed.
555 *
556 * Expires rtc timers. Reprograms next alarm event if needed.
557 * Called via worktask.
558 *
559 * Serializes access to timerqueue via ops_lock mutex
560 */
561void rtc_timer_do_work(struct work_struct *work)
562{
563 struct rtc_timer *timer;
564 struct timerqueue_node *next;
565 ktime_t now;
566 struct rtc_time tm;
567
568 struct rtc_device *rtc =
569 container_of(work, struct rtc_device, irqwork);
570
571 mutex_lock(&rtc->ops_lock);
572again:
573 __rtc_read_time(rtc, &tm);
574 now = rtc_tm_to_ktime(tm);
575 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
576 if (next->expires.tv64 > now.tv64)
577 break;
578
579 /* expire timer */
580 timer = container_of(next, struct rtc_timer, node);
581 timerqueue_del(&rtc->timerqueue, &timer->node);
582 timer->enabled = 0;
583 if (timer->task.func)
584 timer->task.func(timer->task.private_data);
585
586 /* Re-add/fwd periodic timers */
587 if (ktime_to_ns(timer->period)) {
588 timer->node.expires = ktime_add(timer->node.expires,
589 timer->period);
590 timer->enabled = 1;
591 timerqueue_add(&rtc->timerqueue, &timer->node);
592 }
593 }
594
595 /* Set next alarm */
596 if (next) {
597 struct rtc_wkalrm alarm;
598 int err;
599 alarm.time = rtc_ktime_to_tm(next->expires);
600 alarm.enabled = 1;
601 err = __rtc_set_alarm(rtc, &alarm);
602 if (err == -ETIME)
603 goto again;
604 }
605
606 mutex_unlock(&rtc->ops_lock);
607}
608
609
610/* rtc_timer_init - Initializes an rtc_timer
611 * @timer: timer to be intiialized
612 * @f: function pointer to be called when timer fires
613 * @data: private data passed to function pointer
614 *
615 * Kernel interface to initializing an rtc_timer.
616 */
617void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
618{
619 timerqueue_init(&timer->node);
620 timer->enabled = 0;
621 timer->task.func = f;
622 timer->task.private_data = data;
623}
624
625/* rtc_timer_start - Sets an rtc_timer to fire in the future
626 * @ rtc: rtc device to be used
627 * @ timer: timer being set
628 * @ expires: time at which to expire the timer
629 * @ period: period that the timer will recur
630 *
631 * Kernel interface to set an rtc_timer
632 */
633int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
634 ktime_t expires, ktime_t period)
635{
636 int ret = 0;
637 mutex_lock(&rtc->ops_lock);
638 if (timer->enabled)
639 rtc_timer_remove(rtc, timer);
640
641 timer->node.expires = expires;
642 timer->period = period;
643
644 ret = rtc_timer_enqueue(rtc, timer);
645
646 mutex_unlock(&rtc->ops_lock);
647 return ret;
648}
649
650/* rtc_timer_cancel - Stops an rtc_timer
651 * @ rtc: rtc device to be used
652 * @ timer: timer being set
653 *
654 * Kernel interface to cancel an rtc_timer
655 */
656int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
657{
658 int ret = 0;
659 mutex_lock(&rtc->ops_lock);
660 if (timer->enabled)
661 rtc_timer_remove(rtc, timer);
662 mutex_unlock(&rtc->ops_lock);
663 return ret;
664}
665
666
diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index 5856167a0c90..c7ff8df347e7 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -36,6 +36,7 @@
36#include <linux/platform_device.h> 36#include <linux/platform_device.h>
37#include <linux/mod_devicetable.h> 37#include <linux/mod_devicetable.h>
38#include <linux/log2.h> 38#include <linux/log2.h>
39#include <linux/pm.h>
39 40
40/* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */ 41/* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
41#include <asm-generic/rtc.h> 42#include <asm-generic/rtc.h>
@@ -687,7 +688,8 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
687#if defined(CONFIG_ATARI) 688#if defined(CONFIG_ATARI)
688 address_space = 64; 689 address_space = 64;
689#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \ 690#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
690 || defined(__sparc__) || defined(__mips__) 691 || defined(__sparc__) || defined(__mips__) \
692 || defined(__powerpc__)
691 address_space = 128; 693 address_space = 128;
692#else 694#else
693#warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes. 695#warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
@@ -850,7 +852,7 @@ static void __exit cmos_do_remove(struct device *dev)
850 852
851#ifdef CONFIG_PM 853#ifdef CONFIG_PM
852 854
853static int cmos_suspend(struct device *dev, pm_message_t mesg) 855static int cmos_suspend(struct device *dev)
854{ 856{
855 struct cmos_rtc *cmos = dev_get_drvdata(dev); 857 struct cmos_rtc *cmos = dev_get_drvdata(dev);
856 unsigned char tmp; 858 unsigned char tmp;
@@ -898,7 +900,7 @@ static int cmos_suspend(struct device *dev, pm_message_t mesg)
898 */ 900 */
899static inline int cmos_poweroff(struct device *dev) 901static inline int cmos_poweroff(struct device *dev)
900{ 902{
901 return cmos_suspend(dev, PMSG_HIBERNATE); 903 return cmos_suspend(dev);
902} 904}
903 905
904static int cmos_resume(struct device *dev) 906static int cmos_resume(struct device *dev)
@@ -945,9 +947,9 @@ static int cmos_resume(struct device *dev)
945 return 0; 947 return 0;
946} 948}
947 949
950static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
951
948#else 952#else
949#define cmos_suspend NULL
950#define cmos_resume NULL
951 953
952static inline int cmos_poweroff(struct device *dev) 954static inline int cmos_poweroff(struct device *dev)
953{ 955{
@@ -1077,7 +1079,7 @@ static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
1077 1079
1078static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg) 1080static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
1079{ 1081{
1080 return cmos_suspend(&pnp->dev, mesg); 1082 return cmos_suspend(&pnp->dev);
1081} 1083}
1082 1084
1083static int cmos_pnp_resume(struct pnp_dev *pnp) 1085static int cmos_pnp_resume(struct pnp_dev *pnp)
@@ -1157,8 +1159,9 @@ static struct platform_driver cmos_platform_driver = {
1157 .shutdown = cmos_platform_shutdown, 1159 .shutdown = cmos_platform_shutdown,
1158 .driver = { 1160 .driver = {
1159 .name = (char *) driver_name, 1161 .name = (char *) driver_name,
1160 .suspend = cmos_suspend, 1162#ifdef CONFIG_PM
1161 .resume = cmos_resume, 1163 .pm = &cmos_pm_ops,
1164#endif
1162 } 1165 }
1163}; 1166};
1164 1167
diff --git a/drivers/rtc/rtc-dev.c b/drivers/rtc/rtc-dev.c
index 62227cd52410..212b16edafc0 100644
--- a/drivers/rtc/rtc-dev.c
+++ b/drivers/rtc/rtc-dev.c
@@ -46,105 +46,6 @@ static int rtc_dev_open(struct inode *inode, struct file *file)
46 return err; 46 return err;
47} 47}
48 48
49#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
50/*
51 * Routine to poll RTC seconds field for change as often as possible,
52 * after first RTC_UIE use timer to reduce polling
53 */
54static void rtc_uie_task(struct work_struct *work)
55{
56 struct rtc_device *rtc =
57 container_of(work, struct rtc_device, uie_task);
58 struct rtc_time tm;
59 int num = 0;
60 int err;
61
62 err = rtc_read_time(rtc, &tm);
63
64 spin_lock_irq(&rtc->irq_lock);
65 if (rtc->stop_uie_polling || err) {
66 rtc->uie_task_active = 0;
67 } else if (rtc->oldsecs != tm.tm_sec) {
68 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
69 rtc->oldsecs = tm.tm_sec;
70 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
71 rtc->uie_timer_active = 1;
72 rtc->uie_task_active = 0;
73 add_timer(&rtc->uie_timer);
74 } else if (schedule_work(&rtc->uie_task) == 0) {
75 rtc->uie_task_active = 0;
76 }
77 spin_unlock_irq(&rtc->irq_lock);
78 if (num)
79 rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
80}
81static void rtc_uie_timer(unsigned long data)
82{
83 struct rtc_device *rtc = (struct rtc_device *)data;
84 unsigned long flags;
85
86 spin_lock_irqsave(&rtc->irq_lock, flags);
87 rtc->uie_timer_active = 0;
88 rtc->uie_task_active = 1;
89 if ((schedule_work(&rtc->uie_task) == 0))
90 rtc->uie_task_active = 0;
91 spin_unlock_irqrestore(&rtc->irq_lock, flags);
92}
93
94static int clear_uie(struct rtc_device *rtc)
95{
96 spin_lock_irq(&rtc->irq_lock);
97 if (rtc->uie_irq_active) {
98 rtc->stop_uie_polling = 1;
99 if (rtc->uie_timer_active) {
100 spin_unlock_irq(&rtc->irq_lock);
101 del_timer_sync(&rtc->uie_timer);
102 spin_lock_irq(&rtc->irq_lock);
103 rtc->uie_timer_active = 0;
104 }
105 if (rtc->uie_task_active) {
106 spin_unlock_irq(&rtc->irq_lock);
107 flush_scheduled_work();
108 spin_lock_irq(&rtc->irq_lock);
109 }
110 rtc->uie_irq_active = 0;
111 }
112 spin_unlock_irq(&rtc->irq_lock);
113 return 0;
114}
115
116static int set_uie(struct rtc_device *rtc)
117{
118 struct rtc_time tm;
119 int err;
120
121 err = rtc_read_time(rtc, &tm);
122 if (err)
123 return err;
124 spin_lock_irq(&rtc->irq_lock);
125 if (!rtc->uie_irq_active) {
126 rtc->uie_irq_active = 1;
127 rtc->stop_uie_polling = 0;
128 rtc->oldsecs = tm.tm_sec;
129 rtc->uie_task_active = 1;
130 if (schedule_work(&rtc->uie_task) == 0)
131 rtc->uie_task_active = 0;
132 }
133 rtc->irq_data = 0;
134 spin_unlock_irq(&rtc->irq_lock);
135 return 0;
136}
137
138int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
139{
140 if (enabled)
141 return set_uie(rtc);
142 else
143 return clear_uie(rtc);
144}
145EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
146
147#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
148 49
149static ssize_t 50static ssize_t
150rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 51rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
@@ -493,11 +394,6 @@ void rtc_dev_prepare(struct rtc_device *rtc)
493 394
494 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id); 395 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
495 396
496#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
497 INIT_WORK(&rtc->uie_task, rtc_uie_task);
498 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
499#endif
500
501 cdev_init(&rtc->char_dev, &rtc_dev_fops); 397 cdev_init(&rtc->char_dev, &rtc_dev_fops);
502 rtc->char_dev.owner = rtc->owner; 398 rtc->char_dev.owner = rtc->owner;
503} 399}
diff --git a/drivers/rtc/rtc-ds1305.c b/drivers/rtc/rtc-ds1305.c
index 48da85e97ca4..077af1d7b9e4 100644
--- a/drivers/rtc/rtc-ds1305.c
+++ b/drivers/rtc/rtc-ds1305.c
@@ -813,7 +813,7 @@ static int __devexit ds1305_remove(struct spi_device *spi)
813 if (spi->irq) { 813 if (spi->irq) {
814 set_bit(FLAG_EXITING, &ds1305->flags); 814 set_bit(FLAG_EXITING, &ds1305->flags);
815 free_irq(spi->irq, ds1305); 815 free_irq(spi->irq, ds1305);
816 flush_scheduled_work(); 816 cancel_work_sync(&ds1305->work);
817 } 817 }
818 818
819 rtc_device_unregister(ds1305->rtc); 819 rtc_device_unregister(ds1305->rtc);
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index d827ce570a8c..0d559b6416dd 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -106,9 +106,9 @@ struct ds1307 {
106 struct i2c_client *client; 106 struct i2c_client *client;
107 struct rtc_device *rtc; 107 struct rtc_device *rtc;
108 struct work_struct work; 108 struct work_struct work;
109 s32 (*read_block_data)(struct i2c_client *client, u8 command, 109 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
110 u8 length, u8 *values); 110 u8 length, u8 *values);
111 s32 (*write_block_data)(struct i2c_client *client, u8 command, 111 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
112 u8 length, const u8 *values); 112 u8 length, const u8 *values);
113}; 113};
114 114
@@ -158,8 +158,8 @@ MODULE_DEVICE_TABLE(i2c, ds1307_id);
158 158
159#define BLOCK_DATA_MAX_TRIES 10 159#define BLOCK_DATA_MAX_TRIES 10
160 160
161static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command, 161static s32 ds1307_read_block_data_once(const struct i2c_client *client,
162 u8 length, u8 *values) 162 u8 command, u8 length, u8 *values)
163{ 163{
164 s32 i, data; 164 s32 i, data;
165 165
@@ -172,7 +172,7 @@ static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command,
172 return i; 172 return i;
173} 173}
174 174
175static s32 ds1307_read_block_data(struct i2c_client *client, u8 command, 175static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
176 u8 length, u8 *values) 176 u8 length, u8 *values)
177{ 177{
178 u8 oldvalues[I2C_SMBUS_BLOCK_MAX]; 178 u8 oldvalues[I2C_SMBUS_BLOCK_MAX];
@@ -198,7 +198,7 @@ static s32 ds1307_read_block_data(struct i2c_client *client, u8 command,
198 return length; 198 return length;
199} 199}
200 200
201static s32 ds1307_write_block_data(struct i2c_client *client, u8 command, 201static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
202 u8 length, const u8 *values) 202 u8 length, const u8 *values)
203{ 203{
204 u8 currvalues[I2C_SMBUS_BLOCK_MAX]; 204 u8 currvalues[I2C_SMBUS_BLOCK_MAX];
diff --git a/drivers/rtc/rtc-ds1374.c b/drivers/rtc/rtc-ds1374.c
index 1f0007fd4431..47fb6357c346 100644
--- a/drivers/rtc/rtc-ds1374.c
+++ b/drivers/rtc/rtc-ds1374.c
@@ -417,7 +417,7 @@ static int __devexit ds1374_remove(struct i2c_client *client)
417 mutex_unlock(&ds1374->mutex); 417 mutex_unlock(&ds1374->mutex);
418 418
419 free_irq(client->irq, client); 419 free_irq(client->irq, client);
420 flush_scheduled_work(); 420 cancel_work_sync(&ds1374->work);
421 } 421 }
422 422
423 rtc_device_unregister(ds1374->rtc); 423 rtc_device_unregister(ds1374->rtc);
diff --git a/drivers/rtc/rtc-ds3232.c b/drivers/rtc/rtc-ds3232.c
index 57063552d3b7..23a9ee19764c 100644
--- a/drivers/rtc/rtc-ds3232.c
+++ b/drivers/rtc/rtc-ds3232.c
@@ -463,7 +463,7 @@ static int __devexit ds3232_remove(struct i2c_client *client)
463 mutex_unlock(&ds3232->mutex); 463 mutex_unlock(&ds3232->mutex);
464 464
465 free_irq(client->irq, client); 465 free_irq(client->irq, client);
466 flush_scheduled_work(); 466 cancel_work_sync(&ds3232->work);
467 } 467 }
468 468
469 rtc_device_unregister(ds3232->rtc); 469 rtc_device_unregister(ds3232->rtc);
diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index 773851f338b8..075f1708deae 100644
--- a/drivers/rtc/rtc-lib.c
+++ b/drivers/rtc/rtc-lib.c
@@ -117,4 +117,32 @@ int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
117} 117}
118EXPORT_SYMBOL(rtc_tm_to_time); 118EXPORT_SYMBOL(rtc_tm_to_time);
119 119
120/*
121 * Convert rtc_time to ktime
122 */
123ktime_t rtc_tm_to_ktime(struct rtc_time tm)
124{
125 time_t time;
126 rtc_tm_to_time(&tm, &time);
127 return ktime_set(time, 0);
128}
129EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
130
131/*
132 * Convert ktime to rtc_time
133 */
134struct rtc_time rtc_ktime_to_tm(ktime_t kt)
135{
136 struct timespec ts;
137 struct rtc_time ret;
138
139 ts = ktime_to_timespec(kt);
140 /* Round up any ns */
141 if (ts.tv_nsec)
142 ts.tv_sec++;
143 rtc_time_to_tm(ts.tv_sec, &ret);
144 return ret;
145}
146EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
147
120MODULE_LICENSE("GPL"); 148MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-max6902.c b/drivers/rtc/rtc-max6902.c
index 657403ebd54a..0ec3f588a255 100644
--- a/drivers/rtc/rtc-max6902.c
+++ b/drivers/rtc/rtc-max6902.c
@@ -139,12 +139,13 @@ static int __devinit max6902_probe(struct spi_device *spi)
139 if (IS_ERR(rtc)) 139 if (IS_ERR(rtc))
140 return PTR_ERR(rtc); 140 return PTR_ERR(rtc);
141 141
142 dev_set_drvdata(&spi->dev, rtc);
142 return 0; 143 return 0;
143} 144}
144 145
145static int __devexit max6902_remove(struct spi_device *spi) 146static int __devexit max6902_remove(struct spi_device *spi)
146{ 147{
147 struct rtc_device *rtc = platform_get_drvdata(spi); 148 struct rtc_device *rtc = dev_get_drvdata(&spi->dev);
148 149
149 rtc_device_unregister(rtc); 150 rtc_device_unregister(rtc);
150 return 0; 151 return 0;
diff --git a/drivers/rtc/rtc-max8998.c b/drivers/rtc/rtc-max8998.c
index f22dee35f330..3f7bc6b9fefa 100644
--- a/drivers/rtc/rtc-max8998.c
+++ b/drivers/rtc/rtc-max8998.c
@@ -20,6 +20,7 @@
20#include <linux/platform_device.h> 20#include <linux/platform_device.h>
21#include <linux/mfd/max8998.h> 21#include <linux/mfd/max8998.h>
22#include <linux/mfd/max8998-private.h> 22#include <linux/mfd/max8998-private.h>
23#include <linux/delay.h>
23 24
24#define MAX8998_RTC_SEC 0x00 25#define MAX8998_RTC_SEC 0x00
25#define MAX8998_RTC_MIN 0x01 26#define MAX8998_RTC_MIN 0x01
@@ -73,6 +74,7 @@ struct max8998_rtc_info {
73 struct i2c_client *rtc; 74 struct i2c_client *rtc;
74 struct rtc_device *rtc_dev; 75 struct rtc_device *rtc_dev;
75 int irq; 76 int irq;
77 bool lp3974_bug_workaround;
76}; 78};
77 79
78static void max8998_data_to_tm(u8 *data, struct rtc_time *tm) 80static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
@@ -124,10 +126,16 @@ static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
124{ 126{
125 struct max8998_rtc_info *info = dev_get_drvdata(dev); 127 struct max8998_rtc_info *info = dev_get_drvdata(dev);
126 u8 data[8]; 128 u8 data[8];
129 int ret;
127 130
128 max8998_tm_to_data(tm, data); 131 max8998_tm_to_data(tm, data);
129 132
130 return max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data); 133 ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
134
135 if (info->lp3974_bug_workaround)
136 msleep(2000);
137
138 return ret;
131} 139}
132 140
133static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 141static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
@@ -163,12 +171,29 @@ static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
163 171
164static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info) 172static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
165{ 173{
166 return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0); 174 int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
175
176 if (info->lp3974_bug_workaround)
177 msleep(2000);
178
179 return ret;
167} 180}
168 181
169static int max8998_rtc_start_alarm(struct max8998_rtc_info *info) 182static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
170{ 183{
171 return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0x77); 184 int ret;
185 u8 alarm0_conf = 0x77;
186
187 /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
188 if (info->lp3974_bug_workaround)
189 alarm0_conf = 0x57;
190
191 ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
192
193 if (info->lp3974_bug_workaround)
194 msleep(2000);
195
196 return ret;
172} 197}
173 198
174static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 199static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
@@ -187,10 +212,13 @@ static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
187 if (ret < 0) 212 if (ret < 0)
188 return ret; 213 return ret;
189 214
215 if (info->lp3974_bug_workaround)
216 msleep(2000);
217
190 if (alrm->enabled) 218 if (alrm->enabled)
191 return max8998_rtc_start_alarm(info); 219 ret = max8998_rtc_start_alarm(info);
192 220
193 return 0; 221 return ret;
194} 222}
195 223
196static int max8998_rtc_alarm_irq_enable(struct device *dev, 224static int max8998_rtc_alarm_irq_enable(struct device *dev,
@@ -224,6 +252,7 @@ static const struct rtc_class_ops max8998_rtc_ops = {
224static int __devinit max8998_rtc_probe(struct platform_device *pdev) 252static int __devinit max8998_rtc_probe(struct platform_device *pdev)
225{ 253{
226 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent); 254 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
255 struct max8998_platform_data *pdata = dev_get_platdata(max8998->dev);
227 struct max8998_rtc_info *info; 256 struct max8998_rtc_info *info;
228 int ret; 257 int ret;
229 258
@@ -249,10 +278,18 @@ static int __devinit max8998_rtc_probe(struct platform_device *pdev)
249 278
250 ret = request_threaded_irq(info->irq, NULL, max8998_rtc_alarm_irq, 0, 279 ret = request_threaded_irq(info->irq, NULL, max8998_rtc_alarm_irq, 0,
251 "rtc-alarm0", info); 280 "rtc-alarm0", info);
281
252 if (ret < 0) 282 if (ret < 0)
253 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", 283 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
254 info->irq, ret); 284 info->irq, ret);
255 285
286 dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
287 if (pdata->rtc_delay) {
288 info->lp3974_bug_workaround = true;
289 dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
290 " RTC updates will be extremely slow.\n");
291 }
292
256 return 0; 293 return 0;
257 294
258out_rtc: 295out_rtc:
@@ -273,6 +310,12 @@ static int __devexit max8998_rtc_remove(struct platform_device *pdev)
273 return 0; 310 return 0;
274} 311}
275 312
313static const struct platform_device_id max8998_rtc_id[] = {
314 { "max8998-rtc", TYPE_MAX8998 },
315 { "lp3974-rtc", TYPE_LP3974 },
316 { }
317};
318
276static struct platform_driver max8998_rtc_driver = { 319static struct platform_driver max8998_rtc_driver = {
277 .driver = { 320 .driver = {
278 .name = "max8998-rtc", 321 .name = "max8998-rtc",
@@ -280,6 +323,7 @@ static struct platform_driver max8998_rtc_driver = {
280 }, 323 },
281 .probe = max8998_rtc_probe, 324 .probe = max8998_rtc_probe,
282 .remove = __devexit_p(max8998_rtc_remove), 325 .remove = __devexit_p(max8998_rtc_remove),
326 .id_table = max8998_rtc_id,
283}; 327};
284 328
285static int __init max8998_rtc_init(void) 329static int __init max8998_rtc_init(void)
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
index 73377b0d65da..e72b523c79a5 100644
--- a/drivers/rtc/rtc-omap.c
+++ b/drivers/rtc/rtc-omap.c
@@ -429,13 +429,14 @@ fail1:
429fail0: 429fail0:
430 iounmap(rtc_base); 430 iounmap(rtc_base);
431fail: 431fail:
432 release_resource(mem); 432 release_mem_region(mem->start, resource_size(mem));
433 return -EIO; 433 return -EIO;
434} 434}
435 435
436static int __exit omap_rtc_remove(struct platform_device *pdev) 436static int __exit omap_rtc_remove(struct platform_device *pdev)
437{ 437{
438 struct rtc_device *rtc = platform_get_drvdata(pdev); 438 struct rtc_device *rtc = platform_get_drvdata(pdev);
439 struct resource *mem = dev_get_drvdata(&rtc->dev);
439 440
440 device_init_wakeup(&pdev->dev, 0); 441 device_init_wakeup(&pdev->dev, 0);
441 442
@@ -447,8 +448,9 @@ static int __exit omap_rtc_remove(struct platform_device *pdev)
447 if (omap_rtc_timer != omap_rtc_alarm) 448 if (omap_rtc_timer != omap_rtc_alarm)
448 free_irq(omap_rtc_alarm, rtc); 449 free_irq(omap_rtc_alarm, rtc);
449 450
450 release_resource(dev_get_drvdata(&rtc->dev));
451 rtc_device_unregister(rtc); 451 rtc_device_unregister(rtc);
452 iounmap(rtc_base);
453 release_mem_region(mem->start, resource_size(mem));
452 return 0; 454 return 0;
453} 455}
454 456
diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c
index 90cf0a6ff23e..dd14e202c2c8 100644
--- a/drivers/rtc/rtc-rs5c372.c
+++ b/drivers/rtc/rtc-rs5c372.c
@@ -207,7 +207,7 @@ static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
207static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm) 207static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
208{ 208{
209 struct rs5c372 *rs5c = i2c_get_clientdata(client); 209 struct rs5c372 *rs5c = i2c_get_clientdata(client);
210 unsigned char buf[8]; 210 unsigned char buf[7];
211 int addr; 211 int addr;
212 212
213 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d " 213 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
diff --git a/drivers/rtc/rtc-rx8025.c b/drivers/rtc/rtc-rx8025.c
index 1146e3522d3c..af32a62e12a8 100644
--- a/drivers/rtc/rtc-rx8025.c
+++ b/drivers/rtc/rtc-rx8025.c
@@ -650,7 +650,7 @@ static int __devexit rx8025_remove(struct i2c_client *client)
650 mutex_unlock(lock); 650 mutex_unlock(lock);
651 651
652 free_irq(client->irq, client); 652 free_irq(client->irq, client);
653 flush_scheduled_work(); 653 cancel_work_sync(&rx8025->work);
654 } 654 }
655 655
656 rx8025_sysfs_unregister(&client->dev); 656 rx8025_sysfs_unregister(&client->dev);
diff --git a/drivers/rtc/rtc-sa1100.c b/drivers/rtc/rtc-sa1100.c
index e4a44b641702..88ea52b8647a 100644
--- a/drivers/rtc/rtc-sa1100.c
+++ b/drivers/rtc/rtc-sa1100.c
@@ -39,10 +39,10 @@
39#include <mach/regs-ost.h> 39#include <mach/regs-ost.h>
40#endif 40#endif
41 41
42#define RTC_DEF_DIVIDER 32768 - 1 42#define RTC_DEF_DIVIDER (32768 - 1)
43#define RTC_DEF_TRIM 0 43#define RTC_DEF_TRIM 0
44 44
45static unsigned long rtc_freq = 1024; 45static const unsigned long RTC_FREQ = 1024;
46static unsigned long timer_freq; 46static unsigned long timer_freq;
47static struct rtc_time rtc_alarm; 47static struct rtc_time rtc_alarm;
48static DEFINE_SPINLOCK(sa1100_rtc_lock); 48static DEFINE_SPINLOCK(sa1100_rtc_lock);
@@ -61,7 +61,8 @@ static inline int rtc_periodic_alarm(struct rtc_time *tm)
61 * Calculate the next alarm time given the requested alarm time mask 61 * Calculate the next alarm time given the requested alarm time mask
62 * and the current time. 62 * and the current time.
63 */ 63 */
64static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm) 64static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
65 struct rtc_time *alrm)
65{ 66{
66 unsigned long next_time; 67 unsigned long next_time;
67 unsigned long now_time; 68 unsigned long now_time;
@@ -116,7 +117,23 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
116 rtsr = RTSR; 117 rtsr = RTSR;
117 /* clear interrupt sources */ 118 /* clear interrupt sources */
118 RTSR = 0; 119 RTSR = 0;
119 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2); 120 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
121 * See also the comments in sa1100_rtc_probe(). */
122 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
123 /* This is the original code, before there was the if test
124 * above. This code does not clear interrupts that were not
125 * enabled. */
126 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
127 } else {
128 /* For some reason, it is possible to enter this routine
129 * without interruptions enabled, it has been tested with
130 * several units (Bug in SA11xx chip?).
131 *
132 * This situation leads to an infinite "loop" of interrupt
133 * routine calling and as a result the processor seems to
134 * lock on its first call to open(). */
135 RTSR = RTSR_AL | RTSR_HZ;
136 }
120 137
121 /* clear alarm interrupt if it has occurred */ 138 /* clear alarm interrupt if it has occurred */
122 if (rtsr & RTSR_AL) 139 if (rtsr & RTSR_AL)
@@ -139,8 +156,58 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
139 return IRQ_HANDLED; 156 return IRQ_HANDLED;
140} 157}
141 158
159static int sa1100_irq_set_freq(struct device *dev, int freq)
160{
161 if (freq < 1 || freq > timer_freq) {
162 return -EINVAL;
163 } else {
164 struct rtc_device *rtc = (struct rtc_device *)dev;
165
166 rtc->irq_freq = freq;
167
168 return 0;
169 }
170}
171
142static int rtc_timer1_count; 172static int rtc_timer1_count;
143 173
174static int sa1100_irq_set_state(struct device *dev, int enabled)
175{
176 spin_lock_irq(&sa1100_rtc_lock);
177 if (enabled) {
178 struct rtc_device *rtc = (struct rtc_device *)dev;
179
180 OSMR1 = timer_freq / rtc->irq_freq + OSCR;
181 OIER |= OIER_E1;
182 rtc_timer1_count = 1;
183 } else {
184 OIER &= ~OIER_E1;
185 }
186 spin_unlock_irq(&sa1100_rtc_lock);
187
188 return 0;
189}
190
191static inline int sa1100_timer1_retrigger(struct rtc_device *rtc)
192{
193 unsigned long diff;
194 unsigned long period = timer_freq / rtc->irq_freq;
195
196 spin_lock_irq(&sa1100_rtc_lock);
197
198 do {
199 OSMR1 += period;
200 diff = OSMR1 - OSCR;
201 /* If OSCR > OSMR1, diff is a very large number (unsigned
202 * math). This means we have a lost interrupt. */
203 } while (diff > period);
204 OIER |= OIER_E1;
205
206 spin_unlock_irq(&sa1100_rtc_lock);
207
208 return 0;
209}
210
144static irqreturn_t timer1_interrupt(int irq, void *dev_id) 211static irqreturn_t timer1_interrupt(int irq, void *dev_id)
145{ 212{
146 struct platform_device *pdev = to_platform_device(dev_id); 213 struct platform_device *pdev = to_platform_device(dev_id);
@@ -158,7 +225,11 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id)
158 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF); 225 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
159 226
160 if (rtc_timer1_count == 1) 227 if (rtc_timer1_count == 1)
161 rtc_timer1_count = (rtc_freq * ((1 << 30) / (timer_freq >> 2))); 228 rtc_timer1_count =
229 (rtc->irq_freq * ((1 << 30) / (timer_freq >> 2)));
230
231 /* retrigger. */
232 sa1100_timer1_retrigger(rtc);
162 233
163 return IRQ_HANDLED; 234 return IRQ_HANDLED;
164} 235}
@@ -166,8 +237,10 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id)
166static int sa1100_rtc_read_callback(struct device *dev, int data) 237static int sa1100_rtc_read_callback(struct device *dev, int data)
167{ 238{
168 if (data & RTC_PF) { 239 if (data & RTC_PF) {
240 struct rtc_device *rtc = (struct rtc_device *)dev;
241
169 /* interpolate missed periods and set match for the next */ 242 /* interpolate missed periods and set match for the next */
170 unsigned long period = timer_freq / rtc_freq; 243 unsigned long period = timer_freq / rtc->irq_freq;
171 unsigned long oscr = OSCR; 244 unsigned long oscr = OSCR;
172 unsigned long osmr1 = OSMR1; 245 unsigned long osmr1 = OSMR1;
173 unsigned long missed = (oscr - osmr1)/period; 246 unsigned long missed = (oscr - osmr1)/period;
@@ -178,7 +251,7 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
178 * Here we compare (match - OSCR) 8 instead of 0 -- 251 * Here we compare (match - OSCR) 8 instead of 0 --
179 * see comment in pxa_timer_interrupt() for explanation. 252 * see comment in pxa_timer_interrupt() for explanation.
180 */ 253 */
181 while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) { 254 while ((signed long)((osmr1 = OSMR1) - OSCR) <= 8) {
182 data += 0x100; 255 data += 0x100;
183 OSSR = OSSR_M1; /* clear match on timer 1 */ 256 OSSR = OSSR_M1; /* clear match on timer 1 */
184 OSMR1 = osmr1 + period; 257 OSMR1 = osmr1 + period;
@@ -190,25 +263,29 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
190static int sa1100_rtc_open(struct device *dev) 263static int sa1100_rtc_open(struct device *dev)
191{ 264{
192 int ret; 265 int ret;
266 struct rtc_device *rtc = (struct rtc_device *)dev;
193 267
194 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED, 268 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
195 "rtc 1Hz", dev); 269 "rtc 1Hz", dev);
196 if (ret) { 270 if (ret) {
197 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz); 271 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
198 goto fail_ui; 272 goto fail_ui;
199 } 273 }
200 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED, 274 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
201 "rtc Alrm", dev); 275 "rtc Alrm", dev);
202 if (ret) { 276 if (ret) {
203 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm); 277 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
204 goto fail_ai; 278 goto fail_ai;
205 } 279 }
206 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED, 280 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
207 "rtc timer", dev); 281 "rtc timer", dev);
208 if (ret) { 282 if (ret) {
209 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1); 283 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
210 goto fail_pi; 284 goto fail_pi;
211 } 285 }
286 rtc->max_user_freq = RTC_FREQ;
287 sa1100_irq_set_freq(dev, RTC_FREQ);
288
212 return 0; 289 return 0;
213 290
214 fail_pi: 291 fail_pi:
@@ -236,7 +313,7 @@ static void sa1100_rtc_release(struct device *dev)
236static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd, 313static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
237 unsigned long arg) 314 unsigned long arg)
238{ 315{
239 switch(cmd) { 316 switch (cmd) {
240 case RTC_AIE_OFF: 317 case RTC_AIE_OFF:
241 spin_lock_irq(&sa1100_rtc_lock); 318 spin_lock_irq(&sa1100_rtc_lock);
242 RTSR &= ~RTSR_ALE; 319 RTSR &= ~RTSR_ALE;
@@ -257,25 +334,6 @@ static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
257 RTSR |= RTSR_HZE; 334 RTSR |= RTSR_HZE;
258 spin_unlock_irq(&sa1100_rtc_lock); 335 spin_unlock_irq(&sa1100_rtc_lock);
259 return 0; 336 return 0;
260 case RTC_PIE_OFF:
261 spin_lock_irq(&sa1100_rtc_lock);
262 OIER &= ~OIER_E1;
263 spin_unlock_irq(&sa1100_rtc_lock);
264 return 0;
265 case RTC_PIE_ON:
266 spin_lock_irq(&sa1100_rtc_lock);
267 OSMR1 = timer_freq / rtc_freq + OSCR;
268 OIER |= OIER_E1;
269 rtc_timer1_count = 1;
270 spin_unlock_irq(&sa1100_rtc_lock);
271 return 0;
272 case RTC_IRQP_READ:
273 return put_user(rtc_freq, (unsigned long *)arg);
274 case RTC_IRQP_SET:
275 if (arg < 1 || arg > timer_freq)
276 return -EINVAL;
277 rtc_freq = arg;
278 return 0;
279 } 337 }
280 return -ENOIOCTLCMD; 338 return -ENOIOCTLCMD;
281} 339}
@@ -327,12 +385,15 @@ static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
327 385
328static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) 386static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
329{ 387{
388 struct rtc_device *rtc = (struct rtc_device *)dev;
389
330 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR); 390 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
331 seq_printf(seq, "update_IRQ\t: %s\n", 391 seq_printf(seq, "update_IRQ\t: %s\n",
332 (RTSR & RTSR_HZE) ? "yes" : "no"); 392 (RTSR & RTSR_HZE) ? "yes" : "no");
333 seq_printf(seq, "periodic_IRQ\t: %s\n", 393 seq_printf(seq, "periodic_IRQ\t: %s\n",
334 (OIER & OIER_E1) ? "yes" : "no"); 394 (OIER & OIER_E1) ? "yes" : "no");
335 seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq); 395 seq_printf(seq, "periodic_freq\t: %d\n", rtc->irq_freq);
396 seq_printf(seq, "RTSR\t\t: 0x%08x\n", (u32)RTSR);
336 397
337 return 0; 398 return 0;
338} 399}
@@ -347,6 +408,8 @@ static const struct rtc_class_ops sa1100_rtc_ops = {
347 .read_alarm = sa1100_rtc_read_alarm, 408 .read_alarm = sa1100_rtc_read_alarm,
348 .set_alarm = sa1100_rtc_set_alarm, 409 .set_alarm = sa1100_rtc_set_alarm,
349 .proc = sa1100_rtc_proc, 410 .proc = sa1100_rtc_proc,
411 .irq_set_freq = sa1100_irq_set_freq,
412 .irq_set_state = sa1100_irq_set_state,
350}; 413};
351 414
352static int sa1100_rtc_probe(struct platform_device *pdev) 415static int sa1100_rtc_probe(struct platform_device *pdev)
@@ -364,7 +427,8 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
364 */ 427 */
365 if (RTTR == 0) { 428 if (RTTR == 0) {
366 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); 429 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
367 dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n"); 430 dev_warn(&pdev->dev, "warning: "
431 "initializing default clock divider/trim value\n");
368 /* The current RTC value probably doesn't make sense either */ 432 /* The current RTC value probably doesn't make sense either */
369 RCNR = 0; 433 RCNR = 0;
370 } 434 }
@@ -372,13 +436,42 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
372 device_init_wakeup(&pdev->dev, 1); 436 device_init_wakeup(&pdev->dev, 1);
373 437
374 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops, 438 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
375 THIS_MODULE); 439 THIS_MODULE);
376 440
377 if (IS_ERR(rtc)) 441 if (IS_ERR(rtc))
378 return PTR_ERR(rtc); 442 return PTR_ERR(rtc);
379 443
380 platform_set_drvdata(pdev, rtc); 444 platform_set_drvdata(pdev, rtc);
381 445
446 /* Set the irq_freq */
447 /*TODO: Find out who is messing with this value after we initialize
448 * it here.*/
449 rtc->irq_freq = RTC_FREQ;
450
451 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
452 * See also the comments in sa1100_rtc_interrupt().
453 *
454 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
455 * interrupt pending, even though interrupts were never enabled.
456 * In this case, this bit it must be reset before enabling
457 * interruptions to avoid a nonexistent interrupt to occur.
458 *
459 * In principle, the same problem would apply to bit 0, although it has
460 * never been observed to happen.
461 *
462 * This issue is addressed both here and in sa1100_rtc_interrupt().
463 * If the issue is not addressed here, in the times when the processor
464 * wakes up with the bit set there will be one spurious interrupt.
465 *
466 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
467 * safe side, once the condition that lead to this strange
468 * initialization is unknown and could in principle happen during
469 * normal processing.
470 *
471 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
472 * the corresponding bits in RTSR. */
473 RTSR = RTSR_AL | RTSR_HZ;
474
382 return 0; 475 return 0;
383} 476}
384 477
@@ -386,7 +479,7 @@ static int sa1100_rtc_remove(struct platform_device *pdev)
386{ 479{
387 struct rtc_device *rtc = platform_get_drvdata(pdev); 480 struct rtc_device *rtc = platform_get_drvdata(pdev);
388 481
389 if (rtc) 482 if (rtc)
390 rtc_device_unregister(rtc); 483 rtc_device_unregister(rtc);
391 484
392 return 0; 485 return 0;
diff --git a/drivers/rtc/rtc-sh.c b/drivers/rtc/rtc-sh.c
index 5efbd5990ff8..06e41ed93230 100644
--- a/drivers/rtc/rtc-sh.c
+++ b/drivers/rtc/rtc-sh.c
@@ -761,7 +761,7 @@ err_unmap:
761 clk_put(rtc->clk); 761 clk_put(rtc->clk);
762 iounmap(rtc->regbase); 762 iounmap(rtc->regbase);
763err_badmap: 763err_badmap:
764 release_resource(rtc->res); 764 release_mem_region(rtc->res->start, rtc->regsize);
765err_badres: 765err_badres:
766 kfree(rtc); 766 kfree(rtc);
767 767
@@ -786,7 +786,7 @@ static int __exit sh_rtc_remove(struct platform_device *pdev)
786 } 786 }
787 787
788 iounmap(rtc->regbase); 788 iounmap(rtc->regbase);
789 release_resource(rtc->res); 789 release_mem_region(rtc->res->start, rtc->regsize);
790 790
791 clk_disable(rtc->clk); 791 clk_disable(rtc->clk);
792 clk_put(rtc->clk); 792 clk_put(rtc->clk);