blob: d119c6e6353e7fafca53808aa9b2f4da87686781 [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 -0700322#ifdef CONFIG_OF
323static const struct of_device_id at91_rtc_dt_ids[] = {
324 {
325 .compatible = "atmel,at91rm9200-rtc",
326 .data = &at91rm9200_config,
327 }, {
Johan Hovoldbba00e52013-06-12 14:04:57 -0700328 .compatible = "atmel,at91sam9x5-rtc",
329 .data = &at91sam9x5_config,
330 }, {
Johan Hovoldde645472013-06-12 14:04:53 -0700331 /* sentinel */
332 }
333};
334MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
335#endif
336
337static const struct at91_rtc_config *
338at91_rtc_get_config(struct platform_device *pdev)
339{
340 const struct of_device_id *match;
341
342 if (pdev->dev.of_node) {
343 match = of_match_node(at91_rtc_dt_ids, pdev->dev.of_node);
344 if (!match)
345 return NULL;
346 return (const struct at91_rtc_config *)match->data;
347 }
348
349 return &at91rm9200_config;
350}
351
David Brownellff8371a2006-09-30 23:28:17 -0700352static const struct rtc_class_ops at91_rtc_ops = {
Andrew Victor788b1fc2006-06-25 05:48:27 -0700353 .read_time = at91_rtc_readtime,
354 .set_time = at91_rtc_settime,
355 .read_alarm = at91_rtc_readalarm,
356 .set_alarm = at91_rtc_setalarm,
357 .proc = at91_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800358 .alarm_irq_enable = at91_rtc_alarm_irq_enable,
Andrew Victor788b1fc2006-06-25 05:48:27 -0700359};
360
361/*
362 * Initialize and install RTC driver
363 */
364static int __init at91_rtc_probe(struct platform_device *pdev)
365{
366 struct rtc_device *rtc;
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800367 struct resource *regs;
368 int ret = 0;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700369
Johan Hovoldde645472013-06-12 14:04:53 -0700370 at91_rtc_config = at91_rtc_get_config(pdev);
371 if (!at91_rtc_config)
372 return -ENODEV;
373
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800374 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
375 if (!regs) {
376 dev_err(&pdev->dev, "no mmio resource defined\n");
377 return -ENXIO;
378 }
379
380 irq = platform_get_irq(pdev, 0);
Stephen Boydfaac9102019-07-30 11:15:39 -0700381 if (irq < 0)
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800382 return -ENXIO;
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800383
Sachin Kamatf3766252013-11-12 15:10:29 -0800384 at91_rtc_regs = devm_ioremap(&pdev->dev, regs->start,
385 resource_size(regs));
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800386 if (!at91_rtc_regs) {
387 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
388 return -ENOMEM;
389 }
390
Alexandre Belloni735ae202017-07-06 11:42:01 +0200391 rtc = devm_rtc_allocate_device(&pdev->dev);
392 if (IS_ERR(rtc))
393 return PTR_ERR(rtc);
394 platform_set_drvdata(pdev, rtc);
395
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200396 sclk = devm_clk_get(&pdev->dev, NULL);
397 if (IS_ERR(sclk))
398 return PTR_ERR(sclk);
399
400 ret = clk_prepare_enable(sclk);
401 if (ret) {
402 dev_err(&pdev->dev, "Could not enable slow clock\n");
403 return ret;
404 }
405
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800406 at91_rtc_write(AT91_RTC_CR, 0);
407 at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */
Andrew Victor788b1fc2006-06-25 05:48:27 -0700408
409 /* Disable all interrupts */
Johan Hovolde304fcd02013-06-12 14:04:55 -0700410 at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700411 AT91_RTC_SECEV | AT91_RTC_TIMEV |
412 AT91_RTC_CALEV);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700413
Sachin Kamatf3766252013-11-12 15:10:29 -0800414 ret = devm_request_irq(&pdev->dev, irq, at91_rtc_interrupt,
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100415 IRQF_SHARED | IRQF_COND_SUSPEND,
416 "at91_rtc", pdev);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700417 if (ret) {
Jingoo Han65882082013-02-21 16:45:28 -0800418 dev_err(&pdev->dev, "IRQ %d already in use.\n", irq);
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200419 goto err_clk;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700420 }
421
David Brownell5d4675a2007-02-20 13:58:14 -0800422 /* cpu init code should really have flagged this device as
423 * being wake-capable; if it didn't, do that here.
424 */
425 if (!device_can_wakeup(&pdev->dev))
426 device_init_wakeup(&pdev->dev, 1);
427
Alexandre Belloni735ae202017-07-06 11:42:01 +0200428 rtc->ops = &at91_rtc_ops;
Alexandre Belloni6c78a872018-05-17 22:17:28 +0200429 rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
430 rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Belloni735ae202017-07-06 11:42:01 +0200431 ret = rtc_register_device(rtc);
432 if (ret)
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200433 goto err_clk;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700434
Boris BREZILLON2fe121e2014-06-06 14:36:09 -0700435 /* enable SECEV interrupt in order to initialize at91_rtc_upd_rdy
436 * completion.
437 */
438 at91_rtc_write_ier(AT91_RTC_SECEV);
439
Jingoo Han65882082013-02-21 16:45:28 -0800440 dev_info(&pdev->dev, "AT91 Real Time Clock driver.\n");
Andrew Victor788b1fc2006-06-25 05:48:27 -0700441 return 0;
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200442
443err_clk:
444 clk_disable_unprepare(sclk);
445
446 return ret;
Andrew Victor788b1fc2006-06-25 05:48:27 -0700447}
448
449/*
450 * Disable and remove the RTC driver
451 */
David Brownell5d4675a2007-02-20 13:58:14 -0800452static int __exit at91_rtc_remove(struct platform_device *pdev)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700453{
Andrew Victor788b1fc2006-06-25 05:48:27 -0700454 /* Disable all interrupts */
Johan Hovolde304fcd02013-06-12 14:04:55 -0700455 at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
Andrew Mortone7a8bb12006-06-25 05:48:27 -0700456 AT91_RTC_SECEV | AT91_RTC_TIMEV |
457 AT91_RTC_CALEV);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700458
Alexandre Belloni11f67a82015-07-31 11:39:51 +0200459 clk_disable_unprepare(sclk);
460
Andrew Victor788b1fc2006-06-25 05:48:27 -0700461 return 0;
462}
463
Johan Hovold51a0d032013-11-21 14:32:04 -0800464static void at91_rtc_shutdown(struct platform_device *pdev)
465{
466 /* Disable all interrupts */
467 at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
468 AT91_RTC_SECEV | AT91_RTC_TIMEV |
469 AT91_RTC_CALEV);
470}
471
Jingoo Han6975a9c2013-04-29 16:19:56 -0700472#ifdef CONFIG_PM_SLEEP
Andrew Victor788b1fc2006-06-25 05:48:27 -0700473
474/* AT91RM9200 RTC Power management control */
475
David Brownelldac94d92009-09-22 16:46:31 -0700476static int at91_rtc_suspend(struct device *dev)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700477{
David Brownell90b4d642006-09-30 23:28:17 -0700478 /* this IRQ is shared with DBGU and other hardware which isn't
479 * necessarily doing PM like we are...
480 */
Wenyou Yang921372b2015-10-12 16:39:23 +0800481 at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
482
Johan Hovolde304fcd02013-06-12 14:04:55 -0700483 at91_rtc_imr = at91_rtc_read_imr()
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200484 & (AT91_RTC_ALARM|AT91_RTC_SECEV);
485 if (at91_rtc_imr) {
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100486 if (device_may_wakeup(dev)) {
487 unsigned long flags;
488
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800489 enable_irq_wake(irq);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100490
491 spin_lock_irqsave(&suspended_lock, flags);
492 suspended = true;
493 spin_unlock_irqrestore(&suspended_lock, flags);
494 } else {
Johan Hovolde304fcd02013-06-12 14:04:55 -0700495 at91_rtc_write_idr(at91_rtc_imr);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100496 }
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200497 }
Andrew Victor788b1fc2006-06-25 05:48:27 -0700498 return 0;
499}
500
David Brownelldac94d92009-09-22 16:46:31 -0700501static int at91_rtc_resume(struct device *dev)
Andrew Victor788b1fc2006-06-25 05:48:27 -0700502{
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100503 struct rtc_device *rtc = dev_get_drvdata(dev);
504
Johan Hovolde24b0bf2013-04-05 18:16:34 +0200505 if (at91_rtc_imr) {
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100506 if (device_may_wakeup(dev)) {
507 unsigned long flags;
508
509 spin_lock_irqsave(&suspended_lock, flags);
510
511 if (cached_events) {
512 rtc_update_irq(rtc, 1, cached_events);
513 cached_events = 0;
514 }
515
516 suspended = false;
517 spin_unlock_irqrestore(&suspended_lock, flags);
518
Jean-Christophe PLAGNIOL-VILLARDd28bdfc2011-11-14 14:24:53 +0800519 disable_irq_wake(irq);
Boris BREZILLONdd1f1f32015-03-02 10:18:15 +0100520 }
521 at91_rtc_write_ier(at91_rtc_imr);
David Brownell90b4d642006-09-30 23:28:17 -0700522 }
Andrew Victor788b1fc2006-06-25 05:48:27 -0700523 return 0;
524}
Andrew Victor788b1fc2006-06-25 05:48:27 -0700525#endif
526
Jingoo Han6975a9c2013-04-29 16:19:56 -0700527static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
528
Andrew Victor788b1fc2006-06-25 05:48:27 -0700529static struct platform_driver at91_rtc_driver = {
David Brownell5d4675a2007-02-20 13:58:14 -0800530 .remove = __exit_p(at91_rtc_remove),
Johan Hovold51a0d032013-11-21 14:32:04 -0800531 .shutdown = at91_rtc_shutdown,
Andrew Victor788b1fc2006-06-25 05:48:27 -0700532 .driver = {
533 .name = "at91_rtc",
Jingoo Han6975a9c2013-04-29 16:19:56 -0700534 .pm = &at91_rtc_pm_ops,
Joachim Eastwood7c1b68d2013-04-29 16:20:15 -0700535 .of_match_table = of_match_ptr(at91_rtc_dt_ids),
Andrew Victor788b1fc2006-06-25 05:48:27 -0700536 },
537};
538
Jingoo Hanac369602013-04-29 16:18:36 -0700539module_platform_driver_probe(at91_rtc_driver, at91_rtc_probe);
Andrew Victor788b1fc2006-06-25 05:48:27 -0700540
541MODULE_AUTHOR("Rick Bronson");
542MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
543MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700544MODULE_ALIAS("platform:at91_rtc");