Blame view

drivers/rtc/rtc-bfin.c 13.1 KB
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
1
2
  /*
   * Blackfin On-Chip Real Time Clock Driver
9980060ba   Mike Frysinger   bfin: delay IRQ r...
3
   *  Supports BF51x/BF52x/BF53[123]/BF53[467]/BF54x
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
4
   *
d7c7ef908   Mike Frysinger   rtc-bfin: add deb...
5
   * Copyright 2004-2010 Analog Devices Inc.
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   *
   * Enter bugs at http://blackfin.uclinux.org/
   *
   * Licensed under the GPL-2 or later.
   */
  
  /* The biggest issue we deal with in this driver is that register writes are
   * synced to the RTC frequency of 1Hz.  So if you write to a register and
   * attempt to write again before the first write has completed, the new write
   * is simply discarded.  This can easily be troublesome if userspace disables
   * one event (say periodic) and then right after enables an event (say alarm).
   * Since all events are maintained in the same interrupt mask register, if
   * we wrote to it to disable the first event and then wrote to it again to
   * enable the second event, that second event would not be enabled as the
   * write would be discarded and things quickly fall apart.
   *
   * To keep this delay from significantly degrading performance (we, in theory,
25985edce   Lucas De Marchi   Fix common misspe...
23
   * would have to sleep for up to 1 second every time we wanted to write a
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
24
   * register), we only check the write pending status before we start to issue
25985edce   Lucas De Marchi   Fix common misspe...
25
   * a new write.  We bank on the idea that it doesn't matter when the sync
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
26
27
28
29
30
31
32
33
   * happens so long as we don't attempt another write before it does.  The only
   * time userspace would take this penalty is when they try and do multiple
   * operations right after another ... but in this case, they need to take the
   * sync penalty, so we should be OK.
   *
   * Also note that the RTC_ISTAT register does not suffer this penalty; its
   * writes to clear status registers complete immediately.
   */
26cb8bb21   Mike Frysinger   blackfin RTC driv...
34
35
36
37
38
39
40
41
  /* It may seem odd that there is no SWCNT code in here (which would be exposed
   * via the periodic interrupt event, or PIE).  Since the Blackfin RTC peripheral
   * runs in units of seconds (N/HZ) but the Linux framework runs in units of HZ
   * (2^N HZ), there is no point in keeping code that only provides 1 HZ PIEs.
   * The same exact behavior can be accomplished by using the update interrupt
   * event (UIE).  Maybe down the line the RTC peripheral will suck less in which
   * case we can re-introduce PIE support.
   */
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
42
  #include <linux/bcd.h>
095b9d546   Mike Frysinger   Blackfin RTC driv...
43
44
  #include <linux/completion.h>
  #include <linux/delay.h>
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
45
  #include <linux/init.h>
095b9d546   Mike Frysinger   Blackfin RTC driv...
46
47
48
  #include <linux/interrupt.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
49
  #include <linux/platform_device.h>
095b9d546   Mike Frysinger   Blackfin RTC driv...
50
  #include <linux/rtc.h>
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
51
  #include <linux/seq_file.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
52
  #include <linux/slab.h>
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
53
54
  
  #include <asm/blackfin.h>
5438de442   Mike Frysinger   Blackfin RTC driv...
55
56
  #define dev_dbg_stamp(dev) dev_dbg(dev, "%s:%i: here i am
  ", __func__, __LINE__)
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
57
58
59
60
  
  struct bfin_rtc {
  	struct rtc_device *rtc_dev;
  	struct rtc_time rtc_alarm;
095b9d546   Mike Frysinger   Blackfin RTC driv...
61
  	u16 rtc_wrote_regs;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  };
  
  /* Bit values for the ISTAT / ICTL registers */
  #define RTC_ISTAT_WRITE_COMPLETE  0x8000
  #define RTC_ISTAT_WRITE_PENDING   0x4000
  #define RTC_ISTAT_ALARM_DAY       0x0040
  #define RTC_ISTAT_24HR            0x0020
  #define RTC_ISTAT_HOUR            0x0010
  #define RTC_ISTAT_MIN             0x0008
  #define RTC_ISTAT_SEC             0x0004
  #define RTC_ISTAT_ALARM           0x0002
  #define RTC_ISTAT_STOPWATCH       0x0001
  
  /* Shift values for RTC_STAT register */
  #define DAY_BITS_OFF    17
  #define HOUR_BITS_OFF   12
  #define MIN_BITS_OFF    6
  #define SEC_BITS_OFF    0
  
  /* Some helper functions to convert between the common RTC notion of time
5c236343e   Mike Frysinger   Blackfin RTC driv...
82
   * and the internal Blackfin notion that is encoded in 32bits.
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
   */
  static inline u32 rtc_time_to_bfin(unsigned long now)
  {
  	u32 sec  = (now % 60);
  	u32 min  = (now % (60 * 60)) / 60;
  	u32 hour = (now % (60 * 60 * 24)) / (60 * 60);
  	u32 days = (now / (60 * 60 * 24));
  	return (sec  << SEC_BITS_OFF) +
  	       (min  << MIN_BITS_OFF) +
  	       (hour << HOUR_BITS_OFF) +
  	       (days << DAY_BITS_OFF);
  }
  static inline unsigned long rtc_bfin_to_time(u32 rtc_bfin)
  {
  	return (((rtc_bfin >> SEC_BITS_OFF)  & 0x003F)) +
  	       (((rtc_bfin >> MIN_BITS_OFF)  & 0x003F) * 60) +
  	       (((rtc_bfin >> HOUR_BITS_OFF) & 0x001F) * 60 * 60) +
  	       (((rtc_bfin >> DAY_BITS_OFF)  & 0x7FFF) * 60 * 60 * 24);
  }
  static inline void rtc_bfin_to_tm(u32 rtc_bfin, struct rtc_time *tm)
  {
  	rtc_time_to_tm(rtc_bfin_to_time(rtc_bfin), tm);
  }
095b9d546   Mike Frysinger   Blackfin RTC driv...
106
107
108
109
  /**
   *	bfin_rtc_sync_pending - make sure pending writes have complete
   *
   * Wait for the previous write to a RTC register to complete.
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
   * Unfortunately, we can't sleep here as that introduces a race condition when
   * turning on interrupt events.  Consider this:
   *  - process sets alarm
   *  - process enables alarm
   *  - process sleeps while waiting for rtc write to sync
   *  - interrupt fires while process is sleeping
   *  - interrupt acks the event by writing to ISTAT
   *  - interrupt sets the WRITE PENDING bit
   *  - interrupt handler finishes
   *  - process wakes up, sees WRITE PENDING bit set, goes to sleep
   *  - interrupt fires while process is sleeping
   * If anyone can point out the obvious solution here, i'm listening :).  This
   * shouldn't be an issue on an SMP or preempt system as this function should
   * only be called with the rtc lock held.
5c236343e   Mike Frysinger   Blackfin RTC driv...
124
125
126
127
128
   *
   * Other options:
   *  - disable PREN so the sync happens at 32.768kHZ ... but this changes the
   *    inc rate for all RTC registers from 1HZ to 32.768kHZ ...
   *  - use the write complete IRQ
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
129
   */
095b9d546   Mike Frysinger   Blackfin RTC driv...
130
131
  /*
  static void bfin_rtc_sync_pending_polled(void)
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
132
  {
095b9d546   Mike Frysinger   Blackfin RTC driv...
133
  	while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_COMPLETE))
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
134
135
  		if (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING))
  			break;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
136
137
  	bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
  }
095b9d546   Mike Frysinger   Blackfin RTC driv...
138
139
140
141
142
143
144
145
146
  */
  static DECLARE_COMPLETION(bfin_write_complete);
  static void bfin_rtc_sync_pending(struct device *dev)
  {
  	dev_dbg_stamp(dev);
  	while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
  		wait_for_completion_timeout(&bfin_write_complete, HZ * 5);
  	dev_dbg_stamp(dev);
  }
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
147

095b9d546   Mike Frysinger   Blackfin RTC driv...
148
149
150
151
152
153
  /**
   *	bfin_rtc_reset - set RTC to sane/known state
   *
   * Initialize the RTC.  Enable pre-scaler to scale RTC clock
   * to 1Hz and clear interrupt/status registers.
   */
3b128fe04   Mike Frysinger   blackfin RTC driv...
154
  static void bfin_rtc_reset(struct device *dev, u16 rtc_ictl)
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
155
  {
5438de442   Mike Frysinger   Blackfin RTC driv...
156
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
095b9d546   Mike Frysinger   Blackfin RTC driv...
157
158
  	dev_dbg_stamp(dev);
  	bfin_rtc_sync_pending(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
159
  	bfin_write_RTC_PREN(0x1);
3b128fe04   Mike Frysinger   blackfin RTC driv...
160
  	bfin_write_RTC_ICTL(rtc_ictl);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
161
162
  	bfin_write_RTC_ALARM(0);
  	bfin_write_RTC_ISTAT(0xFFFF);
095b9d546   Mike Frysinger   Blackfin RTC driv...
163
  	rtc->rtc_wrote_regs = 0;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
164
  }
095b9d546   Mike Frysinger   Blackfin RTC driv...
165
166
167
168
169
170
171
172
173
174
175
  /**
   *	bfin_rtc_interrupt - handle interrupt from RTC
   *
   * Since we handle all RTC events here, we have to make sure the requested
   * interrupt is enabled (in RTC_ICTL) as the event status register (RTC_ISTAT)
   * always gets updated regardless of the interrupt being enabled.  So when one
   * even we care about (e.g. stopwatch) goes off, we don't want to turn around
   * and say that other events have happened as well (e.g. second).  We do not
   * have to worry about pending writes to the RTC_ICTL register as interrupts
   * only fire if they are enabled in the RTC_ICTL register.
   */
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
176
177
  static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
  {
d7827d889   Mike Frysinger   Blackfin RTC driv...
178
179
  	struct device *dev = dev_id;
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
180
  	unsigned long events = 0;
095b9d546   Mike Frysinger   Blackfin RTC driv...
181
  	bool write_complete = false;
286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
182
  	u16 rtc_istat, rtc_istat_clear, rtc_ictl, bits;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
183

5438de442   Mike Frysinger   Blackfin RTC driv...
184
  	dev_dbg_stamp(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
185

8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
186
  	rtc_istat = bfin_read_RTC_ISTAT();
095b9d546   Mike Frysinger   Blackfin RTC driv...
187
  	rtc_ictl = bfin_read_RTC_ICTL();
286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
188
  	rtc_istat_clear = 0;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
189

286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
190
191
192
  	bits = RTC_ISTAT_WRITE_COMPLETE;
  	if (rtc_istat & bits) {
  		rtc_istat_clear |= bits;
095b9d546   Mike Frysinger   Blackfin RTC driv...
193
194
  		write_complete = true;
  		complete(&bfin_write_complete);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
195
  	}
286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
196
197
198
199
  	bits = (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
  	if (rtc_ictl & bits) {
  		if (rtc_istat & bits) {
  			rtc_istat_clear |= bits;
095b9d546   Mike Frysinger   Blackfin RTC driv...
200
201
  			events |= RTC_AF | RTC_IRQF;
  		}
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
202
  	}
286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
203
204
205
206
  	bits = RTC_ISTAT_SEC;
  	if (rtc_ictl & bits) {
  		if (rtc_istat & bits) {
  			rtc_istat_clear |= bits;
095b9d546   Mike Frysinger   Blackfin RTC driv...
207
208
209
  			events |= RTC_UF | RTC_IRQF;
  		}
  	}
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
210

095b9d546   Mike Frysinger   Blackfin RTC driv...
211
212
  	if (events)
  		rtc_update_irq(rtc->rtc_dev, 1, events);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
213

286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
214
215
  	if (write_complete || events) {
  		bfin_write_RTC_ISTAT(rtc_istat_clear);
095b9d546   Mike Frysinger   Blackfin RTC driv...
216
  		return IRQ_HANDLED;
286f9f95f   Mike Frysinger   rtc-bfin: shrink/...
217
  	} else
095b9d546   Mike Frysinger   Blackfin RTC driv...
218
  		return IRQ_NONE;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
219
  }
605eb8b3c   Mike Frysinger   blackfin RTC driv...
220
  static void bfin_rtc_int_set(u16 rtc_int)
095b9d546   Mike Frysinger   Blackfin RTC driv...
221
222
223
224
  {
  	bfin_write_RTC_ISTAT(rtc_int);
  	bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | rtc_int);
  }
605eb8b3c   Mike Frysinger   blackfin RTC driv...
225
  static void bfin_rtc_int_clear(u16 rtc_int)
095b9d546   Mike Frysinger   Blackfin RTC driv...
226
227
228
229
230
231
232
233
  {
  	bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & rtc_int);
  }
  static void bfin_rtc_int_set_alarm(struct bfin_rtc *rtc)
  {
  	/* Blackfin has different bits for whether the alarm is
  	 * more than 24 hours away.
  	 */
605eb8b3c   Mike Frysinger   blackfin RTC driv...
234
  	bfin_rtc_int_set(rtc->rtc_alarm.tm_yday == -1 ? RTC_ISTAT_ALARM : RTC_ISTAT_ALARM_DAY);
095b9d546   Mike Frysinger   Blackfin RTC driv...
235
  }
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
236

16380c153   John Stultz   RTC: Convert rtc ...
237
238
239
240
241
242
243
244
245
  static int bfin_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  {
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
  
  	dev_dbg_stamp(dev);
  	if (enabled)
  		bfin_rtc_int_set_alarm(rtc);
  	else
  		bfin_rtc_int_clear(~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
8c122b968   Mike Frysinger   RTC: add missing ...
246
247
  
  	return 0;
16380c153   John Stultz   RTC: Convert rtc ...
248
  }
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
249
250
251
  static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
  {
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
5438de442   Mike Frysinger   Blackfin RTC driv...
252
  	dev_dbg_stamp(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
253

095b9d546   Mike Frysinger   Blackfin RTC driv...
254
255
  	if (rtc->rtc_wrote_regs & 0x1)
  		bfin_rtc_sync_pending(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
256
  	rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
257
258
259
260
261
262
263
264
265
  
  	return 0;
  }
  
  static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
  {
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
  	int ret;
  	unsigned long now;
5438de442   Mike Frysinger   Blackfin RTC driv...
266
  	dev_dbg_stamp(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
267

8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
268
269
  	ret = rtc_tm_to_time(tm, &now);
  	if (ret == 0) {
095b9d546   Mike Frysinger   Blackfin RTC driv...
270
271
  		if (rtc->rtc_wrote_regs & 0x1)
  			bfin_rtc_sync_pending(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
272
  		bfin_write_RTC_STAT(rtc_time_to_bfin(now));
095b9d546   Mike Frysinger   Blackfin RTC driv...
273
  		rtc->rtc_wrote_regs = 0x1;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
274
  	}
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
275
276
277
278
279
280
  	return ret;
  }
  
  static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
5438de442   Mike Frysinger   Blackfin RTC driv...
281
  	dev_dbg_stamp(dev);
48c1a56b4   Mike Frysinger   Blackfin RTC driv...
282
  	alrm->time = rtc->rtc_alarm;
095b9d546   Mike Frysinger   Blackfin RTC driv...
283
  	bfin_rtc_sync_pending(dev);
68db30472   Mike Frysinger   Blackfin RTC driv...
284
  	alrm->enabled = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
285
286
287
288
289
290
  	return 0;
  }
  
  static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  {
  	struct bfin_rtc *rtc = dev_get_drvdata(dev);
095b9d546   Mike Frysinger   Blackfin RTC driv...
291
  	unsigned long rtc_alarm;
5438de442   Mike Frysinger   Blackfin RTC driv...
292
  	dev_dbg_stamp(dev);
095b9d546   Mike Frysinger   Blackfin RTC driv...
293
294
295
  
  	if (rtc_tm_to_time(&alrm->time, &rtc_alarm))
  		return -EINVAL;
68db30472   Mike Frysinger   Blackfin RTC driv...
296
  	rtc->rtc_alarm = alrm->time;
095b9d546   Mike Frysinger   Blackfin RTC driv...
297
298
299
300
301
  
  	bfin_rtc_sync_pending(dev);
  	bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
  	if (alrm->enabled)
  		bfin_rtc_int_set_alarm(rtc);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
302
303
304
305
306
  	return 0;
  }
  
  static int bfin_rtc_proc(struct device *dev, struct seq_file *seq)
  {
640611608   Mike Frysinger   Blackfin RTC driv...
307
  #define yesno(x) ((x) ? "yes" : "no")
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
308
  	u16 ictl = bfin_read_RTC_ICTL();
5438de442   Mike Frysinger   Blackfin RTC driv...
309
  	dev_dbg_stamp(dev);
640611608   Mike Frysinger   Blackfin RTC driv...
310
311
312
313
314
  	seq_printf(seq,
  		"alarm_IRQ\t: %s
  "
  		"wkalarm_IRQ\t: %s
  "
26cb8bb21   Mike Frysinger   blackfin RTC driv...
315
316
  		"seconds_IRQ\t: %s
  ",
640611608   Mike Frysinger   Blackfin RTC driv...
317
318
  		yesno(ictl & RTC_ISTAT_ALARM),
  		yesno(ictl & RTC_ISTAT_ALARM_DAY),
26cb8bb21   Mike Frysinger   blackfin RTC driv...
319
  		yesno(ictl & RTC_ISTAT_SEC));
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
320
  	return 0;
640611608   Mike Frysinger   Blackfin RTC driv...
321
  #undef yesno
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
322
  }
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
323
  static struct rtc_class_ops bfin_rtc_ops = {
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
324
325
326
327
328
  	.read_time     = bfin_rtc_read_time,
  	.set_time      = bfin_rtc_set_time,
  	.read_alarm    = bfin_rtc_read_alarm,
  	.set_alarm     = bfin_rtc_set_alarm,
  	.proc          = bfin_rtc_proc,
16380c153   John Stultz   RTC: Convert rtc ...
329
  	.alarm_irq_enable = bfin_rtc_alarm_irq_enable,
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
330
331
332
333
334
  };
  
  static int __devinit bfin_rtc_probe(struct platform_device *pdev)
  {
  	struct bfin_rtc *rtc;
fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
335
  	struct device *dev = &pdev->dev;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
336
  	int ret = 0;
9980060ba   Mike Frysinger   bfin: delay IRQ r...
337
  	unsigned long timeout = jiffies + HZ;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
338

fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
339
  	dev_dbg_stamp(dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
340

fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
341
  	/* Allocate memory for our RTC struct */
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
342
343
344
  	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  	if (unlikely(!rtc))
  		return -ENOMEM;
fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
345
  	platform_set_drvdata(pdev, rtc);
8c9166f7a   Mike Frysinger   Blackfin RTC Driv...
346
  	device_init_wakeup(dev, 1);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
347

9980060ba   Mike Frysinger   bfin: delay IRQ r...
348
349
350
351
352
353
354
  	/* Register our RTC with the RTC framework */
  	rtc->rtc_dev = rtc_device_register(pdev->name, dev, &bfin_rtc_ops,
  						THIS_MODULE);
  	if (unlikely(IS_ERR(rtc->rtc_dev))) {
  		ret = PTR_ERR(rtc->rtc_dev);
  		goto err;
  	}
fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
355
  	/* Grab the IRQ and init the hardware */
6bff5fb80   Michael Hennerich   rtc-bfin: do not ...
356
  	ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, 0, pdev->name, dev);
fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
357
  	if (unlikely(ret))
9980060ba   Mike Frysinger   bfin: delay IRQ r...
358
  		goto err_reg;
d0fd93781   Mike Frysinger   Blackfin RTC Driv...
359
360
361
  	/* sometimes the bootloader touched things, but the write complete was not
  	 * enabled, so let's just do a quick timeout here since the IRQ will not fire ...
  	 */
d0fd93781   Mike Frysinger   Blackfin RTC Driv...
362
363
364
  	while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
  		if (time_after(jiffies, timeout))
  			break;
fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
365
  	bfin_rtc_reset(dev, RTC_ISTAT_WRITE_COMPLETE);
26cb8bb21   Mike Frysinger   blackfin RTC driv...
366
  	bfin_write_RTC_SWCNT(0);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
367

8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
368
  	return 0;
9980060ba   Mike Frysinger   bfin: delay IRQ r...
369
370
371
  err_reg:
  	rtc_device_unregister(rtc->rtc_dev);
  err:
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
372
373
374
375
376
377
378
  	kfree(rtc);
  	return ret;
  }
  
  static int __devexit bfin_rtc_remove(struct platform_device *pdev)
  {
  	struct bfin_rtc *rtc = platform_get_drvdata(pdev);
fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
379
  	struct device *dev = &pdev->dev;
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
380

fe2e1cf83   Mike Frysinger   Blackfin RTC Driv...
381
382
  	bfin_rtc_reset(dev, 0);
  	free_irq(IRQ_RTC, dev);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
383
384
385
386
387
388
  	rtc_device_unregister(rtc->rtc_dev);
  	platform_set_drvdata(pdev, NULL);
  	kfree(rtc);
  
  	return 0;
  }
5aeb776d0   Sonic Zhang   blackfin RTC driv...
389
390
391
  #ifdef CONFIG_PM
  static int bfin_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  {
d7c7ef908   Mike Frysinger   rtc-bfin: add deb...
392
393
394
395
396
  	struct device *dev = &pdev->dev;
  
  	dev_dbg_stamp(dev);
  
  	if (device_may_wakeup(dev)) {
813006f4b   Mike Frysinger   blackfin RTC driv...
397
  		enable_irq_wake(IRQ_RTC);
d7c7ef908   Mike Frysinger   rtc-bfin: add deb...
398
  		bfin_rtc_sync_pending(dev);
140fab14a   Mike Frysinger   blackfin RTC driv...
399
  	} else
110b7e969   Mike Frysinger   rtc-bfin: fix inv...
400
  		bfin_rtc_int_clear(0);
813006f4b   Mike Frysinger   blackfin RTC driv...
401

5aeb776d0   Sonic Zhang   blackfin RTC driv...
402
403
404
405
406
  	return 0;
  }
  
  static int bfin_rtc_resume(struct platform_device *pdev)
  {
d7c7ef908   Mike Frysinger   rtc-bfin: add deb...
407
408
409
410
411
  	struct device *dev = &pdev->dev;
  
  	dev_dbg_stamp(dev);
  
  	if (device_may_wakeup(dev))
813006f4b   Mike Frysinger   blackfin RTC driv...
412
  		disable_irq_wake(IRQ_RTC);
b6de86065   Mike Frysinger   rtc-bfin: fix sta...
413
414
415
416
417
418
419
420
421
422
423
  
  	/*
  	 * Since only some of the RTC bits are maintained externally in the
  	 * Vbat domain, we need to wait for the RTC MMRs to be synced into
  	 * the core after waking up.  This happens every RTC 1HZ.  Once that
  	 * has happened, we can go ahead and re-enable the important write
  	 * complete interrupt event.
  	 */
  	while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_SEC))
  		continue;
  	bfin_rtc_int_set(RTC_ISTAT_WRITE_COMPLETE);
813006f4b   Mike Frysinger   blackfin RTC driv...
424

5aeb776d0   Sonic Zhang   blackfin RTC driv...
425
426
  	return 0;
  }
813006f4b   Mike Frysinger   blackfin RTC driv...
427
428
429
  #else
  # define bfin_rtc_suspend NULL
  # define bfin_rtc_resume  NULL
5aeb776d0   Sonic Zhang   blackfin RTC driv...
430
  #endif
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
431
432
433
434
435
436
437
  static struct platform_driver bfin_rtc_driver = {
  	.driver		= {
  		.name	= "rtc-bfin",
  		.owner	= THIS_MODULE,
  	},
  	.probe		= bfin_rtc_probe,
  	.remove		= __devexit_p(bfin_rtc_remove),
5aeb776d0   Sonic Zhang   blackfin RTC driv...
438
439
  	.suspend	= bfin_rtc_suspend,
  	.resume		= bfin_rtc_resume,
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
440
  };
0c4eae665   Axel Lin   rtc: convert driv...
441
  module_platform_driver(bfin_rtc_driver);
8cc75c9a1   Wu, Bryan   Blackfin: on-chip...
442
443
444
445
  
  MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
  MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  MODULE_LICENSE("GPL");
ad28a07bc   Kay Sievers   rtc: fix platform...
446
  MODULE_ALIAS("platform:rtc-bfin");