blob: 7fbb1741692f64c5b13ff2b2963d90413a9aba0e [file] [log] [blame]
Alexandre Bellonib6838272019-04-07 23:16:46 +02001// SPDX-License-Identifier: GPL-2.0+
Andrew Chewff859ba2011-03-22 16:34:55 -07002/*
3 * An RTC driver for the NVIDIA Tegra 200 series internal RTC.
4 *
Thierry Reding3e483e592019-05-27 12:13:59 +02005 * Copyright (c) 2010-2019, NVIDIA Corporation.
Andrew Chewff859ba2011-03-22 16:34:55 -07006 */
Thierry Reding0ae20592017-01-12 17:07:42 +01007
Thierry Reding5fa40862017-01-12 17:07:43 +01008#include <linux/clk.h>
Andrew Chewff859ba2011-03-22 16:34:55 -07009#include <linux/delay.h>
Thierry Reding0ae20592017-01-12 17:07:42 +010010#include <linux/init.h>
11#include <linux/io.h>
12#include <linux/irq.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070015#include <linux/mod_devicetable.h>
Andrew Chewff859ba2011-03-22 16:34:55 -070016#include <linux/platform_device.h>
Laxman Dewangan3443ad02013-04-29 16:19:23 -070017#include <linux/pm.h>
Thierry Reding0ae20592017-01-12 17:07:42 +010018#include <linux/rtc.h>
19#include <linux/slab.h>
Andrew Chewff859ba2011-03-22 16:34:55 -070020
Thierry Redinga2d29232019-05-27 12:13:57 +020021/* Set to 1 = busy every eight 32 kHz clocks during copy of sec+msec to AHB. */
Andrew Chewff859ba2011-03-22 16:34:55 -070022#define TEGRA_RTC_REG_BUSY 0x004
23#define TEGRA_RTC_REG_SECONDS 0x008
Thierry Redinga2d29232019-05-27 12:13:57 +020024/* When msec is read, the seconds are buffered into shadow seconds. */
Andrew Chewff859ba2011-03-22 16:34:55 -070025#define TEGRA_RTC_REG_SHADOW_SECONDS 0x00c
26#define TEGRA_RTC_REG_MILLI_SECONDS 0x010
27#define TEGRA_RTC_REG_SECONDS_ALARM0 0x014
28#define TEGRA_RTC_REG_SECONDS_ALARM1 0x018
29#define TEGRA_RTC_REG_MILLI_SECONDS_ALARM0 0x01c
30#define TEGRA_RTC_REG_INTR_MASK 0x028
31/* write 1 bits to clear status bits */
32#define TEGRA_RTC_REG_INTR_STATUS 0x02c
33
34/* bits in INTR_MASK */
35#define TEGRA_RTC_INTR_MASK_MSEC_CDN_ALARM (1<<4)
36#define TEGRA_RTC_INTR_MASK_SEC_CDN_ALARM (1<<3)
37#define TEGRA_RTC_INTR_MASK_MSEC_ALARM (1<<2)
38#define TEGRA_RTC_INTR_MASK_SEC_ALARM1 (1<<1)
39#define TEGRA_RTC_INTR_MASK_SEC_ALARM0 (1<<0)
40
41/* bits in INTR_STATUS */
42#define TEGRA_RTC_INTR_STATUS_MSEC_CDN_ALARM (1<<4)
43#define TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM (1<<3)
44#define TEGRA_RTC_INTR_STATUS_MSEC_ALARM (1<<2)
45#define TEGRA_RTC_INTR_STATUS_SEC_ALARM1 (1<<1)
46#define TEGRA_RTC_INTR_STATUS_SEC_ALARM0 (1<<0)
47
48struct tegra_rtc_info {
Thierry Redinga2d29232019-05-27 12:13:57 +020049 struct platform_device *pdev;
Thierry Redingc6af5612019-05-27 12:13:58 +020050 struct rtc_device *rtc;
51 void __iomem *base; /* NULL if not initialized */
Thierry Redinga2d29232019-05-27 12:13:57 +020052 struct clk *clk;
Thierry Redingc6af5612019-05-27 12:13:58 +020053 int irq; /* alarm and periodic IRQ */
54 spinlock_t lock;
Andrew Chewff859ba2011-03-22 16:34:55 -070055};
56
Thierry Redinga2d29232019-05-27 12:13:57 +020057/*
58 * RTC hardware is busy when it is updating its values over AHB once every
59 * eight 32 kHz clocks (~250 us). Outside of these updates the CPU is free to
60 * write. CPU is always free to read.
Andrew Chewff859ba2011-03-22 16:34:55 -070061 */
62static inline u32 tegra_rtc_check_busy(struct tegra_rtc_info *info)
63{
Thierry Redingc6af5612019-05-27 12:13:58 +020064 return readl(info->base + TEGRA_RTC_REG_BUSY) & 1;
Andrew Chewff859ba2011-03-22 16:34:55 -070065}
66
Thierry Redinga2d29232019-05-27 12:13:57 +020067/*
68 * Wait for hardware to be ready for writing. This function tries to maximize
69 * the amount of time before the next update. It does this by waiting for the
70 * RTC to become busy with its periodic update, then returning once the RTC
71 * first becomes not busy.
72 *
Andrew Chewff859ba2011-03-22 16:34:55 -070073 * This periodic update (where the seconds and milliseconds are copied to the
Thierry Redinga2d29232019-05-27 12:13:57 +020074 * AHB side) occurs every eight 32 kHz clocks (~250 us). The behavior of this
75 * function allows us to make some assumptions without introducing a race,
76 * because 250 us is plenty of time to read/write a value.
Andrew Chewff859ba2011-03-22 16:34:55 -070077 */
78static int tegra_rtc_wait_while_busy(struct device *dev)
79{
80 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redinga2d29232019-05-27 12:13:57 +020081 int retries = 500; /* ~490 us is the worst case, ~250 us is best */
Andrew Chewff859ba2011-03-22 16:34:55 -070082
Thierry Redinga2d29232019-05-27 12:13:57 +020083 /*
84 * First wait for the RTC to become busy. This is when it posts its
85 * updated seconds+msec registers to AHB side.
86 */
Andrew Chewff859ba2011-03-22 16:34:55 -070087 while (tegra_rtc_check_busy(info)) {
88 if (!retries--)
89 goto retry_failed;
Thierry Redinga2d29232019-05-27 12:13:57 +020090
Andrew Chewff859ba2011-03-22 16:34:55 -070091 udelay(1);
92 }
93
94 /* now we have about 250 us to manipulate registers */
95 return 0;
96
97retry_failed:
Thierry Redinga2d29232019-05-27 12:13:57 +020098 dev_err(dev, "write failed: retry count exceeded\n");
Andrew Chewff859ba2011-03-22 16:34:55 -070099 return -ETIMEDOUT;
100}
101
102static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm)
103{
104 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200105 unsigned long flags;
Alexandre Belloni8321c2e2019-11-22 11:22:11 +0100106 u32 sec;
Andrew Chewff859ba2011-03-22 16:34:55 -0700107
Thierry Redinga2d29232019-05-27 12:13:57 +0200108 /*
109 * RTC hardware copies seconds to shadow seconds when a read of
110 * milliseconds occurs. use a lock to keep other threads out.
111 */
Thierry Redingc6af5612019-05-27 12:13:58 +0200112 spin_lock_irqsave(&info->lock, flags);
Andrew Chewff859ba2011-03-22 16:34:55 -0700113
Alexandre Belloni8321c2e2019-11-22 11:22:11 +0100114 readl(info->base + TEGRA_RTC_REG_MILLI_SECONDS);
Thierry Redingc6af5612019-05-27 12:13:58 +0200115 sec = readl(info->base + TEGRA_RTC_REG_SHADOW_SECONDS);
Andrew Chewff859ba2011-03-22 16:34:55 -0700116
Thierry Redingc6af5612019-05-27 12:13:58 +0200117 spin_unlock_irqrestore(&info->lock, flags);
Andrew Chewff859ba2011-03-22 16:34:55 -0700118
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200119 rtc_time64_to_tm(sec, tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700120
Thierry Redinga2d29232019-05-27 12:13:57 +0200121 dev_vdbg(dev, "time read as %u, %ptR\n", sec, tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700122
123 return 0;
124}
125
126static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm)
127{
128 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200129 u32 sec;
Andrew Chewff859ba2011-03-22 16:34:55 -0700130 int ret;
131
Thierry Redinga2d29232019-05-27 12:13:57 +0200132 /* convert tm to seconds */
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200133 sec = rtc_tm_to_time64(tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700134
Thierry Redinga2d29232019-05-27 12:13:57 +0200135 dev_vdbg(dev, "time set to %u, %ptR\n", sec, tm);
Andrew Chewff859ba2011-03-22 16:34:55 -0700136
Thierry Redinga2d29232019-05-27 12:13:57 +0200137 /* seconds only written if wait succeeded */
Andrew Chewff859ba2011-03-22 16:34:55 -0700138 ret = tegra_rtc_wait_while_busy(dev);
139 if (!ret)
Thierry Redingc6af5612019-05-27 12:13:58 +0200140 writel(sec, info->base + TEGRA_RTC_REG_SECONDS);
Andrew Chewff859ba2011-03-22 16:34:55 -0700141
142 dev_vdbg(dev, "time read back as %d\n",
Thierry Redingc6af5612019-05-27 12:13:58 +0200143 readl(info->base + TEGRA_RTC_REG_SECONDS));
Andrew Chewff859ba2011-03-22 16:34:55 -0700144
145 return ret;
146}
147
148static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
149{
150 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200151 u32 sec, value;
Andrew Chewff859ba2011-03-22 16:34:55 -0700152
Thierry Redingc6af5612019-05-27 12:13:58 +0200153 sec = readl(info->base + TEGRA_RTC_REG_SECONDS_ALARM0);
Andrew Chewff859ba2011-03-22 16:34:55 -0700154
155 if (sec == 0) {
Thierry Redinga2d29232019-05-27 12:13:57 +0200156 /* alarm is disabled */
Andrew Chewff859ba2011-03-22 16:34:55 -0700157 alarm->enabled = 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700158 } else {
Thierry Redinga2d29232019-05-27 12:13:57 +0200159 /* alarm is enabled */
Andrew Chewff859ba2011-03-22 16:34:55 -0700160 alarm->enabled = 1;
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200161 rtc_time64_to_tm(sec, &alarm->time);
Andrew Chewff859ba2011-03-22 16:34:55 -0700162 }
163
Thierry Redingc6af5612019-05-27 12:13:58 +0200164 value = readl(info->base + TEGRA_RTC_REG_INTR_STATUS);
165 alarm->pending = (value & TEGRA_RTC_INTR_STATUS_SEC_ALARM0) != 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700166
167 return 0;
168}
169
170static int tegra_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
171{
172 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200173 unsigned long flags;
174 u32 status;
Andrew Chewff859ba2011-03-22 16:34:55 -0700175
176 tegra_rtc_wait_while_busy(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200177 spin_lock_irqsave(&info->lock, flags);
Andrew Chewff859ba2011-03-22 16:34:55 -0700178
Thierry Redinga2d29232019-05-27 12:13:57 +0200179 /* read the original value, and OR in the flag */
Thierry Redingc6af5612019-05-27 12:13:58 +0200180 status = readl(info->base + TEGRA_RTC_REG_INTR_MASK);
Andrew Chewff859ba2011-03-22 16:34:55 -0700181 if (enabled)
182 status |= TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* set it */
183 else
184 status &= ~TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* clear it */
185
Thierry Redingc6af5612019-05-27 12:13:58 +0200186 writel(status, info->base + TEGRA_RTC_REG_INTR_MASK);
Andrew Chewff859ba2011-03-22 16:34:55 -0700187
Thierry Redingc6af5612019-05-27 12:13:58 +0200188 spin_unlock_irqrestore(&info->lock, flags);
Andrew Chewff859ba2011-03-22 16:34:55 -0700189
190 return 0;
191}
192
193static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
194{
195 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200196 u32 sec;
Andrew Chewff859ba2011-03-22 16:34:55 -0700197
198 if (alarm->enabled)
Alexandre Belloni34ea0ac2019-04-07 23:16:45 +0200199 sec = rtc_tm_to_time64(&alarm->time);
Andrew Chewff859ba2011-03-22 16:34:55 -0700200 else
201 sec = 0;
202
203 tegra_rtc_wait_while_busy(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200204 writel(sec, info->base + TEGRA_RTC_REG_SECONDS_ALARM0);
Andrew Chewff859ba2011-03-22 16:34:55 -0700205 dev_vdbg(dev, "alarm read back as %d\n",
Thierry Redingc6af5612019-05-27 12:13:58 +0200206 readl(info->base + TEGRA_RTC_REG_SECONDS_ALARM0));
Andrew Chewff859ba2011-03-22 16:34:55 -0700207
208 /* if successfully written and alarm is enabled ... */
209 if (sec) {
210 tegra_rtc_alarm_irq_enable(dev, 1);
Thierry Redinga2d29232019-05-27 12:13:57 +0200211 dev_vdbg(dev, "alarm set as %u, %ptR\n", sec, &alarm->time);
Andrew Chewff859ba2011-03-22 16:34:55 -0700212 } else {
Thierry Redinga2d29232019-05-27 12:13:57 +0200213 /* disable alarm if 0 or write error */
Andrew Chewff859ba2011-03-22 16:34:55 -0700214 dev_vdbg(dev, "alarm disabled\n");
215 tegra_rtc_alarm_irq_enable(dev, 0);
216 }
217
218 return 0;
219}
220
221static int tegra_rtc_proc(struct device *dev, struct seq_file *seq)
222{
223 if (!dev || !dev->driver)
224 return 0;
225
Joe Perches4395eb12015-04-15 16:17:51 -0700226 seq_printf(seq, "name\t\t: %s\n", dev_name(dev));
227
228 return 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700229}
230
231static irqreturn_t tegra_rtc_irq_handler(int irq, void *data)
232{
233 struct device *dev = data;
234 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Thierry Redingc6af5612019-05-27 12:13:58 +0200235 unsigned long events = 0, flags;
236 u32 status;
Andrew Chewff859ba2011-03-22 16:34:55 -0700237
Thierry Redingc6af5612019-05-27 12:13:58 +0200238 status = readl(info->base + TEGRA_RTC_REG_INTR_STATUS);
Andrew Chewff859ba2011-03-22 16:34:55 -0700239 if (status) {
Thierry Redinga2d29232019-05-27 12:13:57 +0200240 /* clear the interrupt masks and status on any IRQ */
Andrew Chewff859ba2011-03-22 16:34:55 -0700241 tegra_rtc_wait_while_busy(dev);
Thierry Redinga2d29232019-05-27 12:13:57 +0200242
Thierry Redingc6af5612019-05-27 12:13:58 +0200243 spin_lock_irqsave(&info->lock, flags);
244 writel(0, info->base + TEGRA_RTC_REG_INTR_MASK);
245 writel(status, info->base + TEGRA_RTC_REG_INTR_STATUS);
246 spin_unlock_irqrestore(&info->lock, flags);
Andrew Chewff859ba2011-03-22 16:34:55 -0700247 }
248
Thierry Redinga2d29232019-05-27 12:13:57 +0200249 /* check if alarm */
250 if (status & TEGRA_RTC_INTR_STATUS_SEC_ALARM0)
Andrew Chewff859ba2011-03-22 16:34:55 -0700251 events |= RTC_IRQF | RTC_AF;
252
Thierry Redinga2d29232019-05-27 12:13:57 +0200253 /* check if periodic */
254 if (status & TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM)
Andrew Chewff859ba2011-03-22 16:34:55 -0700255 events |= RTC_IRQF | RTC_PF;
256
Thierry Redingc6af5612019-05-27 12:13:58 +0200257 rtc_update_irq(info->rtc, 1, events);
Andrew Chewff859ba2011-03-22 16:34:55 -0700258
259 return IRQ_HANDLED;
260}
261
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200262static const struct rtc_class_ops tegra_rtc_ops = {
Thierry Redinga2d29232019-05-27 12:13:57 +0200263 .read_time = tegra_rtc_read_time,
264 .set_time = tegra_rtc_set_time,
265 .read_alarm = tegra_rtc_read_alarm,
266 .set_alarm = tegra_rtc_set_alarm,
267 .proc = tegra_rtc_proc,
Andrew Chewff859ba2011-03-22 16:34:55 -0700268 .alarm_irq_enable = tegra_rtc_alarm_irq_enable,
269};
270
Joseph Lo2d79cf82013-01-04 15:34:45 -0800271static const struct of_device_id tegra_rtc_dt_match[] = {
272 { .compatible = "nvidia,tegra20-rtc", },
273 {}
274};
275MODULE_DEVICE_TABLE(of, tegra_rtc_dt_match);
276
Thierry Reding3e483e592019-05-27 12:13:59 +0200277static int tegra_rtc_probe(struct platform_device *pdev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700278{
279 struct tegra_rtc_info *info;
Andrew Chewff859ba2011-03-22 16:34:55 -0700280 int ret;
281
Thierry Redinga2d29232019-05-27 12:13:57 +0200282 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Andrew Chewff859ba2011-03-22 16:34:55 -0700283 if (!info)
284 return -ENOMEM;
285
YueHaibing09ef18b2019-10-06 18:29:20 +0800286 info->base = devm_platform_ioremap_resource(pdev, 0);
Thierry Redingc6af5612019-05-27 12:13:58 +0200287 if (IS_ERR(info->base))
288 return PTR_ERR(info->base);
Andrew Chewff859ba2011-03-22 16:34:55 -0700289
Thierry Redingfe0b5ce2018-09-21 12:12:09 +0200290 ret = platform_get_irq(pdev, 0);
Stephen Boydfaac9102019-07-30 11:15:39 -0700291 if (ret <= 0)
Thierry Redingfe0b5ce2018-09-21 12:12:09 +0200292 return ret;
Thierry Redingfe0b5ce2018-09-21 12:12:09 +0200293
Thierry Redingc6af5612019-05-27 12:13:58 +0200294 info->irq = ret;
Andrew Chewff859ba2011-03-22 16:34:55 -0700295
Thierry Redingc6af5612019-05-27 12:13:58 +0200296 info->rtc = devm_rtc_allocate_device(&pdev->dev);
297 if (IS_ERR(info->rtc))
298 return PTR_ERR(info->rtc);
Alexandre Bellonie1089802019-04-07 23:16:44 +0200299
Thierry Redingc6af5612019-05-27 12:13:58 +0200300 info->rtc->ops = &tegra_rtc_ops;
301 info->rtc->range_max = U32_MAX;
Alexandre Bellonie1089802019-04-07 23:16:44 +0200302
Thierry Reding5fa40862017-01-12 17:07:43 +0100303 info->clk = devm_clk_get(&pdev->dev, NULL);
304 if (IS_ERR(info->clk))
305 return PTR_ERR(info->clk);
306
307 ret = clk_prepare_enable(info->clk);
308 if (ret < 0)
309 return ret;
310
Thierry Redinga2d29232019-05-27 12:13:57 +0200311 /* set context info */
Andrew Chewff859ba2011-03-22 16:34:55 -0700312 info->pdev = pdev;
Thierry Redingc6af5612019-05-27 12:13:58 +0200313 spin_lock_init(&info->lock);
Andrew Chewff859ba2011-03-22 16:34:55 -0700314
315 platform_set_drvdata(pdev, info);
316
Thierry Redinga2d29232019-05-27 12:13:57 +0200317 /* clear out the hardware */
Thierry Redingc6af5612019-05-27 12:13:58 +0200318 writel(0, info->base + TEGRA_RTC_REG_SECONDS_ALARM0);
319 writel(0xffffffff, info->base + TEGRA_RTC_REG_INTR_STATUS);
320 writel(0, info->base + TEGRA_RTC_REG_INTR_MASK);
Andrew Chewff859ba2011-03-22 16:34:55 -0700321
322 device_init_wakeup(&pdev->dev, 1);
323
Thierry Redingc6af5612019-05-27 12:13:58 +0200324 ret = devm_request_irq(&pdev->dev, info->irq, tegra_rtc_irq_handler,
325 IRQF_TRIGGER_HIGH, dev_name(&pdev->dev),
326 &pdev->dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700327 if (ret) {
Thierry Redinga2d29232019-05-27 12:13:57 +0200328 dev_err(&pdev->dev, "failed to request interrupt: %d\n", ret);
Thierry Reding5fa40862017-01-12 17:07:43 +0100329 goto disable_clk;
Andrew Chewff859ba2011-03-22 16:34:55 -0700330 }
331
Thierry Redingc6af5612019-05-27 12:13:58 +0200332 ret = rtc_register_device(info->rtc);
Alexandre Belloni44c638c2019-08-19 00:00:41 +0200333 if (ret)
Alexandre Bellonie1089802019-04-07 23:16:44 +0200334 goto disable_clk;
Alexandre Bellonie1089802019-04-07 23:16:44 +0200335
Andrew Chewff859ba2011-03-22 16:34:55 -0700336 dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
337
338 return 0;
Thierry Reding5fa40862017-01-12 17:07:43 +0100339
340disable_clk:
341 clk_disable_unprepare(info->clk);
342 return ret;
343}
344
345static int tegra_rtc_remove(struct platform_device *pdev)
346{
347 struct tegra_rtc_info *info = platform_get_drvdata(pdev);
348
349 clk_disable_unprepare(info->clk);
350
351 return 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700352}
353
Laxman Dewangan38a62762013-04-29 16:19:21 -0700354#ifdef CONFIG_PM_SLEEP
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700355static int tegra_rtc_suspend(struct device *dev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700356{
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700357 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700358
359 tegra_rtc_wait_while_busy(dev);
360
Thierry Redinga2d29232019-05-27 12:13:57 +0200361 /* only use ALARM0 as a wake source */
Thierry Redingc6af5612019-05-27 12:13:58 +0200362 writel(0xffffffff, info->base + TEGRA_RTC_REG_INTR_STATUS);
Andrew Chewff859ba2011-03-22 16:34:55 -0700363 writel(TEGRA_RTC_INTR_STATUS_SEC_ALARM0,
Thierry Redingc6af5612019-05-27 12:13:58 +0200364 info->base + TEGRA_RTC_REG_INTR_MASK);
Andrew Chewff859ba2011-03-22 16:34:55 -0700365
366 dev_vdbg(dev, "alarm sec = %d\n",
Thierry Redingc6af5612019-05-27 12:13:58 +0200367 readl(info->base + TEGRA_RTC_REG_SECONDS_ALARM0));
Andrew Chewff859ba2011-03-22 16:34:55 -0700368
Thierry Redinga2d29232019-05-27 12:13:57 +0200369 dev_vdbg(dev, "Suspend (device_may_wakeup=%d) IRQ:%d\n",
Thierry Redingc6af5612019-05-27 12:13:58 +0200370 device_may_wakeup(dev), info->irq);
Andrew Chewff859ba2011-03-22 16:34:55 -0700371
Thierry Redinga2d29232019-05-27 12:13:57 +0200372 /* leave the alarms on as a wake source */
Andrew Chewff859ba2011-03-22 16:34:55 -0700373 if (device_may_wakeup(dev))
Thierry Redingc6af5612019-05-27 12:13:58 +0200374 enable_irq_wake(info->irq);
Andrew Chewff859ba2011-03-22 16:34:55 -0700375
376 return 0;
377}
378
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700379static int tegra_rtc_resume(struct device *dev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700380{
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700381 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700382
383 dev_vdbg(dev, "Resume (device_may_wakeup=%d)\n",
Thierry Redinga2d29232019-05-27 12:13:57 +0200384 device_may_wakeup(dev));
385
386 /* alarms were left on as a wake source, turn them off */
Andrew Chewff859ba2011-03-22 16:34:55 -0700387 if (device_may_wakeup(dev))
Thierry Redingc6af5612019-05-27 12:13:58 +0200388 disable_irq_wake(info->irq);
Andrew Chewff859ba2011-03-22 16:34:55 -0700389
390 return 0;
391}
392#endif
393
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700394static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
395
Andrew Chewff859ba2011-03-22 16:34:55 -0700396static void tegra_rtc_shutdown(struct platform_device *pdev)
397{
Thierry Redinga2d29232019-05-27 12:13:57 +0200398 dev_vdbg(&pdev->dev, "disabling interrupts\n");
Andrew Chewff859ba2011-03-22 16:34:55 -0700399 tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
400}
401
Andrew Chewff859ba2011-03-22 16:34:55 -0700402static struct platform_driver tegra_rtc_driver = {
Thierry Reding3e483e592019-05-27 12:13:59 +0200403 .probe = tegra_rtc_probe,
Thierry Redinga2d29232019-05-27 12:13:57 +0200404 .remove = tegra_rtc_remove,
405 .shutdown = tegra_rtc_shutdown,
406 .driver = {
407 .name = "tegra_rtc",
Joseph Lo2d79cf82013-01-04 15:34:45 -0800408 .of_match_table = tegra_rtc_dt_match,
Thierry Redinga2d29232019-05-27 12:13:57 +0200409 .pm = &tegra_rtc_pm_ops,
Andrew Chewff859ba2011-03-22 16:34:55 -0700410 },
Andrew Chewff859ba2011-03-22 16:34:55 -0700411};
Thierry Reding3e483e592019-05-27 12:13:59 +0200412module_platform_driver(tegra_rtc_driver);
Andrew Chewff859ba2011-03-22 16:34:55 -0700413
414MODULE_AUTHOR("Jon Mayo <jmayo@nvidia.com>");
415MODULE_DESCRIPTION("driver for Tegra internal RTC");
Andrew Chewff859ba2011-03-22 16:34:55 -0700416MODULE_LICENSE("GPL");