blob: 1da610b8981f9b1b8f785379fe59f1e7ba1a06f0 [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
Johan Hovold55ba9532014-12-10 15:52:55 -0800121struct omap_rtc {
122 struct rtc_device *rtc;
123 void __iomem *base;
124 int irq_alarm;
125 int irq_timer;
126 u8 interrupts_reg;
127 unsigned long flags;
128};
David Brownelldb68b182006-12-06 20:38:36 -0800129
Johan Hovold55ba9532014-12-10 15:52:55 -0800130static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
131{
132 return readb(rtc->base + reg);
133}
Afzal Mohammedcab14582012-12-17 16:02:11 -0800134
Johan Hovold55ba9532014-12-10 15:52:55 -0800135static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
136{
137 writeb(val, rtc->base + reg);
138}
David Brownelldb68b182006-12-06 20:38:36 -0800139
Johan Hovold55ba9532014-12-10 15:52:55 -0800140static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
141{
142 writel(val, rtc->base + reg);
143}
David Brownelldb68b182006-12-06 20:38:36 -0800144
David Brownelldb68b182006-12-06 20:38:36 -0800145/* we rely on the rtc framework to handle locking (rtc->ops_lock),
146 * so the only other requirement is that register accesses which
147 * require BUSY to be clear are made with IRQs locally disabled
148 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800149static void rtc_wait_not_busy(struct omap_rtc *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800150{
151 int count = 0;
152 u8 status;
153
154 /* BUSY may stay active for 1/32768 second (~30 usec) */
155 for (count = 0; count < 50; count++) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800156 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800157 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
158 break;
159 udelay(1);
160 }
161 /* now we have ~15 usec to read/write various registers */
162}
163
Johan Hovold55ba9532014-12-10 15:52:55 -0800164static irqreturn_t rtc_irq(int irq, void *dev_id)
David Brownelldb68b182006-12-06 20:38:36 -0800165{
Johan Hovold55ba9532014-12-10 15:52:55 -0800166 struct omap_rtc *rtc = dev_id;
David Brownelldb68b182006-12-06 20:38:36 -0800167 unsigned long events = 0;
168 u8 irq_data;
169
Johan Hovold55ba9532014-12-10 15:52:55 -0800170 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800171
172 /* alarm irq? */
173 if (irq_data & OMAP_RTC_STATUS_ALARM) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800174 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800175 events |= RTC_IRQF | RTC_AF;
176 }
177
178 /* 1/sec periodic/update irq? */
179 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
180 events |= RTC_IRQF | RTC_UF;
181
Johan Hovold55ba9532014-12-10 15:52:55 -0800182 rtc_update_irq(rtc->rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800183
184 return IRQ_HANDLED;
185}
186
John Stultz16380c12011-02-02 17:02:41 -0800187static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
188{
Johan Hovold55ba9532014-12-10 15:52:55 -0800189 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700190 u8 reg, irqwake_reg = 0;
John Stultz16380c12011-02-02 17:02:41 -0800191
192 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800193 rtc_wait_not_busy(rtc);
194 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
195 if (rtc->flags & OMAP_RTC_HAS_IRQWAKEEN)
196 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700197
198 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800199 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700200 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
201 } else {
John Stultz16380c12011-02-02 17:02:41 -0800202 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700203 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
204 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800205 rtc_wait_not_busy(rtc);
206 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
207 if (rtc->flags & OMAP_RTC_HAS_IRQWAKEEN)
208 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
John Stultz16380c12011-02-02 17:02:41 -0800209 local_irq_enable();
210
211 return 0;
212}
213
David Brownelldb68b182006-12-06 20:38:36 -0800214/* this hardware doesn't support "don't care" alarm fields */
215static int tm2bcd(struct rtc_time *tm)
216{
217 if (rtc_valid_tm(tm) != 0)
218 return -EINVAL;
219
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700220 tm->tm_sec = bin2bcd(tm->tm_sec);
221 tm->tm_min = bin2bcd(tm->tm_min);
222 tm->tm_hour = bin2bcd(tm->tm_hour);
223 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800224
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700225 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800226
227 /* epoch == 1900 */
228 if (tm->tm_year < 100 || tm->tm_year > 199)
229 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700230 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800231
232 return 0;
233}
234
235static void bcd2tm(struct rtc_time *tm)
236{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700237 tm->tm_sec = bcd2bin(tm->tm_sec);
238 tm->tm_min = bcd2bin(tm->tm_min);
239 tm->tm_hour = bcd2bin(tm->tm_hour);
240 tm->tm_mday = bcd2bin(tm->tm_mday);
241 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800242 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700243 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800244}
245
246
247static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
248{
Johan Hovold55ba9532014-12-10 15:52:55 -0800249 struct omap_rtc *rtc = dev_get_drvdata(dev);
250
David Brownelldb68b182006-12-06 20:38:36 -0800251 /* we don't report wday/yday/isdst ... */
252 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800253 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800254
Johan Hovold55ba9532014-12-10 15:52:55 -0800255 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
256 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
257 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
258 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
259 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
260 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800261
262 local_irq_enable();
263
264 bcd2tm(tm);
265 return 0;
266}
267
268static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
269{
Johan Hovold55ba9532014-12-10 15:52:55 -0800270 struct omap_rtc *rtc = dev_get_drvdata(dev);
271
David Brownelldb68b182006-12-06 20:38:36 -0800272 if (tm2bcd(tm) < 0)
273 return -EINVAL;
274 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800275 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800276
Johan Hovold55ba9532014-12-10 15:52:55 -0800277 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
278 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
279 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
280 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
281 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
282 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800283
284 local_irq_enable();
285
286 return 0;
287}
288
289static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
290{
Johan Hovold55ba9532014-12-10 15:52:55 -0800291 struct omap_rtc *rtc = dev_get_drvdata(dev);
David Brownelldb68b182006-12-06 20:38:36 -0800292
Johan Hovold55ba9532014-12-10 15:52:55 -0800293 local_irq_disable();
294 rtc_wait_not_busy(rtc);
295
296 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
297 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
298 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
299 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
300 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
301 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800302
303 local_irq_enable();
304
305 bcd2tm(&alm->time);
Johan Hovold55ba9532014-12-10 15:52:55 -0800306 alm->enabled = !!(rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800307 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800308
309 return 0;
310}
311
312static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
313{
Johan Hovold55ba9532014-12-10 15:52:55 -0800314 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700315 u8 reg, irqwake_reg = 0;
David Brownelldb68b182006-12-06 20:38:36 -0800316
David Brownelldb68b182006-12-06 20:38:36 -0800317 if (tm2bcd(&alm->time) < 0)
318 return -EINVAL;
319
320 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800321 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800322
Johan Hovold55ba9532014-12-10 15:52:55 -0800323 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
324 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
325 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
326 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
327 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
328 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800329
Johan Hovold55ba9532014-12-10 15:52:55 -0800330 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
331 if (rtc->flags & OMAP_RTC_HAS_IRQWAKEEN)
332 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700333
334 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800335 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700336 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
337 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800338 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700339 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
340 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800341 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
342 if (rtc->flags & OMAP_RTC_HAS_IRQWAKEEN)
343 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
David Brownelldb68b182006-12-06 20:38:36 -0800344
345 local_irq_enable();
346
347 return 0;
348}
349
350static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800351 .read_time = omap_rtc_read_time,
352 .set_time = omap_rtc_set_time,
353 .read_alarm = omap_rtc_read_alarm,
354 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800355 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800356};
357
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700358#define OMAP_RTC_DATA_AM3352_IDX 1
359#define OMAP_RTC_DATA_DA830_IDX 2
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800360
Johan Hovoldd17a82e2014-12-10 15:52:52 -0800361static const struct platform_device_id omap_rtc_devtype[] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800362 {
363 .name = DRIVER_NAME,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700364 },
365 [OMAP_RTC_DATA_AM3352_IDX] = {
366 .name = "am3352-rtc",
Sekhar Noricd914bb2014-06-06 14:36:06 -0700367 .driver_data = OMAP_RTC_HAS_KICKER | OMAP_RTC_HAS_IRQWAKEEN |
368 OMAP_RTC_HAS_32KCLK_EN,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700369 },
370 [OMAP_RTC_DATA_DA830_IDX] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800371 .name = "da830-rtc",
372 .driver_data = OMAP_RTC_HAS_KICKER,
373 },
374 {},
375};
376MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
377
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800378static const struct of_device_id omap_rtc_of_match[] = {
379 { .compatible = "ti,da830-rtc",
380 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
381 },
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700382 { .compatible = "ti,am3352-rtc",
383 .data = &omap_rtc_devtype[OMAP_RTC_DATA_AM3352_IDX],
384 },
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800385 {},
386};
387MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
388
David Brownell71fc8222008-07-23 21:30:38 -0700389static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800390{
Johan Hovold55ba9532014-12-10 15:52:55 -0800391 struct omap_rtc *rtc;
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700392 struct resource *res;
David Brownelldb68b182006-12-06 20:38:36 -0800393 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800394 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800395 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800396 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800397
Johan Hovold55ba9532014-12-10 15:52:55 -0800398 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
399 if (!rtc)
400 return -ENOMEM;
401
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800402 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
403 if (of_id)
404 pdev->id_entry = of_id->data;
David Brownelldb68b182006-12-06 20:38:36 -0800405
Sekhar Nori337b6002014-06-06 14:36:04 -0700406 id_entry = platform_get_device_id(pdev);
407 if (!id_entry) {
408 dev_err(&pdev->dev, "no matching device entry\n");
409 return -ENODEV;
410 }
411
Johan Hovold55ba9532014-12-10 15:52:55 -0800412 rtc->flags = id_entry->driver_data;
413
414 rtc->irq_timer = platform_get_irq(pdev, 0);
415 if (rtc->irq_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800416 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800417
Johan Hovold55ba9532014-12-10 15:52:55 -0800418 rtc->irq_alarm = platform_get_irq(pdev, 1);
419 if (rtc->irq_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800420 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800421
David Brownelldb68b182006-12-06 20:38:36 -0800422 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Johan Hovold55ba9532014-12-10 15:52:55 -0800423 rtc->base = devm_ioremap_resource(&pdev->dev, res);
424 if (IS_ERR(rtc->base))
425 return PTR_ERR(rtc->base);
426
427 platform_set_drvdata(pdev, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800428
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800429 /* Enable the clock/module so that we can access the registers */
430 pm_runtime_enable(&pdev->dev);
431 pm_runtime_get_sync(&pdev->dev);
432
Johan Hovold55ba9532014-12-10 15:52:55 -0800433 if (rtc->flags & OMAP_RTC_HAS_KICKER) {
434 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
435 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800436 }
437
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800438 /*
439 * disable interrupts
440 *
441 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800442 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800443 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800444
Sekhar Noricd914bb2014-06-06 14:36:06 -0700445 /* enable RTC functional clock */
Johan Hovold55ba9532014-12-10 15:52:55 -0800446 if (rtc->flags & OMAP_RTC_HAS_32KCLK_EN) {
447 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
448 rtc_writel(rtc, OMAP_RTC_OSC_REG,
449 reg | OMAP_RTC_OSC_32KCLK_EN);
Johan Hovold44c63a52014-12-10 15:52:30 -0800450 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700451
David Brownelldb68b182006-12-06 20:38:36 -0800452 /* clear old status */
Johan Hovold55ba9532014-12-10 15:52:55 -0800453 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800454 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
Johan Hovold397b6302014-12-10 15:52:49 -0800455 dev_info(&pdev->dev, "RTC power up reset detected\n");
Johan Hovold55ba9532014-12-10 15:52:55 -0800456 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_POWER_UP);
David Brownelldb68b182006-12-06 20:38:36 -0800457 }
458 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
Johan Hovold55ba9532014-12-10 15:52:55 -0800459 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800460
David Brownelldb68b182006-12-06 20:38:36 -0800461 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
Johan Hovold55ba9532014-12-10 15:52:55 -0800462 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800463 if (reg & (u8) OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800464 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800465
466 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700467 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800468 new_ctrl |= OMAP_RTC_CTRL_STOP;
469
470 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
471 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700472 * - Device wake-up capability setting should come through chip
473 * init logic. OMAP1 boards should initialize the "wakeup capable"
474 * flag in the platform device if the board is wired right for
475 * being woken up by RTC alarm. For OMAP-L138, this capability
476 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800477 *
478 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
479 * rather than nPWRON_RESET, should forcibly enable split
480 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
481 * is write-only, and always reads as zero...)
482 */
David Brownelldb68b182006-12-06 20:38:36 -0800483
484 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800485 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800486
487 if (reg != new_ctrl)
Johan Hovold55ba9532014-12-10 15:52:55 -0800488 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
David Brownelldb68b182006-12-06 20:38:36 -0800489
Johan Hovold4390ce02014-12-10 15:52:43 -0800490 device_init_wakeup(&pdev->dev, true);
491
Johan Hovold55ba9532014-12-10 15:52:55 -0800492 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Johan Hovold4390ce02014-12-10 15:52:43 -0800493 &omap_rtc_ops, THIS_MODULE);
Johan Hovold55ba9532014-12-10 15:52:55 -0800494 if (IS_ERR(rtc->rtc)) {
495 ret = PTR_ERR(rtc->rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800496 goto err;
497 }
Johan Hovold4390ce02014-12-10 15:52:43 -0800498
499 /* handle periodic and alarm irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800500 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
501 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800502 if (ret)
503 goto err;
504
Johan Hovold55ba9532014-12-10 15:52:55 -0800505 if (rtc->irq_timer != rtc->irq_alarm) {
506 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
507 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800508 if (ret)
509 goto err;
510 }
511
David Brownelldb68b182006-12-06 20:38:36 -0800512 return 0;
513
Johan Hovold437b37a2014-12-10 15:52:40 -0800514err:
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800515 device_init_wakeup(&pdev->dev, false);
Johan Hovold55ba9532014-12-10 15:52:55 -0800516 if (rtc->flags & OMAP_RTC_HAS_KICKER)
517 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800518 pm_runtime_put_sync(&pdev->dev);
519 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800520
521 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800522}
523
David Brownell71fc8222008-07-23 21:30:38 -0700524static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800525{
Johan Hovold55ba9532014-12-10 15:52:55 -0800526 struct omap_rtc *rtc = platform_get_drvdata(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800527
528 device_init_wakeup(&pdev->dev, 0);
529
530 /* leave rtc running, but disable irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800531 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800532
Johan Hovold55ba9532014-12-10 15:52:55 -0800533 if (rtc->flags & OMAP_RTC_HAS_KICKER)
534 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800535
536 /* Disable the clock/module */
537 pm_runtime_put_sync(&pdev->dev);
538 pm_runtime_disable(&pdev->dev);
539
David Brownelldb68b182006-12-06 20:38:36 -0800540 return 0;
541}
542
Jingoo Han04ebc352013-04-29 16:21:01 -0700543#ifdef CONFIG_PM_SLEEP
Jingoo Han04ebc352013-04-29 16:21:01 -0700544static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800545{
Johan Hovold55ba9532014-12-10 15:52:55 -0800546 struct omap_rtc *rtc = dev_get_drvdata(dev);
547
548 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800549
550 /* FIXME the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700551 * source on some platforms, and in fact this enable() call is just
552 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800553 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700554 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800555 enable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700556 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800557 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800558
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800559 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700560 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800561
David Brownelldb68b182006-12-06 20:38:36 -0800562 return 0;
563}
564
Jingoo Han04ebc352013-04-29 16:21:01 -0700565static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800566{
Johan Hovold55ba9532014-12-10 15:52:55 -0800567 struct omap_rtc *rtc = dev_get_drvdata(dev);
568
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800569 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700570 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800571
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700572 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800573 disable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700574 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800575 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700576
David Brownelldb68b182006-12-06 20:38:36 -0800577 return 0;
578}
David Brownelldb68b182006-12-06 20:38:36 -0800579#endif
580
Jingoo Han04ebc352013-04-29 16:21:01 -0700581static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
582
David Brownelldb68b182006-12-06 20:38:36 -0800583static void omap_rtc_shutdown(struct platform_device *pdev)
584{
Johan Hovold55ba9532014-12-10 15:52:55 -0800585 struct omap_rtc *rtc = platform_get_drvdata(pdev);
586
587 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800588}
589
Kay Sieversad28a072008-04-10 21:29:25 -0700590MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800591static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700592 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800593 .shutdown = omap_rtc_shutdown,
594 .driver = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800595 .name = DRIVER_NAME,
David Brownelldb68b182006-12-06 20:38:36 -0800596 .owner = THIS_MODULE,
Jingoo Han04ebc352013-04-29 16:21:01 -0700597 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800598 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800599 },
Afzal Mohammedcab14582012-12-17 16:02:11 -0800600 .id_table = omap_rtc_devtype,
David Brownelldb68b182006-12-06 20:38:36 -0800601};
602
Jingoo Han09c5a362013-04-29 16:18:46 -0700603module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800604
605MODULE_AUTHOR("George G. Davis (and others)");
606MODULE_LICENSE("GPL");