blob: 8b6355ffaff990621354dcb8687230f527faf2b0 [file] [log] [blame]
David Brownelldb68b182006-12-06 20:38:36 -08001/*
Johan Hovold10211ae2014-12-10 15:53:19 -08002 * TI OMAP Real Time Clock interface for Linux
David Brownelldb68b182006-12-06 20:38:36 -08003 *
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)
Johan Hovold01251382014-12-10 15:53:22 -08008 * Copyright (C) 2014 Johan Hovold <johan@kernel.org>
David Brownelldb68b182006-12-06 20:38:36 -08009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/ioport.h>
20#include <linux/delay.h>
21#include <linux/rtc.h>
22#include <linux/bcd.h>
23#include <linux/platform_device.h>
Afzal Mohammed9e0344d2012-12-17 16:02:15 -080024#include <linux/of.h>
25#include <linux/of_device.h>
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -080026#include <linux/pm_runtime.h>
Sachin Kamat4b30c9f2013-07-03 15:06:00 -070027#include <linux/io.h>
David Brownelldb68b182006-12-06 20:38:36 -080028
Johan Hovold10211ae2014-12-10 15:53:19 -080029/*
30 * The OMAP RTC is a year/month/day/hours/minutes/seconds BCD clock
David Brownelldb68b182006-12-06 20:38:36 -080031 * with century-range alarm matching, driven by the 32kHz clock.
32 *
33 * The main user-visible ways it differs from PC RTCs are by omitting
34 * "don't care" alarm fields and sub-second periodic IRQs, and having
35 * an autoadjust mechanism to calibrate to the true oscillator rate.
36 *
37 * Board-specific wiring options include using split power mode with
38 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
39 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
Sekhar Norifa5b0782010-10-27 15:33:05 -070040 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
41 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
David Brownelldb68b182006-12-06 20:38:36 -080042 */
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
Johan Hovold222a12f2014-12-10 15:53:13 -080073#define OMAP_RTC_ALARM2_SECONDS_REG 0x80
74#define OMAP_RTC_ALARM2_MINUTES_REG 0x84
75#define OMAP_RTC_ALARM2_HOURS_REG 0x88
76#define OMAP_RTC_ALARM2_DAYS_REG 0x8c
77#define OMAP_RTC_ALARM2_MONTHS_REG 0x90
78#define OMAP_RTC_ALARM2_YEARS_REG 0x94
79
80#define OMAP_RTC_PMIC_REG 0x98
81
David Brownelldb68b182006-12-06 20:38:36 -080082/* OMAP_RTC_CTRL_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070083#define OMAP_RTC_CTRL_SPLIT BIT(7)
84#define OMAP_RTC_CTRL_DISABLE BIT(6)
85#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
86#define OMAP_RTC_CTRL_TEST BIT(4)
87#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
88#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
89#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
90#define OMAP_RTC_CTRL_STOP BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080091
92/* OMAP_RTC_STATUS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070093#define OMAP_RTC_STATUS_POWER_UP BIT(7)
Johan Hovold222a12f2014-12-10 15:53:13 -080094#define OMAP_RTC_STATUS_ALARM2 BIT(7)
Sekhar Nori92adb962014-06-06 14:36:05 -070095#define OMAP_RTC_STATUS_ALARM BIT(6)
96#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
97#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
98#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
99#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
100#define OMAP_RTC_STATUS_RUN BIT(1)
101#define OMAP_RTC_STATUS_BUSY BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -0800102
103/* OMAP_RTC_INTERRUPTS_REG bit fields: */
Johan Hovold222a12f2014-12-10 15:53:13 -0800104#define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
Sekhar Nori92adb962014-06-06 14:36:05 -0700105#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
106#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
David Brownelldb68b182006-12-06 20:38:36 -0800107
Sekhar Noricd914bb2014-06-06 14:36:06 -0700108/* OMAP_RTC_OSC_REG bit fields: */
109#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
110
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700111/* OMAP_RTC_IRQWAKEEN bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -0700112#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700113
Johan Hovold222a12f2014-12-10 15:53:13 -0800114/* OMAP_RTC_PMIC bit fields: */
115#define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
116
Afzal Mohammedcab14582012-12-17 16:02:11 -0800117/* OMAP_RTC_KICKER values */
118#define KICK0_VALUE 0x83e70b13
119#define KICK1_VALUE 0x95a4f1e0
120
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700121struct omap_rtc;
122
Johan Hovold2153f942014-12-10 15:53:01 -0800123struct omap_rtc_device_type {
124 bool has_32kclk_en;
Johan Hovold2153f942014-12-10 15:53:01 -0800125 bool has_irqwakeen;
Johan Hovold222a12f2014-12-10 15:53:13 -0800126 bool has_pmic_mode;
Johan Hovold9291e342014-12-10 15:53:04 -0800127 bool has_power_up_reset;
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700128 void (*lock)(struct omap_rtc *rtc);
129 void (*unlock)(struct omap_rtc *rtc);
Johan Hovold2153f942014-12-10 15:53:01 -0800130};
Sekhar Noricd914bb2014-06-06 14:36:06 -0700131
Johan Hovold55ba9532014-12-10 15:52:55 -0800132struct omap_rtc {
133 struct rtc_device *rtc;
134 void __iomem *base;
135 int irq_alarm;
136 int irq_timer;
137 u8 interrupts_reg;
Johan Hovold222a12f2014-12-10 15:53:13 -0800138 bool is_pmic_controller;
Johan Hovold2153f942014-12-10 15:53:01 -0800139 const struct omap_rtc_device_type *type;
Johan Hovold55ba9532014-12-10 15:52:55 -0800140};
David Brownelldb68b182006-12-06 20:38:36 -0800141
Johan Hovold55ba9532014-12-10 15:52:55 -0800142static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
143{
144 return readb(rtc->base + reg);
145}
Afzal Mohammedcab14582012-12-17 16:02:11 -0800146
Johan Hovoldc253a892014-12-10 15:53:10 -0800147static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
148{
149 return readl(rtc->base + reg);
150}
151
Johan Hovold55ba9532014-12-10 15:52:55 -0800152static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
153{
154 writeb(val, rtc->base + reg);
155}
David Brownelldb68b182006-12-06 20:38:36 -0800156
Johan Hovold55ba9532014-12-10 15:52:55 -0800157static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
158{
159 writel(val, rtc->base + reg);
160}
David Brownelldb68b182006-12-06 20:38:36 -0800161
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700162static void am3352_rtc_unlock(struct omap_rtc *rtc)
163{
164 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
165 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
166}
167
168static void am3352_rtc_lock(struct omap_rtc *rtc)
169{
170 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
171 rtc_writel(rtc, OMAP_RTC_KICK1_REG, 0);
172}
173
174static void default_rtc_unlock(struct omap_rtc *rtc)
175{
176}
177
178static void default_rtc_lock(struct omap_rtc *rtc)
179{
180}
181
Johan Hovold10211ae2014-12-10 15:53:19 -0800182/*
183 * We rely on the rtc framework to handle locking (rtc->ops_lock),
David Brownelldb68b182006-12-06 20:38:36 -0800184 * so the only other requirement is that register accesses which
185 * require BUSY to be clear are made with IRQs locally disabled
186 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800187static void rtc_wait_not_busy(struct omap_rtc *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800188{
Johan Hovold10211ae2014-12-10 15:53:19 -0800189 int count;
190 u8 status;
David Brownelldb68b182006-12-06 20:38:36 -0800191
192 /* BUSY may stay active for 1/32768 second (~30 usec) */
193 for (count = 0; count < 50; count++) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800194 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold10211ae2014-12-10 15:53:19 -0800195 if (!(status & OMAP_RTC_STATUS_BUSY))
David Brownelldb68b182006-12-06 20:38:36 -0800196 break;
197 udelay(1);
198 }
199 /* now we have ~15 usec to read/write various registers */
200}
201
Johan Hovold55ba9532014-12-10 15:52:55 -0800202static irqreturn_t rtc_irq(int irq, void *dev_id)
David Brownelldb68b182006-12-06 20:38:36 -0800203{
Johan Hovold10211ae2014-12-10 15:53:19 -0800204 struct omap_rtc *rtc = dev_id;
205 unsigned long events = 0;
206 u8 irq_data;
David Brownelldb68b182006-12-06 20:38:36 -0800207
Johan Hovold55ba9532014-12-10 15:52:55 -0800208 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800209
210 /* alarm irq? */
211 if (irq_data & OMAP_RTC_STATUS_ALARM) {
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700212 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800213 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700214 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800215 events |= RTC_IRQF | RTC_AF;
216 }
217
218 /* 1/sec periodic/update irq? */
219 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
220 events |= RTC_IRQF | RTC_UF;
221
Johan Hovold55ba9532014-12-10 15:52:55 -0800222 rtc_update_irq(rtc->rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800223
224 return IRQ_HANDLED;
225}
226
John Stultz16380c12011-02-02 17:02:41 -0800227static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
228{
Johan Hovold55ba9532014-12-10 15:52:55 -0800229 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700230 u8 reg, irqwake_reg = 0;
John Stultz16380c12011-02-02 17:02:41 -0800231
232 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800233 rtc_wait_not_busy(rtc);
234 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800235 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800236 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700237
238 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800239 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700240 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
241 } else {
John Stultz16380c12011-02-02 17:02:41 -0800242 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700243 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
244 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800245 rtc_wait_not_busy(rtc);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700246 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800247 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800248 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800249 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700250 rtc->type->lock(rtc);
John Stultz16380c12011-02-02 17:02:41 -0800251 local_irq_enable();
252
253 return 0;
254}
255
David Brownelldb68b182006-12-06 20:38:36 -0800256/* this hardware doesn't support "don't care" alarm fields */
257static int tm2bcd(struct rtc_time *tm)
258{
259 if (rtc_valid_tm(tm) != 0)
260 return -EINVAL;
261
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700262 tm->tm_sec = bin2bcd(tm->tm_sec);
263 tm->tm_min = bin2bcd(tm->tm_min);
264 tm->tm_hour = bin2bcd(tm->tm_hour);
265 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800266
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700267 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800268
269 /* epoch == 1900 */
270 if (tm->tm_year < 100 || tm->tm_year > 199)
271 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700272 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800273
274 return 0;
275}
276
277static void bcd2tm(struct rtc_time *tm)
278{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700279 tm->tm_sec = bcd2bin(tm->tm_sec);
280 tm->tm_min = bcd2bin(tm->tm_min);
281 tm->tm_hour = bcd2bin(tm->tm_hour);
282 tm->tm_mday = bcd2bin(tm->tm_mday);
283 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800284 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700285 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800286}
287
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800288static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
289{
290 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
291 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
292 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
293 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
294 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
295 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
296}
David Brownelldb68b182006-12-06 20:38:36 -0800297
298static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
299{
Johan Hovold55ba9532014-12-10 15:52:55 -0800300 struct omap_rtc *rtc = dev_get_drvdata(dev);
301
David Brownelldb68b182006-12-06 20:38:36 -0800302 /* we don't report wday/yday/isdst ... */
303 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800304 rtc_wait_not_busy(rtc);
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800305 omap_rtc_read_time_raw(rtc, tm);
David Brownelldb68b182006-12-06 20:38:36 -0800306 local_irq_enable();
307
308 bcd2tm(tm);
Johan Hovold10211ae2014-12-10 15:53:19 -0800309
David Brownelldb68b182006-12-06 20:38:36 -0800310 return 0;
311}
312
313static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
314{
Johan Hovold55ba9532014-12-10 15:52:55 -0800315 struct omap_rtc *rtc = dev_get_drvdata(dev);
316
David Brownelldb68b182006-12-06 20:38:36 -0800317 if (tm2bcd(tm) < 0)
318 return -EINVAL;
Johan Hovold10211ae2014-12-10 15:53:19 -0800319
David Brownelldb68b182006-12-06 20:38:36 -0800320 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800321 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800322
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700323 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800324 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
325 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
326 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
327 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
328 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
329 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700330 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800331
332 local_irq_enable();
333
334 return 0;
335}
336
337static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
338{
Johan Hovold55ba9532014-12-10 15:52:55 -0800339 struct omap_rtc *rtc = dev_get_drvdata(dev);
Johan Hovold10211ae2014-12-10 15:53:19 -0800340 u8 interrupts;
David Brownelldb68b182006-12-06 20:38:36 -0800341
Johan Hovold55ba9532014-12-10 15:52:55 -0800342 local_irq_disable();
343 rtc_wait_not_busy(rtc);
344
345 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
346 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
347 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
348 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
349 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
350 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800351
352 local_irq_enable();
353
354 bcd2tm(&alm->time);
Johan Hovold10211ae2014-12-10 15:53:19 -0800355
356 interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
357 alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800358
359 return 0;
360}
361
362static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
363{
Johan Hovold55ba9532014-12-10 15:52:55 -0800364 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700365 u8 reg, irqwake_reg = 0;
David Brownelldb68b182006-12-06 20:38:36 -0800366
David Brownelldb68b182006-12-06 20:38:36 -0800367 if (tm2bcd(&alm->time) < 0)
368 return -EINVAL;
369
370 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800371 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800372
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700373 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800374 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
375 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
376 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
377 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
378 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
379 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800380
Johan Hovold55ba9532014-12-10 15:52:55 -0800381 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800382 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800383 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700384
385 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800386 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700387 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
388 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800389 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700390 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
391 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800392 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800393 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800394 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700395 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800396
397 local_irq_enable();
398
399 return 0;
400}
401
Johan Hovold222a12f2014-12-10 15:53:13 -0800402static struct omap_rtc *omap_rtc_power_off_rtc;
403
404/*
405 * omap_rtc_poweroff: RTC-controlled power off
406 *
407 * The RTC can be used to control an external PMIC via the pmic_power_en pin,
408 * which can be configured to transition to OFF on ALARM2 events.
409 *
410 * Notes:
411 * The two-second alarm offset is the shortest offset possible as the alarm
412 * registers must be set before the next timer update and the offset
413 * calculation is too heavy for everything to be done within a single access
414 * period (~15 us).
415 *
416 * Called with local interrupts disabled.
417 */
418static void omap_rtc_power_off(void)
419{
420 struct omap_rtc *rtc = omap_rtc_power_off_rtc;
421 struct rtc_time tm;
422 unsigned long now;
423 u32 val;
424
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700425 rtc->type->unlock(rtc);
Johan Hovold222a12f2014-12-10 15:53:13 -0800426 /* enable pmic_power_en control */
427 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
428 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
429
430 /* set alarm two seconds from now */
431 omap_rtc_read_time_raw(rtc, &tm);
432 bcd2tm(&tm);
433 rtc_tm_to_time(&tm, &now);
434 rtc_time_to_tm(now + 2, &tm);
435
436 if (tm2bcd(&tm) < 0) {
437 dev_err(&rtc->rtc->dev, "power off failed\n");
438 return;
439 }
440
441 rtc_wait_not_busy(rtc);
442
443 rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
444 rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
445 rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
446 rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
447 rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
448 rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
449
450 /*
451 * enable ALARM2 interrupt
452 *
453 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
454 */
455 val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
456 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
457 val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700458 rtc->type->lock(rtc);
Johan Hovold222a12f2014-12-10 15:53:13 -0800459
460 /*
461 * Wait for alarm to trigger (within two seconds) and external PMIC to
462 * power off the system. Add a 500 ms margin for external latencies
463 * (e.g. debounce circuits).
464 */
465 mdelay(2500);
466}
467
David Brownelldb68b182006-12-06 20:38:36 -0800468static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800469 .read_time = omap_rtc_read_time,
470 .set_time = omap_rtc_set_time,
471 .read_alarm = omap_rtc_read_alarm,
472 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800473 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800474};
475
Johan Hovold2153f942014-12-10 15:53:01 -0800476static const struct omap_rtc_device_type omap_rtc_default_type = {
Johan Hovold9291e342014-12-10 15:53:04 -0800477 .has_power_up_reset = true,
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700478 .lock = default_rtc_lock,
479 .unlock = default_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800480};
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800481
Johan Hovold2153f942014-12-10 15:53:01 -0800482static const struct omap_rtc_device_type omap_rtc_am3352_type = {
483 .has_32kclk_en = true,
Johan Hovold2153f942014-12-10 15:53:01 -0800484 .has_irqwakeen = true,
Johan Hovold222a12f2014-12-10 15:53:13 -0800485 .has_pmic_mode = true,
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700486 .lock = am3352_rtc_lock,
487 .unlock = am3352_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800488};
489
490static const struct omap_rtc_device_type omap_rtc_da830_type = {
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700491 .lock = am3352_rtc_lock,
492 .unlock = am3352_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800493};
494
495static const struct platform_device_id omap_rtc_id_table[] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800496 {
Johan Hovolda430ca22014-12-10 15:52:58 -0800497 .name = "omap_rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800498 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
499 }, {
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700500 .name = "am3352-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800501 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
502 }, {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800503 .name = "da830-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800504 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
505 }, {
506 /* sentinel */
507 }
Afzal Mohammedcab14582012-12-17 16:02:11 -0800508};
Johan Hovold2153f942014-12-10 15:53:01 -0800509MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800510
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800511static const struct of_device_id omap_rtc_of_match[] = {
Johan Hovold2153f942014-12-10 15:53:01 -0800512 {
513 .compatible = "ti,am3352-rtc",
514 .data = &omap_rtc_am3352_type,
515 }, {
516 .compatible = "ti,da830-rtc",
517 .data = &omap_rtc_da830_type,
518 }, {
519 /* sentinel */
520 }
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800521};
522MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
523
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700524static int omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800525{
Johan Hovold10211ae2014-12-10 15:53:19 -0800526 struct omap_rtc *rtc;
527 struct resource *res;
528 u8 reg, mask, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800529 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800530 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800531 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800532
Johan Hovold55ba9532014-12-10 15:52:55 -0800533 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
534 if (!rtc)
535 return -ENOMEM;
536
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800537 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
Johan Hovold2153f942014-12-10 15:53:01 -0800538 if (of_id) {
539 rtc->type = of_id->data;
Johan Hovold222a12f2014-12-10 15:53:13 -0800540 rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
541 of_property_read_bool(pdev->dev.of_node,
Johan Hovold094d3ee2014-12-10 15:54:14 -0800542 "system-power-controller");
Johan Hovold2153f942014-12-10 15:53:01 -0800543 } else {
544 id_entry = platform_get_device_id(pdev);
545 rtc->type = (void *)id_entry->driver_data;
Sekhar Nori337b6002014-06-06 14:36:04 -0700546 }
547
Johan Hovold55ba9532014-12-10 15:52:55 -0800548 rtc->irq_timer = platform_get_irq(pdev, 0);
549 if (rtc->irq_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800550 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800551
Johan Hovold55ba9532014-12-10 15:52:55 -0800552 rtc->irq_alarm = platform_get_irq(pdev, 1);
553 if (rtc->irq_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800554 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800555
David Brownelldb68b182006-12-06 20:38:36 -0800556 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Johan Hovold55ba9532014-12-10 15:52:55 -0800557 rtc->base = devm_ioremap_resource(&pdev->dev, res);
558 if (IS_ERR(rtc->base))
559 return PTR_ERR(rtc->base);
560
561 platform_set_drvdata(pdev, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800562
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800563 /* Enable the clock/module so that we can access the registers */
564 pm_runtime_enable(&pdev->dev);
565 pm_runtime_get_sync(&pdev->dev);
566
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700567 rtc->type->unlock(rtc);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800568
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800569 /*
570 * disable interrupts
571 *
572 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800573 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800574 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800575
Sekhar Noricd914bb2014-06-06 14:36:06 -0700576 /* enable RTC functional clock */
Johan Hovold2153f942014-12-10 15:53:01 -0800577 if (rtc->type->has_32kclk_en) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800578 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
579 rtc_writel(rtc, OMAP_RTC_OSC_REG,
580 reg | OMAP_RTC_OSC_32KCLK_EN);
Johan Hovold44c63a52014-12-10 15:52:30 -0800581 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700582
David Brownelldb68b182006-12-06 20:38:36 -0800583 /* clear old status */
Johan Hovold55ba9532014-12-10 15:52:55 -0800584 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold9291e342014-12-10 15:53:04 -0800585
586 mask = OMAP_RTC_STATUS_ALARM;
587
Johan Hovold222a12f2014-12-10 15:53:13 -0800588 if (rtc->type->has_pmic_mode)
589 mask |= OMAP_RTC_STATUS_ALARM2;
590
Johan Hovold9291e342014-12-10 15:53:04 -0800591 if (rtc->type->has_power_up_reset) {
592 mask |= OMAP_RTC_STATUS_POWER_UP;
593 if (reg & OMAP_RTC_STATUS_POWER_UP)
594 dev_info(&pdev->dev, "RTC power up reset detected\n");
David Brownelldb68b182006-12-06 20:38:36 -0800595 }
Johan Hovold9291e342014-12-10 15:53:04 -0800596
597 if (reg & mask)
598 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
David Brownelldb68b182006-12-06 20:38:36 -0800599
David Brownelldb68b182006-12-06 20:38:36 -0800600 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
Johan Hovold55ba9532014-12-10 15:52:55 -0800601 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
Johan Hovold10211ae2014-12-10 15:53:19 -0800602 if (reg & OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800603 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800604
605 /* force to 24 hour mode */
Johan Hovold10211ae2014-12-10 15:53:19 -0800606 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800607 new_ctrl |= OMAP_RTC_CTRL_STOP;
608
Johan Hovold10211ae2014-12-10 15:53:19 -0800609 /*
610 * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
David Brownelldb68b182006-12-06 20:38:36 -0800611 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700612 * - Device wake-up capability setting should come through chip
613 * init logic. OMAP1 boards should initialize the "wakeup capable"
614 * flag in the platform device if the board is wired right for
615 * being woken up by RTC alarm. For OMAP-L138, this capability
616 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800617 *
618 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
619 * rather than nPWRON_RESET, should forcibly enable split
620 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
621 * is write-only, and always reads as zero...)
622 */
David Brownelldb68b182006-12-06 20:38:36 -0800623
Johan Hovold10211ae2014-12-10 15:53:19 -0800624 if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800625 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800626
627 if (reg != new_ctrl)
Johan Hovold55ba9532014-12-10 15:52:55 -0800628 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
David Brownelldb68b182006-12-06 20:38:36 -0800629
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700630 rtc->type->lock(rtc);
631
Johan Hovold4390ce02014-12-10 15:52:43 -0800632 device_init_wakeup(&pdev->dev, true);
633
Johan Hovold55ba9532014-12-10 15:52:55 -0800634 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Johan Hovold4390ce02014-12-10 15:52:43 -0800635 &omap_rtc_ops, THIS_MODULE);
Johan Hovold55ba9532014-12-10 15:52:55 -0800636 if (IS_ERR(rtc->rtc)) {
637 ret = PTR_ERR(rtc->rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800638 goto err;
639 }
Johan Hovold4390ce02014-12-10 15:52:43 -0800640
641 /* handle periodic and alarm irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800642 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
643 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800644 if (ret)
645 goto err;
646
Johan Hovold55ba9532014-12-10 15:52:55 -0800647 if (rtc->irq_timer != rtc->irq_alarm) {
648 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
649 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800650 if (ret)
651 goto err;
652 }
653
Johan Hovold222a12f2014-12-10 15:53:13 -0800654 if (rtc->is_pmic_controller) {
655 if (!pm_power_off) {
656 omap_rtc_power_off_rtc = rtc;
657 pm_power_off = omap_rtc_power_off;
658 }
659 }
660
David Brownelldb68b182006-12-06 20:38:36 -0800661 return 0;
662
Johan Hovold437b37a2014-12-10 15:52:40 -0800663err:
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800664 device_init_wakeup(&pdev->dev, false);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700665 rtc->type->lock(rtc);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800666 pm_runtime_put_sync(&pdev->dev);
667 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800668
669 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800670}
671
David Brownell71fc8222008-07-23 21:30:38 -0700672static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800673{
Johan Hovold55ba9532014-12-10 15:52:55 -0800674 struct omap_rtc *rtc = platform_get_drvdata(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800675
Johan Hovold222a12f2014-12-10 15:53:13 -0800676 if (pm_power_off == omap_rtc_power_off &&
677 omap_rtc_power_off_rtc == rtc) {
678 pm_power_off = NULL;
679 omap_rtc_power_off_rtc = NULL;
680 }
681
David Brownelldb68b182006-12-06 20:38:36 -0800682 device_init_wakeup(&pdev->dev, 0);
683
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700684 rtc->type->unlock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800685 /* leave rtc running, but disable irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800686 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800687
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700688 rtc->type->lock(rtc);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800689
690 /* Disable the clock/module */
691 pm_runtime_put_sync(&pdev->dev);
692 pm_runtime_disable(&pdev->dev);
693
David Brownelldb68b182006-12-06 20:38:36 -0800694 return 0;
695}
696
Jingoo Han04ebc352013-04-29 16:21:01 -0700697#ifdef CONFIG_PM_SLEEP
Jingoo Han04ebc352013-04-29 16:21:01 -0700698static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800699{
Johan Hovold55ba9532014-12-10 15:52:55 -0800700 struct omap_rtc *rtc = dev_get_drvdata(dev);
701
702 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800703
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700704 rtc->type->unlock(rtc);
Johan Hovold10211ae2014-12-10 15:53:19 -0800705 /*
706 * FIXME: the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700707 * source on some platforms, and in fact this enable() call is just
708 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800709 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700710 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800711 enable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700712 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800713 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700714 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800715
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800716 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700717 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800718
David Brownelldb68b182006-12-06 20:38:36 -0800719 return 0;
720}
721
Jingoo Han04ebc352013-04-29 16:21:01 -0700722static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800723{
Johan Hovold55ba9532014-12-10 15:52:55 -0800724 struct omap_rtc *rtc = dev_get_drvdata(dev);
725
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800726 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700727 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800728
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700729 rtc->type->unlock(rtc);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700730 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800731 disable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700732 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800733 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700734 rtc->type->lock(rtc);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700735
David Brownelldb68b182006-12-06 20:38:36 -0800736 return 0;
737}
David Brownelldb68b182006-12-06 20:38:36 -0800738#endif
739
Jingoo Han04ebc352013-04-29 16:21:01 -0700740static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
741
David Brownelldb68b182006-12-06 20:38:36 -0800742static void omap_rtc_shutdown(struct platform_device *pdev)
743{
Johan Hovold55ba9532014-12-10 15:52:55 -0800744 struct omap_rtc *rtc = platform_get_drvdata(pdev);
Johan Hovold8ad5c722014-12-10 15:53:16 -0800745 u8 mask;
Johan Hovold55ba9532014-12-10 15:52:55 -0800746
Johan Hovold8ad5c722014-12-10 15:53:16 -0800747 /*
748 * Keep the ALARM interrupt enabled to allow the system to power up on
749 * alarm events.
750 */
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700751 rtc->type->unlock(rtc);
Johan Hovold8ad5c722014-12-10 15:53:16 -0800752 mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
753 mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
754 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700755 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800756}
757
David Brownelldb68b182006-12-06 20:38:36 -0800758static struct platform_driver omap_rtc_driver = {
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700759 .probe = omap_rtc_probe,
David Brownell71fc8222008-07-23 21:30:38 -0700760 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800761 .shutdown = omap_rtc_shutdown,
762 .driver = {
Johan Hovolda430ca22014-12-10 15:52:58 -0800763 .name = "omap_rtc",
Jingoo Han04ebc352013-04-29 16:21:01 -0700764 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800765 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800766 },
Johan Hovold2153f942014-12-10 15:53:01 -0800767 .id_table = omap_rtc_id_table,
David Brownelldb68b182006-12-06 20:38:36 -0800768};
769
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700770module_platform_driver(omap_rtc_driver);
David Brownelldb68b182006-12-06 20:38:36 -0800771
Johan Hovolda430ca22014-12-10 15:52:58 -0800772MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800773MODULE_AUTHOR("George G. Davis (and others)");
774MODULE_LICENSE("GPL");