blob: dbb88e46c25df4e06901ed11c6fc6b2411f71d2e [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/* RTC registers */
45#define OMAP_RTC_SECONDS_REG 0x00
46#define OMAP_RTC_MINUTES_REG 0x04
47#define OMAP_RTC_HOURS_REG 0x08
48#define OMAP_RTC_DAYS_REG 0x0C
49#define OMAP_RTC_MONTHS_REG 0x10
50#define OMAP_RTC_YEARS_REG 0x14
51#define OMAP_RTC_WEEKS_REG 0x18
52
53#define OMAP_RTC_ALARM_SECONDS_REG 0x20
54#define OMAP_RTC_ALARM_MINUTES_REG 0x24
55#define OMAP_RTC_ALARM_HOURS_REG 0x28
56#define OMAP_RTC_ALARM_DAYS_REG 0x2c
57#define OMAP_RTC_ALARM_MONTHS_REG 0x30
58#define OMAP_RTC_ALARM_YEARS_REG 0x34
59
60#define OMAP_RTC_CTRL_REG 0x40
61#define OMAP_RTC_STATUS_REG 0x44
62#define OMAP_RTC_INTERRUPTS_REG 0x48
63
64#define OMAP_RTC_COMP_LSB_REG 0x4c
65#define OMAP_RTC_COMP_MSB_REG 0x50
66#define OMAP_RTC_OSC_REG 0x54
67
Afzal Mohammedcab14582012-12-17 16:02:11 -080068#define OMAP_RTC_KICK0_REG 0x6c
69#define OMAP_RTC_KICK1_REG 0x70
70
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070071#define OMAP_RTC_IRQWAKEEN 0x7c
72
David Brownelldb68b182006-12-06 20:38:36 -080073/* OMAP_RTC_CTRL_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070074#define OMAP_RTC_CTRL_SPLIT BIT(7)
75#define OMAP_RTC_CTRL_DISABLE BIT(6)
76#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
77#define OMAP_RTC_CTRL_TEST BIT(4)
78#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
79#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
80#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
81#define OMAP_RTC_CTRL_STOP BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080082
83/* OMAP_RTC_STATUS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070084#define OMAP_RTC_STATUS_POWER_UP BIT(7)
85#define OMAP_RTC_STATUS_ALARM BIT(6)
86#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
87#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
88#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
89#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
90#define OMAP_RTC_STATUS_RUN BIT(1)
91#define OMAP_RTC_STATUS_BUSY BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080092
93/* OMAP_RTC_INTERRUPTS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070094#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
95#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
David Brownelldb68b182006-12-06 20:38:36 -080096
Sekhar Noricd914bb2014-06-06 14:36:06 -070097/* OMAP_RTC_OSC_REG bit fields: */
98#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
99
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700100/* OMAP_RTC_IRQWAKEEN bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -0700101#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700102
Afzal Mohammedcab14582012-12-17 16:02:11 -0800103/* OMAP_RTC_KICKER values */
104#define KICK0_VALUE 0x83e70b13
105#define KICK1_VALUE 0x95a4f1e0
106
Sekhar Nori92adb962014-06-06 14:36:05 -0700107#define OMAP_RTC_HAS_KICKER BIT(0)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800108
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700109/*
110 * Few RTC IP revisions has special WAKE-EN Register to enable Wakeup
111 * generation for event Alarm.
112 */
Sekhar Nori92adb962014-06-06 14:36:05 -0700113#define OMAP_RTC_HAS_IRQWAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700114
Sekhar Noricd914bb2014-06-06 14:36:06 -0700115/*
116 * Some RTC IP revisions (like those in AM335x and DRA7x) need
117 * the 32KHz clock to be explicitly enabled.
118 */
119#define OMAP_RTC_HAS_32KCLK_EN BIT(2)
120
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800121static void __iomem *rtc_base;
David Brownelldb68b182006-12-06 20:38:36 -0800122
Afzal Mohammedcab14582012-12-17 16:02:11 -0800123#define rtc_read(addr) readb(rtc_base + (addr))
124#define rtc_write(val, addr) writeb(val, rtc_base + (addr))
125
126#define rtc_writel(val, addr) writel(val, rtc_base + (addr))
David Brownelldb68b182006-12-06 20:38:36 -0800127
128
David Brownelldb68b182006-12-06 20:38:36 -0800129/* we rely on the rtc framework to handle locking (rtc->ops_lock),
130 * so the only other requirement is that register accesses which
131 * require BUSY to be clear are made with IRQs locally disabled
132 */
133static void rtc_wait_not_busy(void)
134{
135 int count = 0;
136 u8 status;
137
138 /* BUSY may stay active for 1/32768 second (~30 usec) */
139 for (count = 0; count < 50; count++) {
140 status = rtc_read(OMAP_RTC_STATUS_REG);
141 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
142 break;
143 udelay(1);
144 }
145 /* now we have ~15 usec to read/write various registers */
146}
147
David Brownellab6a2d72007-05-08 00:33:30 -0700148static irqreturn_t rtc_irq(int irq, void *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800149{
150 unsigned long events = 0;
151 u8 irq_data;
152
153 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
154
155 /* alarm irq? */
156 if (irq_data & OMAP_RTC_STATUS_ALARM) {
157 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
158 events |= RTC_IRQF | RTC_AF;
159 }
160
161 /* 1/sec periodic/update irq? */
162 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
163 events |= RTC_IRQF | RTC_UF;
164
David Brownellab6a2d72007-05-08 00:33:30 -0700165 rtc_update_irq(rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800166
167 return IRQ_HANDLED;
168}
169
John Stultz16380c12011-02-02 17:02:41 -0800170static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
171{
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700172 u8 reg, irqwake_reg = 0;
173 struct platform_device *pdev = to_platform_device(dev);
174 const struct platform_device_id *id_entry =
175 platform_get_device_id(pdev);
John Stultz16380c12011-02-02 17:02:41 -0800176
177 local_irq_disable();
178 rtc_wait_not_busy();
179 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700180 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN)
181 irqwake_reg = rtc_read(OMAP_RTC_IRQWAKEEN);
182
183 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800184 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700185 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
186 } else {
John Stultz16380c12011-02-02 17:02:41 -0800187 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700188 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
189 }
John Stultz16380c12011-02-02 17:02:41 -0800190 rtc_wait_not_busy();
191 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700192 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN)
193 rtc_write(irqwake_reg, OMAP_RTC_IRQWAKEEN);
John Stultz16380c12011-02-02 17:02:41 -0800194 local_irq_enable();
195
196 return 0;
197}
198
David Brownelldb68b182006-12-06 20:38:36 -0800199/* this hardware doesn't support "don't care" alarm fields */
200static int tm2bcd(struct rtc_time *tm)
201{
202 if (rtc_valid_tm(tm) != 0)
203 return -EINVAL;
204
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700205 tm->tm_sec = bin2bcd(tm->tm_sec);
206 tm->tm_min = bin2bcd(tm->tm_min);
207 tm->tm_hour = bin2bcd(tm->tm_hour);
208 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800209
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700210 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800211
212 /* epoch == 1900 */
213 if (tm->tm_year < 100 || tm->tm_year > 199)
214 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700215 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800216
217 return 0;
218}
219
220static void bcd2tm(struct rtc_time *tm)
221{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700222 tm->tm_sec = bcd2bin(tm->tm_sec);
223 tm->tm_min = bcd2bin(tm->tm_min);
224 tm->tm_hour = bcd2bin(tm->tm_hour);
225 tm->tm_mday = bcd2bin(tm->tm_mday);
226 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800227 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700228 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800229}
230
231
232static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
233{
234 /* we don't report wday/yday/isdst ... */
235 local_irq_disable();
236 rtc_wait_not_busy();
237
238 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
239 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
240 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
241 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
242 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
243 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
244
245 local_irq_enable();
246
247 bcd2tm(tm);
248 return 0;
249}
250
251static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
252{
253 if (tm2bcd(tm) < 0)
254 return -EINVAL;
255 local_irq_disable();
256 rtc_wait_not_busy();
257
258 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
259 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
260 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
261 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
262 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
263 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
264
265 local_irq_enable();
266
267 return 0;
268}
269
270static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
271{
272 local_irq_disable();
273 rtc_wait_not_busy();
274
275 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
276 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
277 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
278 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
279 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
280 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
281
282 local_irq_enable();
283
284 bcd2tm(&alm->time);
David Brownella2db8df2006-12-13 00:35:08 -0800285 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800286 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800287
288 return 0;
289}
290
291static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
292{
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700293 u8 reg, irqwake_reg = 0;
294 struct platform_device *pdev = to_platform_device(dev);
295 const struct platform_device_id *id_entry =
296 platform_get_device_id(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800297
David Brownelldb68b182006-12-06 20:38:36 -0800298 if (tm2bcd(&alm->time) < 0)
299 return -EINVAL;
300
301 local_irq_disable();
302 rtc_wait_not_busy();
303
304 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
305 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
306 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
307 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
308 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
309 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
310
311 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700312 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN)
313 irqwake_reg = rtc_read(OMAP_RTC_IRQWAKEEN);
314
315 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800316 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700317 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
318 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800319 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700320 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
321 }
David Brownelldb68b182006-12-06 20:38:36 -0800322 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700323 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN)
324 rtc_write(irqwake_reg, OMAP_RTC_IRQWAKEEN);
David Brownelldb68b182006-12-06 20:38:36 -0800325
326 local_irq_enable();
327
328 return 0;
329}
330
331static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800332 .read_time = omap_rtc_read_time,
333 .set_time = omap_rtc_set_time,
334 .read_alarm = omap_rtc_read_alarm,
335 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800336 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800337};
338
339static int omap_rtc_alarm;
340static int omap_rtc_timer;
341
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700342#define OMAP_RTC_DATA_AM3352_IDX 1
343#define OMAP_RTC_DATA_DA830_IDX 2
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800344
Afzal Mohammedcab14582012-12-17 16:02:11 -0800345static struct platform_device_id omap_rtc_devtype[] = {
346 {
347 .name = DRIVER_NAME,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700348 },
349 [OMAP_RTC_DATA_AM3352_IDX] = {
350 .name = "am3352-rtc",
Sekhar Noricd914bb2014-06-06 14:36:06 -0700351 .driver_data = OMAP_RTC_HAS_KICKER | OMAP_RTC_HAS_IRQWAKEEN |
352 OMAP_RTC_HAS_32KCLK_EN,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700353 },
354 [OMAP_RTC_DATA_DA830_IDX] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800355 .name = "da830-rtc",
356 .driver_data = OMAP_RTC_HAS_KICKER,
357 },
358 {},
359};
360MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
361
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800362static const struct of_device_id omap_rtc_of_match[] = {
363 { .compatible = "ti,da830-rtc",
364 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
365 },
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700366 { .compatible = "ti,am3352-rtc",
367 .data = &omap_rtc_devtype[OMAP_RTC_DATA_AM3352_IDX],
368 },
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800369 {},
370};
371MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
372
David Brownell71fc8222008-07-23 21:30:38 -0700373static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800374{
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700375 struct resource *res;
David Brownelldb68b182006-12-06 20:38:36 -0800376 struct rtc_device *rtc;
377 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800378 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800379 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800380 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800381
382 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
383 if (of_id)
384 pdev->id_entry = of_id->data;
David Brownelldb68b182006-12-06 20:38:36 -0800385
Sekhar Nori337b6002014-06-06 14:36:04 -0700386 id_entry = platform_get_device_id(pdev);
387 if (!id_entry) {
388 dev_err(&pdev->dev, "no matching device entry\n");
389 return -ENODEV;
390 }
391
David Brownelldb68b182006-12-06 20:38:36 -0800392 omap_rtc_timer = platform_get_irq(pdev, 0);
Johan Hovold437b37a2014-12-10 15:52:40 -0800393 if (omap_rtc_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800394 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800395
396 omap_rtc_alarm = platform_get_irq(pdev, 1);
Johan Hovold437b37a2014-12-10 15:52:40 -0800397 if (omap_rtc_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800398 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800399
David Brownelldb68b182006-12-06 20:38:36 -0800400 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700401 rtc_base = devm_ioremap_resource(&pdev->dev, res);
402 if (IS_ERR(rtc_base))
403 return PTR_ERR(rtc_base);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800404
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800405 /* Enable the clock/module so that we can access the registers */
406 pm_runtime_enable(&pdev->dev);
407 pm_runtime_get_sync(&pdev->dev);
408
Sekhar Nori337b6002014-06-06 14:36:04 -0700409 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER) {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800410 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
411 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
412 }
413
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800414 /*
415 * disable interrupts
416 *
417 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800418 */
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800419 rtc_writel(0, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800420
Sekhar Noricd914bb2014-06-06 14:36:06 -0700421 /* enable RTC functional clock */
Johan Hovold44c63a52014-12-10 15:52:30 -0800422 if (id_entry->driver_data & OMAP_RTC_HAS_32KCLK_EN) {
423 reg = rtc_read(OMAP_RTC_OSC_REG);
424 rtc_writel(reg | OMAP_RTC_OSC_32KCLK_EN, OMAP_RTC_OSC_REG);
425 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700426
David Brownelldb68b182006-12-06 20:38:36 -0800427 /* clear old status */
428 reg = rtc_read(OMAP_RTC_STATUS_REG);
429 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
Johan Hovold397b6302014-12-10 15:52:49 -0800430 dev_info(&pdev->dev, "RTC power up reset detected\n");
David Brownelldb68b182006-12-06 20:38:36 -0800431 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
432 }
433 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
434 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
435
David Brownelldb68b182006-12-06 20:38:36 -0800436 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
437 reg = rtc_read(OMAP_RTC_CTRL_REG);
438 if (reg & (u8) OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800439 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800440
441 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700442 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800443 new_ctrl |= OMAP_RTC_CTRL_STOP;
444
445 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
446 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700447 * - Device wake-up capability setting should come through chip
448 * init logic. OMAP1 boards should initialize the "wakeup capable"
449 * flag in the platform device if the board is wired right for
450 * being woken up by RTC alarm. For OMAP-L138, this capability
451 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800452 *
453 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
454 * rather than nPWRON_RESET, should forcibly enable split
455 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
456 * is write-only, and always reads as zero...)
457 */
David Brownelldb68b182006-12-06 20:38:36 -0800458
459 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800460 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800461
462 if (reg != new_ctrl)
463 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
464
Johan Hovold4390ce02014-12-10 15:52:43 -0800465 device_init_wakeup(&pdev->dev, true);
466
467 rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
468 &omap_rtc_ops, THIS_MODULE);
469 if (IS_ERR(rtc)) {
470 ret = PTR_ERR(rtc);
471 goto err;
472 }
473 platform_set_drvdata(pdev, rtc);
474
475 /* handle periodic and alarm irqs */
476 ret = devm_request_irq(&pdev->dev, omap_rtc_timer, rtc_irq, 0,
477 dev_name(&rtc->dev), rtc);
478 if (ret)
479 goto err;
480
481 if (omap_rtc_timer != omap_rtc_alarm) {
482 ret = devm_request_irq(&pdev->dev, omap_rtc_alarm, rtc_irq, 0,
483 dev_name(&rtc->dev), rtc);
484 if (ret)
485 goto err;
486 }
487
David Brownelldb68b182006-12-06 20:38:36 -0800488 return 0;
489
Johan Hovold437b37a2014-12-10 15:52:40 -0800490err:
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800491 device_init_wakeup(&pdev->dev, false);
Sekhar Nori337b6002014-06-06 14:36:04 -0700492 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800493 rtc_writel(0, OMAP_RTC_KICK0_REG);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800494 pm_runtime_put_sync(&pdev->dev);
495 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800496
497 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800498}
499
David Brownell71fc8222008-07-23 21:30:38 -0700500static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800501{
Afzal Mohammedcab14582012-12-17 16:02:11 -0800502 const struct platform_device_id *id_entry =
503 platform_get_device_id(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800504
505 device_init_wakeup(&pdev->dev, 0);
506
507 /* leave rtc running, but disable irqs */
508 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
509
Sekhar Nori337b6002014-06-06 14:36:04 -0700510 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800511 rtc_writel(0, OMAP_RTC_KICK0_REG);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800512
513 /* Disable the clock/module */
514 pm_runtime_put_sync(&pdev->dev);
515 pm_runtime_disable(&pdev->dev);
516
David Brownelldb68b182006-12-06 20:38:36 -0800517 return 0;
518}
519
Jingoo Han04ebc352013-04-29 16:21:01 -0700520#ifdef CONFIG_PM_SLEEP
David Brownelldb68b182006-12-06 20:38:36 -0800521static u8 irqstat;
522
Jingoo Han04ebc352013-04-29 16:21:01 -0700523static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800524{
David Brownelldb68b182006-12-06 20:38:36 -0800525 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
526
527 /* FIXME the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700528 * source on some platforms, and in fact this enable() call is just
529 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800530 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700531 if (device_may_wakeup(dev))
David Brownelldb68b182006-12-06 20:38:36 -0800532 enable_irq_wake(omap_rtc_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700533 else
David Brownelldb68b182006-12-06 20:38:36 -0800534 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
535
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800536 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700537 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800538
David Brownelldb68b182006-12-06 20:38:36 -0800539 return 0;
540}
541
Jingoo Han04ebc352013-04-29 16:21:01 -0700542static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800543{
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800544 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700545 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800546
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700547 if (device_may_wakeup(dev))
David Brownelldb68b182006-12-06 20:38:36 -0800548 disable_irq_wake(omap_rtc_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700549 else
David Brownelldb68b182006-12-06 20:38:36 -0800550 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700551
David Brownelldb68b182006-12-06 20:38:36 -0800552 return 0;
553}
David Brownelldb68b182006-12-06 20:38:36 -0800554#endif
555
Jingoo Han04ebc352013-04-29 16:21:01 -0700556static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
557
David Brownelldb68b182006-12-06 20:38:36 -0800558static void omap_rtc_shutdown(struct platform_device *pdev)
559{
560 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
561}
562
Kay Sieversad28a072008-04-10 21:29:25 -0700563MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800564static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700565 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800566 .shutdown = omap_rtc_shutdown,
567 .driver = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800568 .name = DRIVER_NAME,
David Brownelldb68b182006-12-06 20:38:36 -0800569 .owner = THIS_MODULE,
Jingoo Han04ebc352013-04-29 16:21:01 -0700570 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800571 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800572 },
Afzal Mohammedcab14582012-12-17 16:02:11 -0800573 .id_table = omap_rtc_devtype,
David Brownelldb68b182006-12-06 20:38:36 -0800574};
575
Jingoo Han09c5a362013-04-29 16:18:46 -0700576module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800577
578MODULE_AUTHOR("George G. Davis (and others)");
579MODULE_LICENSE("GPL");