blob: 8fa1b3febf69d42ce76efa50859c67aea8f77290 [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;
106 u32 sec, msec;
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
Thierry Redingc6af5612019-05-27 12:13:58 +0200114 msec = readl(info->base + TEGRA_RTC_REG_MILLI_SECONDS);
115 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;
280 struct resource *res;
281 int ret;
282
Thierry Redinga2d29232019-05-27 12:13:57 +0200283 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Andrew Chewff859ba2011-03-22 16:34:55 -0700284 if (!info)
285 return -ENOMEM;
286
287 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingc6af5612019-05-27 12:13:58 +0200288 info->base = devm_ioremap_resource(&pdev->dev, res);
289 if (IS_ERR(info->base))
290 return PTR_ERR(info->base);
Andrew Chewff859ba2011-03-22 16:34:55 -0700291
Thierry Redingfe0b5ce2018-09-21 12:12:09 +0200292 ret = platform_get_irq(pdev, 0);
293 if (ret <= 0) {
294 dev_err(&pdev->dev, "failed to get platform IRQ: %d\n", ret);
295 return ret;
296 }
297
Thierry Redingc6af5612019-05-27 12:13:58 +0200298 info->irq = ret;
Andrew Chewff859ba2011-03-22 16:34:55 -0700299
Thierry Redingc6af5612019-05-27 12:13:58 +0200300 info->rtc = devm_rtc_allocate_device(&pdev->dev);
301 if (IS_ERR(info->rtc))
302 return PTR_ERR(info->rtc);
Alexandre Bellonie1089802019-04-07 23:16:44 +0200303
Thierry Redingc6af5612019-05-27 12:13:58 +0200304 info->rtc->ops = &tegra_rtc_ops;
305 info->rtc->range_max = U32_MAX;
Alexandre Bellonie1089802019-04-07 23:16:44 +0200306
Thierry Reding5fa40862017-01-12 17:07:43 +0100307 info->clk = devm_clk_get(&pdev->dev, NULL);
308 if (IS_ERR(info->clk))
309 return PTR_ERR(info->clk);
310
311 ret = clk_prepare_enable(info->clk);
312 if (ret < 0)
313 return ret;
314
Thierry Redinga2d29232019-05-27 12:13:57 +0200315 /* set context info */
Andrew Chewff859ba2011-03-22 16:34:55 -0700316 info->pdev = pdev;
Thierry Redingc6af5612019-05-27 12:13:58 +0200317 spin_lock_init(&info->lock);
Andrew Chewff859ba2011-03-22 16:34:55 -0700318
319 platform_set_drvdata(pdev, info);
320
Thierry Redinga2d29232019-05-27 12:13:57 +0200321 /* clear out the hardware */
Thierry Redingc6af5612019-05-27 12:13:58 +0200322 writel(0, info->base + TEGRA_RTC_REG_SECONDS_ALARM0);
323 writel(0xffffffff, info->base + TEGRA_RTC_REG_INTR_STATUS);
324 writel(0, info->base + TEGRA_RTC_REG_INTR_MASK);
Andrew Chewff859ba2011-03-22 16:34:55 -0700325
326 device_init_wakeup(&pdev->dev, 1);
327
Thierry Redingc6af5612019-05-27 12:13:58 +0200328 ret = devm_request_irq(&pdev->dev, info->irq, tegra_rtc_irq_handler,
329 IRQF_TRIGGER_HIGH, dev_name(&pdev->dev),
330 &pdev->dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700331 if (ret) {
Thierry Redinga2d29232019-05-27 12:13:57 +0200332 dev_err(&pdev->dev, "failed to request interrupt: %d\n", ret);
Thierry Reding5fa40862017-01-12 17:07:43 +0100333 goto disable_clk;
Andrew Chewff859ba2011-03-22 16:34:55 -0700334 }
335
Thierry Redingc6af5612019-05-27 12:13:58 +0200336 ret = rtc_register_device(info->rtc);
Alexandre Bellonie1089802019-04-07 23:16:44 +0200337 if (ret) {
Thierry Redinga2d29232019-05-27 12:13:57 +0200338 dev_err(&pdev->dev, "failed to register device: %d\n", ret);
Alexandre Bellonie1089802019-04-07 23:16:44 +0200339 goto disable_clk;
340 }
341
Andrew Chewff859ba2011-03-22 16:34:55 -0700342 dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
343
344 return 0;
Thierry Reding5fa40862017-01-12 17:07:43 +0100345
346disable_clk:
347 clk_disable_unprepare(info->clk);
348 return ret;
349}
350
351static int tegra_rtc_remove(struct platform_device *pdev)
352{
353 struct tegra_rtc_info *info = platform_get_drvdata(pdev);
354
355 clk_disable_unprepare(info->clk);
356
357 return 0;
Andrew Chewff859ba2011-03-22 16:34:55 -0700358}
359
Laxman Dewangan38a62762013-04-29 16:19:21 -0700360#ifdef CONFIG_PM_SLEEP
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700361static int tegra_rtc_suspend(struct device *dev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700362{
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700363 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700364
365 tegra_rtc_wait_while_busy(dev);
366
Thierry Redinga2d29232019-05-27 12:13:57 +0200367 /* only use ALARM0 as a wake source */
Thierry Redingc6af5612019-05-27 12:13:58 +0200368 writel(0xffffffff, info->base + TEGRA_RTC_REG_INTR_STATUS);
Andrew Chewff859ba2011-03-22 16:34:55 -0700369 writel(TEGRA_RTC_INTR_STATUS_SEC_ALARM0,
Thierry Redingc6af5612019-05-27 12:13:58 +0200370 info->base + TEGRA_RTC_REG_INTR_MASK);
Andrew Chewff859ba2011-03-22 16:34:55 -0700371
372 dev_vdbg(dev, "alarm sec = %d\n",
Thierry Redingc6af5612019-05-27 12:13:58 +0200373 readl(info->base + TEGRA_RTC_REG_SECONDS_ALARM0));
Andrew Chewff859ba2011-03-22 16:34:55 -0700374
Thierry Redinga2d29232019-05-27 12:13:57 +0200375 dev_vdbg(dev, "Suspend (device_may_wakeup=%d) IRQ:%d\n",
Thierry Redingc6af5612019-05-27 12:13:58 +0200376 device_may_wakeup(dev), info->irq);
Andrew Chewff859ba2011-03-22 16:34:55 -0700377
Thierry Redinga2d29232019-05-27 12:13:57 +0200378 /* leave the alarms on as a wake source */
Andrew Chewff859ba2011-03-22 16:34:55 -0700379 if (device_may_wakeup(dev))
Thierry Redingc6af5612019-05-27 12:13:58 +0200380 enable_irq_wake(info->irq);
Andrew Chewff859ba2011-03-22 16:34:55 -0700381
382 return 0;
383}
384
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700385static int tegra_rtc_resume(struct device *dev)
Andrew Chewff859ba2011-03-22 16:34:55 -0700386{
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700387 struct tegra_rtc_info *info = dev_get_drvdata(dev);
Andrew Chewff859ba2011-03-22 16:34:55 -0700388
389 dev_vdbg(dev, "Resume (device_may_wakeup=%d)\n",
Thierry Redinga2d29232019-05-27 12:13:57 +0200390 device_may_wakeup(dev));
391
392 /* alarms were left on as a wake source, turn them off */
Andrew Chewff859ba2011-03-22 16:34:55 -0700393 if (device_may_wakeup(dev))
Thierry Redingc6af5612019-05-27 12:13:58 +0200394 disable_irq_wake(info->irq);
Andrew Chewff859ba2011-03-22 16:34:55 -0700395
396 return 0;
397}
398#endif
399
Laxman Dewangan3443ad02013-04-29 16:19:23 -0700400static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
401
Andrew Chewff859ba2011-03-22 16:34:55 -0700402static void tegra_rtc_shutdown(struct platform_device *pdev)
403{
Thierry Redinga2d29232019-05-27 12:13:57 +0200404 dev_vdbg(&pdev->dev, "disabling interrupts\n");
Andrew Chewff859ba2011-03-22 16:34:55 -0700405 tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
406}
407
Andrew Chewff859ba2011-03-22 16:34:55 -0700408static struct platform_driver tegra_rtc_driver = {
Thierry Reding3e483e592019-05-27 12:13:59 +0200409 .probe = tegra_rtc_probe,
Thierry Redinga2d29232019-05-27 12:13:57 +0200410 .remove = tegra_rtc_remove,
411 .shutdown = tegra_rtc_shutdown,
412 .driver = {
413 .name = "tegra_rtc",
Joseph Lo2d79cf82013-01-04 15:34:45 -0800414 .of_match_table = tegra_rtc_dt_match,
Thierry Redinga2d29232019-05-27 12:13:57 +0200415 .pm = &tegra_rtc_pm_ops,
Andrew Chewff859ba2011-03-22 16:34:55 -0700416 },
Andrew Chewff859ba2011-03-22 16:34:55 -0700417};
Thierry Reding3e483e592019-05-27 12:13:59 +0200418module_platform_driver(tegra_rtc_driver);
Andrew Chewff859ba2011-03-22 16:34:55 -0700419
420MODULE_AUTHOR("Jon Mayo <jmayo@nvidia.com>");
421MODULE_DESCRIPTION("driver for Tegra internal RTC");
Andrew Chewff859ba2011-03-22 16:34:55 -0700422MODULE_LICENSE("GPL");