blob: b04ea9b5ae67348c69210d29f3f138e7d5b649e3 [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
Marcin Niestroj97ea1902016-09-16 11:26:28 +020016#include <dt-bindings/gpio/gpio.h>
David Brownelldb68b182006-12-06 20:38:36 -080017#include <linux/bcd.h>
Marcin Niestroj97ea1902016-09-16 11:26:28 +020018#include <linux/clk.h>
19#include <linux/delay.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/ioport.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
Afzal Mohammed9e0344d2012-12-17 16:02:15 -080025#include <linux/of.h>
26#include <linux/of_device.h>
Marcin Niestroj97ea1902016-09-16 11:26:28 +020027#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/pinconf.h>
29#include <linux/pinctrl/pinconf-generic.h>
30#include <linux/platform_device.h>
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -080031#include <linux/pm_runtime.h>
Marcin Niestroj97ea1902016-09-16 11:26:28 +020032#include <linux/rtc.h>
David Brownelldb68b182006-12-06 20:38:36 -080033
Johan Hovold10211ae2014-12-10 15:53:19 -080034/*
35 * The OMAP RTC is a year/month/day/hours/minutes/seconds BCD clock
David Brownelldb68b182006-12-06 20:38:36 -080036 * with century-range alarm matching, driven by the 32kHz clock.
37 *
38 * The main user-visible ways it differs from PC RTCs are by omitting
39 * "don't care" alarm fields and sub-second periodic IRQs, and having
40 * an autoadjust mechanism to calibrate to the true oscillator rate.
41 *
42 * Board-specific wiring options include using split power mode with
43 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
44 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
Sekhar Norifa5b0782010-10-27 15:33:05 -070045 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
46 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
David Brownelldb68b182006-12-06 20:38:36 -080047 */
48
David Brownelldb68b182006-12-06 20:38:36 -080049/* RTC registers */
50#define OMAP_RTC_SECONDS_REG 0x00
51#define OMAP_RTC_MINUTES_REG 0x04
52#define OMAP_RTC_HOURS_REG 0x08
53#define OMAP_RTC_DAYS_REG 0x0C
54#define OMAP_RTC_MONTHS_REG 0x10
55#define OMAP_RTC_YEARS_REG 0x14
56#define OMAP_RTC_WEEKS_REG 0x18
57
58#define OMAP_RTC_ALARM_SECONDS_REG 0x20
59#define OMAP_RTC_ALARM_MINUTES_REG 0x24
60#define OMAP_RTC_ALARM_HOURS_REG 0x28
61#define OMAP_RTC_ALARM_DAYS_REG 0x2c
62#define OMAP_RTC_ALARM_MONTHS_REG 0x30
63#define OMAP_RTC_ALARM_YEARS_REG 0x34
64
65#define OMAP_RTC_CTRL_REG 0x40
66#define OMAP_RTC_STATUS_REG 0x44
67#define OMAP_RTC_INTERRUPTS_REG 0x48
68
69#define OMAP_RTC_COMP_LSB_REG 0x4c
70#define OMAP_RTC_COMP_MSB_REG 0x50
71#define OMAP_RTC_OSC_REG 0x54
72
Afzal Mohammedcab14582012-12-17 16:02:11 -080073#define OMAP_RTC_KICK0_REG 0x6c
74#define OMAP_RTC_KICK1_REG 0x70
75
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070076#define OMAP_RTC_IRQWAKEEN 0x7c
77
Johan Hovold222a12f2014-12-10 15:53:13 -080078#define OMAP_RTC_ALARM2_SECONDS_REG 0x80
79#define OMAP_RTC_ALARM2_MINUTES_REG 0x84
80#define OMAP_RTC_ALARM2_HOURS_REG 0x88
81#define OMAP_RTC_ALARM2_DAYS_REG 0x8c
82#define OMAP_RTC_ALARM2_MONTHS_REG 0x90
83#define OMAP_RTC_ALARM2_YEARS_REG 0x94
84
85#define OMAP_RTC_PMIC_REG 0x98
86
David Brownelldb68b182006-12-06 20:38:36 -080087/* OMAP_RTC_CTRL_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070088#define OMAP_RTC_CTRL_SPLIT BIT(7)
89#define OMAP_RTC_CTRL_DISABLE BIT(6)
90#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
91#define OMAP_RTC_CTRL_TEST BIT(4)
92#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
93#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
94#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
95#define OMAP_RTC_CTRL_STOP BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080096
97/* OMAP_RTC_STATUS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070098#define OMAP_RTC_STATUS_POWER_UP BIT(7)
Johan Hovold222a12f2014-12-10 15:53:13 -080099#define OMAP_RTC_STATUS_ALARM2 BIT(7)
Sekhar Nori92adb962014-06-06 14:36:05 -0700100#define OMAP_RTC_STATUS_ALARM BIT(6)
101#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
102#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
103#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
104#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
105#define OMAP_RTC_STATUS_RUN BIT(1)
106#define OMAP_RTC_STATUS_BUSY BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -0800107
108/* OMAP_RTC_INTERRUPTS_REG bit fields: */
Johan Hovold222a12f2014-12-10 15:53:13 -0800109#define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
Sekhar Nori92adb962014-06-06 14:36:05 -0700110#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
111#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
David Brownelldb68b182006-12-06 20:38:36 -0800112
Sekhar Noricd914bb2014-06-06 14:36:06 -0700113/* OMAP_RTC_OSC_REG bit fields: */
114#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
Keerthy399cf0f2015-08-18 15:11:16 +0530115#define OMAP_RTC_OSC_SEL_32KCLK_SRC BIT(3)
Sekhar Noricd914bb2014-06-06 14:36:06 -0700116
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700117/* OMAP_RTC_IRQWAKEEN bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -0700118#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700119
Johan Hovold222a12f2014-12-10 15:53:13 -0800120/* OMAP_RTC_PMIC bit fields: */
121#define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200122#define OMAP_RTC_PMIC_EXT_WKUP_EN(x) BIT(x)
123#define OMAP_RTC_PMIC_EXT_WKUP_POL(x) BIT(4 + x)
Johan Hovold222a12f2014-12-10 15:53:13 -0800124
Afzal Mohammedcab14582012-12-17 16:02:11 -0800125/* OMAP_RTC_KICKER values */
126#define KICK0_VALUE 0x83e70b13
127#define KICK1_VALUE 0x95a4f1e0
128
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700129struct omap_rtc;
130
Johan Hovold2153f942014-12-10 15:53:01 -0800131struct omap_rtc_device_type {
132 bool has_32kclk_en;
Johan Hovold2153f942014-12-10 15:53:01 -0800133 bool has_irqwakeen;
Johan Hovold222a12f2014-12-10 15:53:13 -0800134 bool has_pmic_mode;
Johan Hovold9291e342014-12-10 15:53:04 -0800135 bool has_power_up_reset;
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700136 void (*lock)(struct omap_rtc *rtc);
137 void (*unlock)(struct omap_rtc *rtc);
Johan Hovold2153f942014-12-10 15:53:01 -0800138};
Sekhar Noricd914bb2014-06-06 14:36:06 -0700139
Johan Hovold55ba9532014-12-10 15:52:55 -0800140struct omap_rtc {
141 struct rtc_device *rtc;
142 void __iomem *base;
Keerthy532409a2015-08-18 15:11:15 +0530143 struct clk *clk;
Johan Hovold55ba9532014-12-10 15:52:55 -0800144 int irq_alarm;
145 int irq_timer;
146 u8 interrupts_reg;
Johan Hovold222a12f2014-12-10 15:53:13 -0800147 bool is_pmic_controller;
Keerthy399cf0f2015-08-18 15:11:16 +0530148 bool has_ext_clk;
Johan Hovold2153f942014-12-10 15:53:01 -0800149 const struct omap_rtc_device_type *type;
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200150 struct pinctrl_dev *pctldev;
Johan Hovold55ba9532014-12-10 15:52:55 -0800151};
David Brownelldb68b182006-12-06 20:38:36 -0800152
Johan Hovold55ba9532014-12-10 15:52:55 -0800153static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
154{
155 return readb(rtc->base + reg);
156}
Afzal Mohammedcab14582012-12-17 16:02:11 -0800157
Johan Hovoldc253a892014-12-10 15:53:10 -0800158static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
159{
160 return readl(rtc->base + reg);
161}
162
Johan Hovold55ba9532014-12-10 15:52:55 -0800163static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
164{
165 writeb(val, rtc->base + reg);
166}
David Brownelldb68b182006-12-06 20:38:36 -0800167
Johan Hovold55ba9532014-12-10 15:52:55 -0800168static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
169{
170 writel(val, rtc->base + reg);
171}
David Brownelldb68b182006-12-06 20:38:36 -0800172
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700173static void am3352_rtc_unlock(struct omap_rtc *rtc)
174{
175 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
176 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
177}
178
179static void am3352_rtc_lock(struct omap_rtc *rtc)
180{
181 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
182 rtc_writel(rtc, OMAP_RTC_KICK1_REG, 0);
183}
184
185static void default_rtc_unlock(struct omap_rtc *rtc)
186{
187}
188
189static void default_rtc_lock(struct omap_rtc *rtc)
190{
191}
192
Johan Hovold10211ae2014-12-10 15:53:19 -0800193/*
194 * We rely on the rtc framework to handle locking (rtc->ops_lock),
David Brownelldb68b182006-12-06 20:38:36 -0800195 * so the only other requirement is that register accesses which
196 * require BUSY to be clear are made with IRQs locally disabled
197 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800198static void rtc_wait_not_busy(struct omap_rtc *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800199{
Johan Hovold10211ae2014-12-10 15:53:19 -0800200 int count;
201 u8 status;
David Brownelldb68b182006-12-06 20:38:36 -0800202
203 /* BUSY may stay active for 1/32768 second (~30 usec) */
204 for (count = 0; count < 50; count++) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800205 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold10211ae2014-12-10 15:53:19 -0800206 if (!(status & OMAP_RTC_STATUS_BUSY))
David Brownelldb68b182006-12-06 20:38:36 -0800207 break;
208 udelay(1);
209 }
210 /* now we have ~15 usec to read/write various registers */
211}
212
Johan Hovold55ba9532014-12-10 15:52:55 -0800213static irqreturn_t rtc_irq(int irq, void *dev_id)
David Brownelldb68b182006-12-06 20:38:36 -0800214{
Johan Hovold10211ae2014-12-10 15:53:19 -0800215 struct omap_rtc *rtc = dev_id;
216 unsigned long events = 0;
217 u8 irq_data;
David Brownelldb68b182006-12-06 20:38:36 -0800218
Johan Hovold55ba9532014-12-10 15:52:55 -0800219 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800220
221 /* alarm irq? */
222 if (irq_data & OMAP_RTC_STATUS_ALARM) {
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700223 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800224 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700225 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800226 events |= RTC_IRQF | RTC_AF;
227 }
228
229 /* 1/sec periodic/update irq? */
230 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
231 events |= RTC_IRQF | RTC_UF;
232
Johan Hovold55ba9532014-12-10 15:52:55 -0800233 rtc_update_irq(rtc->rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800234
235 return IRQ_HANDLED;
236}
237
John Stultz16380c12011-02-02 17:02:41 -0800238static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
239{
Johan Hovold55ba9532014-12-10 15:52:55 -0800240 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700241 u8 reg, irqwake_reg = 0;
John Stultz16380c12011-02-02 17:02:41 -0800242
243 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800244 rtc_wait_not_busy(rtc);
245 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800246 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800247 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700248
249 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800250 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700251 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
252 } else {
John Stultz16380c12011-02-02 17:02:41 -0800253 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700254 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
255 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800256 rtc_wait_not_busy(rtc);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700257 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800258 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800259 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800260 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700261 rtc->type->lock(rtc);
John Stultz16380c12011-02-02 17:02:41 -0800262 local_irq_enable();
263
264 return 0;
265}
266
David Brownelldb68b182006-12-06 20:38:36 -0800267/* this hardware doesn't support "don't care" alarm fields */
268static int tm2bcd(struct rtc_time *tm)
269{
270 if (rtc_valid_tm(tm) != 0)
271 return -EINVAL;
272
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700273 tm->tm_sec = bin2bcd(tm->tm_sec);
274 tm->tm_min = bin2bcd(tm->tm_min);
275 tm->tm_hour = bin2bcd(tm->tm_hour);
276 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800277
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700278 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800279
280 /* epoch == 1900 */
281 if (tm->tm_year < 100 || tm->tm_year > 199)
282 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700283 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800284
285 return 0;
286}
287
288static void bcd2tm(struct rtc_time *tm)
289{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700290 tm->tm_sec = bcd2bin(tm->tm_sec);
291 tm->tm_min = bcd2bin(tm->tm_min);
292 tm->tm_hour = bcd2bin(tm->tm_hour);
293 tm->tm_mday = bcd2bin(tm->tm_mday);
294 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800295 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700296 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800297}
298
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800299static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
300{
301 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
302 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
303 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
304 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
305 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
306 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
307}
David Brownelldb68b182006-12-06 20:38:36 -0800308
309static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
310{
Johan Hovold55ba9532014-12-10 15:52:55 -0800311 struct omap_rtc *rtc = dev_get_drvdata(dev);
312
David Brownelldb68b182006-12-06 20:38:36 -0800313 /* we don't report wday/yday/isdst ... */
314 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800315 rtc_wait_not_busy(rtc);
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800316 omap_rtc_read_time_raw(rtc, tm);
David Brownelldb68b182006-12-06 20:38:36 -0800317 local_irq_enable();
318
319 bcd2tm(tm);
Johan Hovold10211ae2014-12-10 15:53:19 -0800320
David Brownelldb68b182006-12-06 20:38:36 -0800321 return 0;
322}
323
324static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
325{
Johan Hovold55ba9532014-12-10 15:52:55 -0800326 struct omap_rtc *rtc = dev_get_drvdata(dev);
327
David Brownelldb68b182006-12-06 20:38:36 -0800328 if (tm2bcd(tm) < 0)
329 return -EINVAL;
Johan Hovold10211ae2014-12-10 15:53:19 -0800330
David Brownelldb68b182006-12-06 20:38:36 -0800331 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800332 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800333
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700334 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800335 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
336 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
337 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
338 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
339 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
340 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700341 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800342
343 local_irq_enable();
344
345 return 0;
346}
347
348static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
349{
Johan Hovold55ba9532014-12-10 15:52:55 -0800350 struct omap_rtc *rtc = dev_get_drvdata(dev);
Johan Hovold10211ae2014-12-10 15:53:19 -0800351 u8 interrupts;
David Brownelldb68b182006-12-06 20:38:36 -0800352
Johan Hovold55ba9532014-12-10 15:52:55 -0800353 local_irq_disable();
354 rtc_wait_not_busy(rtc);
355
356 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
357 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
358 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
359 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
360 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
361 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800362
363 local_irq_enable();
364
365 bcd2tm(&alm->time);
Johan Hovold10211ae2014-12-10 15:53:19 -0800366
367 interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
368 alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800369
370 return 0;
371}
372
373static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
374{
Johan Hovold55ba9532014-12-10 15:52:55 -0800375 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700376 u8 reg, irqwake_reg = 0;
David Brownelldb68b182006-12-06 20:38:36 -0800377
David Brownelldb68b182006-12-06 20:38:36 -0800378 if (tm2bcd(&alm->time) < 0)
379 return -EINVAL;
380
381 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800382 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800383
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700384 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800385 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
386 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
387 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
388 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
389 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
390 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800391
Johan Hovold55ba9532014-12-10 15:52:55 -0800392 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800393 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800394 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700395
396 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800397 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700398 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
399 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800400 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700401 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
402 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800403 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800404 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800405 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700406 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800407
408 local_irq_enable();
409
410 return 0;
411}
412
Johan Hovold222a12f2014-12-10 15:53:13 -0800413static struct omap_rtc *omap_rtc_power_off_rtc;
414
415/*
416 * omap_rtc_poweroff: RTC-controlled power off
417 *
418 * The RTC can be used to control an external PMIC via the pmic_power_en pin,
419 * which can be configured to transition to OFF on ALARM2 events.
420 *
421 * Notes:
422 * The two-second alarm offset is the shortest offset possible as the alarm
423 * registers must be set before the next timer update and the offset
424 * calculation is too heavy for everything to be done within a single access
425 * period (~15 us).
426 *
427 * Called with local interrupts disabled.
428 */
429static void omap_rtc_power_off(void)
430{
431 struct omap_rtc *rtc = omap_rtc_power_off_rtc;
432 struct rtc_time tm;
433 unsigned long now;
434 u32 val;
435
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700436 rtc->type->unlock(rtc);
Johan Hovold222a12f2014-12-10 15:53:13 -0800437 /* enable pmic_power_en control */
438 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
439 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
440
441 /* set alarm two seconds from now */
442 omap_rtc_read_time_raw(rtc, &tm);
443 bcd2tm(&tm);
444 rtc_tm_to_time(&tm, &now);
445 rtc_time_to_tm(now + 2, &tm);
446
447 if (tm2bcd(&tm) < 0) {
448 dev_err(&rtc->rtc->dev, "power off failed\n");
449 return;
450 }
451
452 rtc_wait_not_busy(rtc);
453
454 rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
455 rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
456 rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
457 rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
458 rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
459 rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
460
461 /*
462 * enable ALARM2 interrupt
463 *
464 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
465 */
466 val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
467 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
468 val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700469 rtc->type->lock(rtc);
Johan Hovold222a12f2014-12-10 15:53:13 -0800470
471 /*
472 * Wait for alarm to trigger (within two seconds) and external PMIC to
473 * power off the system. Add a 500 ms margin for external latencies
474 * (e.g. debounce circuits).
475 */
476 mdelay(2500);
477}
478
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200479static const struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800480 .read_time = omap_rtc_read_time,
481 .set_time = omap_rtc_set_time,
482 .read_alarm = omap_rtc_read_alarm,
483 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800484 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800485};
486
Johan Hovold2153f942014-12-10 15:53:01 -0800487static const struct omap_rtc_device_type omap_rtc_default_type = {
Johan Hovold9291e342014-12-10 15:53:04 -0800488 .has_power_up_reset = true,
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700489 .lock = default_rtc_lock,
490 .unlock = default_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800491};
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800492
Johan Hovold2153f942014-12-10 15:53:01 -0800493static const struct omap_rtc_device_type omap_rtc_am3352_type = {
494 .has_32kclk_en = true,
Johan Hovold2153f942014-12-10 15:53:01 -0800495 .has_irqwakeen = true,
Johan Hovold222a12f2014-12-10 15:53:13 -0800496 .has_pmic_mode = true,
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700497 .lock = am3352_rtc_lock,
498 .unlock = am3352_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800499};
500
501static const struct omap_rtc_device_type omap_rtc_da830_type = {
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700502 .lock = am3352_rtc_lock,
503 .unlock = am3352_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800504};
505
506static const struct platform_device_id omap_rtc_id_table[] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800507 {
Johan Hovolda430ca22014-12-10 15:52:58 -0800508 .name = "omap_rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800509 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
510 }, {
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700511 .name = "am3352-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800512 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
513 }, {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800514 .name = "da830-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800515 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
516 }, {
517 /* sentinel */
518 }
Afzal Mohammedcab14582012-12-17 16:02:11 -0800519};
Johan Hovold2153f942014-12-10 15:53:01 -0800520MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800521
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800522static const struct of_device_id omap_rtc_of_match[] = {
Johan Hovold2153f942014-12-10 15:53:01 -0800523 {
524 .compatible = "ti,am3352-rtc",
525 .data = &omap_rtc_am3352_type,
526 }, {
527 .compatible = "ti,da830-rtc",
528 .data = &omap_rtc_da830_type,
529 }, {
530 /* sentinel */
531 }
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800532};
533MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
534
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200535static const struct pinctrl_pin_desc rtc_pins_desc[] = {
536 PINCTRL_PIN(0, "ext_wakeup0"),
537 PINCTRL_PIN(1, "ext_wakeup1"),
538 PINCTRL_PIN(2, "ext_wakeup2"),
539 PINCTRL_PIN(3, "ext_wakeup3"),
540};
541
542static int rtc_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
543{
544 return 0;
545}
546
547static const char *rtc_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
548 unsigned int group)
549{
550 return NULL;
551}
552
553static const struct pinctrl_ops rtc_pinctrl_ops = {
554 .get_groups_count = rtc_pinctrl_get_groups_count,
555 .get_group_name = rtc_pinctrl_get_group_name,
556 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
557 .dt_free_map = pinconf_generic_dt_free_map,
558};
559
560enum rtc_pin_config_param {
561 PIN_CONFIG_ACTIVE_HIGH = PIN_CONFIG_END + 1,
562};
563
564static const struct pinconf_generic_params rtc_params[] = {
565 {"ti,active-high", PIN_CONFIG_ACTIVE_HIGH, 0},
566};
567
568#ifdef CONFIG_DEBUG_FS
569static const struct pin_config_item rtc_conf_items[ARRAY_SIZE(rtc_params)] = {
570 PCONFDUMP(PIN_CONFIG_ACTIVE_HIGH, "input active high", NULL, false),
571};
572#endif
573
574static int rtc_pinconf_get(struct pinctrl_dev *pctldev,
575 unsigned int pin, unsigned long *config)
576{
577 struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
578 unsigned int param = pinconf_to_config_param(*config);
579 u32 val;
580 u16 arg = 0;
581
582 rtc->type->unlock(rtc);
583 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
584 rtc->type->lock(rtc);
585
586 switch (param) {
587 case PIN_CONFIG_INPUT_ENABLE:
588 if (!(val & OMAP_RTC_PMIC_EXT_WKUP_EN(pin)))
589 return -EINVAL;
590 break;
591 case PIN_CONFIG_ACTIVE_HIGH:
592 if (val & OMAP_RTC_PMIC_EXT_WKUP_POL(pin))
593 return -EINVAL;
594 break;
595 default:
596 return -ENOTSUPP;
597 };
598
599 *config = pinconf_to_config_packed(param, arg);
600
601 return 0;
602}
603
604static int rtc_pinconf_set(struct pinctrl_dev *pctldev,
605 unsigned int pin, unsigned long *configs,
606 unsigned int num_configs)
607{
608 struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
609 u32 val;
610 unsigned int param;
611 u16 param_val;
612 int i;
613
614 rtc->type->unlock(rtc);
615 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
616 rtc->type->lock(rtc);
617
618 /* active low by default */
619 val |= OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
620
621 for (i = 0; i < num_configs; i++) {
622 param = pinconf_to_config_param(configs[i]);
623 param_val = pinconf_to_config_argument(configs[i]);
624
625 switch (param) {
626 case PIN_CONFIG_INPUT_ENABLE:
627 if (param_val)
628 val |= OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
629 else
630 val &= ~OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
631 break;
632 case PIN_CONFIG_ACTIVE_HIGH:
633 val &= ~OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
634 break;
635 default:
636 dev_err(&rtc->rtc->dev, "Property %u not supported\n",
637 param);
638 return -ENOTSUPP;
639 }
640 }
641
642 rtc->type->unlock(rtc);
643 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
644 rtc->type->lock(rtc);
645
646 return 0;
647}
648
649static const struct pinconf_ops rtc_pinconf_ops = {
650 .is_generic = true,
651 .pin_config_get = rtc_pinconf_get,
652 .pin_config_set = rtc_pinconf_set,
653};
654
655static struct pinctrl_desc rtc_pinctrl_desc = {
656 .pins = rtc_pins_desc,
657 .npins = ARRAY_SIZE(rtc_pins_desc),
658 .pctlops = &rtc_pinctrl_ops,
659 .confops = &rtc_pinconf_ops,
660 .custom_params = rtc_params,
661 .num_custom_params = ARRAY_SIZE(rtc_params),
662#ifdef CONFIG_DEBUG_FS
663 .custom_conf_items = rtc_conf_items,
664#endif
665 .owner = THIS_MODULE,
666};
667
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700668static int omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800669{
Johan Hovold10211ae2014-12-10 15:53:19 -0800670 struct omap_rtc *rtc;
671 struct resource *res;
672 u8 reg, mask, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800673 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800674 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800675 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800676
Johan Hovold55ba9532014-12-10 15:52:55 -0800677 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
678 if (!rtc)
679 return -ENOMEM;
680
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800681 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
Johan Hovold2153f942014-12-10 15:53:01 -0800682 if (of_id) {
683 rtc->type = of_id->data;
Johan Hovold222a12f2014-12-10 15:53:13 -0800684 rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
685 of_property_read_bool(pdev->dev.of_node,
Johan Hovold094d3ee2014-12-10 15:54:14 -0800686 "system-power-controller");
Johan Hovold2153f942014-12-10 15:53:01 -0800687 } else {
688 id_entry = platform_get_device_id(pdev);
689 rtc->type = (void *)id_entry->driver_data;
Sekhar Nori337b6002014-06-06 14:36:04 -0700690 }
691
Johan Hovold55ba9532014-12-10 15:52:55 -0800692 rtc->irq_timer = platform_get_irq(pdev, 0);
693 if (rtc->irq_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800694 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800695
Johan Hovold55ba9532014-12-10 15:52:55 -0800696 rtc->irq_alarm = platform_get_irq(pdev, 1);
697 if (rtc->irq_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800698 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800699
Keerthy399cf0f2015-08-18 15:11:16 +0530700 rtc->clk = devm_clk_get(&pdev->dev, "ext-clk");
701 if (!IS_ERR(rtc->clk))
702 rtc->has_ext_clk = true;
703 else
704 rtc->clk = devm_clk_get(&pdev->dev, "int-clk");
Keerthy532409a2015-08-18 15:11:15 +0530705
706 if (!IS_ERR(rtc->clk))
707 clk_prepare_enable(rtc->clk);
708
David Brownelldb68b182006-12-06 20:38:36 -0800709 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Johan Hovold55ba9532014-12-10 15:52:55 -0800710 rtc->base = devm_ioremap_resource(&pdev->dev, res);
711 if (IS_ERR(rtc->base))
712 return PTR_ERR(rtc->base);
713
714 platform_set_drvdata(pdev, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800715
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800716 /* Enable the clock/module so that we can access the registers */
717 pm_runtime_enable(&pdev->dev);
718 pm_runtime_get_sync(&pdev->dev);
719
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700720 rtc->type->unlock(rtc);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800721
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800722 /*
723 * disable interrupts
724 *
725 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800726 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800727 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800728
Sekhar Noricd914bb2014-06-06 14:36:06 -0700729 /* enable RTC functional clock */
Johan Hovold2153f942014-12-10 15:53:01 -0800730 if (rtc->type->has_32kclk_en) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800731 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
732 rtc_writel(rtc, OMAP_RTC_OSC_REG,
733 reg | OMAP_RTC_OSC_32KCLK_EN);
Johan Hovold44c63a52014-12-10 15:52:30 -0800734 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700735
David Brownelldb68b182006-12-06 20:38:36 -0800736 /* clear old status */
Johan Hovold55ba9532014-12-10 15:52:55 -0800737 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold9291e342014-12-10 15:53:04 -0800738
739 mask = OMAP_RTC_STATUS_ALARM;
740
Johan Hovold222a12f2014-12-10 15:53:13 -0800741 if (rtc->type->has_pmic_mode)
742 mask |= OMAP_RTC_STATUS_ALARM2;
743
Johan Hovold9291e342014-12-10 15:53:04 -0800744 if (rtc->type->has_power_up_reset) {
745 mask |= OMAP_RTC_STATUS_POWER_UP;
746 if (reg & OMAP_RTC_STATUS_POWER_UP)
747 dev_info(&pdev->dev, "RTC power up reset detected\n");
David Brownelldb68b182006-12-06 20:38:36 -0800748 }
Johan Hovold9291e342014-12-10 15:53:04 -0800749
750 if (reg & mask)
751 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
David Brownelldb68b182006-12-06 20:38:36 -0800752
David Brownelldb68b182006-12-06 20:38:36 -0800753 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
Johan Hovold55ba9532014-12-10 15:52:55 -0800754 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
Johan Hovold10211ae2014-12-10 15:53:19 -0800755 if (reg & OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800756 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800757
758 /* force to 24 hour mode */
Johan Hovold10211ae2014-12-10 15:53:19 -0800759 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800760 new_ctrl |= OMAP_RTC_CTRL_STOP;
761
Johan Hovold10211ae2014-12-10 15:53:19 -0800762 /*
763 * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
David Brownelldb68b182006-12-06 20:38:36 -0800764 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700765 * - Device wake-up capability setting should come through chip
766 * init logic. OMAP1 boards should initialize the "wakeup capable"
767 * flag in the platform device if the board is wired right for
768 * being woken up by RTC alarm. For OMAP-L138, this capability
769 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800770 *
771 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
772 * rather than nPWRON_RESET, should forcibly enable split
773 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
774 * is write-only, and always reads as zero...)
775 */
David Brownelldb68b182006-12-06 20:38:36 -0800776
Johan Hovold10211ae2014-12-10 15:53:19 -0800777 if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800778 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800779
780 if (reg != new_ctrl)
Johan Hovold55ba9532014-12-10 15:52:55 -0800781 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
David Brownelldb68b182006-12-06 20:38:36 -0800782
Keerthy399cf0f2015-08-18 15:11:16 +0530783 /*
784 * If we have the external clock then switch to it so we can keep
785 * ticking across suspend.
786 */
787 if (rtc->has_ext_clk) {
788 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
789 rtc_write(rtc, OMAP_RTC_OSC_REG,
790 reg | OMAP_RTC_OSC_SEL_32KCLK_SRC);
791 }
792
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700793 rtc->type->lock(rtc);
794
Johan Hovold4390ce02014-12-10 15:52:43 -0800795 device_init_wakeup(&pdev->dev, true);
796
Johan Hovold55ba9532014-12-10 15:52:55 -0800797 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Johan Hovold4390ce02014-12-10 15:52:43 -0800798 &omap_rtc_ops, THIS_MODULE);
Johan Hovold55ba9532014-12-10 15:52:55 -0800799 if (IS_ERR(rtc->rtc)) {
800 ret = PTR_ERR(rtc->rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800801 goto err;
802 }
Johan Hovold4390ce02014-12-10 15:52:43 -0800803
804 /* handle periodic and alarm irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800805 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
806 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800807 if (ret)
808 goto err;
809
Johan Hovold55ba9532014-12-10 15:52:55 -0800810 if (rtc->irq_timer != rtc->irq_alarm) {
811 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
812 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800813 if (ret)
814 goto err;
815 }
816
Johan Hovold222a12f2014-12-10 15:53:13 -0800817 if (rtc->is_pmic_controller) {
818 if (!pm_power_off) {
819 omap_rtc_power_off_rtc = rtc;
820 pm_power_off = omap_rtc_power_off;
821 }
822 }
823
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200824 /* Support ext_wakeup pinconf */
825 rtc_pinctrl_desc.name = dev_name(&pdev->dev);
826
827 rtc->pctldev = pinctrl_register(&rtc_pinctrl_desc, &pdev->dev, rtc);
828 if (IS_ERR(rtc->pctldev)) {
829 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
830 return PTR_ERR(rtc->pctldev);
831 }
832
David Brownelldb68b182006-12-06 20:38:36 -0800833 return 0;
834
Johan Hovold437b37a2014-12-10 15:52:40 -0800835err:
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800836 device_init_wakeup(&pdev->dev, false);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700837 rtc->type->lock(rtc);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800838 pm_runtime_put_sync(&pdev->dev);
839 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800840
841 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800842}
843
David Brownell71fc8222008-07-23 21:30:38 -0700844static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800845{
Johan Hovold55ba9532014-12-10 15:52:55 -0800846 struct omap_rtc *rtc = platform_get_drvdata(pdev);
Keerthy399cf0f2015-08-18 15:11:16 +0530847 u8 reg;
David Brownelldb68b182006-12-06 20:38:36 -0800848
Johan Hovold222a12f2014-12-10 15:53:13 -0800849 if (pm_power_off == omap_rtc_power_off &&
850 omap_rtc_power_off_rtc == rtc) {
851 pm_power_off = NULL;
852 omap_rtc_power_off_rtc = NULL;
853 }
854
David Brownelldb68b182006-12-06 20:38:36 -0800855 device_init_wakeup(&pdev->dev, 0);
856
Keerthy532409a2015-08-18 15:11:15 +0530857 if (!IS_ERR(rtc->clk))
858 clk_disable_unprepare(rtc->clk);
859
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700860 rtc->type->unlock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800861 /* leave rtc running, but disable irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800862 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800863
Keerthy399cf0f2015-08-18 15:11:16 +0530864 if (rtc->has_ext_clk) {
865 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
866 reg &= ~OMAP_RTC_OSC_SEL_32KCLK_SRC;
867 rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
868 }
869
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700870 rtc->type->lock(rtc);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800871
872 /* Disable the clock/module */
873 pm_runtime_put_sync(&pdev->dev);
874 pm_runtime_disable(&pdev->dev);
875
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200876 /* Remove ext_wakeup pinconf */
877 pinctrl_unregister(rtc->pctldev);
878
David Brownelldb68b182006-12-06 20:38:36 -0800879 return 0;
880}
881
Jingoo Han04ebc352013-04-29 16:21:01 -0700882#ifdef CONFIG_PM_SLEEP
Jingoo Han04ebc352013-04-29 16:21:01 -0700883static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800884{
Johan Hovold55ba9532014-12-10 15:52:55 -0800885 struct omap_rtc *rtc = dev_get_drvdata(dev);
886
887 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800888
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700889 rtc->type->unlock(rtc);
Johan Hovold10211ae2014-12-10 15:53:19 -0800890 /*
891 * FIXME: the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700892 * source on some platforms, and in fact this enable() call is just
893 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800894 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700895 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800896 enable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700897 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800898 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700899 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800900
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800901 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700902 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800903
David Brownelldb68b182006-12-06 20:38:36 -0800904 return 0;
905}
906
Jingoo Han04ebc352013-04-29 16:21:01 -0700907static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800908{
Johan Hovold55ba9532014-12-10 15:52:55 -0800909 struct omap_rtc *rtc = dev_get_drvdata(dev);
910
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800911 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700912 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800913
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700914 rtc->type->unlock(rtc);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700915 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800916 disable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700917 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800918 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700919 rtc->type->lock(rtc);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700920
David Brownelldb68b182006-12-06 20:38:36 -0800921 return 0;
922}
David Brownelldb68b182006-12-06 20:38:36 -0800923#endif
924
Jingoo Han04ebc352013-04-29 16:21:01 -0700925static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
926
David Brownelldb68b182006-12-06 20:38:36 -0800927static void omap_rtc_shutdown(struct platform_device *pdev)
928{
Johan Hovold55ba9532014-12-10 15:52:55 -0800929 struct omap_rtc *rtc = platform_get_drvdata(pdev);
Johan Hovold8ad5c722014-12-10 15:53:16 -0800930 u8 mask;
Johan Hovold55ba9532014-12-10 15:52:55 -0800931
Johan Hovold8ad5c722014-12-10 15:53:16 -0800932 /*
933 * Keep the ALARM interrupt enabled to allow the system to power up on
934 * alarm events.
935 */
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700936 rtc->type->unlock(rtc);
Johan Hovold8ad5c722014-12-10 15:53:16 -0800937 mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
938 mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
939 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700940 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800941}
942
David Brownelldb68b182006-12-06 20:38:36 -0800943static struct platform_driver omap_rtc_driver = {
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700944 .probe = omap_rtc_probe,
David Brownell71fc8222008-07-23 21:30:38 -0700945 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800946 .shutdown = omap_rtc_shutdown,
947 .driver = {
Johan Hovolda430ca22014-12-10 15:52:58 -0800948 .name = "omap_rtc",
Jingoo Han04ebc352013-04-29 16:21:01 -0700949 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800950 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800951 },
Johan Hovold2153f942014-12-10 15:53:01 -0800952 .id_table = omap_rtc_id_table,
David Brownelldb68b182006-12-06 20:38:36 -0800953};
954
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700955module_platform_driver(omap_rtc_driver);
David Brownelldb68b182006-12-06 20:38:36 -0800956
Johan Hovolda430ca22014-12-10 15:52:58 -0800957MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800958MODULE_AUTHOR("George G. Davis (and others)");
959MODULE_LICENSE("GPL");