blob: 3b833e02a6575fd4bcaf6ec16b0433a0e9f2b552 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Victor788b1fc2006-06-25 05:48:27 -07002/*
3 * Real Time Clock interface for Linux on Atmel AT91RM9200
4 *
5 * Copyright (C) 2002 Rick Bronson
6 *
7 * Converted to RTC class model by Andrew Victor
8 *
9 * Ported to Linux 2.6 by Steven Scholz
10 * Based on s3c2410-rtc.c Simtec Electronics
11 *
12 * Based on sa1100-rtc.c by Nils Faerber
13 * Based on rtc.c by Paul Gortmaker
Andrew Victor788b1fc2006-06-25 05:48:27 -070014 */
15
Andrew Victor788b1fc2006-06-25 05:48:27 -070016#include <linux/bcd.h>
Alexandre Belloni11f67a82015-07-31 11:39:51 +020017#include <linux/clk.h>
Andrew Victor788b1fc2006-06-25 05:48:27 -070018#include <linux/completion.h>
Alexandre Belloni74000eb2015-07-29 02:01:33 +020019#include <linux/interrupt.h>
20#include <linux/ioctl.h>
Arnd Bergmann14070ad2012-07-04 07:45:16 +000021#include <linux/io.h>
Alexandre Belloni74000eb2015-07-29 02:01:33 +020022#include <linux/kernel.h>
23#include <linux/module.h>
Joachim Eastwood7c1b68d2013-04-29 16:20:15 -070024#include <linux/of_device.h>
Alexandre Belloni74000eb2015-07-29 02:01:33 +020025#include <linux/of.h>
26#include <linux/platform_device.h>
27#include <linux/rtc.h>
28#include <linux/spinlock.h>
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +010029#include <linux/suspend.h>
Alexandre Belloni74000eb2015-07-29 02:01:33 +020030#include <linux/time.h>
Sachin Kamat8ecc0bf2013-07-03 15:05:44 -070031#include <linux/uaccess.h>
Andrew Victorfb0d4ec2008-10-15 22:03:08 -070032
Jean-Christophe PLAGNIOL-VILLARD75984df2012-10-30 08:03:39 +080033#include "rtc-at91rm9200.h"
David Brownelld73e3cd2007-01-05 16:36:25 -080034
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +080035#define at91_rtc_read(field) \
Ben Dooks6da7bb12015-04-16 12:49:32 -070036 readl_relaxed(at91_rtc_regs + field)
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +080037#define at91_rtc_write(field, val) \
Ben Dooks6da7bb12015-04-16 12:49:32 -070038 writel_relaxed((val), at91_rtc_regs + field)
Andrew Victor788b1fc2006-06-25 05:48:27 -070039
Johan Hovoldde645472013-06-12 14:04:53 -070040struct at91_rtc_config {
Johan Hovolde9f08bb2013-06-12 14:04:56 -070041 bool use_shadow_imr;
Johan Hovoldde645472013-06-12 14:04:53 -070042};
43
44static const struct at91_rtc_config *at91_rtc_config;
Andrew Victor788b1fc2006-06-25 05:48:27 -070045static DECLARE_COMPLETION(at91_rtc_updated);
Boris BREZILLON2fe121e2014-06-06 14:36:09 -070046static DECLARE_COMPLETION(at91_rtc_upd_rdy);
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +080047static void __iomem *at91_rtc_regs;
48static int irq;
Johan Hovolde9f08bb2013-06-12 14:04:56 -070049static DEFINE_SPINLOCK(at91_rtc_lock);
50static u32 at91_rtc_shadow_imr;
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +010051static bool suspended;
52static DEFINE_SPINLOCK(suspended_lock);
53static unsigned long cached_events;
54static u32 at91_rtc_imr;
Alexandre Belloni11f67a82015-07-31 11:39:51 +020055static struct clk *sclk;
Andrew Victor788b1fc2006-06-25 05:48:27 -070056
Johan Hovolde304fcd02013-06-12 14:04:55 -070057static void at91_rtc_write_ier(u32 mask)
58{
Johan Hovolde9f08bb2013-06-12 14:04:56 -070059 unsigned long flags;
60
61 spin_lock_irqsave(&at91_rtc_lock, flags);
62 at91_rtc_shadow_imr |= mask;
Johan Hovolde304fcd02013-06-12 14:04:55 -070063 at91_rtc_write(AT91_RTC_IER, mask);
Johan Hovolde9f08bb2013-06-12 14:04:56 -070064 spin_unlock_irqrestore(&at91_rtc_lock, flags);
Johan Hovolde304fcd02013-06-12 14:04:55 -070065}
66
67static void at91_rtc_write_idr(u32 mask)
68{
Johan Hovolde9f08bb2013-06-12 14:04:56 -070069 unsigned long flags;
70
71 spin_lock_irqsave(&at91_rtc_lock, flags);
Johan Hovolde304fcd02013-06-12 14:04:55 -070072 at91_rtc_write(AT91_RTC_IDR, mask);
Johan Hovolde9f08bb2013-06-12 14:04:56 -070073 /*
74 * Register read back (of any RTC-register) needed to make sure
75 * IDR-register write has reached the peripheral before updating
76 * shadow mask.
77 *
78 * Note that there is still a possibility that the mask is updated
79 * before interrupts have actually been disabled in hardware. The only
80 * way to be certain would be to poll the IMR-register, which is is
81 * the very register we are trying to emulate. The register read back
82 * is a reasonable heuristic.
83 */
84 at91_rtc_read(AT91_RTC_SR);
85 at91_rtc_shadow_imr &= ~mask;
86 spin_unlock_irqrestore(&at91_rtc_lock, flags);
Johan Hovolde304fcd02013-06-12 14:04:55 -070087}
88
89static u32 at91_rtc_read_imr(void)
90{
Johan Hovolde9f08bb2013-06-12 14:04:56 -070091 unsigned long flags;
92 u32 mask;
93
94 if (at91_rtc_config->use_shadow_imr) {
95 spin_lock_irqsave(&at91_rtc_lock, flags);
96 mask = at91_rtc_shadow_imr;
97 spin_unlock_irqrestore(&at91_rtc_lock, flags);
98 } else {
99 mask = at91_rtc_read(AT91_RTC_IMR);
100 }
101
102 return mask;
Johan Hovolde304fcd02013-06-12 14:04:55 -0700103}
104
Andrew Victor788b1fc2006-06-25 05:48:27 -0700105/*
106 * Decode time/date into rtc_time structure
107 */
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700108static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
109 struct rtc_time *tm)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700110{
111 unsigned int time, date;
112
113 /* must read twice in case it changes */
114 do {
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800115 time = at91_rtc_read(timereg);
116 date = at91_rtc_read(calreg);
117 } while ((time != at91_rtc_read(timereg)) ||
118 (date != at91_rtc_read(calreg)));
Andrew Victor788b1fc2006-06-25 05:48:27 -0700119
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700120 tm->tm_sec = bcd2bin((time & AT91_RTC_SEC) >> 0);
121 tm->tm_min = bcd2bin((time & AT91_RTC_MIN) >> 8);
122 tm->tm_hour = bcd2bin((time & AT91_RTC_HOUR) >> 16);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700123
124 /*
125 * The Calendar Alarm register does not have a field for
Alexandre Bellonieaa1dc72017-11-10 09:59:31 +0100126 * the year - so these will return an invalid value.
Andrew Victor788b1fc2006-06-25 05:48:27 -0700127 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700128 tm->tm_year = bcd2bin(date & AT91_RTC_CENT) * 100; /* century */
129 tm->tm_year += bcd2bin((date & AT91_RTC_YEAR) >> 8); /* year */
Andrew Victor788b1fc2006-06-25 05:48:27 -0700130
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700131 tm->tm_wday = bcd2bin((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */
132 tm->tm_mon = bcd2bin((date & AT91_RTC_MONTH) >> 16) - 1;
133 tm->tm_mday = bcd2bin((date & AT91_RTC_DATE) >> 24);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700134}
135
136/*
137 * Read current time and date in RTC
138 */
139static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
140{
141 at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
142 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
143 tm->tm_year = tm->tm_year - 1900;
144
Andy Shevchenkod422f882018-12-04 23:23:13 +0200145 dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700146
147 return 0;
148}
149
150/*
151 * Set current time and date in RTC
152 */
153static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
154{
155 unsigned long cr;
156
Andy Shevchenkod422f882018-12-04 23:23:13 +0200157 dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700158
Boris BREZILLON2fe121e2014-06-06 14:36:09 -0700159 wait_for_completion(&at91_rtc_upd_rdy);
160
Andrew Victor788b1fc2006-06-25 05:48:27 -0700161 /* Stop Time/Calendar from counting */
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800162 cr = at91_rtc_read(AT91_RTC_CR);
163 at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700164
Johan Hovolde304fcd02013-06-12 14:04:55 -0700165 at91_rtc_write_ier(AT91_RTC_ACKUPD);
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700166 wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */
Johan Hovolde304fcd02013-06-12 14:04:55 -0700167 at91_rtc_write_idr(AT91_RTC_ACKUPD);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700168
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800169 at91_rtc_write(AT91_RTC_TIMR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700170 bin2bcd(tm->tm_sec) << 0
171 | bin2bcd(tm->tm_min) << 8
172 | bin2bcd(tm->tm_hour) << 16);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700173
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800174 at91_rtc_write(AT91_RTC_CALR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700175 bin2bcd((tm->tm_year + 1900) / 100) /* century */
176 | bin2bcd(tm->tm_year % 100) << 8 /* year */
177 | bin2bcd(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */
178 | bin2bcd(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */
179 | bin2bcd(tm->tm_mday) << 24);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700180
181 /* Restart Time/Calendar */
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800182 cr = at91_rtc_read(AT91_RTC_CR);
Boris BREZILLON2fe121e2014-06-06 14:36:09 -0700183 at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_SECEV);
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800184 at91_rtc_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
Boris BREZILLON2fe121e2014-06-06 14:36:09 -0700185 at91_rtc_write_ier(AT91_RTC_SECEV);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700186
187 return 0;
188}
189
190/*
191 * Read alarm time and date in RTC
192 */
193static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
194{
195 struct rtc_time *tm = &alrm->time;
196
197 at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
Alexandre Bellonieaa1dc72017-11-10 09:59:31 +0100198 tm->tm_year = -1;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700199
Johan Hovolde304fcd02013-06-12 14:04:55 -0700200 alrm->enabled = (at91_rtc_read_imr() & AT91_RTC_ALARM)
David Brownella2db8df2006-12-13 00:35:08 -0800201 ? 1 : 0;
202
Andy Shevchenkod422f882018-12-04 23:23:13 +0200203 dev_dbg(dev, "%s(): %ptR %sabled\n", __func__, tm,
Alexandre Bellonieaa1dc72017-11-10 09:59:31 +0100204 alrm->enabled ? "en" : "dis");
Andrew Victor788b1fc2006-06-25 05:48:27 -0700205
206 return 0;
207}
208
209/*
210 * Set alarm time and date in RTC
211 */
212static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
213{
214 struct rtc_time tm;
215
216 at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, &tm);
217
Linus Pizunskieb3c2272013-12-12 17:12:23 -0800218 tm.tm_mon = alrm->time.tm_mon;
219 tm.tm_mday = alrm->time.tm_mday;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700220 tm.tm_hour = alrm->time.tm_hour;
221 tm.tm_min = alrm->time.tm_min;
222 tm.tm_sec = alrm->time.tm_sec;
223
Johan Hovolde304fcd02013-06-12 14:04:55 -0700224 at91_rtc_write_idr(AT91_RTC_ALARM);
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800225 at91_rtc_write(AT91_RTC_TIMALR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700226 bin2bcd(tm.tm_sec) << 0
227 | bin2bcd(tm.tm_min) << 8
228 | bin2bcd(tm.tm_hour) << 16
Andrew Victor788b1fc2006-06-25 05:48:27 -0700229 | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800230 at91_rtc_write(AT91_RTC_CALALR,
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700231 bin2bcd(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */
232 | bin2bcd(tm.tm_mday) << 24
Andrew Victor788b1fc2006-06-25 05:48:27 -0700233 | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
234
David Brownell449321b2008-07-23 21:30:46 -0700235 if (alrm->enabled) {
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800236 at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
Johan Hovolde304fcd02013-06-12 14:04:55 -0700237 at91_rtc_write_ier(AT91_RTC_ALARM);
David Brownell449321b2008-07-23 21:30:46 -0700238 }
David Brownell5d4675a2007-02-20 13:58:14 -0800239
Andy Shevchenkod422f882018-12-04 23:23:13 +0200240 dev_dbg(dev, "%s(): %ptR\n", __func__, &tm);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700241
242 return 0;
243}
244
John Stultz16380c12011-02-02 17:02:41 -0800245static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
246{
Jingoo Han65882082013-02-21 16:45:28 -0800247 dev_dbg(dev, "%s(): cmd=%08x\n", __func__, enabled);
John Stultz16380c12011-02-02 17:02:41 -0800248
249 if (enabled) {
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800250 at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
Johan Hovolde304fcd02013-06-12 14:04:55 -0700251 at91_rtc_write_ier(AT91_RTC_ALARM);
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200252 } else
Johan Hovolde304fcd02013-06-12 14:04:55 -0700253 at91_rtc_write_idr(AT91_RTC_ALARM);
John Stultz16380c12011-02-02 17:02:41 -0800254
255 return 0;
256}
Andrew Victor788b1fc2006-06-25 05:48:27 -0700257/*
258 * Provide additional RTC information in /proc/driver/rtc
259 */
260static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
261{
Johan Hovolde304fcd02013-06-12 14:04:55 -0700262 unsigned long imr = at91_rtc_read_imr();
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200263
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700264 seq_printf(seq, "update_IRQ\t: %s\n",
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200265 (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700266 seq_printf(seq, "periodic_IRQ\t: %s\n",
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200267 (imr & AT91_RTC_SECEV) ? "yes" : "no");
Andrew Victor788b1fc2006-06-25 05:48:27 -0700268
269 return 0;
270}
271
272/*
273 * IRQ handler for the RTC
274 */
David Howells7d12e782006-10-05 14:55:46 +0100275static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700276{
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700277 struct platform_device *pdev = dev_id;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700278 struct rtc_device *rtc = platform_get_drvdata(pdev);
279 unsigned int rtsr;
280 unsigned long events = 0;
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100281 int ret = IRQ_NONE;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700282
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100283 spin_lock(&suspended_lock);
Johan Hovolde304fcd02013-06-12 14:04:55 -0700284 rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read_imr();
Andrew Victor788b1fc2006-06-25 05:48:27 -0700285 if (rtsr) { /* this interrupt is shared! Is it ours? */
286 if (rtsr & AT91_RTC_ALARM)
287 events |= (RTC_AF | RTC_IRQF);
Boris BREZILLON2fe121e2014-06-06 14:36:09 -0700288 if (rtsr & AT91_RTC_SECEV) {
289 complete(&at91_rtc_upd_rdy);
290 at91_rtc_write_idr(AT91_RTC_SECEV);
291 }
Andrew Victor788b1fc2006-06-25 05:48:27 -0700292 if (rtsr & AT91_RTC_ACKUPD)
293 complete(&at91_rtc_updated);
294
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800295 at91_rtc_write(AT91_RTC_SCCR, rtsr); /* clear status reg */
Andrew Victor788b1fc2006-06-25 05:48:27 -0700296
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100297 if (!suspended) {
298 rtc_update_irq(rtc, 1, events);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700299
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100300 dev_dbg(&pdev->dev, "%s(): num=%ld, events=0x%02lx\n",
301 __func__, events >> 8, events & 0x000000FF);
302 } else {
303 cached_events |= events;
304 at91_rtc_write_idr(at91_rtc_imr);
305 pm_system_wakeup();
306 }
Andrew Victor788b1fc2006-06-25 05:48:27 -0700307
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100308 ret = IRQ_HANDLED;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700309 }
Dan Carpenter88601682015-03-17 16:38:10 +0100310 spin_unlock(&suspended_lock);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100311
312 return ret;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700313}
314
Johan Hovoldde645472013-06-12 14:04:53 -0700315static const struct at91_rtc_config at91rm9200_config = {
316};
317
Johan Hovoldbba00e52013-06-12 14:04:57 -0700318static const struct at91_rtc_config at91sam9x5_config = {
319 .use_shadow_imr = true,
320};
321
Johan Hovoldde645472013-06-12 14:04:53 -0700322static const struct of_device_id at91_rtc_dt_ids[] = {
323 {
324 .compatible = "atmel,at91rm9200-rtc",
325 .data = &at91rm9200_config,
326 }, {
Johan Hovoldbba00e52013-06-12 14:04:57 -0700327 .compatible = "atmel,at91sam9x5-rtc",
328 .data = &at91sam9x5_config,
329 }, {
Johan Hovoldde645472013-06-12 14:04:53 -0700330 /* sentinel */
331 }
332};
333MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
Johan Hovoldde645472013-06-12 14:04:53 -0700334
David Brownellff8371a2006-09-30 23:28:17 -0700335static const struct rtc_class_ops at91_rtc_ops = {
Andrew Victor788b1fc2006-06-25 05:48:27 -0700336 .read_time = at91_rtc_readtime,
337 .set_time = at91_rtc_settime,
338 .read_alarm = at91_rtc_readalarm,
339 .set_alarm = at91_rtc_setalarm,
340 .proc = at91_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800341 .alarm_irq_enable = at91_rtc_alarm_irq_enable,
Andrew Victor788b1fc2006-06-25 05:48:27 -0700342};
343
344/*
345 * Initialize and install RTC driver
346 */
347static int __init at91_rtc_probe(struct platform_device *pdev)
348{
349 struct rtc_device *rtc;
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800350 struct resource *regs;
351 int ret = 0;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700352
Claudiu Beznea288d9cf2019-09-26 15:15:32 +0300353 at91_rtc_config = of_device_get_match_data(&pdev->dev);
Johan Hovoldde645472013-06-12 14:04:53 -0700354 if (!at91_rtc_config)
355 return -ENODEV;
356
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800357 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
358 if (!regs) {
359 dev_err(&pdev->dev, "no mmio resource defined\n");
360 return -ENXIO;
361 }
362
363 irq = platform_get_irq(pdev, 0);
Stephen Boydfaac9102019-07-30 11:15:39 -0700364 if (irq < 0)
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800365 return -ENXIO;
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800366
Sachin Kamatf3766252013-11-12 15:10:29 -0800367 at91_rtc_regs = devm_ioremap(&pdev->dev, regs->start,
368 resource_size(regs));
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800369 if (!at91_rtc_regs) {
370 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
371 return -ENOMEM;
372 }
373
Alexandre Belloni735ae202017-07-06 11:42:01 +0200374 rtc = devm_rtc_allocate_device(&pdev->dev);
375 if (IS_ERR(rtc))
376 return PTR_ERR(rtc);
377 platform_set_drvdata(pdev, rtc);
378
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200379 sclk = devm_clk_get(&pdev->dev, NULL);
380 if (IS_ERR(sclk))
381 return PTR_ERR(sclk);
382
383 ret = clk_prepare_enable(sclk);
384 if (ret) {
385 dev_err(&pdev->dev, "Could not enable slow clock\n");
386 return ret;
387 }
388
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800389 at91_rtc_write(AT91_RTC_CR, 0);
390 at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */
Andrew Victor788b1fc2006-06-25 05:48:27 -0700391
392 /* Disable all interrupts */
Johan Hovolde304fcd02013-06-12 14:04:55 -0700393 at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700394 AT91_RTC_SECEV | AT91_RTC_TIMEV |
395 AT91_RTC_CALEV);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700396
Sachin Kamatf3766252013-11-12 15:10:29 -0800397 ret = devm_request_irq(&pdev->dev, irq, at91_rtc_interrupt,
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100398 IRQF_SHARED | IRQF_COND_SUSPEND,
399 "at91_rtc", pdev);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700400 if (ret) {
Jingoo Han65882082013-02-21 16:45:28 -0800401 dev_err(&pdev->dev, "IRQ %d already in use.\n", irq);
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200402 goto err_clk;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700403 }
404
David Brownell5d4675a2007-02-20 13:58:14 -0800405 /* cpu init code should really have flagged this device as
406 * being wake-capable; if it didn't, do that here.
407 */
408 if (!device_can_wakeup(&pdev->dev))
409 device_init_wakeup(&pdev->dev, 1);
410
Alexandre Belloni735ae202017-07-06 11:42:01 +0200411 rtc->ops = &at91_rtc_ops;
Alexandre Belloni6c78a872018-05-17 22:17:28 +0200412 rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
413 rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Belloni735ae202017-07-06 11:42:01 +0200414 ret = rtc_register_device(rtc);
415 if (ret)
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200416 goto err_clk;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700417
Boris BREZILLON2fe121e2014-06-06 14:36:09 -0700418 /* enable SECEV interrupt in order to initialize at91_rtc_upd_rdy
419 * completion.
420 */
421 at91_rtc_write_ier(AT91_RTC_SECEV);
422
Jingoo Han65882082013-02-21 16:45:28 -0800423 dev_info(&pdev->dev, "AT91 Real Time Clock driver.\n");
Andrew Victor788b1fc2006-06-25 05:48:27 -0700424 return 0;
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200425
426err_clk:
427 clk_disable_unprepare(sclk);
428
429 return ret;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700430}
431
432/*
433 * Disable and remove the RTC driver
434 */
David Brownell5d4675a2007-02-20 13:58:14 -0800435static int __exit at91_rtc_remove(struct platform_device *pdev)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700436{
Andrew Victor788b1fc2006-06-25 05:48:27 -0700437 /* Disable all interrupts */
Johan Hovolde304fcd02013-06-12 14:04:55 -0700438 at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700439 AT91_RTC_SECEV | AT91_RTC_TIMEV |
440 AT91_RTC_CALEV);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700441
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200442 clk_disable_unprepare(sclk);
443
Andrew Victor788b1fc2006-06-25 05:48:27 -0700444 return 0;
445}
446
Johan Hovold51a0d032013-11-21 14:32:04 -0800447static void at91_rtc_shutdown(struct platform_device *pdev)
448{
449 /* Disable all interrupts */
450 at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
451 AT91_RTC_SECEV | AT91_RTC_TIMEV |
452 AT91_RTC_CALEV);
453}
454
Jingoo Han6975a9c2013-04-29 16:19:56 -0700455#ifdef CONFIG_PM_SLEEP
Andrew Victor788b1fc2006-06-25 05:48:27 -0700456
457/* AT91RM9200 RTC Power management control */
458
David Brownelldac94d92009-09-22 16:46:31 -0700459static int at91_rtc_suspend(struct device *dev)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700460{
David Brownell90b4d642006-09-30 23:28:17 -0700461 /* this IRQ is shared with DBGU and other hardware which isn't
462 * necessarily doing PM like we are...
463 */
Wenyou Yang921372b2015-10-12 16:39:23 +0800464 at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
465
Johan Hovolde304fcd02013-06-12 14:04:55 -0700466 at91_rtc_imr = at91_rtc_read_imr()
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200467 & (AT91_RTC_ALARM|AT91_RTC_SECEV);
468 if (at91_rtc_imr) {
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100469 if (device_may_wakeup(dev)) {
470 unsigned long flags;
471
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800472 enable_irq_wake(irq);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100473
474 spin_lock_irqsave(&suspended_lock, flags);
475 suspended = true;
476 spin_unlock_irqrestore(&suspended_lock, flags);
477 } else {
Johan Hovolde304fcd02013-06-12 14:04:55 -0700478 at91_rtc_write_idr(at91_rtc_imr);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100479 }
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200480 }
Andrew Victor788b1fc2006-06-25 05:48:27 -0700481 return 0;
482}
483
David Brownelldac94d92009-09-22 16:46:31 -0700484static int at91_rtc_resume(struct device *dev)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700485{
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100486 struct rtc_device *rtc = dev_get_drvdata(dev);
487
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200488 if (at91_rtc_imr) {
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100489 if (device_may_wakeup(dev)) {
490 unsigned long flags;
491
492 spin_lock_irqsave(&suspended_lock, flags);
493
494 if (cached_events) {
495 rtc_update_irq(rtc, 1, cached_events);
496 cached_events = 0;
497 }
498
499 suspended = false;
500 spin_unlock_irqrestore(&suspended_lock, flags);
501
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800502 disable_irq_wake(irq);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100503 }
504 at91_rtc_write_ier(at91_rtc_imr);
David Brownell90b4d642006-09-30 23:28:17 -0700505 }
Andrew Victor788b1fc2006-06-25 05:48:27 -0700506 return 0;
507}
Andrew Victor788b1fc2006-06-25 05:48:27 -0700508#endif
509
Jingoo Han6975a9c2013-04-29 16:19:56 -0700510static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
511
Andrew Victor788b1fc2006-06-25 05:48:27 -0700512static struct platform_driver at91_rtc_driver = {
David Brownell5d4675a2007-02-20 13:58:14 -0800513 .remove = __exit_p(at91_rtc_remove),
Johan Hovold51a0d032013-11-21 14:32:04 -0800514 .shutdown = at91_rtc_shutdown,
Andrew Victor788b1fc2006-06-25 05:48:27 -0700515 .driver = {
516 .name = "at91_rtc",
Jingoo Han6975a9c2013-04-29 16:19:56 -0700517 .pm = &at91_rtc_pm_ops,
Joachim Eastwood7c1b68d2013-04-29 16:20:15 -0700518 .of_match_table = of_match_ptr(at91_rtc_dt_ids),
Andrew Victor788b1fc2006-06-25 05:48:27 -0700519 },
520};
521
Jingoo Hanac369602013-04-29 16:18:36 -0700522module_platform_driver_probe(at91_rtc_driver, at91_rtc_probe);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700523
524MODULE_AUTHOR("Rick Bronson");
525MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
526MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700527MODULE_ALIAS("platform:at91_rtc");