blob: dff9ff476b0dd4769fa86c9ec8f578e962a00c6b [file] [log] [blame]
David Brownelldb68b182006-12-06 20:38:36 -08001/*
2 * TI OMAP1 Real Time Clock interface for Linux
3 *
4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6 *
7 * Copyright (C) 2006 David Brownell (new RTC framework)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/ioport.h>
19#include <linux/delay.h>
20#include <linux/rtc.h>
21#include <linux/bcd.h>
22#include <linux/platform_device.h>
Afzal Mohammed9e0344d2012-12-17 16:02:15 -080023#include <linux/of.h>
24#include <linux/of_device.h>
David Brownelldb68b182006-12-06 20:38:36 -080025
26#include <asm/io.h>
David Brownelldb68b182006-12-06 20:38:36 -080027
28
29/* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
30 * with century-range alarm matching, driven by the 32kHz clock.
31 *
32 * The main user-visible ways it differs from PC RTCs are by omitting
33 * "don't care" alarm fields and sub-second periodic IRQs, and having
34 * an autoadjust mechanism to calibrate to the true oscillator rate.
35 *
36 * Board-specific wiring options include using split power mode with
37 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
38 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
Sekhar Norifa5b0782010-10-27 15:33:05 -070039 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
40 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
David Brownelldb68b182006-12-06 20:38:36 -080041 */
42
Afzal Mohammedcab14582012-12-17 16:02:11 -080043#define DRIVER_NAME "omap_rtc"
44
David Brownelldb68b182006-12-06 20:38:36 -080045#define OMAP_RTC_BASE 0xfffb4800
46
47/* RTC registers */
48#define OMAP_RTC_SECONDS_REG 0x00
49#define OMAP_RTC_MINUTES_REG 0x04
50#define OMAP_RTC_HOURS_REG 0x08
51#define OMAP_RTC_DAYS_REG 0x0C
52#define OMAP_RTC_MONTHS_REG 0x10
53#define OMAP_RTC_YEARS_REG 0x14
54#define OMAP_RTC_WEEKS_REG 0x18
55
56#define OMAP_RTC_ALARM_SECONDS_REG 0x20
57#define OMAP_RTC_ALARM_MINUTES_REG 0x24
58#define OMAP_RTC_ALARM_HOURS_REG 0x28
59#define OMAP_RTC_ALARM_DAYS_REG 0x2c
60#define OMAP_RTC_ALARM_MONTHS_REG 0x30
61#define OMAP_RTC_ALARM_YEARS_REG 0x34
62
63#define OMAP_RTC_CTRL_REG 0x40
64#define OMAP_RTC_STATUS_REG 0x44
65#define OMAP_RTC_INTERRUPTS_REG 0x48
66
67#define OMAP_RTC_COMP_LSB_REG 0x4c
68#define OMAP_RTC_COMP_MSB_REG 0x50
69#define OMAP_RTC_OSC_REG 0x54
70
Afzal Mohammedcab14582012-12-17 16:02:11 -080071#define OMAP_RTC_KICK0_REG 0x6c
72#define OMAP_RTC_KICK1_REG 0x70
73
David Brownelldb68b182006-12-06 20:38:36 -080074/* OMAP_RTC_CTRL_REG bit fields: */
75#define OMAP_RTC_CTRL_SPLIT (1<<7)
76#define OMAP_RTC_CTRL_DISABLE (1<<6)
77#define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5)
78#define OMAP_RTC_CTRL_TEST (1<<4)
79#define OMAP_RTC_CTRL_MODE_12_24 (1<<3)
80#define OMAP_RTC_CTRL_AUTO_COMP (1<<2)
81#define OMAP_RTC_CTRL_ROUND_30S (1<<1)
82#define OMAP_RTC_CTRL_STOP (1<<0)
83
84/* OMAP_RTC_STATUS_REG bit fields: */
85#define OMAP_RTC_STATUS_POWER_UP (1<<7)
86#define OMAP_RTC_STATUS_ALARM (1<<6)
87#define OMAP_RTC_STATUS_1D_EVENT (1<<5)
88#define OMAP_RTC_STATUS_1H_EVENT (1<<4)
89#define OMAP_RTC_STATUS_1M_EVENT (1<<3)
90#define OMAP_RTC_STATUS_1S_EVENT (1<<2)
91#define OMAP_RTC_STATUS_RUN (1<<1)
92#define OMAP_RTC_STATUS_BUSY (1<<0)
93
94/* OMAP_RTC_INTERRUPTS_REG bit fields: */
95#define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3)
96#define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2)
97
Afzal Mohammedcab14582012-12-17 16:02:11 -080098/* OMAP_RTC_KICKER values */
99#define KICK0_VALUE 0x83e70b13
100#define KICK1_VALUE 0x95a4f1e0
101
102#define OMAP_RTC_HAS_KICKER 0x1
103
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800104static void __iomem *rtc_base;
David Brownelldb68b182006-12-06 20:38:36 -0800105
Afzal Mohammedcab14582012-12-17 16:02:11 -0800106#define rtc_read(addr) readb(rtc_base + (addr))
107#define rtc_write(val, addr) writeb(val, rtc_base + (addr))
108
109#define rtc_writel(val, addr) writel(val, rtc_base + (addr))
David Brownelldb68b182006-12-06 20:38:36 -0800110
111
David Brownelldb68b182006-12-06 20:38:36 -0800112/* we rely on the rtc framework to handle locking (rtc->ops_lock),
113 * so the only other requirement is that register accesses which
114 * require BUSY to be clear are made with IRQs locally disabled
115 */
116static void rtc_wait_not_busy(void)
117{
118 int count = 0;
119 u8 status;
120
121 /* BUSY may stay active for 1/32768 second (~30 usec) */
122 for (count = 0; count < 50; count++) {
123 status = rtc_read(OMAP_RTC_STATUS_REG);
124 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
125 break;
126 udelay(1);
127 }
128 /* now we have ~15 usec to read/write various registers */
129}
130
David Brownellab6a2d72007-05-08 00:33:30 -0700131static irqreturn_t rtc_irq(int irq, void *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800132{
133 unsigned long events = 0;
134 u8 irq_data;
135
136 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
137
138 /* alarm irq? */
139 if (irq_data & OMAP_RTC_STATUS_ALARM) {
140 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
141 events |= RTC_IRQF | RTC_AF;
142 }
143
144 /* 1/sec periodic/update irq? */
145 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
146 events |= RTC_IRQF | RTC_UF;
147
David Brownellab6a2d72007-05-08 00:33:30 -0700148 rtc_update_irq(rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800149
150 return IRQ_HANDLED;
151}
152
John Stultz16380c12011-02-02 17:02:41 -0800153static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
154{
155 u8 reg;
156
157 local_irq_disable();
158 rtc_wait_not_busy();
159 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
160 if (enabled)
161 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
162 else
163 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
164 rtc_wait_not_busy();
165 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
166 local_irq_enable();
167
168 return 0;
169}
170
David Brownelldb68b182006-12-06 20:38:36 -0800171/* this hardware doesn't support "don't care" alarm fields */
172static int tm2bcd(struct rtc_time *tm)
173{
174 if (rtc_valid_tm(tm) != 0)
175 return -EINVAL;
176
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700177 tm->tm_sec = bin2bcd(tm->tm_sec);
178 tm->tm_min = bin2bcd(tm->tm_min);
179 tm->tm_hour = bin2bcd(tm->tm_hour);
180 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800181
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700182 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800183
184 /* epoch == 1900 */
185 if (tm->tm_year < 100 || tm->tm_year > 199)
186 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700187 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800188
189 return 0;
190}
191
192static void bcd2tm(struct rtc_time *tm)
193{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700194 tm->tm_sec = bcd2bin(tm->tm_sec);
195 tm->tm_min = bcd2bin(tm->tm_min);
196 tm->tm_hour = bcd2bin(tm->tm_hour);
197 tm->tm_mday = bcd2bin(tm->tm_mday);
198 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800199 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700200 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800201}
202
203
204static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
205{
206 /* we don't report wday/yday/isdst ... */
207 local_irq_disable();
208 rtc_wait_not_busy();
209
210 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
211 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
212 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
213 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
214 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
215 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
216
217 local_irq_enable();
218
219 bcd2tm(tm);
220 return 0;
221}
222
223static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
224{
225 if (tm2bcd(tm) < 0)
226 return -EINVAL;
227 local_irq_disable();
228 rtc_wait_not_busy();
229
230 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
231 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
232 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
233 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
234 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
235 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
236
237 local_irq_enable();
238
239 return 0;
240}
241
242static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
243{
244 local_irq_disable();
245 rtc_wait_not_busy();
246
247 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
248 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
249 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
250 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
251 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
252 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
253
254 local_irq_enable();
255
256 bcd2tm(&alm->time);
David Brownella2db8df2006-12-13 00:35:08 -0800257 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800258 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800259
260 return 0;
261}
262
263static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
264{
265 u8 reg;
266
David Brownelldb68b182006-12-06 20:38:36 -0800267 if (tm2bcd(&alm->time) < 0)
268 return -EINVAL;
269
270 local_irq_disable();
271 rtc_wait_not_busy();
272
273 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
274 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
275 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
276 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
277 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
278 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
279
280 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
281 if (alm->enabled)
282 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
283 else
284 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
285 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
286
287 local_irq_enable();
288
289 return 0;
290}
291
292static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800293 .read_time = omap_rtc_read_time,
294 .set_time = omap_rtc_set_time,
295 .read_alarm = omap_rtc_read_alarm,
296 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800297 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800298};
299
300static int omap_rtc_alarm;
301static int omap_rtc_timer;
302
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800303#define OMAP_RTC_DATA_DA830_IDX 1
304
Afzal Mohammedcab14582012-12-17 16:02:11 -0800305static struct platform_device_id omap_rtc_devtype[] = {
306 {
307 .name = DRIVER_NAME,
308 }, {
309 .name = "da830-rtc",
310 .driver_data = OMAP_RTC_HAS_KICKER,
311 },
312 {},
313};
314MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
315
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800316static const struct of_device_id omap_rtc_of_match[] = {
317 { .compatible = "ti,da830-rtc",
318 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
319 },
320 {},
321};
322MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
323
David Brownell71fc8222008-07-23 21:30:38 -0700324static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800325{
326 struct resource *res, *mem;
327 struct rtc_device *rtc;
328 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800329 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800330 const struct of_device_id *of_id;
331
332 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
333 if (of_id)
334 pdev->id_entry = of_id->data;
David Brownelldb68b182006-12-06 20:38:36 -0800335
336 omap_rtc_timer = platform_get_irq(pdev, 0);
337 if (omap_rtc_timer <= 0) {
338 pr_debug("%s: no update irq?\n", pdev->name);
339 return -ENOENT;
340 }
341
342 omap_rtc_alarm = platform_get_irq(pdev, 1);
343 if (omap_rtc_alarm <= 0) {
344 pr_debug("%s: no alarm irq?\n", pdev->name);
345 return -ENOENT;
346 }
347
David Brownelldb68b182006-12-06 20:38:36 -0800348 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800349 if (!res) {
350 pr_debug("%s: RTC resource data missing\n", pdev->name);
David Brownelldb68b182006-12-06 20:38:36 -0800351 return -ENOENT;
352 }
353
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800354 mem = request_mem_region(res->start, resource_size(res), pdev->name);
David Brownelldb68b182006-12-06 20:38:36 -0800355 if (!mem) {
356 pr_debug("%s: RTC registers at %08x are not free\n",
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800357 pdev->name, res->start);
David Brownelldb68b182006-12-06 20:38:36 -0800358 return -EBUSY;
359 }
360
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800361 rtc_base = ioremap(res->start, resource_size(res));
362 if (!rtc_base) {
363 pr_debug("%s: RTC registers can't be mapped\n", pdev->name);
364 goto fail;
365 }
366
Afzal Mohammedcab14582012-12-17 16:02:11 -0800367 id_entry = platform_get_device_id(pdev);
368 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) {
369 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
370 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
371 }
372
David Brownelldb68b182006-12-06 20:38:36 -0800373 rtc = rtc_device_register(pdev->name, &pdev->dev,
374 &omap_rtc_ops, THIS_MODULE);
375 if (IS_ERR(rtc)) {
376 pr_debug("%s: can't register RTC device, err %ld\n",
377 pdev->name, PTR_ERR(rtc));
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800378 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800379 }
380 platform_set_drvdata(pdev, rtc);
David Brownell558a40f2007-05-16 22:11:14 -0700381 dev_set_drvdata(&rtc->dev, mem);
David Brownelldb68b182006-12-06 20:38:36 -0800382
383 /* clear pending irqs, and set 1/second periodic,
384 * which we'll use instead of update irqs
385 */
386 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
387
388 /* clear old status */
389 reg = rtc_read(OMAP_RTC_STATUS_REG);
390 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
391 pr_info("%s: RTC power up reset detected\n",
392 pdev->name);
393 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
394 }
395 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
396 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
397
398 /* handle periodic and alarm irqs */
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700399 if (request_irq(omap_rtc_timer, rtc_irq, 0,
Kay Sievers744bcb12009-03-24 16:38:22 -0700400 dev_name(&rtc->dev), rtc)) {
David Brownelldb68b182006-12-06 20:38:36 -0800401 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
402 pdev->name, omap_rtc_timer);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800403 goto fail1;
David Brownelldb68b182006-12-06 20:38:36 -0800404 }
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800405 if ((omap_rtc_timer != omap_rtc_alarm) &&
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700406 (request_irq(omap_rtc_alarm, rtc_irq, 0,
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800407 dev_name(&rtc->dev), rtc))) {
David Brownelldb68b182006-12-06 20:38:36 -0800408 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
409 pdev->name, omap_rtc_alarm);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800410 goto fail2;
David Brownelldb68b182006-12-06 20:38:36 -0800411 }
412
413 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
414 reg = rtc_read(OMAP_RTC_CTRL_REG);
415 if (reg & (u8) OMAP_RTC_CTRL_STOP)
416 pr_info("%s: already running\n", pdev->name);
417
418 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700419 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800420 new_ctrl |= OMAP_RTC_CTRL_STOP;
421
422 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
423 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700424 * - Device wake-up capability setting should come through chip
425 * init logic. OMAP1 boards should initialize the "wakeup capable"
426 * flag in the platform device if the board is wired right for
427 * being woken up by RTC alarm. For OMAP-L138, this capability
428 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800429 *
430 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
431 * rather than nPWRON_RESET, should forcibly enable split
432 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
433 * is write-only, and always reads as zero...)
434 */
David Brownelldb68b182006-12-06 20:38:36 -0800435
436 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
437 pr_info("%s: split power mode\n", pdev->name);
438
439 if (reg != new_ctrl)
440 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
441
442 return 0;
443
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800444fail2:
Axel Lin2dd93c42011-04-17 10:02:58 +0800445 free_irq(omap_rtc_timer, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800446fail1:
David Brownelldb68b182006-12-06 20:38:36 -0800447 rtc_device_unregister(rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800448fail0:
Afzal Mohammedcab14582012-12-17 16:02:11 -0800449 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
450 rtc_writel(0, OMAP_RTC_KICK0_REG);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800451 iounmap(rtc_base);
David Brownelldb68b182006-12-06 20:38:36 -0800452fail:
Axel Lin19412ce2011-01-12 17:00:05 -0800453 release_mem_region(mem->start, resource_size(mem));
David Brownelldb68b182006-12-06 20:38:36 -0800454 return -EIO;
455}
456
David Brownell71fc8222008-07-23 21:30:38 -0700457static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800458{
Joe Perchesa419aef2009-08-18 11:18:35 -0700459 struct rtc_device *rtc = platform_get_drvdata(pdev);
Axel Lin19412ce2011-01-12 17:00:05 -0800460 struct resource *mem = dev_get_drvdata(&rtc->dev);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800461 const struct platform_device_id *id_entry =
462 platform_get_device_id(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800463
464 device_init_wakeup(&pdev->dev, 0);
465
466 /* leave rtc running, but disable irqs */
467 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
468
469 free_irq(omap_rtc_timer, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800470
471 if (omap_rtc_timer != omap_rtc_alarm)
472 free_irq(omap_rtc_alarm, rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800473
David Brownelldb68b182006-12-06 20:38:36 -0800474 rtc_device_unregister(rtc);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800475 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
476 rtc_writel(0, OMAP_RTC_KICK0_REG);
Axel Lin19412ce2011-01-12 17:00:05 -0800477 iounmap(rtc_base);
478 release_mem_region(mem->start, resource_size(mem));
David Brownelldb68b182006-12-06 20:38:36 -0800479 return 0;
480}
481
482#ifdef CONFIG_PM
483
David Brownelldb68b182006-12-06 20:38:36 -0800484static u8 irqstat;
485
486static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state)
487{
David Brownelldb68b182006-12-06 20:38:36 -0800488 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
489
490 /* FIXME the RTC alarm is not currently acting as a wakeup event
491 * source, and in fact this enable() call is just saving a flag
492 * that's never used...
493 */
494 if (device_may_wakeup(&pdev->dev))
495 enable_irq_wake(omap_rtc_alarm);
496 else
497 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
498
499 return 0;
500}
501
502static int omap_rtc_resume(struct platform_device *pdev)
503{
David Brownelldb68b182006-12-06 20:38:36 -0800504 if (device_may_wakeup(&pdev->dev))
505 disable_irq_wake(omap_rtc_alarm);
506 else
507 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
508 return 0;
509}
510
511#else
512#define omap_rtc_suspend NULL
513#define omap_rtc_resume NULL
514#endif
515
516static void omap_rtc_shutdown(struct platform_device *pdev)
517{
518 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
519}
520
Kay Sieversad28a072008-04-10 21:29:25 -0700521MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800522static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700523 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800524 .suspend = omap_rtc_suspend,
525 .resume = omap_rtc_resume,
526 .shutdown = omap_rtc_shutdown,
527 .driver = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800528 .name = DRIVER_NAME,
David Brownelldb68b182006-12-06 20:38:36 -0800529 .owner = THIS_MODULE,
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800530 .of_match_table = of_match_ptr(omap_rtc_of_match),
David Brownelldb68b182006-12-06 20:38:36 -0800531 },
Afzal Mohammedcab14582012-12-17 16:02:11 -0800532 .id_table = omap_rtc_devtype,
David Brownelldb68b182006-12-06 20:38:36 -0800533};
534
535static int __init rtc_init(void)
536{
David Brownell71fc8222008-07-23 21:30:38 -0700537 return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800538}
539module_init(rtc_init);
540
541static void __exit rtc_exit(void)
542{
543 platform_driver_unregister(&omap_rtc_driver);
544}
545module_exit(rtc_exit);
546
547MODULE_AUTHOR("George G. Davis (and others)");
548MODULE_LICENSE("GPL");