blob: 03bce134414c71bf1fc18616f49b2ce9180793f1 [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>
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -080025#include <linux/pm_runtime.h>
Sachin Kamat4b30c9f2013-07-03 15:06:00 -070026#include <linux/io.h>
David Brownelldb68b182006-12-06 20:38:36 -080027
28/* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
29 * with century-range alarm matching, driven by the 32kHz clock.
30 *
31 * The main user-visible ways it differs from PC RTCs are by omitting
32 * "don't care" alarm fields and sub-second periodic IRQs, and having
33 * an autoadjust mechanism to calibrate to the true oscillator rate.
34 *
35 * Board-specific wiring options include using split power mode with
36 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
37 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
Sekhar Norifa5b0782010-10-27 15:33:05 -070038 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
39 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
David Brownelldb68b182006-12-06 20:38:36 -080040 */
41
Afzal Mohammedcab14582012-12-17 16:02:11 -080042#define DRIVER_NAME "omap_rtc"
43
David Brownelldb68b182006-12-06 20:38:36 -080044#define OMAP_RTC_BASE 0xfffb4800
45
46/* RTC registers */
47#define OMAP_RTC_SECONDS_REG 0x00
48#define OMAP_RTC_MINUTES_REG 0x04
49#define OMAP_RTC_HOURS_REG 0x08
50#define OMAP_RTC_DAYS_REG 0x0C
51#define OMAP_RTC_MONTHS_REG 0x10
52#define OMAP_RTC_YEARS_REG 0x14
53#define OMAP_RTC_WEEKS_REG 0x18
54
55#define OMAP_RTC_ALARM_SECONDS_REG 0x20
56#define OMAP_RTC_ALARM_MINUTES_REG 0x24
57#define OMAP_RTC_ALARM_HOURS_REG 0x28
58#define OMAP_RTC_ALARM_DAYS_REG 0x2c
59#define OMAP_RTC_ALARM_MONTHS_REG 0x30
60#define OMAP_RTC_ALARM_YEARS_REG 0x34
61
62#define OMAP_RTC_CTRL_REG 0x40
63#define OMAP_RTC_STATUS_REG 0x44
64#define OMAP_RTC_INTERRUPTS_REG 0x48
65
66#define OMAP_RTC_COMP_LSB_REG 0x4c
67#define OMAP_RTC_COMP_MSB_REG 0x50
68#define OMAP_RTC_OSC_REG 0x54
69
Afzal Mohammedcab14582012-12-17 16:02:11 -080070#define OMAP_RTC_KICK0_REG 0x6c
71#define OMAP_RTC_KICK1_REG 0x70
72
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070073#define OMAP_RTC_IRQWAKEEN 0x7c
74
David Brownelldb68b182006-12-06 20:38:36 -080075/* OMAP_RTC_CTRL_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070076#define OMAP_RTC_CTRL_SPLIT BIT(7)
77#define OMAP_RTC_CTRL_DISABLE BIT(6)
78#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
79#define OMAP_RTC_CTRL_TEST BIT(4)
80#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
81#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
82#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
83#define OMAP_RTC_CTRL_STOP BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080084
85/* OMAP_RTC_STATUS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070086#define OMAP_RTC_STATUS_POWER_UP BIT(7)
87#define OMAP_RTC_STATUS_ALARM BIT(6)
88#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
89#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
90#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
91#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
92#define OMAP_RTC_STATUS_RUN BIT(1)
93#define OMAP_RTC_STATUS_BUSY BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080094
95/* OMAP_RTC_INTERRUPTS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070096#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
97#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
David Brownelldb68b182006-12-06 20:38:36 -080098
Sekhar Noricd914bb2014-06-06 14:36:06 -070099/* OMAP_RTC_OSC_REG bit fields: */
100#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
101
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700102/* OMAP_RTC_IRQWAKEEN bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -0700103#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700104
Afzal Mohammedcab14582012-12-17 16:02:11 -0800105/* OMAP_RTC_KICKER values */
106#define KICK0_VALUE 0x83e70b13
107#define KICK1_VALUE 0x95a4f1e0
108
Sekhar Nori92adb962014-06-06 14:36:05 -0700109#define OMAP_RTC_HAS_KICKER BIT(0)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800110
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700111/*
112 * Few RTC IP revisions has special WAKE-EN Register to enable Wakeup
113 * generation for event Alarm.
114 */
Sekhar Nori92adb962014-06-06 14:36:05 -0700115#define OMAP_RTC_HAS_IRQWAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700116
Sekhar Noricd914bb2014-06-06 14:36:06 -0700117/*
118 * Some RTC IP revisions (like those in AM335x and DRA7x) need
119 * the 32KHz clock to be explicitly enabled.
120 */
121#define OMAP_RTC_HAS_32KCLK_EN BIT(2)
122
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800123static void __iomem *rtc_base;
David Brownelldb68b182006-12-06 20:38:36 -0800124
Afzal Mohammedcab14582012-12-17 16:02:11 -0800125#define rtc_read(addr) readb(rtc_base + (addr))
126#define rtc_write(val, addr) writeb(val, rtc_base + (addr))
127
128#define rtc_writel(val, addr) writel(val, rtc_base + (addr))
David Brownelldb68b182006-12-06 20:38:36 -0800129
130
David Brownelldb68b182006-12-06 20:38:36 -0800131/* we rely on the rtc framework to handle locking (rtc->ops_lock),
132 * so the only other requirement is that register accesses which
133 * require BUSY to be clear are made with IRQs locally disabled
134 */
135static void rtc_wait_not_busy(void)
136{
137 int count = 0;
138 u8 status;
139
140 /* BUSY may stay active for 1/32768 second (~30 usec) */
141 for (count = 0; count < 50; count++) {
142 status = rtc_read(OMAP_RTC_STATUS_REG);
143 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
144 break;
145 udelay(1);
146 }
147 /* now we have ~15 usec to read/write various registers */
148}
149
David Brownellab6a2d72007-05-08 00:33:30 -0700150static irqreturn_t rtc_irq(int irq, void *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800151{
152 unsigned long events = 0;
153 u8 irq_data;
154
155 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
156
157 /* alarm irq? */
158 if (irq_data & OMAP_RTC_STATUS_ALARM) {
159 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
160 events |= RTC_IRQF | RTC_AF;
161 }
162
163 /* 1/sec periodic/update irq? */
164 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
165 events |= RTC_IRQF | RTC_UF;
166
David Brownellab6a2d72007-05-08 00:33:30 -0700167 rtc_update_irq(rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800168
169 return IRQ_HANDLED;
170}
171
John Stultz16380c12011-02-02 17:02:41 -0800172static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
173{
174 u8 reg;
175
176 local_irq_disable();
177 rtc_wait_not_busy();
178 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
179 if (enabled)
180 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
181 else
182 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
183 rtc_wait_not_busy();
184 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
185 local_irq_enable();
186
187 return 0;
188}
189
David Brownelldb68b182006-12-06 20:38:36 -0800190/* this hardware doesn't support "don't care" alarm fields */
191static int tm2bcd(struct rtc_time *tm)
192{
193 if (rtc_valid_tm(tm) != 0)
194 return -EINVAL;
195
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700196 tm->tm_sec = bin2bcd(tm->tm_sec);
197 tm->tm_min = bin2bcd(tm->tm_min);
198 tm->tm_hour = bin2bcd(tm->tm_hour);
199 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800200
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700201 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800202
203 /* epoch == 1900 */
204 if (tm->tm_year < 100 || tm->tm_year > 199)
205 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700206 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800207
208 return 0;
209}
210
211static void bcd2tm(struct rtc_time *tm)
212{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700213 tm->tm_sec = bcd2bin(tm->tm_sec);
214 tm->tm_min = bcd2bin(tm->tm_min);
215 tm->tm_hour = bcd2bin(tm->tm_hour);
216 tm->tm_mday = bcd2bin(tm->tm_mday);
217 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800218 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700219 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800220}
221
222
223static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
224{
225 /* we don't report wday/yday/isdst ... */
226 local_irq_disable();
227 rtc_wait_not_busy();
228
229 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
230 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
231 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
232 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
233 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
234 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
235
236 local_irq_enable();
237
238 bcd2tm(tm);
239 return 0;
240}
241
242static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
243{
244 if (tm2bcd(tm) < 0)
245 return -EINVAL;
246 local_irq_disable();
247 rtc_wait_not_busy();
248
249 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
250 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
251 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
252 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
253 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
254 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
255
256 local_irq_enable();
257
258 return 0;
259}
260
261static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
262{
263 local_irq_disable();
264 rtc_wait_not_busy();
265
266 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
267 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
268 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
269 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
270 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
271 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
272
273 local_irq_enable();
274
275 bcd2tm(&alm->time);
David Brownella2db8df2006-12-13 00:35:08 -0800276 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800277 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800278
279 return 0;
280}
281
282static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
283{
284 u8 reg;
285
David Brownelldb68b182006-12-06 20:38:36 -0800286 if (tm2bcd(&alm->time) < 0)
287 return -EINVAL;
288
289 local_irq_disable();
290 rtc_wait_not_busy();
291
292 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
293 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
294 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
295 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
296 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
297 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
298
299 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
300 if (alm->enabled)
301 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
302 else
303 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
304 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
305
306 local_irq_enable();
307
308 return 0;
309}
310
311static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800312 .read_time = omap_rtc_read_time,
313 .set_time = omap_rtc_set_time,
314 .read_alarm = omap_rtc_read_alarm,
315 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800316 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800317};
318
319static int omap_rtc_alarm;
320static int omap_rtc_timer;
321
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700322#define OMAP_RTC_DATA_AM3352_IDX 1
323#define OMAP_RTC_DATA_DA830_IDX 2
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800324
Afzal Mohammedcab14582012-12-17 16:02:11 -0800325static struct platform_device_id omap_rtc_devtype[] = {
326 {
327 .name = DRIVER_NAME,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700328 },
329 [OMAP_RTC_DATA_AM3352_IDX] = {
330 .name = "am3352-rtc",
Sekhar Noricd914bb2014-06-06 14:36:06 -0700331 .driver_data = OMAP_RTC_HAS_KICKER | OMAP_RTC_HAS_IRQWAKEEN |
332 OMAP_RTC_HAS_32KCLK_EN,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700333 },
334 [OMAP_RTC_DATA_DA830_IDX] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800335 .name = "da830-rtc",
336 .driver_data = OMAP_RTC_HAS_KICKER,
337 },
338 {},
339};
340MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
341
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800342static const struct of_device_id omap_rtc_of_match[] = {
343 { .compatible = "ti,da830-rtc",
344 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
345 },
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700346 { .compatible = "ti,am3352-rtc",
347 .data = &omap_rtc_devtype[OMAP_RTC_DATA_AM3352_IDX],
348 },
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800349 {},
350};
351MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
352
David Brownell71fc8222008-07-23 21:30:38 -0700353static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800354{
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700355 struct resource *res;
David Brownelldb68b182006-12-06 20:38:36 -0800356 struct rtc_device *rtc;
357 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800358 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800359 const struct of_device_id *of_id;
360
361 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
362 if (of_id)
363 pdev->id_entry = of_id->data;
David Brownelldb68b182006-12-06 20:38:36 -0800364
Sekhar Nori337b6002014-06-06 14:36:04 -0700365 id_entry = platform_get_device_id(pdev);
366 if (!id_entry) {
367 dev_err(&pdev->dev, "no matching device entry\n");
368 return -ENODEV;
369 }
370
David Brownelldb68b182006-12-06 20:38:36 -0800371 omap_rtc_timer = platform_get_irq(pdev, 0);
372 if (omap_rtc_timer <= 0) {
373 pr_debug("%s: no update irq?\n", pdev->name);
374 return -ENOENT;
375 }
376
377 omap_rtc_alarm = platform_get_irq(pdev, 1);
378 if (omap_rtc_alarm <= 0) {
379 pr_debug("%s: no alarm irq?\n", pdev->name);
380 return -ENOENT;
381 }
382
David Brownelldb68b182006-12-06 20:38:36 -0800383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700384 rtc_base = devm_ioremap_resource(&pdev->dev, res);
385 if (IS_ERR(rtc_base))
386 return PTR_ERR(rtc_base);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800387
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800388 /* Enable the clock/module so that we can access the registers */
389 pm_runtime_enable(&pdev->dev);
390 pm_runtime_get_sync(&pdev->dev);
391
Sekhar Nori337b6002014-06-06 14:36:04 -0700392 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER) {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800393 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
394 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
395 }
396
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700397 rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
David Brownelldb68b182006-12-06 20:38:36 -0800398 &omap_rtc_ops, THIS_MODULE);
399 if (IS_ERR(rtc)) {
400 pr_debug("%s: can't register RTC device, err %ld\n",
401 pdev->name, PTR_ERR(rtc));
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800402 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800403 }
404 platform_set_drvdata(pdev, rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800405
406 /* clear pending irqs, and set 1/second periodic,
407 * which we'll use instead of update irqs
408 */
409 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
410
Sekhar Noricd914bb2014-06-06 14:36:06 -0700411 /* enable RTC functional clock */
412 if (id_entry->driver_data & OMAP_RTC_HAS_32KCLK_EN)
413 rtc_writel(OMAP_RTC_OSC_32KCLK_EN, OMAP_RTC_OSC_REG);
414
David Brownelldb68b182006-12-06 20:38:36 -0800415 /* clear old status */
416 reg = rtc_read(OMAP_RTC_STATUS_REG);
417 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
418 pr_info("%s: RTC power up reset detected\n",
419 pdev->name);
420 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
421 }
422 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
423 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
424
425 /* handle periodic and alarm irqs */
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700426 if (devm_request_irq(&pdev->dev, omap_rtc_timer, rtc_irq, 0,
Kay Sievers744bcb12009-03-24 16:38:22 -0700427 dev_name(&rtc->dev), rtc)) {
David Brownelldb68b182006-12-06 20:38:36 -0800428 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
429 pdev->name, omap_rtc_timer);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700430 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800431 }
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800432 if ((omap_rtc_timer != omap_rtc_alarm) &&
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700433 (devm_request_irq(&pdev->dev, omap_rtc_alarm, rtc_irq, 0,
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800434 dev_name(&rtc->dev), rtc))) {
David Brownelldb68b182006-12-06 20:38:36 -0800435 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
436 pdev->name, omap_rtc_alarm);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700437 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800438 }
439
440 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
441 reg = rtc_read(OMAP_RTC_CTRL_REG);
442 if (reg & (u8) OMAP_RTC_CTRL_STOP)
443 pr_info("%s: already running\n", pdev->name);
444
445 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700446 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800447 new_ctrl |= OMAP_RTC_CTRL_STOP;
448
449 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
450 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700451 * - Device wake-up capability setting should come through chip
452 * init logic. OMAP1 boards should initialize the "wakeup capable"
453 * flag in the platform device if the board is wired right for
454 * being woken up by RTC alarm. For OMAP-L138, this capability
455 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800456 *
457 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
458 * rather than nPWRON_RESET, should forcibly enable split
459 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
460 * is write-only, and always reads as zero...)
461 */
David Brownelldb68b182006-12-06 20:38:36 -0800462
Hebbar Gururaja1d2e2b62013-07-03 15:08:02 -0700463 device_init_wakeup(&pdev->dev, true);
464
David Brownelldb68b182006-12-06 20:38:36 -0800465 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
466 pr_info("%s: split power mode\n", pdev->name);
467
468 if (reg != new_ctrl)
469 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
470
471 return 0;
472
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800473fail0:
Sekhar Nori337b6002014-06-06 14:36:04 -0700474 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800475 rtc_writel(0, OMAP_RTC_KICK0_REG);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800476 pm_runtime_put_sync(&pdev->dev);
477 pm_runtime_disable(&pdev->dev);
David Brownelldb68b182006-12-06 20:38:36 -0800478 return -EIO;
479}
480
David Brownell71fc8222008-07-23 21:30:38 -0700481static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800482{
Afzal Mohammedcab14582012-12-17 16:02:11 -0800483 const struct platform_device_id *id_entry =
484 platform_get_device_id(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800485
486 device_init_wakeup(&pdev->dev, 0);
487
488 /* leave rtc running, but disable irqs */
489 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
490
Sekhar Nori337b6002014-06-06 14:36:04 -0700491 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800492 rtc_writel(0, OMAP_RTC_KICK0_REG);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800493
494 /* Disable the clock/module */
495 pm_runtime_put_sync(&pdev->dev);
496 pm_runtime_disable(&pdev->dev);
497
David Brownelldb68b182006-12-06 20:38:36 -0800498 return 0;
499}
500
Jingoo Han04ebc352013-04-29 16:21:01 -0700501#ifdef CONFIG_PM_SLEEP
David Brownelldb68b182006-12-06 20:38:36 -0800502static u8 irqstat;
503
Jingoo Han04ebc352013-04-29 16:21:01 -0700504static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800505{
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700506 u8 irqwake_stat;
507 struct platform_device *pdev = to_platform_device(dev);
508 const struct platform_device_id *id_entry =
509 platform_get_device_id(pdev);
510
David Brownelldb68b182006-12-06 20:38:36 -0800511 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
512
513 /* FIXME the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700514 * source on some platforms, and in fact this enable() call is just
515 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800516 */
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700517 if (device_may_wakeup(dev)) {
David Brownelldb68b182006-12-06 20:38:36 -0800518 enable_irq_wake(omap_rtc_alarm);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700519
520 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) {
521 irqwake_stat = rtc_read(OMAP_RTC_IRQWAKEEN);
522 irqwake_stat |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
523 rtc_write(irqwake_stat, OMAP_RTC_IRQWAKEEN);
524 }
525 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800526 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700527 }
David Brownelldb68b182006-12-06 20:38:36 -0800528
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800529 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700530 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800531
David Brownelldb68b182006-12-06 20:38:36 -0800532 return 0;
533}
534
Jingoo Han04ebc352013-04-29 16:21:01 -0700535static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800536{
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700537 u8 irqwake_stat;
538 struct platform_device *pdev = to_platform_device(dev);
539 const struct platform_device_id *id_entry =
540 platform_get_device_id(pdev);
541
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800542 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700543 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800544
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700545 if (device_may_wakeup(dev)) {
David Brownelldb68b182006-12-06 20:38:36 -0800546 disable_irq_wake(omap_rtc_alarm);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700547
548 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) {
549 irqwake_stat = rtc_read(OMAP_RTC_IRQWAKEEN);
550 irqwake_stat &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
551 rtc_write(irqwake_stat, OMAP_RTC_IRQWAKEEN);
552 }
553 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800554 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700555 }
David Brownelldb68b182006-12-06 20:38:36 -0800556 return 0;
557}
David Brownelldb68b182006-12-06 20:38:36 -0800558#endif
559
Jingoo Han04ebc352013-04-29 16:21:01 -0700560static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
561
David Brownelldb68b182006-12-06 20:38:36 -0800562static void omap_rtc_shutdown(struct platform_device *pdev)
563{
564 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
565}
566
Kay Sieversad28a072008-04-10 21:29:25 -0700567MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800568static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700569 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800570 .shutdown = omap_rtc_shutdown,
571 .driver = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800572 .name = DRIVER_NAME,
David Brownelldb68b182006-12-06 20:38:36 -0800573 .owner = THIS_MODULE,
Jingoo Han04ebc352013-04-29 16:21:01 -0700574 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800575 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800576 },
Afzal Mohammedcab14582012-12-17 16:02:11 -0800577 .id_table = omap_rtc_devtype,
David Brownelldb68b182006-12-06 20:38:36 -0800578};
579
Jingoo Han09c5a362013-04-29 16:18:46 -0700580module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800581
582MODULE_AUTHOR("George G. Davis (and others)");
583MODULE_LICENSE("GPL");