18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	Real Time Clock interface for Linux on Atmel AT91RM9200
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	Copyright (C) 2002 Rick Bronson
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *	Converted to RTC class model by Andrew Victor
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *	Ported to Linux 2.6 by Steven Scholz
108c2ecf20Sopenharmony_ci *	Based on s3c2410-rtc.c Simtec Electronics
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *	Based on sa1100-rtc.c by Nils Faerber
138c2ecf20Sopenharmony_ci *	Based on rtc.c by Paul Gortmaker
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/bcd.h>
178c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
188c2ecf20Sopenharmony_ci#include <linux/clk.h>
198c2ecf20Sopenharmony_ci#include <linux/completion.h>
208c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
218c2ecf20Sopenharmony_ci#include <linux/ioctl.h>
228c2ecf20Sopenharmony_ci#include <linux/io.h>
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci#include <linux/of_device.h>
268c2ecf20Sopenharmony_ci#include <linux/of.h>
278c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
288c2ecf20Sopenharmony_ci#include <linux/rtc.h>
298c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
308c2ecf20Sopenharmony_ci#include <linux/suspend.h>
318c2ecf20Sopenharmony_ci#include <linux/time.h>
328c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define	AT91_RTC_CR		0x00			/* Control Register */
358c2ecf20Sopenharmony_ci#define		AT91_RTC_UPDTIM		BIT(0)		/* Update Request Time Register */
368c2ecf20Sopenharmony_ci#define		AT91_RTC_UPDCAL		BIT(1)		/* Update Request Calendar Register */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define	AT91_RTC_MR		0x04			/* Mode Register */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define	AT91_RTC_TIMR		0x08			/* Time Register */
418c2ecf20Sopenharmony_ci#define		AT91_RTC_SEC		GENMASK(6, 0)	/* Current Second */
428c2ecf20Sopenharmony_ci#define		AT91_RTC_MIN		GENMASK(14, 8)	/* Current Minute */
438c2ecf20Sopenharmony_ci#define		AT91_RTC_HOUR		GENMASK(21, 16)	/* Current Hour */
448c2ecf20Sopenharmony_ci#define		AT91_RTC_AMPM		BIT(22)		/* Ante Meridiem Post Meridiem Indicator */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define	AT91_RTC_CALR		0x0c			/* Calendar Register */
478c2ecf20Sopenharmony_ci#define		AT91_RTC_CENT		GENMASK(6, 0)	/* Current Century */
488c2ecf20Sopenharmony_ci#define		AT91_RTC_YEAR		GENMASK(15, 8)	/* Current Year */
498c2ecf20Sopenharmony_ci#define		AT91_RTC_MONTH		GENMASK(20, 16)	/* Current Month */
508c2ecf20Sopenharmony_ci#define		AT91_RTC_DAY		GENMASK(23, 21)	/* Current Day */
518c2ecf20Sopenharmony_ci#define		AT91_RTC_DATE		GENMASK(29, 24)	/* Current Date */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define	AT91_RTC_TIMALR		0x10			/* Time Alarm Register */
548c2ecf20Sopenharmony_ci#define		AT91_RTC_SECEN		BIT(7)		/* Second Alarm Enable */
558c2ecf20Sopenharmony_ci#define		AT91_RTC_MINEN		BIT(15)		/* Minute Alarm Enable */
568c2ecf20Sopenharmony_ci#define		AT91_RTC_HOUREN		BIT(23)		/* Hour Alarm Enable */
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define	AT91_RTC_CALALR		0x14			/* Calendar Alarm Register */
598c2ecf20Sopenharmony_ci#define		AT91_RTC_MTHEN		BIT(23)		/* Month Alarm Enable */
608c2ecf20Sopenharmony_ci#define		AT91_RTC_DATEEN		BIT(31)		/* Date Alarm Enable */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define	AT91_RTC_SR		0x18			/* Status Register */
638c2ecf20Sopenharmony_ci#define		AT91_RTC_ACKUPD		BIT(0)		/* Acknowledge for Update */
648c2ecf20Sopenharmony_ci#define		AT91_RTC_ALARM		BIT(1)		/* Alarm Flag */
658c2ecf20Sopenharmony_ci#define		AT91_RTC_SECEV		BIT(2)		/* Second Event */
668c2ecf20Sopenharmony_ci#define		AT91_RTC_TIMEV		BIT(3)		/* Time Event */
678c2ecf20Sopenharmony_ci#define		AT91_RTC_CALEV		BIT(4)		/* Calendar Event */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define	AT91_RTC_SCCR		0x1c			/* Status Clear Command Register */
708c2ecf20Sopenharmony_ci#define	AT91_RTC_IER		0x20			/* Interrupt Enable Register */
718c2ecf20Sopenharmony_ci#define	AT91_RTC_IDR		0x24			/* Interrupt Disable Register */
728c2ecf20Sopenharmony_ci#define	AT91_RTC_IMR		0x28			/* Interrupt Mask Register */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define	AT91_RTC_VER		0x2c			/* Valid Entry Register */
758c2ecf20Sopenharmony_ci#define		AT91_RTC_NVTIM		BIT(0)		/* Non valid Time */
768c2ecf20Sopenharmony_ci#define		AT91_RTC_NVCAL		BIT(1)		/* Non valid Calendar */
778c2ecf20Sopenharmony_ci#define		AT91_RTC_NVTIMALR	BIT(2)		/* Non valid Time Alarm */
788c2ecf20Sopenharmony_ci#define		AT91_RTC_NVCALALR	BIT(3)		/* Non valid Calendar Alarm */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define at91_rtc_read(field) \
818c2ecf20Sopenharmony_ci	readl_relaxed(at91_rtc_regs + field)
828c2ecf20Sopenharmony_ci#define at91_rtc_write(field, val) \
838c2ecf20Sopenharmony_ci	writel_relaxed((val), at91_rtc_regs + field)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistruct at91_rtc_config {
868c2ecf20Sopenharmony_ci	bool use_shadow_imr;
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic const struct at91_rtc_config *at91_rtc_config;
908c2ecf20Sopenharmony_cistatic DECLARE_COMPLETION(at91_rtc_updated);
918c2ecf20Sopenharmony_cistatic DECLARE_COMPLETION(at91_rtc_upd_rdy);
928c2ecf20Sopenharmony_cistatic void __iomem *at91_rtc_regs;
938c2ecf20Sopenharmony_cistatic int irq;
948c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(at91_rtc_lock);
958c2ecf20Sopenharmony_cistatic u32 at91_rtc_shadow_imr;
968c2ecf20Sopenharmony_cistatic bool suspended;
978c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(suspended_lock);
988c2ecf20Sopenharmony_cistatic unsigned long cached_events;
998c2ecf20Sopenharmony_cistatic u32 at91_rtc_imr;
1008c2ecf20Sopenharmony_cistatic struct clk *sclk;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic void at91_rtc_write_ier(u32 mask)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	unsigned long flags;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	spin_lock_irqsave(&at91_rtc_lock, flags);
1078c2ecf20Sopenharmony_ci	at91_rtc_shadow_imr |= mask;
1088c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_IER, mask);
1098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&at91_rtc_lock, flags);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic void at91_rtc_write_idr(u32 mask)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	unsigned long flags;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&at91_rtc_lock, flags);
1178c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_IDR, mask);
1188c2ecf20Sopenharmony_ci	/*
1198c2ecf20Sopenharmony_ci	 * Register read back (of any RTC-register) needed to make sure
1208c2ecf20Sopenharmony_ci	 * IDR-register write has reached the peripheral before updating
1218c2ecf20Sopenharmony_ci	 * shadow mask.
1228c2ecf20Sopenharmony_ci	 *
1238c2ecf20Sopenharmony_ci	 * Note that there is still a possibility that the mask is updated
1248c2ecf20Sopenharmony_ci	 * before interrupts have actually been disabled in hardware. The only
1258c2ecf20Sopenharmony_ci	 * way to be certain would be to poll the IMR-register, which is is
1268c2ecf20Sopenharmony_ci	 * the very register we are trying to emulate. The register read back
1278c2ecf20Sopenharmony_ci	 * is a reasonable heuristic.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	at91_rtc_read(AT91_RTC_SR);
1308c2ecf20Sopenharmony_ci	at91_rtc_shadow_imr &= ~mask;
1318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&at91_rtc_lock, flags);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic u32 at91_rtc_read_imr(void)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	unsigned long flags;
1378c2ecf20Sopenharmony_ci	u32 mask;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (at91_rtc_config->use_shadow_imr) {
1408c2ecf20Sopenharmony_ci		spin_lock_irqsave(&at91_rtc_lock, flags);
1418c2ecf20Sopenharmony_ci		mask = at91_rtc_shadow_imr;
1428c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&at91_rtc_lock, flags);
1438c2ecf20Sopenharmony_ci	} else {
1448c2ecf20Sopenharmony_ci		mask = at91_rtc_read(AT91_RTC_IMR);
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return mask;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/*
1518c2ecf20Sopenharmony_ci * Decode time/date into rtc_time structure
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_cistatic void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
1548c2ecf20Sopenharmony_ci				struct rtc_time *tm)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	unsigned int time, date;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/* must read twice in case it changes */
1598c2ecf20Sopenharmony_ci	do {
1608c2ecf20Sopenharmony_ci		time = at91_rtc_read(timereg);
1618c2ecf20Sopenharmony_ci		date = at91_rtc_read(calreg);
1628c2ecf20Sopenharmony_ci	} while ((time != at91_rtc_read(timereg)) ||
1638c2ecf20Sopenharmony_ci			(date != at91_rtc_read(calreg)));
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	tm->tm_sec  = bcd2bin(FIELD_GET(AT91_RTC_SEC, time));
1668c2ecf20Sopenharmony_ci	tm->tm_min  = bcd2bin(FIELD_GET(AT91_RTC_MIN, time));
1678c2ecf20Sopenharmony_ci	tm->tm_hour = bcd2bin(FIELD_GET(AT91_RTC_HOUR, time));
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/*
1708c2ecf20Sopenharmony_ci	 * The Calendar Alarm register does not have a field for
1718c2ecf20Sopenharmony_ci	 * the year - so these will return an invalid value.
1728c2ecf20Sopenharmony_ci	 */
1738c2ecf20Sopenharmony_ci	tm->tm_year  = bcd2bin(date & AT91_RTC_CENT) * 100;	/* century */
1748c2ecf20Sopenharmony_ci	tm->tm_year += bcd2bin(FIELD_GET(AT91_RTC_YEAR, date));	/* year */
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	tm->tm_wday = bcd2bin(FIELD_GET(AT91_RTC_DAY, date)) - 1;	/* day of the week [0-6], Sunday=0 */
1778c2ecf20Sopenharmony_ci	tm->tm_mon  = bcd2bin(FIELD_GET(AT91_RTC_MONTH, date)) - 1;
1788c2ecf20Sopenharmony_ci	tm->tm_mday = bcd2bin(FIELD_GET(AT91_RTC_DATE, date));
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/*
1828c2ecf20Sopenharmony_ci * Read current time and date in RTC
1838c2ecf20Sopenharmony_ci */
1848c2ecf20Sopenharmony_cistatic int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
1878c2ecf20Sopenharmony_ci	tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
1888c2ecf20Sopenharmony_ci	tm->tm_year = tm->tm_year - 1900;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	return 0;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/*
1968c2ecf20Sopenharmony_ci * Set current time and date in RTC
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_cistatic int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	unsigned long cr;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	wait_for_completion(&at91_rtc_upd_rdy);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* Stop Time/Calendar from counting */
2078c2ecf20Sopenharmony_ci	cr = at91_rtc_read(AT91_RTC_CR);
2088c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	at91_rtc_write_ier(AT91_RTC_ACKUPD);
2118c2ecf20Sopenharmony_ci	wait_for_completion(&at91_rtc_updated);	/* wait for ACKUPD interrupt */
2128c2ecf20Sopenharmony_ci	at91_rtc_write_idr(AT91_RTC_ACKUPD);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_TIMR,
2158c2ecf20Sopenharmony_ci			  FIELD_PREP(AT91_RTC_SEC, bin2bcd(tm->tm_sec))
2168c2ecf20Sopenharmony_ci			| FIELD_PREP(AT91_RTC_MIN, bin2bcd(tm->tm_min))
2178c2ecf20Sopenharmony_ci			| FIELD_PREP(AT91_RTC_HOUR, bin2bcd(tm->tm_hour)));
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_CALR,
2208c2ecf20Sopenharmony_ci			  FIELD_PREP(AT91_RTC_CENT,
2218c2ecf20Sopenharmony_ci				     bin2bcd((tm->tm_year + 1900) / 100))
2228c2ecf20Sopenharmony_ci			| FIELD_PREP(AT91_RTC_YEAR, bin2bcd(tm->tm_year % 100))
2238c2ecf20Sopenharmony_ci			| FIELD_PREP(AT91_RTC_MONTH, bin2bcd(tm->tm_mon + 1))
2248c2ecf20Sopenharmony_ci			| FIELD_PREP(AT91_RTC_DAY, bin2bcd(tm->tm_wday + 1))
2258c2ecf20Sopenharmony_ci			| FIELD_PREP(AT91_RTC_DATE, bin2bcd(tm->tm_mday)));
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* Restart Time/Calendar */
2288c2ecf20Sopenharmony_ci	cr = at91_rtc_read(AT91_RTC_CR);
2298c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_SECEV);
2308c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
2318c2ecf20Sopenharmony_ci	at91_rtc_write_ier(AT91_RTC_SECEV);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/*
2378c2ecf20Sopenharmony_ci * Read alarm time and date in RTC
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_cistatic int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	struct rtc_time *tm = &alrm->time;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
2448c2ecf20Sopenharmony_ci	tm->tm_year = -1;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	alrm->enabled = (at91_rtc_read_imr() & AT91_RTC_ALARM)
2478c2ecf20Sopenharmony_ci			? 1 : 0;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s(): %ptR %sabled\n", __func__, tm,
2508c2ecf20Sopenharmony_ci		alrm->enabled ? "en" : "dis");
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return 0;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/*
2568c2ecf20Sopenharmony_ci * Set alarm time and date in RTC
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_cistatic int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct rtc_time tm = alrm->time;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	at91_rtc_write_idr(AT91_RTC_ALARM);
2638c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_TIMALR,
2648c2ecf20Sopenharmony_ci		  FIELD_PREP(AT91_RTC_SEC, bin2bcd(alrm->time.tm_sec))
2658c2ecf20Sopenharmony_ci		| FIELD_PREP(AT91_RTC_MIN, bin2bcd(alrm->time.tm_min))
2668c2ecf20Sopenharmony_ci		| FIELD_PREP(AT91_RTC_HOUR, bin2bcd(alrm->time.tm_hour))
2678c2ecf20Sopenharmony_ci		| AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
2688c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_CALALR,
2698c2ecf20Sopenharmony_ci		  FIELD_PREP(AT91_RTC_MONTH, bin2bcd(alrm->time.tm_mon + 1))
2708c2ecf20Sopenharmony_ci		| FIELD_PREP(AT91_RTC_DATE, bin2bcd(alrm->time.tm_mday))
2718c2ecf20Sopenharmony_ci		| AT91_RTC_DATEEN | AT91_RTC_MTHEN);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if (alrm->enabled) {
2748c2ecf20Sopenharmony_ci		at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
2758c2ecf20Sopenharmony_ci		at91_rtc_write_ier(AT91_RTC_ALARM);
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s(): %ptR\n", __func__, &tm);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return 0;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s(): cmd=%08x\n", __func__, enabled);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (enabled) {
2888c2ecf20Sopenharmony_ci		at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
2898c2ecf20Sopenharmony_ci		at91_rtc_write_ier(AT91_RTC_ALARM);
2908c2ecf20Sopenharmony_ci	} else
2918c2ecf20Sopenharmony_ci		at91_rtc_write_idr(AT91_RTC_ALARM);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return 0;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/*
2978c2ecf20Sopenharmony_ci * IRQ handler for the RTC
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistatic irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct platform_device *pdev = dev_id;
3028c2ecf20Sopenharmony_ci	struct rtc_device *rtc = platform_get_drvdata(pdev);
3038c2ecf20Sopenharmony_ci	unsigned int rtsr;
3048c2ecf20Sopenharmony_ci	unsigned long events = 0;
3058c2ecf20Sopenharmony_ci	int ret = IRQ_NONE;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	spin_lock(&suspended_lock);
3088c2ecf20Sopenharmony_ci	rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read_imr();
3098c2ecf20Sopenharmony_ci	if (rtsr) {		/* this interrupt is shared!  Is it ours? */
3108c2ecf20Sopenharmony_ci		if (rtsr & AT91_RTC_ALARM)
3118c2ecf20Sopenharmony_ci			events |= (RTC_AF | RTC_IRQF);
3128c2ecf20Sopenharmony_ci		if (rtsr & AT91_RTC_SECEV) {
3138c2ecf20Sopenharmony_ci			complete(&at91_rtc_upd_rdy);
3148c2ecf20Sopenharmony_ci			at91_rtc_write_idr(AT91_RTC_SECEV);
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci		if (rtsr & AT91_RTC_ACKUPD)
3178c2ecf20Sopenharmony_ci			complete(&at91_rtc_updated);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci		at91_rtc_write(AT91_RTC_SCCR, rtsr);	/* clear status reg */
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci		if (!suspended) {
3228c2ecf20Sopenharmony_ci			rtc_update_irq(rtc, 1, events);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci			dev_dbg(&pdev->dev, "%s(): num=%ld, events=0x%02lx\n",
3258c2ecf20Sopenharmony_ci				__func__, events >> 8, events & 0x000000FF);
3268c2ecf20Sopenharmony_ci		} else {
3278c2ecf20Sopenharmony_ci			cached_events |= events;
3288c2ecf20Sopenharmony_ci			at91_rtc_write_idr(at91_rtc_imr);
3298c2ecf20Sopenharmony_ci			pm_system_wakeup();
3308c2ecf20Sopenharmony_ci		}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci	spin_unlock(&suspended_lock);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	return ret;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic const struct at91_rtc_config at91rm9200_config = {
3408c2ecf20Sopenharmony_ci};
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic const struct at91_rtc_config at91sam9x5_config = {
3438c2ecf20Sopenharmony_ci	.use_shadow_imr	= true,
3448c2ecf20Sopenharmony_ci};
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic const struct of_device_id at91_rtc_dt_ids[] = {
3478c2ecf20Sopenharmony_ci	{
3488c2ecf20Sopenharmony_ci		.compatible = "atmel,at91rm9200-rtc",
3498c2ecf20Sopenharmony_ci		.data = &at91rm9200_config,
3508c2ecf20Sopenharmony_ci	}, {
3518c2ecf20Sopenharmony_ci		.compatible = "atmel,at91sam9x5-rtc",
3528c2ecf20Sopenharmony_ci		.data = &at91sam9x5_config,
3538c2ecf20Sopenharmony_ci	}, {
3548c2ecf20Sopenharmony_ci		.compatible = "atmel,sama5d4-rtc",
3558c2ecf20Sopenharmony_ci		.data = &at91rm9200_config,
3568c2ecf20Sopenharmony_ci	}, {
3578c2ecf20Sopenharmony_ci		.compatible = "atmel,sama5d2-rtc",
3588c2ecf20Sopenharmony_ci		.data = &at91rm9200_config,
3598c2ecf20Sopenharmony_ci	}, {
3608c2ecf20Sopenharmony_ci		/* sentinel */
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci};
3638c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic const struct rtc_class_ops at91_rtc_ops = {
3668c2ecf20Sopenharmony_ci	.read_time	= at91_rtc_readtime,
3678c2ecf20Sopenharmony_ci	.set_time	= at91_rtc_settime,
3688c2ecf20Sopenharmony_ci	.read_alarm	= at91_rtc_readalarm,
3698c2ecf20Sopenharmony_ci	.set_alarm	= at91_rtc_setalarm,
3708c2ecf20Sopenharmony_ci	.alarm_irq_enable = at91_rtc_alarm_irq_enable,
3718c2ecf20Sopenharmony_ci};
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/*
3748c2ecf20Sopenharmony_ci * Initialize and install RTC driver
3758c2ecf20Sopenharmony_ci */
3768c2ecf20Sopenharmony_cistatic int __init at91_rtc_probe(struct platform_device *pdev)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
3798c2ecf20Sopenharmony_ci	struct resource *regs;
3808c2ecf20Sopenharmony_ci	int ret = 0;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	at91_rtc_config = of_device_get_match_data(&pdev->dev);
3838c2ecf20Sopenharmony_ci	if (!at91_rtc_config)
3848c2ecf20Sopenharmony_ci		return -ENODEV;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3878c2ecf20Sopenharmony_ci	if (!regs) {
3888c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "no mmio resource defined\n");
3898c2ecf20Sopenharmony_ci		return -ENXIO;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
3938c2ecf20Sopenharmony_ci	if (irq < 0)
3948c2ecf20Sopenharmony_ci		return -ENXIO;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	at91_rtc_regs = devm_ioremap(&pdev->dev, regs->start,
3978c2ecf20Sopenharmony_ci				     resource_size(regs));
3988c2ecf20Sopenharmony_ci	if (!at91_rtc_regs) {
3998c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to map registers, aborting.\n");
4008c2ecf20Sopenharmony_ci		return -ENOMEM;
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	rtc = devm_rtc_allocate_device(&pdev->dev);
4048c2ecf20Sopenharmony_ci	if (IS_ERR(rtc))
4058c2ecf20Sopenharmony_ci		return PTR_ERR(rtc);
4068c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rtc);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	sclk = devm_clk_get(&pdev->dev, NULL);
4098c2ecf20Sopenharmony_ci	if (IS_ERR(sclk))
4108c2ecf20Sopenharmony_ci		return PTR_ERR(sclk);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(sclk);
4138c2ecf20Sopenharmony_ci	if (ret) {
4148c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not enable slow clock\n");
4158c2ecf20Sopenharmony_ci		return ret;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_CR, 0);
4198c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_MR, 0);		/* 24 hour mode */
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/* Disable all interrupts */
4228c2ecf20Sopenharmony_ci	at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
4238c2ecf20Sopenharmony_ci					AT91_RTC_SECEV | AT91_RTC_TIMEV |
4248c2ecf20Sopenharmony_ci					AT91_RTC_CALEV);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, irq, at91_rtc_interrupt,
4278c2ecf20Sopenharmony_ci			       IRQF_SHARED | IRQF_COND_SUSPEND,
4288c2ecf20Sopenharmony_ci			       "at91_rtc", pdev);
4298c2ecf20Sopenharmony_ci	if (ret) {
4308c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "IRQ %d already in use.\n", irq);
4318c2ecf20Sopenharmony_ci		goto err_clk;
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	/* cpu init code should really have flagged this device as
4358c2ecf20Sopenharmony_ci	 * being wake-capable; if it didn't, do that here.
4368c2ecf20Sopenharmony_ci	 */
4378c2ecf20Sopenharmony_ci	if (!device_can_wakeup(&pdev->dev))
4388c2ecf20Sopenharmony_ci		device_init_wakeup(&pdev->dev, 1);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	rtc->ops = &at91_rtc_ops;
4418c2ecf20Sopenharmony_ci	rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
4428c2ecf20Sopenharmony_ci	rtc->range_max = RTC_TIMESTAMP_END_2099;
4438c2ecf20Sopenharmony_ci	ret = rtc_register_device(rtc);
4448c2ecf20Sopenharmony_ci	if (ret)
4458c2ecf20Sopenharmony_ci		goto err_clk;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* enable SECEV interrupt in order to initialize at91_rtc_upd_rdy
4488c2ecf20Sopenharmony_ci	 * completion.
4498c2ecf20Sopenharmony_ci	 */
4508c2ecf20Sopenharmony_ci	at91_rtc_write_ier(AT91_RTC_SECEV);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "AT91 Real Time Clock driver.\n");
4538c2ecf20Sopenharmony_ci	return 0;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cierr_clk:
4568c2ecf20Sopenharmony_ci	clk_disable_unprepare(sclk);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return ret;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci/*
4628c2ecf20Sopenharmony_ci * Disable and remove the RTC driver
4638c2ecf20Sopenharmony_ci */
4648c2ecf20Sopenharmony_cistatic int __exit at91_rtc_remove(struct platform_device *pdev)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	/* Disable all interrupts */
4678c2ecf20Sopenharmony_ci	at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
4688c2ecf20Sopenharmony_ci					AT91_RTC_SECEV | AT91_RTC_TIMEV |
4698c2ecf20Sopenharmony_ci					AT91_RTC_CALEV);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	clk_disable_unprepare(sclk);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	return 0;
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic void at91_rtc_shutdown(struct platform_device *pdev)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	/* Disable all interrupts */
4798c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
4808c2ecf20Sopenharmony_ci					AT91_RTC_SECEV | AT91_RTC_TIMEV |
4818c2ecf20Sopenharmony_ci					AT91_RTC_CALEV);
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/* AT91RM9200 RTC Power management control */
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic int at91_rtc_suspend(struct device *dev)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	/* this IRQ is shared with DBGU and other hardware which isn't
4918c2ecf20Sopenharmony_ci	 * necessarily doing PM like we are...
4928c2ecf20Sopenharmony_ci	 */
4938c2ecf20Sopenharmony_ci	at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	at91_rtc_imr = at91_rtc_read_imr()
4968c2ecf20Sopenharmony_ci			& (AT91_RTC_ALARM|AT91_RTC_SECEV);
4978c2ecf20Sopenharmony_ci	if (at91_rtc_imr) {
4988c2ecf20Sopenharmony_ci		if (device_may_wakeup(dev)) {
4998c2ecf20Sopenharmony_ci			unsigned long flags;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci			enable_irq_wake(irq);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci			spin_lock_irqsave(&suspended_lock, flags);
5048c2ecf20Sopenharmony_ci			suspended = true;
5058c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&suspended_lock, flags);
5068c2ecf20Sopenharmony_ci		} else {
5078c2ecf20Sopenharmony_ci			at91_rtc_write_idr(at91_rtc_imr);
5088c2ecf20Sopenharmony_ci		}
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci	return 0;
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistatic int at91_rtc_resume(struct device *dev)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	struct rtc_device *rtc = dev_get_drvdata(dev);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (at91_rtc_imr) {
5188c2ecf20Sopenharmony_ci		if (device_may_wakeup(dev)) {
5198c2ecf20Sopenharmony_ci			unsigned long flags;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci			spin_lock_irqsave(&suspended_lock, flags);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci			if (cached_events) {
5248c2ecf20Sopenharmony_ci				rtc_update_irq(rtc, 1, cached_events);
5258c2ecf20Sopenharmony_ci				cached_events = 0;
5268c2ecf20Sopenharmony_ci			}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci			suspended = false;
5298c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&suspended_lock, flags);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci			disable_irq_wake(irq);
5328c2ecf20Sopenharmony_ci		}
5338c2ecf20Sopenharmony_ci		at91_rtc_write_ier(at91_rtc_imr);
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci	return 0;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci#endif
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic struct platform_driver at91_rtc_driver = {
5428c2ecf20Sopenharmony_ci	.remove		= __exit_p(at91_rtc_remove),
5438c2ecf20Sopenharmony_ci	.shutdown	= at91_rtc_shutdown,
5448c2ecf20Sopenharmony_ci	.driver		= {
5458c2ecf20Sopenharmony_ci		.name	= "at91_rtc",
5468c2ecf20Sopenharmony_ci		.pm	= &at91_rtc_pm_ops,
5478c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(at91_rtc_dt_ids),
5488c2ecf20Sopenharmony_ci	},
5498c2ecf20Sopenharmony_ci};
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cimodule_platform_driver_probe(at91_rtc_driver, at91_rtc_probe);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rick Bronson");
5548c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
5558c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5568c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:at91_rtc");
557