blob: c508e45ca3ce798e3c321d78814b5f20b18f73c0 [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;
Johan Hovold9291e342014-12-10 15:53:04 -0800109 bool has_power_up_reset;
Johan Hovold2153f942014-12-10 15:53:01 -0800110};
Sekhar Noricd914bb2014-06-06 14:36:06 -0700111
Johan Hovold55ba9532014-12-10 15:52:55 -0800112struct omap_rtc {
113 struct rtc_device *rtc;
114 void __iomem *base;
115 int irq_alarm;
116 int irq_timer;
117 u8 interrupts_reg;
Johan Hovold2153f942014-12-10 15:53:01 -0800118 const struct omap_rtc_device_type *type;
Johan Hovold55ba9532014-12-10 15:52:55 -0800119};
David Brownelldb68b182006-12-06 20:38:36 -0800120
Johan Hovold55ba9532014-12-10 15:52:55 -0800121static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
122{
123 return readb(rtc->base + reg);
124}
Afzal Mohammedcab14582012-12-17 16:02:11 -0800125
Johan Hovoldc253a892014-12-10 15:53:10 -0800126static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
127{
128 return readl(rtc->base + reg);
129}
130
Johan Hovold55ba9532014-12-10 15:52:55 -0800131static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
132{
133 writeb(val, rtc->base + reg);
134}
David Brownelldb68b182006-12-06 20:38:36 -0800135
Johan Hovold55ba9532014-12-10 15:52:55 -0800136static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
137{
138 writel(val, rtc->base + reg);
139}
David Brownelldb68b182006-12-06 20:38:36 -0800140
David Brownelldb68b182006-12-06 20:38:36 -0800141/* we rely on the rtc framework to handle locking (rtc->ops_lock),
142 * so the only other requirement is that register accesses which
143 * require BUSY to be clear are made with IRQs locally disabled
144 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800145static void rtc_wait_not_busy(struct omap_rtc *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800146{
147 int count = 0;
148 u8 status;
149
150 /* BUSY may stay active for 1/32768 second (~30 usec) */
151 for (count = 0; count < 50; count++) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800152 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800153 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
154 break;
155 udelay(1);
156 }
157 /* now we have ~15 usec to read/write various registers */
158}
159
Johan Hovold55ba9532014-12-10 15:52:55 -0800160static irqreturn_t rtc_irq(int irq, void *dev_id)
David Brownelldb68b182006-12-06 20:38:36 -0800161{
Johan Hovold55ba9532014-12-10 15:52:55 -0800162 struct omap_rtc *rtc = dev_id;
David Brownelldb68b182006-12-06 20:38:36 -0800163 unsigned long events = 0;
164 u8 irq_data;
165
Johan Hovold55ba9532014-12-10 15:52:55 -0800166 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800167
168 /* alarm irq? */
169 if (irq_data & OMAP_RTC_STATUS_ALARM) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800170 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800171 events |= RTC_IRQF | RTC_AF;
172 }
173
174 /* 1/sec periodic/update irq? */
175 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
176 events |= RTC_IRQF | RTC_UF;
177
Johan Hovold55ba9532014-12-10 15:52:55 -0800178 rtc_update_irq(rtc->rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800179
180 return IRQ_HANDLED;
181}
182
John Stultz16380c12011-02-02 17:02:41 -0800183static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
184{
Johan Hovold55ba9532014-12-10 15:52:55 -0800185 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700186 u8 reg, irqwake_reg = 0;
John Stultz16380c12011-02-02 17:02:41 -0800187
188 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800189 rtc_wait_not_busy(rtc);
190 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800191 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800192 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700193
194 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800195 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700196 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
197 } else {
John Stultz16380c12011-02-02 17:02:41 -0800198 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700199 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
200 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800201 rtc_wait_not_busy(rtc);
202 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800203 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800204 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
John Stultz16380c12011-02-02 17:02:41 -0800205 local_irq_enable();
206
207 return 0;
208}
209
David Brownelldb68b182006-12-06 20:38:36 -0800210/* this hardware doesn't support "don't care" alarm fields */
211static int tm2bcd(struct rtc_time *tm)
212{
213 if (rtc_valid_tm(tm) != 0)
214 return -EINVAL;
215
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700216 tm->tm_sec = bin2bcd(tm->tm_sec);
217 tm->tm_min = bin2bcd(tm->tm_min);
218 tm->tm_hour = bin2bcd(tm->tm_hour);
219 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800220
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700221 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800222
223 /* epoch == 1900 */
224 if (tm->tm_year < 100 || tm->tm_year > 199)
225 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700226 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800227
228 return 0;
229}
230
231static void bcd2tm(struct rtc_time *tm)
232{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700233 tm->tm_sec = bcd2bin(tm->tm_sec);
234 tm->tm_min = bcd2bin(tm->tm_min);
235 tm->tm_hour = bcd2bin(tm->tm_hour);
236 tm->tm_mday = bcd2bin(tm->tm_mday);
237 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800238 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700239 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800240}
241
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800242static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
243{
244 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
245 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
246 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
247 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
248 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
249 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
250}
David Brownelldb68b182006-12-06 20:38:36 -0800251
252static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
253{
Johan Hovold55ba9532014-12-10 15:52:55 -0800254 struct omap_rtc *rtc = dev_get_drvdata(dev);
255
David Brownelldb68b182006-12-06 20:38:36 -0800256 /* we don't report wday/yday/isdst ... */
257 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800258 rtc_wait_not_busy(rtc);
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800259 omap_rtc_read_time_raw(rtc, tm);
David Brownelldb68b182006-12-06 20:38:36 -0800260 local_irq_enable();
261
262 bcd2tm(tm);
263 return 0;
264}
265
266static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
267{
Johan Hovold55ba9532014-12-10 15:52:55 -0800268 struct omap_rtc *rtc = dev_get_drvdata(dev);
269
David Brownelldb68b182006-12-06 20:38:36 -0800270 if (tm2bcd(tm) < 0)
271 return -EINVAL;
272 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800273 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800274
Johan Hovold55ba9532014-12-10 15:52:55 -0800275 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
276 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
277 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
278 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
279 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
280 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800281
282 local_irq_enable();
283
284 return 0;
285}
286
287static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
288{
Johan Hovold55ba9532014-12-10 15:52:55 -0800289 struct omap_rtc *rtc = dev_get_drvdata(dev);
David Brownelldb68b182006-12-06 20:38:36 -0800290
Johan Hovold55ba9532014-12-10 15:52:55 -0800291 local_irq_disable();
292 rtc_wait_not_busy(rtc);
293
294 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
295 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
296 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
297 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
298 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
299 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800300
301 local_irq_enable();
302
303 bcd2tm(&alm->time);
Johan Hovold55ba9532014-12-10 15:52:55 -0800304 alm->enabled = !!(rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800305 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800306
307 return 0;
308}
309
310static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
311{
Johan Hovold55ba9532014-12-10 15:52:55 -0800312 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700313 u8 reg, irqwake_reg = 0;
David Brownelldb68b182006-12-06 20:38:36 -0800314
David Brownelldb68b182006-12-06 20:38:36 -0800315 if (tm2bcd(&alm->time) < 0)
316 return -EINVAL;
317
318 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800319 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800320
Johan Hovold55ba9532014-12-10 15:52:55 -0800321 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
322 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
323 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
324 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
325 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
326 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800327
Johan Hovold55ba9532014-12-10 15:52:55 -0800328 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800329 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800330 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700331
332 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800333 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700334 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
335 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800336 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700337 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
338 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800339 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800340 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800341 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
David Brownelldb68b182006-12-06 20:38:36 -0800342
343 local_irq_enable();
344
345 return 0;
346}
347
348static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800349 .read_time = omap_rtc_read_time,
350 .set_time = omap_rtc_set_time,
351 .read_alarm = omap_rtc_read_alarm,
352 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800353 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800354};
355
Johan Hovold2153f942014-12-10 15:53:01 -0800356static const struct omap_rtc_device_type omap_rtc_default_type = {
Johan Hovold9291e342014-12-10 15:53:04 -0800357 .has_power_up_reset = true,
Johan Hovold2153f942014-12-10 15:53:01 -0800358};
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800359
Johan Hovold2153f942014-12-10 15:53:01 -0800360static const struct omap_rtc_device_type omap_rtc_am3352_type = {
361 .has_32kclk_en = true,
362 .has_kicker = true,
363 .has_irqwakeen = true,
364};
365
366static const struct omap_rtc_device_type omap_rtc_da830_type = {
367 .has_kicker = true,
368};
369
370static const struct platform_device_id omap_rtc_id_table[] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800371 {
Johan Hovolda430ca22014-12-10 15:52:58 -0800372 .name = "omap_rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800373 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
374 }, {
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700375 .name = "am3352-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800376 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
377 }, {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800378 .name = "da830-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800379 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
380 }, {
381 /* sentinel */
382 }
Afzal Mohammedcab14582012-12-17 16:02:11 -0800383};
Johan Hovold2153f942014-12-10 15:53:01 -0800384MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800385
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800386static const struct of_device_id omap_rtc_of_match[] = {
Johan Hovold2153f942014-12-10 15:53:01 -0800387 {
388 .compatible = "ti,am3352-rtc",
389 .data = &omap_rtc_am3352_type,
390 }, {
391 .compatible = "ti,da830-rtc",
392 .data = &omap_rtc_da830_type,
393 }, {
394 /* sentinel */
395 }
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800396};
397MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
398
David Brownell71fc8222008-07-23 21:30:38 -0700399static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800400{
Johan Hovold55ba9532014-12-10 15:52:55 -0800401 struct omap_rtc *rtc;
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700402 struct resource *res;
Johan Hovold9291e342014-12-10 15:53:04 -0800403 u8 reg, mask, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800404 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800405 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800406 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800407
Johan Hovold55ba9532014-12-10 15:52:55 -0800408 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
409 if (!rtc)
410 return -ENOMEM;
411
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800412 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
Johan Hovold2153f942014-12-10 15:53:01 -0800413 if (of_id) {
414 rtc->type = of_id->data;
415 } else {
416 id_entry = platform_get_device_id(pdev);
417 rtc->type = (void *)id_entry->driver_data;
Sekhar Nori337b6002014-06-06 14:36:04 -0700418 }
419
Johan Hovold55ba9532014-12-10 15:52:55 -0800420 rtc->irq_timer = platform_get_irq(pdev, 0);
421 if (rtc->irq_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800422 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800423
Johan Hovold55ba9532014-12-10 15:52:55 -0800424 rtc->irq_alarm = platform_get_irq(pdev, 1);
425 if (rtc->irq_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800426 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800427
David Brownelldb68b182006-12-06 20:38:36 -0800428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Johan Hovold55ba9532014-12-10 15:52:55 -0800429 rtc->base = devm_ioremap_resource(&pdev->dev, res);
430 if (IS_ERR(rtc->base))
431 return PTR_ERR(rtc->base);
432
433 platform_set_drvdata(pdev, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800434
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800435 /* Enable the clock/module so that we can access the registers */
436 pm_runtime_enable(&pdev->dev);
437 pm_runtime_get_sync(&pdev->dev);
438
Johan Hovold2153f942014-12-10 15:53:01 -0800439 if (rtc->type->has_kicker) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800440 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
441 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800442 }
443
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800444 /*
445 * disable interrupts
446 *
447 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800448 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800449 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800450
Sekhar Noricd914bb2014-06-06 14:36:06 -0700451 /* enable RTC functional clock */
Johan Hovold2153f942014-12-10 15:53:01 -0800452 if (rtc->type->has_32kclk_en) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800453 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
454 rtc_writel(rtc, OMAP_RTC_OSC_REG,
455 reg | OMAP_RTC_OSC_32KCLK_EN);
Johan Hovold44c63a52014-12-10 15:52:30 -0800456 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700457
David Brownelldb68b182006-12-06 20:38:36 -0800458 /* clear old status */
Johan Hovold55ba9532014-12-10 15:52:55 -0800459 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold9291e342014-12-10 15:53:04 -0800460
461 mask = OMAP_RTC_STATUS_ALARM;
462
463 if (rtc->type->has_power_up_reset) {
464 mask |= OMAP_RTC_STATUS_POWER_UP;
465 if (reg & OMAP_RTC_STATUS_POWER_UP)
466 dev_info(&pdev->dev, "RTC power up reset detected\n");
David Brownelldb68b182006-12-06 20:38:36 -0800467 }
Johan Hovold9291e342014-12-10 15:53:04 -0800468
469 if (reg & mask)
470 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
David Brownelldb68b182006-12-06 20:38:36 -0800471
David Brownelldb68b182006-12-06 20:38:36 -0800472 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
Johan Hovold55ba9532014-12-10 15:52:55 -0800473 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800474 if (reg & (u8) OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800475 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800476
477 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700478 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800479 new_ctrl |= OMAP_RTC_CTRL_STOP;
480
481 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
482 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700483 * - Device wake-up capability setting should come through chip
484 * init logic. OMAP1 boards should initialize the "wakeup capable"
485 * flag in the platform device if the board is wired right for
486 * being woken up by RTC alarm. For OMAP-L138, this capability
487 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800488 *
489 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
490 * rather than nPWRON_RESET, should forcibly enable split
491 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
492 * is write-only, and always reads as zero...)
493 */
David Brownelldb68b182006-12-06 20:38:36 -0800494
495 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800496 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800497
498 if (reg != new_ctrl)
Johan Hovold55ba9532014-12-10 15:52:55 -0800499 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
David Brownelldb68b182006-12-06 20:38:36 -0800500
Johan Hovold4390ce02014-12-10 15:52:43 -0800501 device_init_wakeup(&pdev->dev, true);
502
Johan Hovold55ba9532014-12-10 15:52:55 -0800503 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Johan Hovold4390ce02014-12-10 15:52:43 -0800504 &omap_rtc_ops, THIS_MODULE);
Johan Hovold55ba9532014-12-10 15:52:55 -0800505 if (IS_ERR(rtc->rtc)) {
506 ret = PTR_ERR(rtc->rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800507 goto err;
508 }
Johan Hovold4390ce02014-12-10 15:52:43 -0800509
510 /* handle periodic and alarm irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800511 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
512 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800513 if (ret)
514 goto err;
515
Johan Hovold55ba9532014-12-10 15:52:55 -0800516 if (rtc->irq_timer != rtc->irq_alarm) {
517 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
518 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800519 if (ret)
520 goto err;
521 }
522
David Brownelldb68b182006-12-06 20:38:36 -0800523 return 0;
524
Johan Hovold437b37a2014-12-10 15:52:40 -0800525err:
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800526 device_init_wakeup(&pdev->dev, false);
Johan Hovold2153f942014-12-10 15:53:01 -0800527 if (rtc->type->has_kicker)
Johan Hovold55ba9532014-12-10 15:52:55 -0800528 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800529 pm_runtime_put_sync(&pdev->dev);
530 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800531
532 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800533}
534
David Brownell71fc8222008-07-23 21:30:38 -0700535static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800536{
Johan Hovold55ba9532014-12-10 15:52:55 -0800537 struct omap_rtc *rtc = platform_get_drvdata(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800538
539 device_init_wakeup(&pdev->dev, 0);
540
541 /* leave rtc running, but disable irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800542 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800543
Johan Hovold2153f942014-12-10 15:53:01 -0800544 if (rtc->type->has_kicker)
Johan Hovold55ba9532014-12-10 15:52:55 -0800545 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800546
547 /* Disable the clock/module */
548 pm_runtime_put_sync(&pdev->dev);
549 pm_runtime_disable(&pdev->dev);
550
David Brownelldb68b182006-12-06 20:38:36 -0800551 return 0;
552}
553
Jingoo Han04ebc352013-04-29 16:21:01 -0700554#ifdef CONFIG_PM_SLEEP
Jingoo Han04ebc352013-04-29 16:21:01 -0700555static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800556{
Johan Hovold55ba9532014-12-10 15:52:55 -0800557 struct omap_rtc *rtc = dev_get_drvdata(dev);
558
559 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800560
561 /* FIXME the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700562 * source on some platforms, and in fact this enable() call is just
563 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800564 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700565 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800566 enable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700567 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800568 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800569
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800570 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700571 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800572
David Brownelldb68b182006-12-06 20:38:36 -0800573 return 0;
574}
575
Jingoo Han04ebc352013-04-29 16:21:01 -0700576static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800577{
Johan Hovold55ba9532014-12-10 15:52:55 -0800578 struct omap_rtc *rtc = dev_get_drvdata(dev);
579
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800580 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700581 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800582
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700583 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800584 disable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700585 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800586 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700587
David Brownelldb68b182006-12-06 20:38:36 -0800588 return 0;
589}
David Brownelldb68b182006-12-06 20:38:36 -0800590#endif
591
Jingoo Han04ebc352013-04-29 16:21:01 -0700592static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
593
David Brownelldb68b182006-12-06 20:38:36 -0800594static void omap_rtc_shutdown(struct platform_device *pdev)
595{
Johan Hovold55ba9532014-12-10 15:52:55 -0800596 struct omap_rtc *rtc = platform_get_drvdata(pdev);
597
598 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800599}
600
David Brownelldb68b182006-12-06 20:38:36 -0800601static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700602 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800603 .shutdown = omap_rtc_shutdown,
604 .driver = {
Johan Hovolda430ca22014-12-10 15:52:58 -0800605 .name = "omap_rtc",
David Brownelldb68b182006-12-06 20:38:36 -0800606 .owner = THIS_MODULE,
Jingoo Han04ebc352013-04-29 16:21:01 -0700607 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800608 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800609 },
Johan Hovold2153f942014-12-10 15:53:01 -0800610 .id_table = omap_rtc_id_table,
David Brownelldb68b182006-12-06 20:38:36 -0800611};
612
Jingoo Han09c5a362013-04-29 16:18:46 -0700613module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800614
Johan Hovolda430ca22014-12-10 15:52:58 -0800615MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800616MODULE_AUTHOR("George G. Davis (and others)");
617MODULE_LICENSE("GPL");