blob: d948426283dbd1b57fb9f4a5070bf43acfc966b4 [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>
23
24#include <asm/io.h>
David Brownelldb68b182006-12-06 20:38:36 -080025
26
27/* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
28 * with century-range alarm matching, driven by the 32kHz clock.
29 *
30 * The main user-visible ways it differs from PC RTCs are by omitting
31 * "don't care" alarm fields and sub-second periodic IRQs, and having
32 * an autoadjust mechanism to calibrate to the true oscillator rate.
33 *
34 * Board-specific wiring options include using split power mode with
35 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
36 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
Sekhar Norifa5b0782010-10-27 15:33:05 -070037 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
38 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
David Brownelldb68b182006-12-06 20:38:36 -080039 */
40
Afzal Mohammedcab14582012-12-17 16:02:11 -080041#define DRIVER_NAME "omap_rtc"
42
David Brownelldb68b182006-12-06 20:38:36 -080043#define OMAP_RTC_BASE 0xfffb4800
44
45/* RTC registers */
46#define OMAP_RTC_SECONDS_REG 0x00
47#define OMAP_RTC_MINUTES_REG 0x04
48#define OMAP_RTC_HOURS_REG 0x08
49#define OMAP_RTC_DAYS_REG 0x0C
50#define OMAP_RTC_MONTHS_REG 0x10
51#define OMAP_RTC_YEARS_REG 0x14
52#define OMAP_RTC_WEEKS_REG 0x18
53
54#define OMAP_RTC_ALARM_SECONDS_REG 0x20
55#define OMAP_RTC_ALARM_MINUTES_REG 0x24
56#define OMAP_RTC_ALARM_HOURS_REG 0x28
57#define OMAP_RTC_ALARM_DAYS_REG 0x2c
58#define OMAP_RTC_ALARM_MONTHS_REG 0x30
59#define OMAP_RTC_ALARM_YEARS_REG 0x34
60
61#define OMAP_RTC_CTRL_REG 0x40
62#define OMAP_RTC_STATUS_REG 0x44
63#define OMAP_RTC_INTERRUPTS_REG 0x48
64
65#define OMAP_RTC_COMP_LSB_REG 0x4c
66#define OMAP_RTC_COMP_MSB_REG 0x50
67#define OMAP_RTC_OSC_REG 0x54
68
Afzal Mohammedcab14582012-12-17 16:02:11 -080069#define OMAP_RTC_KICK0_REG 0x6c
70#define OMAP_RTC_KICK1_REG 0x70
71
David Brownelldb68b182006-12-06 20:38:36 -080072/* OMAP_RTC_CTRL_REG bit fields: */
73#define OMAP_RTC_CTRL_SPLIT (1<<7)
74#define OMAP_RTC_CTRL_DISABLE (1<<6)
75#define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5)
76#define OMAP_RTC_CTRL_TEST (1<<4)
77#define OMAP_RTC_CTRL_MODE_12_24 (1<<3)
78#define OMAP_RTC_CTRL_AUTO_COMP (1<<2)
79#define OMAP_RTC_CTRL_ROUND_30S (1<<1)
80#define OMAP_RTC_CTRL_STOP (1<<0)
81
82/* OMAP_RTC_STATUS_REG bit fields: */
83#define OMAP_RTC_STATUS_POWER_UP (1<<7)
84#define OMAP_RTC_STATUS_ALARM (1<<6)
85#define OMAP_RTC_STATUS_1D_EVENT (1<<5)
86#define OMAP_RTC_STATUS_1H_EVENT (1<<4)
87#define OMAP_RTC_STATUS_1M_EVENT (1<<3)
88#define OMAP_RTC_STATUS_1S_EVENT (1<<2)
89#define OMAP_RTC_STATUS_RUN (1<<1)
90#define OMAP_RTC_STATUS_BUSY (1<<0)
91
92/* OMAP_RTC_INTERRUPTS_REG bit fields: */
93#define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3)
94#define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2)
95
Afzal Mohammedcab14582012-12-17 16:02:11 -080096/* OMAP_RTC_KICKER values */
97#define KICK0_VALUE 0x83e70b13
98#define KICK1_VALUE 0x95a4f1e0
99
100#define OMAP_RTC_HAS_KICKER 0x1
101
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800102static void __iomem *rtc_base;
David Brownelldb68b182006-12-06 20:38:36 -0800103
Afzal Mohammedcab14582012-12-17 16:02:11 -0800104#define rtc_read(addr) readb(rtc_base + (addr))
105#define rtc_write(val, addr) writeb(val, rtc_base + (addr))
106
107#define rtc_writel(val, addr) writel(val, rtc_base + (addr))
David Brownelldb68b182006-12-06 20:38:36 -0800108
109
David Brownelldb68b182006-12-06 20:38:36 -0800110/* we rely on the rtc framework to handle locking (rtc->ops_lock),
111 * so the only other requirement is that register accesses which
112 * require BUSY to be clear are made with IRQs locally disabled
113 */
114static void rtc_wait_not_busy(void)
115{
116 int count = 0;
117 u8 status;
118
119 /* BUSY may stay active for 1/32768 second (~30 usec) */
120 for (count = 0; count < 50; count++) {
121 status = rtc_read(OMAP_RTC_STATUS_REG);
122 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
123 break;
124 udelay(1);
125 }
126 /* now we have ~15 usec to read/write various registers */
127}
128
David Brownellab6a2d72007-05-08 00:33:30 -0700129static irqreturn_t rtc_irq(int irq, void *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800130{
131 unsigned long events = 0;
132 u8 irq_data;
133
134 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
135
136 /* alarm irq? */
137 if (irq_data & OMAP_RTC_STATUS_ALARM) {
138 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
139 events |= RTC_IRQF | RTC_AF;
140 }
141
142 /* 1/sec periodic/update irq? */
143 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
144 events |= RTC_IRQF | RTC_UF;
145
David Brownellab6a2d72007-05-08 00:33:30 -0700146 rtc_update_irq(rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800147
148 return IRQ_HANDLED;
149}
150
John Stultz16380c12011-02-02 17:02:41 -0800151static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
152{
153 u8 reg;
154
155 local_irq_disable();
156 rtc_wait_not_busy();
157 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
158 if (enabled)
159 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
160 else
161 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
162 rtc_wait_not_busy();
163 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
164 local_irq_enable();
165
166 return 0;
167}
168
David Brownelldb68b182006-12-06 20:38:36 -0800169/* this hardware doesn't support "don't care" alarm fields */
170static int tm2bcd(struct rtc_time *tm)
171{
172 if (rtc_valid_tm(tm) != 0)
173 return -EINVAL;
174
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700175 tm->tm_sec = bin2bcd(tm->tm_sec);
176 tm->tm_min = bin2bcd(tm->tm_min);
177 tm->tm_hour = bin2bcd(tm->tm_hour);
178 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800179
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700180 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800181
182 /* epoch == 1900 */
183 if (tm->tm_year < 100 || tm->tm_year > 199)
184 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700185 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800186
187 return 0;
188}
189
190static void bcd2tm(struct rtc_time *tm)
191{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700192 tm->tm_sec = bcd2bin(tm->tm_sec);
193 tm->tm_min = bcd2bin(tm->tm_min);
194 tm->tm_hour = bcd2bin(tm->tm_hour);
195 tm->tm_mday = bcd2bin(tm->tm_mday);
196 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800197 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700198 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800199}
200
201
202static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
203{
204 /* we don't report wday/yday/isdst ... */
205 local_irq_disable();
206 rtc_wait_not_busy();
207
208 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
209 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
210 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
211 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
212 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
213 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
214
215 local_irq_enable();
216
217 bcd2tm(tm);
218 return 0;
219}
220
221static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
222{
223 if (tm2bcd(tm) < 0)
224 return -EINVAL;
225 local_irq_disable();
226 rtc_wait_not_busy();
227
228 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
229 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
230 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
231 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
232 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
233 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
234
235 local_irq_enable();
236
237 return 0;
238}
239
240static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
241{
242 local_irq_disable();
243 rtc_wait_not_busy();
244
245 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
246 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
247 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
248 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
249 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
250 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
251
252 local_irq_enable();
253
254 bcd2tm(&alm->time);
David Brownella2db8df2006-12-13 00:35:08 -0800255 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800256 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800257
258 return 0;
259}
260
261static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
262{
263 u8 reg;
264
David Brownelldb68b182006-12-06 20:38:36 -0800265 if (tm2bcd(&alm->time) < 0)
266 return -EINVAL;
267
268 local_irq_disable();
269 rtc_wait_not_busy();
270
271 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
272 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
273 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
274 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
275 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
276 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
277
278 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
279 if (alm->enabled)
280 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
281 else
282 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
283 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
284
285 local_irq_enable();
286
287 return 0;
288}
289
290static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800291 .read_time = omap_rtc_read_time,
292 .set_time = omap_rtc_set_time,
293 .read_alarm = omap_rtc_read_alarm,
294 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800295 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800296};
297
298static int omap_rtc_alarm;
299static int omap_rtc_timer;
300
Afzal Mohammedcab14582012-12-17 16:02:11 -0800301static struct platform_device_id omap_rtc_devtype[] = {
302 {
303 .name = DRIVER_NAME,
304 }, {
305 .name = "da830-rtc",
306 .driver_data = OMAP_RTC_HAS_KICKER,
307 },
308 {},
309};
310MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
311
David Brownell71fc8222008-07-23 21:30:38 -0700312static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800313{
314 struct resource *res, *mem;
315 struct rtc_device *rtc;
316 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800317 const struct platform_device_id *id_entry;
David Brownelldb68b182006-12-06 20:38:36 -0800318
319 omap_rtc_timer = platform_get_irq(pdev, 0);
320 if (omap_rtc_timer <= 0) {
321 pr_debug("%s: no update irq?\n", pdev->name);
322 return -ENOENT;
323 }
324
325 omap_rtc_alarm = platform_get_irq(pdev, 1);
326 if (omap_rtc_alarm <= 0) {
327 pr_debug("%s: no alarm irq?\n", pdev->name);
328 return -ENOENT;
329 }
330
David Brownelldb68b182006-12-06 20:38:36 -0800331 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800332 if (!res) {
333 pr_debug("%s: RTC resource data missing\n", pdev->name);
David Brownelldb68b182006-12-06 20:38:36 -0800334 return -ENOENT;
335 }
336
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800337 mem = request_mem_region(res->start, resource_size(res), pdev->name);
David Brownelldb68b182006-12-06 20:38:36 -0800338 if (!mem) {
339 pr_debug("%s: RTC registers at %08x are not free\n",
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800340 pdev->name, res->start);
David Brownelldb68b182006-12-06 20:38:36 -0800341 return -EBUSY;
342 }
343
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800344 rtc_base = ioremap(res->start, resource_size(res));
345 if (!rtc_base) {
346 pr_debug("%s: RTC registers can't be mapped\n", pdev->name);
347 goto fail;
348 }
349
Afzal Mohammedcab14582012-12-17 16:02:11 -0800350 id_entry = platform_get_device_id(pdev);
351 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) {
352 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
353 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
354 }
355
David Brownelldb68b182006-12-06 20:38:36 -0800356 rtc = rtc_device_register(pdev->name, &pdev->dev,
357 &omap_rtc_ops, THIS_MODULE);
358 if (IS_ERR(rtc)) {
359 pr_debug("%s: can't register RTC device, err %ld\n",
360 pdev->name, PTR_ERR(rtc));
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800361 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800362 }
363 platform_set_drvdata(pdev, rtc);
David Brownell558a40f2007-05-16 22:11:14 -0700364 dev_set_drvdata(&rtc->dev, mem);
David Brownelldb68b182006-12-06 20:38:36 -0800365
366 /* clear pending irqs, and set 1/second periodic,
367 * which we'll use instead of update irqs
368 */
369 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
370
371 /* clear old status */
372 reg = rtc_read(OMAP_RTC_STATUS_REG);
373 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
374 pr_info("%s: RTC power up reset detected\n",
375 pdev->name);
376 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
377 }
378 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
379 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
380
381 /* handle periodic and alarm irqs */
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700382 if (request_irq(omap_rtc_timer, rtc_irq, 0,
Kay Sievers744bcb12009-03-24 16:38:22 -0700383 dev_name(&rtc->dev), rtc)) {
David Brownelldb68b182006-12-06 20:38:36 -0800384 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
385 pdev->name, omap_rtc_timer);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800386 goto fail1;
David Brownelldb68b182006-12-06 20:38:36 -0800387 }
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800388 if ((omap_rtc_timer != omap_rtc_alarm) &&
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700389 (request_irq(omap_rtc_alarm, rtc_irq, 0,
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800390 dev_name(&rtc->dev), rtc))) {
David Brownelldb68b182006-12-06 20:38:36 -0800391 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
392 pdev->name, omap_rtc_alarm);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800393 goto fail2;
David Brownelldb68b182006-12-06 20:38:36 -0800394 }
395
396 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
397 reg = rtc_read(OMAP_RTC_CTRL_REG);
398 if (reg & (u8) OMAP_RTC_CTRL_STOP)
399 pr_info("%s: already running\n", pdev->name);
400
401 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700402 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800403 new_ctrl |= OMAP_RTC_CTRL_STOP;
404
405 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
406 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700407 * - Device wake-up capability setting should come through chip
408 * init logic. OMAP1 boards should initialize the "wakeup capable"
409 * flag in the platform device if the board is wired right for
410 * being woken up by RTC alarm. For OMAP-L138, this capability
411 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800412 *
413 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
414 * rather than nPWRON_RESET, should forcibly enable split
415 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
416 * is write-only, and always reads as zero...)
417 */
David Brownelldb68b182006-12-06 20:38:36 -0800418
419 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
420 pr_info("%s: split power mode\n", pdev->name);
421
422 if (reg != new_ctrl)
423 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
424
425 return 0;
426
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800427fail2:
Axel Lin2dd93c42011-04-17 10:02:58 +0800428 free_irq(omap_rtc_timer, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800429fail1:
David Brownelldb68b182006-12-06 20:38:36 -0800430 rtc_device_unregister(rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800431fail0:
Afzal Mohammedcab14582012-12-17 16:02:11 -0800432 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
433 rtc_writel(0, OMAP_RTC_KICK0_REG);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800434 iounmap(rtc_base);
David Brownelldb68b182006-12-06 20:38:36 -0800435fail:
Axel Lin19412ce2011-01-12 17:00:05 -0800436 release_mem_region(mem->start, resource_size(mem));
David Brownelldb68b182006-12-06 20:38:36 -0800437 return -EIO;
438}
439
David Brownell71fc8222008-07-23 21:30:38 -0700440static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800441{
Joe Perchesa419aef2009-08-18 11:18:35 -0700442 struct rtc_device *rtc = platform_get_drvdata(pdev);
Axel Lin19412ce2011-01-12 17:00:05 -0800443 struct resource *mem = dev_get_drvdata(&rtc->dev);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800444 const struct platform_device_id *id_entry =
445 platform_get_device_id(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800446
447 device_init_wakeup(&pdev->dev, 0);
448
449 /* leave rtc running, but disable irqs */
450 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
451
452 free_irq(omap_rtc_timer, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800453
454 if (omap_rtc_timer != omap_rtc_alarm)
455 free_irq(omap_rtc_alarm, rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800456
David Brownelldb68b182006-12-06 20:38:36 -0800457 rtc_device_unregister(rtc);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800458 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
459 rtc_writel(0, OMAP_RTC_KICK0_REG);
Axel Lin19412ce2011-01-12 17:00:05 -0800460 iounmap(rtc_base);
461 release_mem_region(mem->start, resource_size(mem));
David Brownelldb68b182006-12-06 20:38:36 -0800462 return 0;
463}
464
465#ifdef CONFIG_PM
466
David Brownelldb68b182006-12-06 20:38:36 -0800467static u8 irqstat;
468
469static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state)
470{
David Brownelldb68b182006-12-06 20:38:36 -0800471 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
472
473 /* FIXME the RTC alarm is not currently acting as a wakeup event
474 * source, and in fact this enable() call is just saving a flag
475 * that's never used...
476 */
477 if (device_may_wakeup(&pdev->dev))
478 enable_irq_wake(omap_rtc_alarm);
479 else
480 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
481
482 return 0;
483}
484
485static int omap_rtc_resume(struct platform_device *pdev)
486{
David Brownelldb68b182006-12-06 20:38:36 -0800487 if (device_may_wakeup(&pdev->dev))
488 disable_irq_wake(omap_rtc_alarm);
489 else
490 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
491 return 0;
492}
493
494#else
495#define omap_rtc_suspend NULL
496#define omap_rtc_resume NULL
497#endif
498
499static void omap_rtc_shutdown(struct platform_device *pdev)
500{
501 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
502}
503
Kay Sieversad28a072008-04-10 21:29:25 -0700504MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800505static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700506 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800507 .suspend = omap_rtc_suspend,
508 .resume = omap_rtc_resume,
509 .shutdown = omap_rtc_shutdown,
510 .driver = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800511 .name = DRIVER_NAME,
David Brownelldb68b182006-12-06 20:38:36 -0800512 .owner = THIS_MODULE,
513 },
Afzal Mohammedcab14582012-12-17 16:02:11 -0800514 .id_table = omap_rtc_devtype,
David Brownelldb68b182006-12-06 20:38:36 -0800515};
516
517static int __init rtc_init(void)
518{
David Brownell71fc8222008-07-23 21:30:38 -0700519 return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800520}
521module_init(rtc_init);
522
523static void __exit rtc_exit(void)
524{
525 platform_driver_unregister(&omap_rtc_driver);
526}
527module_exit(rtc_exit);
528
529MODULE_AUTHOR("George G. Davis (and others)");
530MODULE_LICENSE("GPL");