blob: c20fc7937dfa817bba8d9d24f589119504ec1e1e [file] [log] [blame]
Alexandre Belloni58c92992019-03-18 14:22:55 +01001// SPDX-License-Identifier: GPL-2.0+
David Brownelldb68b182006-12-06 20:38:36 -08002/*
Johan Hovold10211ae2014-12-10 15:53:19 -08003 * TI OMAP Real Time Clock interface for Linux
David Brownelldb68b182006-12-06 20:38:36 -08004 *
5 * Copyright (C) 2003 MontaVista Software, Inc.
6 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
7 *
8 * Copyright (C) 2006 David Brownell (new RTC framework)
Johan Hovold01251382014-12-10 15:53:22 -08009 * Copyright (C) 2014 Johan Hovold <johan@kernel.org>
David Brownelldb68b182006-12-06 20:38:36 -080010 */
11
David Brownelldb68b182006-12-06 20:38:36 -080012#include <linux/bcd.h>
Marcin Niestroj97ea1902016-09-16 11:26:28 +020013#include <linux/clk.h>
14#include <linux/delay.h>
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/ioport.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
Afzal Mohammed9e0344d2012-12-17 16:02:15 -080020#include <linux/of.h>
21#include <linux/of_device.h>
Marcin Niestroj97ea1902016-09-16 11:26:28 +020022#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinconf.h>
24#include <linux/pinctrl/pinconf-generic.h>
25#include <linux/platform_device.h>
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -080026#include <linux/pm_runtime.h>
Marcin Niestroj97ea1902016-09-16 11:26:28 +020027#include <linux/rtc.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
Alexandre Bellonib6ee15e2017-10-31 17:27:31 +010068#define OMAP_RTC_SCRATCH0_REG 0x60
69#define OMAP_RTC_SCRATCH1_REG 0x64
70#define OMAP_RTC_SCRATCH2_REG 0x68
71
Afzal Mohammedcab14582012-12-17 16:02:11 -080072#define OMAP_RTC_KICK0_REG 0x6c
73#define OMAP_RTC_KICK1_REG 0x70
74
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070075#define OMAP_RTC_IRQWAKEEN 0x7c
76
Johan Hovold222a12f2014-12-10 15:53:13 -080077#define OMAP_RTC_ALARM2_SECONDS_REG 0x80
78#define OMAP_RTC_ALARM2_MINUTES_REG 0x84
79#define OMAP_RTC_ALARM2_HOURS_REG 0x88
80#define OMAP_RTC_ALARM2_DAYS_REG 0x8c
81#define OMAP_RTC_ALARM2_MONTHS_REG 0x90
82#define OMAP_RTC_ALARM2_YEARS_REG 0x94
83
84#define OMAP_RTC_PMIC_REG 0x98
85
David Brownelldb68b182006-12-06 20:38:36 -080086/* OMAP_RTC_CTRL_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070087#define OMAP_RTC_CTRL_SPLIT BIT(7)
88#define OMAP_RTC_CTRL_DISABLE BIT(6)
89#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
90#define OMAP_RTC_CTRL_TEST BIT(4)
91#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
92#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
93#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
94#define OMAP_RTC_CTRL_STOP BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -080095
96/* OMAP_RTC_STATUS_REG bit fields: */
Sekhar Nori92adb962014-06-06 14:36:05 -070097#define OMAP_RTC_STATUS_POWER_UP BIT(7)
Johan Hovold222a12f2014-12-10 15:53:13 -080098#define OMAP_RTC_STATUS_ALARM2 BIT(7)
Sekhar Nori92adb962014-06-06 14:36:05 -070099#define OMAP_RTC_STATUS_ALARM BIT(6)
100#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
101#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
102#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
103#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
104#define OMAP_RTC_STATUS_RUN BIT(1)
105#define OMAP_RTC_STATUS_BUSY BIT(0)
David Brownelldb68b182006-12-06 20:38:36 -0800106
107/* OMAP_RTC_INTERRUPTS_REG bit fields: */
Johan Hovold222a12f2014-12-10 15:53:13 -0800108#define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
Sekhar Nori92adb962014-06-06 14:36:05 -0700109#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
110#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
David Brownelldb68b182006-12-06 20:38:36 -0800111
Sekhar Noricd914bb2014-06-06 14:36:06 -0700112/* OMAP_RTC_OSC_REG bit fields: */
113#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
Keerthy399cf0f2015-08-18 15:11:16 +0530114#define OMAP_RTC_OSC_SEL_32KCLK_SRC BIT(3)
Lokesh Vutla39849032016-10-27 11:27:25 +0530115#define OMAP_RTC_OSC_OSC32K_GZ_DISABLE BIT(4)
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;
Tero Kristoefce21f2016-10-27 11:27:26 +0530149 bool is_suspending;
Johan Hovold2153f942014-12-10 15:53:01 -0800150 const struct omap_rtc_device_type *type;
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200151 struct pinctrl_dev *pctldev;
Johan Hovold55ba9532014-12-10 15:52:55 -0800152};
David Brownelldb68b182006-12-06 20:38:36 -0800153
Johan Hovold55ba9532014-12-10 15:52:55 -0800154static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
155{
156 return readb(rtc->base + reg);
157}
Afzal Mohammedcab14582012-12-17 16:02:11 -0800158
Johan Hovoldc253a892014-12-10 15:53:10 -0800159static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
160{
161 return readl(rtc->base + reg);
162}
163
Johan Hovold55ba9532014-12-10 15:52:55 -0800164static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
165{
166 writeb(val, rtc->base + reg);
167}
David Brownelldb68b182006-12-06 20:38:36 -0800168
Johan Hovold55ba9532014-12-10 15:52:55 -0800169static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
170{
171 writel(val, rtc->base + reg);
172}
David Brownelldb68b182006-12-06 20:38:36 -0800173
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700174static void am3352_rtc_unlock(struct omap_rtc *rtc)
175{
176 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
177 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
178}
179
180static void am3352_rtc_lock(struct omap_rtc *rtc)
181{
182 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
183 rtc_writel(rtc, OMAP_RTC_KICK1_REG, 0);
184}
185
186static void default_rtc_unlock(struct omap_rtc *rtc)
187{
188}
189
190static void default_rtc_lock(struct omap_rtc *rtc)
191{
192}
193
Johan Hovold10211ae2014-12-10 15:53:19 -0800194/*
195 * We rely on the rtc framework to handle locking (rtc->ops_lock),
David Brownelldb68b182006-12-06 20:38:36 -0800196 * so the only other requirement is that register accesses which
197 * require BUSY to be clear are made with IRQs locally disabled
198 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800199static void rtc_wait_not_busy(struct omap_rtc *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800200{
Johan Hovold10211ae2014-12-10 15:53:19 -0800201 int count;
202 u8 status;
David Brownelldb68b182006-12-06 20:38:36 -0800203
204 /* BUSY may stay active for 1/32768 second (~30 usec) */
205 for (count = 0; count < 50; count++) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800206 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold10211ae2014-12-10 15:53:19 -0800207 if (!(status & OMAP_RTC_STATUS_BUSY))
David Brownelldb68b182006-12-06 20:38:36 -0800208 break;
209 udelay(1);
210 }
211 /* now we have ~15 usec to read/write various registers */
212}
213
Johan Hovold55ba9532014-12-10 15:52:55 -0800214static irqreturn_t rtc_irq(int irq, void *dev_id)
David Brownelldb68b182006-12-06 20:38:36 -0800215{
Johan Hovold10211ae2014-12-10 15:53:19 -0800216 struct omap_rtc *rtc = dev_id;
217 unsigned long events = 0;
218 u8 irq_data;
David Brownelldb68b182006-12-06 20:38:36 -0800219
Johan Hovold55ba9532014-12-10 15:52:55 -0800220 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800221
222 /* alarm irq? */
223 if (irq_data & OMAP_RTC_STATUS_ALARM) {
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700224 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800225 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700226 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800227 events |= RTC_IRQF | RTC_AF;
228 }
229
230 /* 1/sec periodic/update irq? */
231 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
232 events |= RTC_IRQF | RTC_UF;
233
Johan Hovold55ba9532014-12-10 15:52:55 -0800234 rtc_update_irq(rtc->rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800235
236 return IRQ_HANDLED;
237}
238
John Stultz16380c12011-02-02 17:02:41 -0800239static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
240{
Johan Hovold55ba9532014-12-10 15:52:55 -0800241 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700242 u8 reg, irqwake_reg = 0;
John Stultz16380c12011-02-02 17:02:41 -0800243
244 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800245 rtc_wait_not_busy(rtc);
246 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800247 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800248 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700249
250 if (enabled) {
John Stultz16380c12011-02-02 17:02:41 -0800251 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700252 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
253 } else {
John Stultz16380c12011-02-02 17:02:41 -0800254 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700255 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
256 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800257 rtc_wait_not_busy(rtc);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700258 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800259 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800260 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800261 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700262 rtc->type->lock(rtc);
John Stultz16380c12011-02-02 17:02:41 -0800263 local_irq_enable();
264
265 return 0;
266}
267
David Brownelldb68b182006-12-06 20:38:36 -0800268/* this hardware doesn't support "don't care" alarm fields */
Alexandre Belloni35118b72019-03-18 13:44:38 +0100269static void tm2bcd(struct rtc_time *tm)
David Brownelldb68b182006-12-06 20:38:36 -0800270{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700271 tm->tm_sec = bin2bcd(tm->tm_sec);
272 tm->tm_min = bin2bcd(tm->tm_min);
273 tm->tm_hour = bin2bcd(tm->tm_hour);
274 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800275
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700276 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700277 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800278}
279
280static void bcd2tm(struct rtc_time *tm)
281{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700282 tm->tm_sec = bcd2bin(tm->tm_sec);
283 tm->tm_min = bcd2bin(tm->tm_min);
284 tm->tm_hour = bcd2bin(tm->tm_hour);
285 tm->tm_mday = bcd2bin(tm->tm_mday);
286 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800287 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700288 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800289}
290
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800291static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
292{
293 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
294 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
295 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
296 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
297 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
298 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
299}
David Brownelldb68b182006-12-06 20:38:36 -0800300
301static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
302{
Johan Hovold55ba9532014-12-10 15:52:55 -0800303 struct omap_rtc *rtc = dev_get_drvdata(dev);
304
David Brownelldb68b182006-12-06 20:38:36 -0800305 /* we don't report wday/yday/isdst ... */
306 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800307 rtc_wait_not_busy(rtc);
Johan Hovoldcbbe3262014-12-10 15:53:07 -0800308 omap_rtc_read_time_raw(rtc, tm);
David Brownelldb68b182006-12-06 20:38:36 -0800309 local_irq_enable();
310
311 bcd2tm(tm);
Johan Hovold10211ae2014-12-10 15:53:19 -0800312
David Brownelldb68b182006-12-06 20:38:36 -0800313 return 0;
314}
315
316static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
317{
Johan Hovold55ba9532014-12-10 15:52:55 -0800318 struct omap_rtc *rtc = dev_get_drvdata(dev);
319
Alexandre Belloni35118b72019-03-18 13:44:38 +0100320 tm2bcd(tm);
Johan Hovold10211ae2014-12-10 15:53:19 -0800321
David Brownelldb68b182006-12-06 20:38:36 -0800322 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800323 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800324
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700325 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800326 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
327 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
328 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
329 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
330 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
331 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700332 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800333
334 local_irq_enable();
335
336 return 0;
337}
338
339static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
340{
Johan Hovold55ba9532014-12-10 15:52:55 -0800341 struct omap_rtc *rtc = dev_get_drvdata(dev);
Johan Hovold10211ae2014-12-10 15:53:19 -0800342 u8 interrupts;
David Brownelldb68b182006-12-06 20:38:36 -0800343
Johan Hovold55ba9532014-12-10 15:52:55 -0800344 local_irq_disable();
345 rtc_wait_not_busy(rtc);
346
347 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
348 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
349 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
350 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
351 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
352 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800353
354 local_irq_enable();
355
356 bcd2tm(&alm->time);
Johan Hovold10211ae2014-12-10 15:53:19 -0800357
358 interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
359 alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800360
361 return 0;
362}
363
364static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
365{
Johan Hovold55ba9532014-12-10 15:52:55 -0800366 struct omap_rtc *rtc = dev_get_drvdata(dev);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700367 u8 reg, irqwake_reg = 0;
David Brownelldb68b182006-12-06 20:38:36 -0800368
Alexandre Belloni35118b72019-03-18 13:44:38 +0100369 tm2bcd(&alm->time);
David Brownelldb68b182006-12-06 20:38:36 -0800370
371 local_irq_disable();
Johan Hovold55ba9532014-12-10 15:52:55 -0800372 rtc_wait_not_busy(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800373
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700374 rtc->type->unlock(rtc);
Johan Hovold55ba9532014-12-10 15:52:55 -0800375 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
376 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
377 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
378 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
379 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
380 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
David Brownelldb68b182006-12-06 20:38:36 -0800381
Johan Hovold55ba9532014-12-10 15:52:55 -0800382 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
Johan Hovold2153f942014-12-10 15:53:01 -0800383 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800384 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700385
386 if (alm->enabled) {
David Brownelldb68b182006-12-06 20:38:36 -0800387 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700388 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
389 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800390 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700391 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
392 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800393 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
Johan Hovold2153f942014-12-10 15:53:01 -0800394 if (rtc->type->has_irqwakeen)
Johan Hovold55ba9532014-12-10 15:52:55 -0800395 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700396 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800397
398 local_irq_enable();
399
400 return 0;
401}
402
Johan Hovold222a12f2014-12-10 15:53:13 -0800403static struct omap_rtc *omap_rtc_power_off_rtc;
404
Keerthy6256f7f2019-04-03 10:27:39 +0530405/**
406 * omap_rtc_power_off_program: Set the pmic power off sequence. The RTC
407 * generates pmic_pwr_enable control, which can be used to control an external
408 * PMIC.
Johan Hovold222a12f2014-12-10 15:53:13 -0800409 */
Keerthy6256f7f2019-04-03 10:27:39 +0530410int omap_rtc_power_off_program(struct device *dev)
Johan Hovold222a12f2014-12-10 15:53:13 -0800411{
412 struct omap_rtc *rtc = omap_rtc_power_off_rtc;
413 struct rtc_time tm;
414 unsigned long now;
Keerthy09058ea2018-08-16 10:39:00 +0530415 int seconds;
Johan Hovold222a12f2014-12-10 15:53:13 -0800416 u32 val;
417
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700418 rtc->type->unlock(rtc);
Johan Hovold222a12f2014-12-10 15:53:13 -0800419 /* enable pmic_power_en control */
420 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
421 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
422
Keerthy09058ea2018-08-16 10:39:00 +0530423again:
Keerthy6256f7f2019-04-03 10:27:39 +0530424 /* Clear any existing ALARM2 event */
425 rtc_writel(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM2);
426
Keerthy09058ea2018-08-16 10:39:00 +0530427 /* set alarm one second from now */
Johan Hovold222a12f2014-12-10 15:53:13 -0800428 omap_rtc_read_time_raw(rtc, &tm);
Keerthy09058ea2018-08-16 10:39:00 +0530429 seconds = tm.tm_sec;
Johan Hovold222a12f2014-12-10 15:53:13 -0800430 bcd2tm(&tm);
Alexandre Belloni28c68522019-03-18 14:26:08 +0100431 now = rtc_tm_to_time64(&tm);
432 rtc_time64_to_tm(now + 1, &tm);
Johan Hovold222a12f2014-12-10 15:53:13 -0800433
Alexandre Belloni35118b72019-03-18 13:44:38 +0100434 tm2bcd(&tm);
Johan Hovold222a12f2014-12-10 15:53:13 -0800435
436 rtc_wait_not_busy(rtc);
437
438 rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
439 rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
440 rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
441 rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
442 rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
443 rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
444
445 /*
446 * enable ALARM2 interrupt
447 *
448 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
449 */
450 val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
451 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
452 val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
Keerthy09058ea2018-08-16 10:39:00 +0530453
454 /* Retry in case roll over happened before alarm was armed. */
455 if (rtc_read(rtc, OMAP_RTC_SECONDS_REG) != seconds) {
456 val = rtc_read(rtc, OMAP_RTC_STATUS_REG);
457 if (!(val & OMAP_RTC_STATUS_ALARM2))
458 goto again;
459 }
460
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700461 rtc->type->lock(rtc);
Johan Hovold222a12f2014-12-10 15:53:13 -0800462
Keerthy6256f7f2019-04-03 10:27:39 +0530463 return 0;
464}
465EXPORT_SYMBOL(omap_rtc_power_off_program);
466
467/*
468 * omap_rtc_poweroff: RTC-controlled power off
469 *
470 * The RTC can be used to control an external PMIC via the pmic_power_en pin,
471 * which can be configured to transition to OFF on ALARM2 events.
472 *
473 * Notes:
474 * The one-second alarm offset is the shortest offset possible as the alarm
475 * registers must be set before the next timer update and the offset
476 * calculation is too heavy for everything to be done within a single access
477 * period (~15 us).
478 *
479 * Called with local interrupts disabled.
480 */
481static void omap_rtc_power_off(void)
482{
483 struct rtc_device *rtc = omap_rtc_power_off_rtc->rtc;
484 u32 val;
485
486 omap_rtc_power_off_program(rtc->dev.parent);
487
488 /* Set PMIC power enable and EXT_WAKEUP in case PB power on is used */
489 omap_rtc_power_off_rtc->type->unlock(omap_rtc_power_off_rtc);
490 val = rtc_readl(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG);
491 val |= OMAP_RTC_PMIC_POWER_EN_EN | OMAP_RTC_PMIC_EXT_WKUP_POL(0) |
492 OMAP_RTC_PMIC_EXT_WKUP_EN(0);
493 rtc_writel(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG, val);
494 omap_rtc_power_off_rtc->type->lock(omap_rtc_power_off_rtc);
495
Johan Hovold222a12f2014-12-10 15:53:13 -0800496 /*
Keerthy09058ea2018-08-16 10:39:00 +0530497 * Wait for alarm to trigger (within one second) and external PMIC to
Johan Hovold222a12f2014-12-10 15:53:13 -0800498 * power off the system. Add a 500 ms margin for external latencies
499 * (e.g. debounce circuits).
500 */
Keerthy09058ea2018-08-16 10:39:00 +0530501 mdelay(1500);
Johan Hovold222a12f2014-12-10 15:53:13 -0800502}
503
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200504static const struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800505 .read_time = omap_rtc_read_time,
506 .set_time = omap_rtc_set_time,
507 .read_alarm = omap_rtc_read_alarm,
508 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800509 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800510};
511
Johan Hovold2153f942014-12-10 15:53:01 -0800512static const struct omap_rtc_device_type omap_rtc_default_type = {
Johan Hovold9291e342014-12-10 15:53:04 -0800513 .has_power_up_reset = true,
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700514 .lock = default_rtc_lock,
515 .unlock = default_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800516};
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800517
Johan Hovold2153f942014-12-10 15:53:01 -0800518static const struct omap_rtc_device_type omap_rtc_am3352_type = {
519 .has_32kclk_en = true,
Johan Hovold2153f942014-12-10 15:53:01 -0800520 .has_irqwakeen = true,
Johan Hovold222a12f2014-12-10 15:53:13 -0800521 .has_pmic_mode = true,
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700522 .lock = am3352_rtc_lock,
523 .unlock = am3352_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800524};
525
526static const struct omap_rtc_device_type omap_rtc_da830_type = {
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700527 .lock = am3352_rtc_lock,
528 .unlock = am3352_rtc_unlock,
Johan Hovold2153f942014-12-10 15:53:01 -0800529};
530
531static const struct platform_device_id omap_rtc_id_table[] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800532 {
Johan Hovolda430ca22014-12-10 15:52:58 -0800533 .name = "omap_rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800534 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
535 }, {
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700536 .name = "am3352-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800537 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
538 }, {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800539 .name = "da830-rtc",
Johan Hovold2153f942014-12-10 15:53:01 -0800540 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
541 }, {
542 /* sentinel */
543 }
Afzal Mohammedcab14582012-12-17 16:02:11 -0800544};
Johan Hovold2153f942014-12-10 15:53:01 -0800545MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800546
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800547static const struct of_device_id omap_rtc_of_match[] = {
Johan Hovold2153f942014-12-10 15:53:01 -0800548 {
549 .compatible = "ti,am3352-rtc",
550 .data = &omap_rtc_am3352_type,
551 }, {
552 .compatible = "ti,da830-rtc",
553 .data = &omap_rtc_da830_type,
554 }, {
555 /* sentinel */
556 }
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800557};
558MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
559
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200560static const struct pinctrl_pin_desc rtc_pins_desc[] = {
561 PINCTRL_PIN(0, "ext_wakeup0"),
562 PINCTRL_PIN(1, "ext_wakeup1"),
563 PINCTRL_PIN(2, "ext_wakeup2"),
564 PINCTRL_PIN(3, "ext_wakeup3"),
565};
566
567static int rtc_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
568{
569 return 0;
570}
571
572static const char *rtc_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
573 unsigned int group)
574{
575 return NULL;
576}
577
578static const struct pinctrl_ops rtc_pinctrl_ops = {
579 .get_groups_count = rtc_pinctrl_get_groups_count,
580 .get_group_name = rtc_pinctrl_get_group_name,
581 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
582 .dt_free_map = pinconf_generic_dt_free_map,
583};
584
Nathan Chancellorc5015652018-10-31 17:55:02 -0700585#define PIN_CONFIG_ACTIVE_HIGH (PIN_CONFIG_END + 1)
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200586
587static const struct pinconf_generic_params rtc_params[] = {
588 {"ti,active-high", PIN_CONFIG_ACTIVE_HIGH, 0},
589};
590
591#ifdef CONFIG_DEBUG_FS
592static const struct pin_config_item rtc_conf_items[ARRAY_SIZE(rtc_params)] = {
593 PCONFDUMP(PIN_CONFIG_ACTIVE_HIGH, "input active high", NULL, false),
594};
595#endif
596
597static int rtc_pinconf_get(struct pinctrl_dev *pctldev,
598 unsigned int pin, unsigned long *config)
599{
600 struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
601 unsigned int param = pinconf_to_config_param(*config);
602 u32 val;
603 u16 arg = 0;
604
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200605 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200606
607 switch (param) {
608 case PIN_CONFIG_INPUT_ENABLE:
609 if (!(val & OMAP_RTC_PMIC_EXT_WKUP_EN(pin)))
610 return -EINVAL;
611 break;
612 case PIN_CONFIG_ACTIVE_HIGH:
613 if (val & OMAP_RTC_PMIC_EXT_WKUP_POL(pin))
614 return -EINVAL;
615 break;
616 default:
617 return -ENOTSUPP;
zhengbinde050562019-12-16 11:19:13 +0800618 }
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200619
620 *config = pinconf_to_config_packed(param, arg);
621
622 return 0;
623}
624
625static int rtc_pinconf_set(struct pinctrl_dev *pctldev,
626 unsigned int pin, unsigned long *configs,
627 unsigned int num_configs)
628{
629 struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
630 u32 val;
631 unsigned int param;
Mika Westerberg58957d22017-01-23 15:34:32 +0300632 u32 param_val;
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200633 int i;
634
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200635 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200636
637 /* active low by default */
638 val |= OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
639
640 for (i = 0; i < num_configs; i++) {
641 param = pinconf_to_config_param(configs[i]);
642 param_val = pinconf_to_config_argument(configs[i]);
643
644 switch (param) {
645 case PIN_CONFIG_INPUT_ENABLE:
646 if (param_val)
647 val |= OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
648 else
649 val &= ~OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
650 break;
651 case PIN_CONFIG_ACTIVE_HIGH:
652 val &= ~OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
653 break;
654 default:
655 dev_err(&rtc->rtc->dev, "Property %u not supported\n",
656 param);
657 return -ENOTSUPP;
658 }
659 }
660
661 rtc->type->unlock(rtc);
662 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
663 rtc->type->lock(rtc);
664
665 return 0;
666}
667
668static const struct pinconf_ops rtc_pinconf_ops = {
669 .is_generic = true,
670 .pin_config_get = rtc_pinconf_get,
671 .pin_config_set = rtc_pinconf_set,
672};
673
674static struct pinctrl_desc rtc_pinctrl_desc = {
675 .pins = rtc_pins_desc,
676 .npins = ARRAY_SIZE(rtc_pins_desc),
677 .pctlops = &rtc_pinctrl_ops,
678 .confops = &rtc_pinconf_ops,
679 .custom_params = rtc_params,
680 .num_custom_params = ARRAY_SIZE(rtc_params),
681#ifdef CONFIG_DEBUG_FS
682 .custom_conf_items = rtc_conf_items,
683#endif
684 .owner = THIS_MODULE,
685};
686
Alexandre Bellonib6ee15e2017-10-31 17:27:31 +0100687static int omap_rtc_scratch_read(void *priv, unsigned int offset, void *_val,
688 size_t bytes)
689{
690 struct omap_rtc *rtc = priv;
691 u32 *val = _val;
692 int i;
693
694 for (i = 0; i < bytes / 4; i++)
695 val[i] = rtc_readl(rtc,
696 OMAP_RTC_SCRATCH0_REG + offset + (i * 4));
697
698 return 0;
699}
700
701static int omap_rtc_scratch_write(void *priv, unsigned int offset, void *_val,
702 size_t bytes)
703{
704 struct omap_rtc *rtc = priv;
705 u32 *val = _val;
706 int i;
707
708 rtc->type->unlock(rtc);
709 for (i = 0; i < bytes / 4; i++)
710 rtc_writel(rtc,
711 OMAP_RTC_SCRATCH0_REG + offset + (i * 4), val[i]);
712 rtc->type->lock(rtc);
713
714 return 0;
715}
716
717static struct nvmem_config omap_rtc_nvmem_config = {
718 .name = "omap_rtc_scratch",
719 .word_size = 4,
720 .stride = 4,
721 .size = OMAP_RTC_KICK0_REG - OMAP_RTC_SCRATCH0_REG,
722 .reg_read = omap_rtc_scratch_read,
723 .reg_write = omap_rtc_scratch_write,
724};
725
Lokesh Vutla5d9094b2015-04-16 12:46:05 -0700726static int omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800727{
Johan Hovold10211ae2014-12-10 15:53:19 -0800728 struct omap_rtc *rtc;
Johan Hovold10211ae2014-12-10 15:53:19 -0800729 u8 reg, mask, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800730 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800731 const struct of_device_id *of_id;
Johan Hovold437b37a2014-12-10 15:52:40 -0800732 int ret;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800733
Johan Hovold55ba9532014-12-10 15:52:55 -0800734 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
735 if (!rtc)
736 return -ENOMEM;
737
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800738 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
Johan Hovold2153f942014-12-10 15:53:01 -0800739 if (of_id) {
740 rtc->type = of_id->data;
Johan Hovold222a12f2014-12-10 15:53:13 -0800741 rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
Keerthy04380022018-08-16 10:38:59 +0530742 of_device_is_system_power_controller(pdev->dev.of_node);
Johan Hovold2153f942014-12-10 15:53:01 -0800743 } else {
744 id_entry = platform_get_device_id(pdev);
745 rtc->type = (void *)id_entry->driver_data;
Sekhar Nori337b6002014-06-06 14:36:04 -0700746 }
747
Johan Hovold55ba9532014-12-10 15:52:55 -0800748 rtc->irq_timer = platform_get_irq(pdev, 0);
749 if (rtc->irq_timer <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800750 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800751
Johan Hovold55ba9532014-12-10 15:52:55 -0800752 rtc->irq_alarm = platform_get_irq(pdev, 1);
753 if (rtc->irq_alarm <= 0)
David Brownelldb68b182006-12-06 20:38:36 -0800754 return -ENOENT;
David Brownelldb68b182006-12-06 20:38:36 -0800755
Keerthy399cf0f2015-08-18 15:11:16 +0530756 rtc->clk = devm_clk_get(&pdev->dev, "ext-clk");
757 if (!IS_ERR(rtc->clk))
758 rtc->has_ext_clk = true;
759 else
760 rtc->clk = devm_clk_get(&pdev->dev, "int-clk");
Keerthy532409a2015-08-18 15:11:15 +0530761
762 if (!IS_ERR(rtc->clk))
763 clk_prepare_enable(rtc->clk);
764
YueHaibing09ef18b2019-10-06 18:29:20 +0800765 rtc->base = devm_platform_ioremap_resource(pdev, 0);
Andreas Platschek2da68772017-12-06 20:42:38 +0100766 if (IS_ERR(rtc->base)) {
767 clk_disable_unprepare(rtc->clk);
Johan Hovold55ba9532014-12-10 15:52:55 -0800768 return PTR_ERR(rtc->base);
Andreas Platschek2da68772017-12-06 20:42:38 +0100769 }
Johan Hovold55ba9532014-12-10 15:52:55 -0800770
771 platform_set_drvdata(pdev, rtc);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800772
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800773 /* Enable the clock/module so that we can access the registers */
774 pm_runtime_enable(&pdev->dev);
775 pm_runtime_get_sync(&pdev->dev);
776
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700777 rtc->type->unlock(rtc);
Afzal Mohammedcab14582012-12-17 16:02:11 -0800778
Johan Hovold1ed8b5d2014-12-10 15:52:36 -0800779 /*
780 * disable interrupts
781 *
782 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
David Brownelldb68b182006-12-06 20:38:36 -0800783 */
Johan Hovold55ba9532014-12-10 15:52:55 -0800784 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800785
Sekhar Noricd914bb2014-06-06 14:36:06 -0700786 /* enable RTC functional clock */
Johan Hovold2153f942014-12-10 15:53:01 -0800787 if (rtc->type->has_32kclk_en) {
Johan Hovold55ba9532014-12-10 15:52:55 -0800788 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
789 rtc_writel(rtc, OMAP_RTC_OSC_REG,
790 reg | OMAP_RTC_OSC_32KCLK_EN);
Johan Hovold44c63a52014-12-10 15:52:30 -0800791 }
Sekhar Noricd914bb2014-06-06 14:36:06 -0700792
David Brownelldb68b182006-12-06 20:38:36 -0800793 /* clear old status */
Johan Hovold55ba9532014-12-10 15:52:55 -0800794 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
Johan Hovold9291e342014-12-10 15:53:04 -0800795
796 mask = OMAP_RTC_STATUS_ALARM;
797
Johan Hovold222a12f2014-12-10 15:53:13 -0800798 if (rtc->type->has_pmic_mode)
799 mask |= OMAP_RTC_STATUS_ALARM2;
800
Johan Hovold9291e342014-12-10 15:53:04 -0800801 if (rtc->type->has_power_up_reset) {
802 mask |= OMAP_RTC_STATUS_POWER_UP;
803 if (reg & OMAP_RTC_STATUS_POWER_UP)
804 dev_info(&pdev->dev, "RTC power up reset detected\n");
David Brownelldb68b182006-12-06 20:38:36 -0800805 }
Johan Hovold9291e342014-12-10 15:53:04 -0800806
807 if (reg & mask)
808 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
David Brownelldb68b182006-12-06 20:38:36 -0800809
David Brownelldb68b182006-12-06 20:38:36 -0800810 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
Johan Hovold55ba9532014-12-10 15:52:55 -0800811 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
Johan Hovold10211ae2014-12-10 15:53:19 -0800812 if (reg & OMAP_RTC_CTRL_STOP)
Johan Hovold397b6302014-12-10 15:52:49 -0800813 dev_info(&pdev->dev, "already running\n");
David Brownelldb68b182006-12-06 20:38:36 -0800814
815 /* force to 24 hour mode */
Johan Hovold10211ae2014-12-10 15:53:19 -0800816 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800817 new_ctrl |= OMAP_RTC_CTRL_STOP;
818
Johan Hovold10211ae2014-12-10 15:53:19 -0800819 /*
820 * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
David Brownelldb68b182006-12-06 20:38:36 -0800821 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700822 * - Device wake-up capability setting should come through chip
823 * init logic. OMAP1 boards should initialize the "wakeup capable"
824 * flag in the platform device if the board is wired right for
825 * being woken up by RTC alarm. For OMAP-L138, this capability
826 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800827 *
828 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
829 * rather than nPWRON_RESET, should forcibly enable split
830 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
831 * is write-only, and always reads as zero...)
832 */
David Brownelldb68b182006-12-06 20:38:36 -0800833
Johan Hovold10211ae2014-12-10 15:53:19 -0800834 if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
Johan Hovold397b6302014-12-10 15:52:49 -0800835 dev_info(&pdev->dev, "split power mode\n");
David Brownelldb68b182006-12-06 20:38:36 -0800836
837 if (reg != new_ctrl)
Johan Hovold55ba9532014-12-10 15:52:55 -0800838 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
David Brownelldb68b182006-12-06 20:38:36 -0800839
Keerthy399cf0f2015-08-18 15:11:16 +0530840 /*
841 * If we have the external clock then switch to it so we can keep
842 * ticking across suspend.
843 */
844 if (rtc->has_ext_clk) {
845 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
Lokesh Vutla39849032016-10-27 11:27:25 +0530846 reg &= ~OMAP_RTC_OSC_OSC32K_GZ_DISABLE;
847 reg |= OMAP_RTC_OSC_32KCLK_EN | OMAP_RTC_OSC_SEL_32KCLK_SRC;
848 rtc_writel(rtc, OMAP_RTC_OSC_REG, reg);
Keerthy399cf0f2015-08-18 15:11:16 +0530849 }
850
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700851 rtc->type->lock(rtc);
852
Johan Hovold4390ce02014-12-10 15:52:43 -0800853 device_init_wakeup(&pdev->dev, true);
854
Alexandre Belloni57072752017-10-13 00:06:45 +0200855 rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
Johan Hovold55ba9532014-12-10 15:52:55 -0800856 if (IS_ERR(rtc->rtc)) {
857 ret = PTR_ERR(rtc->rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800858 goto err;
859 }
Johan Hovold4390ce02014-12-10 15:52:43 -0800860
Alexandre Belloni57072752017-10-13 00:06:45 +0200861 rtc->rtc->ops = &omap_rtc_ops;
Alexandre Belloni35118b72019-03-18 13:44:38 +0100862 rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
863 rtc->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Bellonib6ee15e2017-10-31 17:27:31 +0100864 omap_rtc_nvmem_config.priv = rtc;
Alexandre Belloni57072752017-10-13 00:06:45 +0200865
Johan Hovold4390ce02014-12-10 15:52:43 -0800866 /* handle periodic and alarm irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800867 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
868 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800869 if (ret)
870 goto err;
871
Johan Hovold55ba9532014-12-10 15:52:55 -0800872 if (rtc->irq_timer != rtc->irq_alarm) {
873 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
874 dev_name(&rtc->rtc->dev), rtc);
Johan Hovold4390ce02014-12-10 15:52:43 -0800875 if (ret)
876 goto err;
877 }
878
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200879 /* Support ext_wakeup pinconf */
880 rtc_pinctrl_desc.name = dev_name(&pdev->dev);
881
882 rtc->pctldev = pinctrl_register(&rtc_pinctrl_desc, &pdev->dev, rtc);
883 if (IS_ERR(rtc->pctldev)) {
884 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
Alexandre Belloni26e480f2017-10-13 00:06:44 +0200885 ret = PTR_ERR(rtc->pctldev);
886 goto err;
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200887 }
888
Alexandre Belloni57072752017-10-13 00:06:45 +0200889 ret = rtc_register_device(rtc->rtc);
890 if (ret)
Johan Hovold551757e2018-07-04 11:05:56 +0200891 goto err_deregister_pinctrl;
Alexandre Belloni57072752017-10-13 00:06:45 +0200892
Alexandre Bellonice603842018-02-12 23:47:28 +0100893 rtc_nvmem_register(rtc->rtc, &omap_rtc_nvmem_config);
894
Johan Hovold5c8b84f2018-07-04 11:05:55 +0200895 if (rtc->is_pmic_controller) {
896 if (!pm_power_off) {
897 omap_rtc_power_off_rtc = rtc;
898 pm_power_off = omap_rtc_power_off;
899 }
900 }
901
David Brownelldb68b182006-12-06 20:38:36 -0800902 return 0;
903
Johan Hovold551757e2018-07-04 11:05:56 +0200904err_deregister_pinctrl:
905 pinctrl_unregister(rtc->pctldev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800906err:
Andreas Platschek2da68772017-12-06 20:42:38 +0100907 clk_disable_unprepare(rtc->clk);
Johan Hovold7ecd9a32014-12-10 15:52:33 -0800908 device_init_wakeup(&pdev->dev, false);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700909 rtc->type->lock(rtc);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800910 pm_runtime_put_sync(&pdev->dev);
911 pm_runtime_disable(&pdev->dev);
Johan Hovold437b37a2014-12-10 15:52:40 -0800912
913 return ret;
David Brownelldb68b182006-12-06 20:38:36 -0800914}
915
Dmitry Torokhovb9de1a12017-03-01 15:33:23 -0800916static int omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800917{
Johan Hovold55ba9532014-12-10 15:52:55 -0800918 struct omap_rtc *rtc = platform_get_drvdata(pdev);
Keerthy399cf0f2015-08-18 15:11:16 +0530919 u8 reg;
David Brownelldb68b182006-12-06 20:38:36 -0800920
Johan Hovold222a12f2014-12-10 15:53:13 -0800921 if (pm_power_off == omap_rtc_power_off &&
922 omap_rtc_power_off_rtc == rtc) {
923 pm_power_off = NULL;
924 omap_rtc_power_off_rtc = NULL;
925 }
926
David Brownelldb68b182006-12-06 20:38:36 -0800927 device_init_wakeup(&pdev->dev, 0);
928
Keerthy532409a2015-08-18 15:11:15 +0530929 if (!IS_ERR(rtc->clk))
930 clk_disable_unprepare(rtc->clk);
931
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700932 rtc->type->unlock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800933 /* leave rtc running, but disable irqs */
Johan Hovold55ba9532014-12-10 15:52:55 -0800934 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
David Brownelldb68b182006-12-06 20:38:36 -0800935
Keerthy399cf0f2015-08-18 15:11:16 +0530936 if (rtc->has_ext_clk) {
937 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
938 reg &= ~OMAP_RTC_OSC_SEL_32KCLK_SRC;
939 rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
940 }
941
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700942 rtc->type->lock(rtc);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800943
944 /* Disable the clock/module */
945 pm_runtime_put_sync(&pdev->dev);
946 pm_runtime_disable(&pdev->dev);
947
Marcin Niestroj97ea1902016-09-16 11:26:28 +0200948 /* Remove ext_wakeup pinconf */
949 pinctrl_unregister(rtc->pctldev);
950
David Brownelldb68b182006-12-06 20:38:36 -0800951 return 0;
952}
953
Dmitry Torokhov0c749ea2017-03-01 15:34:38 -0800954static int __maybe_unused omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800955{
Johan Hovold55ba9532014-12-10 15:52:55 -0800956 struct omap_rtc *rtc = dev_get_drvdata(dev);
957
958 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
David Brownelldb68b182006-12-06 20:38:36 -0800959
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700960 rtc->type->unlock(rtc);
Johan Hovold10211ae2014-12-10 15:53:19 -0800961 /*
962 * FIXME: the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700963 * source on some platforms, and in fact this enable() call is just
964 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800965 */
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700966 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800967 enable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700968 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800969 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700970 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800971
Tero Kristoefce21f2016-10-27 11:27:26 +0530972 rtc->is_suspending = true;
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800973
David Brownelldb68b182006-12-06 20:38:36 -0800974 return 0;
975}
976
Dmitry Torokhov0c749ea2017-03-01 15:34:38 -0800977static int __maybe_unused omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800978{
Johan Hovold55ba9532014-12-10 15:52:55 -0800979 struct omap_rtc *rtc = dev_get_drvdata(dev);
980
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700981 rtc->type->unlock(rtc);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700982 if (device_may_wakeup(dev))
Johan Hovold55ba9532014-12-10 15:52:55 -0800983 disable_irq_wake(rtc->irq_alarm);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700984 else
Johan Hovold55ba9532014-12-10 15:52:55 -0800985 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -0700986 rtc->type->lock(rtc);
Lokesh Vutlaab7f5802014-06-06 14:36:12 -0700987
Tero Kristoefce21f2016-10-27 11:27:26 +0530988 rtc->is_suspending = false;
989
David Brownelldb68b182006-12-06 20:38:36 -0800990 return 0;
991}
David Brownelldb68b182006-12-06 20:38:36 -0800992
Dmitry Torokhov0c749ea2017-03-01 15:34:38 -0800993static int __maybe_unused omap_rtc_runtime_suspend(struct device *dev)
Tero Kristoefce21f2016-10-27 11:27:26 +0530994{
995 struct omap_rtc *rtc = dev_get_drvdata(dev);
996
997 if (rtc->is_suspending && !rtc->has_ext_clk)
998 return -EBUSY;
999
1000 return 0;
1001}
1002
Tero Kristoefce21f2016-10-27 11:27:26 +05301003static const struct dev_pm_ops omap_rtc_pm_ops = {
1004 SET_SYSTEM_SLEEP_PM_OPS(omap_rtc_suspend, omap_rtc_resume)
Dmitry Torokhov0c749ea2017-03-01 15:34:38 -08001005 SET_RUNTIME_PM_OPS(omap_rtc_runtime_suspend, NULL, NULL)
Tero Kristoefce21f2016-10-27 11:27:26 +05301006};
Jingoo Han04ebc352013-04-29 16:21:01 -07001007
David Brownelldb68b182006-12-06 20:38:36 -08001008static void omap_rtc_shutdown(struct platform_device *pdev)
1009{
Johan Hovold55ba9532014-12-10 15:52:55 -08001010 struct omap_rtc *rtc = platform_get_drvdata(pdev);
Johan Hovold8ad5c722014-12-10 15:53:16 -08001011 u8 mask;
Johan Hovold55ba9532014-12-10 15:52:55 -08001012
Johan Hovold8ad5c722014-12-10 15:53:16 -08001013 /*
1014 * Keep the ALARM interrupt enabled to allow the system to power up on
1015 * alarm events.
1016 */
Lokesh Vutla9c28bd02015-04-16 12:46:00 -07001017 rtc->type->unlock(rtc);
Johan Hovold8ad5c722014-12-10 15:53:16 -08001018 mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
1019 mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
1020 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
Lokesh Vutla9c28bd02015-04-16 12:46:00 -07001021 rtc->type->lock(rtc);
David Brownelldb68b182006-12-06 20:38:36 -08001022}
1023
David Brownelldb68b182006-12-06 20:38:36 -08001024static struct platform_driver omap_rtc_driver = {
Lokesh Vutla5d9094b2015-04-16 12:46:05 -07001025 .probe = omap_rtc_probe,
Dmitry Torokhovb9de1a12017-03-01 15:33:23 -08001026 .remove = omap_rtc_remove,
David Brownelldb68b182006-12-06 20:38:36 -08001027 .shutdown = omap_rtc_shutdown,
1028 .driver = {
Johan Hovolda430ca22014-12-10 15:52:58 -08001029 .name = "omap_rtc",
Jingoo Han04ebc352013-04-29 16:21:01 -07001030 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -08001031 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -08001032 },
Johan Hovold2153f942014-12-10 15:53:01 -08001033 .id_table = omap_rtc_id_table,
David Brownelldb68b182006-12-06 20:38:36 -08001034};
1035
Lokesh Vutla5d9094b2015-04-16 12:46:05 -07001036module_platform_driver(omap_rtc_driver);
David Brownelldb68b182006-12-06 20:38:36 -08001037
Johan Hovolda430ca22014-12-10 15:52:58 -08001038MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -08001039MODULE_AUTHOR("George G. Davis (and others)");
1040MODULE_LICENSE("GPL");