blob: 1abd88e24c22c51d75710fddbd7d675bf1a96e83 [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
David Brownelldb68b182006-12-06 20:38:36 -080042/* RTC registers */
43#define OMAP_RTC_SECONDS_REG 0x00
44#define OMAP_RTC_MINUTES_REG 0x04
45#define OMAP_RTC_HOURS_REG 0x08
46#define OMAP_RTC_DAYS_REG 0x0C
47#define OMAP_RTC_MONTHS_REG 0x10
48#define OMAP_RTC_YEARS_REG 0x14
49#define OMAP_RTC_WEEKS_REG 0x18
50
51#define OMAP_RTC_ALARM_SECONDS_REG 0x20
52#define OMAP_RTC_ALARM_MINUTES_REG 0x24
53#define OMAP_RTC_ALARM_HOURS_REG 0x28
54#define OMAP_RTC_ALARM_DAYS_REG 0x2c
55#define OMAP_RTC_ALARM_MONTHS_REG 0x30
56#define OMAP_RTC_ALARM_YEARS_REG 0x34
57
58#define OMAP_RTC_CTRL_REG 0x40
59#define OMAP_RTC_STATUS_REG 0x44
60#define OMAP_RTC_INTERRUPTS_REG 0x48
61
62#define OMAP_RTC_COMP_LSB_REG 0x4c
63#define OMAP_RTC_COMP_MSB_REG 0x50
64#define OMAP_RTC_OSC_REG 0x54
65
Afzal Mohammedcab14582012-12-17 16:02:11 -080066#define OMAP_RTC_KICK0_REG 0x6c
67#define OMAP_RTC_KICK1_REG 0x70
68
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070069#define OMAP_RTC_IRQWAKEEN 0x7c
70
David Brownelldb68b182006-12-06 20:38:36 -080071/* OMAP_RTC_CTRL_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070072#define OMAP_RTC_CTRL_SPLIT BIT(7)
73#define OMAP_RTC_CTRL_DISABLE BIT(6)
74#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
75#define OMAP_RTC_CTRL_TEST BIT(4)
76#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
77#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
78#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
79#define OMAP_RTC_CTRL_STOP BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080080
81/* OMAP_RTC_STATUS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070082#define OMAP_RTC_STATUS_POWER_UP BIT(7)
83#define OMAP_RTC_STATUS_ALARM BIT(6)
84#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
85#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
86#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
87#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
88#define OMAP_RTC_STATUS_RUN BIT(1)
89#define OMAP_RTC_STATUS_BUSY BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080090
91/* OMAP_RTC_INTERRUPTS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070092#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
93#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
David Brownelldb68b182006-12-06 20:38:36 -080094
Sekhar Noricd914bb2014-06-06 14:36:06 -070095/* OMAP_RTC_OSC_REG bit fields: */
96#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
97
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070098/* OMAP_RTC_IRQWAKEEN bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070099#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700100
Afzal Mohammedcab14582012-12-17 16:02:11 -0800101/* OMAP_RTC_KICKER values */
102#define KICK0_VALUE 0x83e70b13
103#define KICK1_VALUE 0x95a4f1e0
104
Johan Hovold2153f942014-12-10 15:53:01 -0800105struct omap_rtc_device_type {
106 bool has_32kclk_en;
107 bool has_kicker;
108 bool has_irqwakeen;
109};
Sekhar Noricd914bb2014-06-06 14:36:06 -0700110
Johan Hovold55ba9532014-12-10 15:52:55 -0800111struct omap_rtc {
112 struct rtc_device *rtc;
113 void __iomem *base;
114 int irq_alarm;
115 int irq_timer;
116 u8 interrupts_reg;
Johan Hovold2153f942014-12-10 15:53:01 -0800117 const struct omap_rtc_device_type *type;
Johan Hovold55ba9532014-12-10 15:52:55 -0800118};
David Brownelldb68b182006-12-06 20:38:36 -0800119
Johan Hovold55ba9532014-12-10 15:52:55 -0800120static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
121{
122 return readb(rtc->base + reg);
123}
Afzal Mohammedcab14582012-12-17 16:02:11 -0800124
Johan Hovold55ba9532014-12-10 15:52:55 -0800125static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
126{
127 writeb(val, rtc->base + reg);
128}
David Brownelldb68b182006-12-06 20:38:36 -0800129
Johan Hovold55ba9532014-12-10 15:52:55 -0800130static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
131{
132 writel(val, rtc->base + reg);
133}
David Brownelldb68b182006-12-06 20:38:36 -0800134
David Brownelldb68b182006-12-06 20:38:36 -0800135/* we rely on the rtc framework to handle locking (rtc->ops_lock),
136 * so the only other requirement is that register accesses which
137 * require BUSY to be clear are made with IRQs locally disabled
138 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800139static void rtc_wait_not_busy(struct omap_rtc *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800140{
141 int count = 0;
142 u8 status;
143
144 /* BUSY may stay active for 1/32768 second (~30 usec) */
145 for (count = 0; count < 50; count++) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800146 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800147 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
148 break;
149 udelay(1);
150 }
151 /* now we have ~15 usec to read/write various registers */
152}
153
Johan Hovold55ba9532014-12-10 15:52:55 -0800154static irqreturn_t rtc_irq(int irq, void *dev_id)
David Brownelldb68b182006-12-06 20:38:36 -0800155{
Johan Hovold55ba9532014-12-10 15:52:55 -0800156 struct omap_rtc *rtc = dev_id;
David Brownelldb68b182006-12-06 20:38:36 -0800157 unsigned long events = 0;
158 u8 irq_data;
159
Johan Hovold55ba9532014-12-10 15:52:55 -0800160 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800161
162 /* alarm irq? */
163 if (irq_data & OMAP_RTC_STATUS_ALARM) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800164 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800165 events |= RTC_IRQF | RTC_AF;
166 }
167
168 /* 1/sec periodic/update irq? */
169 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
170 events |= RTC_IRQF | RTC_UF;
171
Johan Hovold55ba9532014-12-10 15:52:55 -0800172 rtc_update_irq(rtc->rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800173
174 return IRQ_HANDLED;
175}
176
John Stultz16380c12011-02-02 17:02:41 -0800177static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
178{
Johan Hovold55ba9532014-12-10 15:52:55 -0800179 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700180 u8 reg, irqwake_reg = 0;
John Stultz16380c12011-02-02 17:02:41 -0800181
182 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800183 rtc_wait_not_busy(rtc);
184 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800185 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800186 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700187
188 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800189 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700190 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
191 } else {
John Stultz16380c12011-02-02 17:02:41 -0800192 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700193 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
194 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800195 rtc_wait_not_busy(rtc);
196 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800197 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800198 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
John Stultz16380c12011-02-02 17:02:41 -0800199 local_irq_enable();
200
201 return 0;
202}
203
David Brownelldb68b182006-12-06 20:38:36 -0800204/* this hardware doesn't support "don't care" alarm fields */
205static int tm2bcd(struct rtc_time *tm)
206{
207 if (rtc_valid_tm(tm) != 0)
208 return -EINVAL;
209
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700210 tm->tm_sec = bin2bcd(tm->tm_sec);
211 tm->tm_min = bin2bcd(tm->tm_min);
212 tm->tm_hour = bin2bcd(tm->tm_hour);
213 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800214
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700215 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800216
217 /* epoch == 1900 */
218 if (tm->tm_year < 100 || tm->tm_year > 199)
219 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700220 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800221
222 return 0;
223}
224
225static void bcd2tm(struct rtc_time *tm)
226{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700227 tm->tm_sec = bcd2bin(tm->tm_sec);
228 tm->tm_min = bcd2bin(tm->tm_min);
229 tm->tm_hour = bcd2bin(tm->tm_hour);
230 tm->tm_mday = bcd2bin(tm->tm_mday);
231 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800232 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700233 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800234}
235
236
237static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
238{
Johan Hovold55ba9532014-12-10 15:52:55 -0800239 struct omap_rtc *rtc = dev_get_drvdata(dev);
240
David Brownelldb68b182006-12-06 20:38:36 -0800241 /* we don't report wday/yday/isdst ... */
242 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800243 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800244
Johan Hovold55ba9532014-12-10 15:52:55 -0800245 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
246 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
247 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
248 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
249 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
250 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800251
252 local_irq_enable();
253
254 bcd2tm(tm);
255 return 0;
256}
257
258static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
259{
Johan Hovold55ba9532014-12-10 15:52:55 -0800260 struct omap_rtc *rtc = dev_get_drvdata(dev);
261
David Brownelldb68b182006-12-06 20:38:36 -0800262 if (tm2bcd(tm) < 0)
263 return -EINVAL;
264 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800265 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800266
Johan Hovold55ba9532014-12-10 15:52:55 -0800267 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
268 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
269 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
270 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
271 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
272 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800273
274 local_irq_enable();
275
276 return 0;
277}
278
279static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
280{
Johan Hovold55ba9532014-12-10 15:52:55 -0800281 struct omap_rtc *rtc = dev_get_drvdata(dev);
David Brownelldb68b182006-12-06 20:38:36 -0800282
Johan Hovold55ba9532014-12-10 15:52:55 -0800283 local_irq_disable();
284 rtc_wait_not_busy(rtc);
285
286 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
287 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
288 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
289 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
290 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
291 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800292
293 local_irq_enable();
294
295 bcd2tm(&alm->time);
Johan Hovold55ba9532014-12-10 15:52:55 -0800296 alm->enabled = !!(rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800297 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800298
299 return 0;
300}
301
302static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
303{
Johan Hovold55ba9532014-12-10 15:52:55 -0800304 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700305 u8 reg, irqwake_reg = 0;
David Brownelldb68b182006-12-06 20:38:36 -0800306
David Brownelldb68b182006-12-06 20:38:36 -0800307 if (tm2bcd(&alm->time) < 0)
308 return -EINVAL;
309
310 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800311 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800312
Johan Hovold55ba9532014-12-10 15:52:55 -0800313 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
314 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
315 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
316 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
317 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
318 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800319
Johan Hovold55ba9532014-12-10 15:52:55 -0800320 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800321 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800322 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700323
324 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800325 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700326 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
327 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800328 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700329 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
330 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800331 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800332 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800333 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
David Brownelldb68b182006-12-06 20:38:36 -0800334
335 local_irq_enable();
336
337 return 0;
338}
339
340static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800341 .read_time = omap_rtc_read_time,
342 .set_time = omap_rtc_set_time,
343 .read_alarm = omap_rtc_read_alarm,
344 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800345 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800346};
347
Johan Hovold2153f942014-12-10 15:53:01 -0800348static const struct omap_rtc_device_type omap_rtc_default_type = {
349};
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800350
Johan Hovold2153f942014-12-10 15:53:01 -0800351static const struct omap_rtc_device_type omap_rtc_am3352_type = {
352 .has_32kclk_en = true,
353 .has_kicker = true,
354 .has_irqwakeen = true,
355};
356
357static const struct omap_rtc_device_type omap_rtc_da830_type = {
358 .has_kicker = true,
359};
360
361static const struct platform_device_id omap_rtc_id_table[] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800362 {
Johan Hovolda430ca22014-12-10 15:52:58 -0800363 .name = "omap_rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800364 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
365 }, {
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700366 .name = "am3352-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800367 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
368 }, {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800369 .name = "da830-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800370 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
371 }, {
372 /* sentinel */
373 }
Afzal Mohammedcab14582012-12-17 16:02:11 -0800374};
Johan Hovold2153f942014-12-10 15:53:01 -0800375MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800376
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800377static const struct of_device_id omap_rtc_of_match[] = {
Johan Hovold2153f942014-12-10 15:53:01 -0800378 {
379 .compatible = "ti,am3352-rtc",
380 .data = &omap_rtc_am3352_type,
381 }, {
382 .compatible = "ti,da830-rtc",
383 .data = &omap_rtc_da830_type,
384 }, {
385 /* sentinel */
386 }
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800387};
388MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
389
David Brownell71fc8222008-07-23 21:30:38 -0700390static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800391{
Johan Hovold55ba9532014-12-10 15:52:55 -0800392 struct omap_rtc *rtc;
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700393 struct resource *res;
David Brownelldb68b182006-12-06 20:38:36 -0800394 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800395 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800396 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800397 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800398
Johan Hovold55ba9532014-12-10 15:52:55 -0800399 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
400 if (!rtc)
401 return -ENOMEM;
402
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800403 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
Johan Hovold2153f942014-12-10 15:53:01 -0800404 if (of_id) {
405 rtc->type = of_id->data;
406 } else {
407 id_entry = platform_get_device_id(pdev);
408 rtc->type = (void *)id_entry->driver_data;
Sekhar Nori337b6002014-06-06 14:36:04 -0700409 }
410
Johan Hovold55ba9532014-12-10 15:52:55 -0800411 rtc->irq_timer = platform_get_irq(pdev, 0);
412 if (rtc->irq_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800413 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800414
Johan Hovold55ba9532014-12-10 15:52:55 -0800415 rtc->irq_alarm = platform_get_irq(pdev, 1);
416 if (rtc->irq_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800417 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800418
David Brownelldb68b182006-12-06 20:38:36 -0800419 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Johan Hovold55ba9532014-12-10 15:52:55 -0800420 rtc->base = devm_ioremap_resource(&pdev->dev, res);
421 if (IS_ERR(rtc->base))
422 return PTR_ERR(rtc->base);
423
424 platform_set_drvdata(pdev, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800425
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800426 /* Enable the clock/module so that we can access the registers */
427 pm_runtime_enable(&pdev->dev);
428 pm_runtime_get_sync(&pdev->dev);
429
Johan Hovold2153f942014-12-10 15:53:01 -0800430 if (rtc->type->has_kicker) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800431 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
432 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800433 }
434
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800435 /*
436 * disable interrupts
437 *
438 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800439 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800440 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800441
Sekhar Noricd914bb2014-06-06 14:36:06 -0700442 /* enable RTC functional clock */
Johan Hovold2153f942014-12-10 15:53:01 -0800443 if (rtc->type->has_32kclk_en) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800444 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
445 rtc_writel(rtc, OMAP_RTC_OSC_REG,
446 reg | OMAP_RTC_OSC_32KCLK_EN);
Johan Hovold44c63a52014-12-10 15:52:30 -0800447 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700448
David Brownelldb68b182006-12-06 20:38:36 -0800449 /* clear old status */
Johan Hovold55ba9532014-12-10 15:52:55 -0800450 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800451 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
Johan Hovold397b6302014-12-10 15:52:49 -0800452 dev_info(&pdev->dev, "RTC power up reset detected\n");
Johan Hovold55ba9532014-12-10 15:52:55 -0800453 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_POWER_UP);
David Brownelldb68b182006-12-06 20:38:36 -0800454 }
455 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
Johan Hovold55ba9532014-12-10 15:52:55 -0800456 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800457
David Brownelldb68b182006-12-06 20:38:36 -0800458 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
Johan Hovold55ba9532014-12-10 15:52:55 -0800459 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800460 if (reg & (u8) OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800461 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800462
463 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700464 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800465 new_ctrl |= OMAP_RTC_CTRL_STOP;
466
467 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
468 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700469 * - Device wake-up capability setting should come through chip
470 * init logic. OMAP1 boards should initialize the "wakeup capable"
471 * flag in the platform device if the board is wired right for
472 * being woken up by RTC alarm. For OMAP-L138, this capability
473 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800474 *
475 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
476 * rather than nPWRON_RESET, should forcibly enable split
477 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
478 * is write-only, and always reads as zero...)
479 */
David Brownelldb68b182006-12-06 20:38:36 -0800480
481 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800482 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800483
484 if (reg != new_ctrl)
Johan Hovold55ba9532014-12-10 15:52:55 -0800485 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
David Brownelldb68b182006-12-06 20:38:36 -0800486
Johan Hovold4390ce02014-12-10 15:52:43 -0800487 device_init_wakeup(&pdev->dev, true);
488
Johan Hovold55ba9532014-12-10 15:52:55 -0800489 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Johan Hovold4390ce02014-12-10 15:52:43 -0800490 &omap_rtc_ops, THIS_MODULE);
Johan Hovold55ba9532014-12-10 15:52:55 -0800491 if (IS_ERR(rtc->rtc)) {
492 ret = PTR_ERR(rtc->rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800493 goto err;
494 }
Johan Hovold4390ce02014-12-10 15:52:43 -0800495
496 /* handle periodic and alarm irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800497 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
498 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800499 if (ret)
500 goto err;
501
Johan Hovold55ba9532014-12-10 15:52:55 -0800502 if (rtc->irq_timer != rtc->irq_alarm) {
503 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
504 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800505 if (ret)
506 goto err;
507 }
508
David Brownelldb68b182006-12-06 20:38:36 -0800509 return 0;
510
Johan Hovold437b37a2014-12-10 15:52:40 -0800511err:
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800512 device_init_wakeup(&pdev->dev, false);
Johan Hovold2153f942014-12-10 15:53:01 -0800513 if (rtc->type->has_kicker)
Johan Hovold55ba9532014-12-10 15:52:55 -0800514 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800515 pm_runtime_put_sync(&pdev->dev);
516 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800517
518 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800519}
520
David Brownell71fc8222008-07-23 21:30:38 -0700521static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800522{
Johan Hovold55ba9532014-12-10 15:52:55 -0800523 struct omap_rtc *rtc = platform_get_drvdata(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800524
525 device_init_wakeup(&pdev->dev, 0);
526
527 /* leave rtc running, but disable irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800528 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800529
Johan Hovold2153f942014-12-10 15:53:01 -0800530 if (rtc->type->has_kicker)
Johan Hovold55ba9532014-12-10 15:52:55 -0800531 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800532
533 /* Disable the clock/module */
534 pm_runtime_put_sync(&pdev->dev);
535 pm_runtime_disable(&pdev->dev);
536
David Brownelldb68b182006-12-06 20:38:36 -0800537 return 0;
538}
539
Jingoo Han04ebc352013-04-29 16:21:01 -0700540#ifdef CONFIG_PM_SLEEP
Jingoo Han04ebc352013-04-29 16:21:01 -0700541static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800542{
Johan Hovold55ba9532014-12-10 15:52:55 -0800543 struct omap_rtc *rtc = dev_get_drvdata(dev);
544
545 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800546
547 /* FIXME the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700548 * source on some platforms, and in fact this enable() call is just
549 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800550 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700551 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800552 enable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700553 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800554 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800555
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800556 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700557 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800558
David Brownelldb68b182006-12-06 20:38:36 -0800559 return 0;
560}
561
Jingoo Han04ebc352013-04-29 16:21:01 -0700562static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800563{
Johan Hovold55ba9532014-12-10 15:52:55 -0800564 struct omap_rtc *rtc = dev_get_drvdata(dev);
565
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800566 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700567 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800568
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700569 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800570 disable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700571 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800572 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700573
David Brownelldb68b182006-12-06 20:38:36 -0800574 return 0;
575}
David Brownelldb68b182006-12-06 20:38:36 -0800576#endif
577
Jingoo Han04ebc352013-04-29 16:21:01 -0700578static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
579
David Brownelldb68b182006-12-06 20:38:36 -0800580static void omap_rtc_shutdown(struct platform_device *pdev)
581{
Johan Hovold55ba9532014-12-10 15:52:55 -0800582 struct omap_rtc *rtc = platform_get_drvdata(pdev);
583
584 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800585}
586
David Brownelldb68b182006-12-06 20:38:36 -0800587static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700588 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800589 .shutdown = omap_rtc_shutdown,
590 .driver = {
Johan Hovolda430ca22014-12-10 15:52:58 -0800591 .name = "omap_rtc",
David Brownelldb68b182006-12-06 20:38:36 -0800592 .owner = THIS_MODULE,
Jingoo Han04ebc352013-04-29 16:21:01 -0700593 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800594 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800595 },
Johan Hovold2153f942014-12-10 15:53:01 -0800596 .id_table = omap_rtc_id_table,
David Brownelldb68b182006-12-06 20:38:36 -0800597};
598
Jingoo Han09c5a362013-04-29 16:18:46 -0700599module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800600
Johan Hovolda430ca22014-12-10 15:52:58 -0800601MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800602MODULE_AUTHOR("George G. Davis (and others)");
603MODULE_LICENSE("GPL");