2008-02-06 09:38:59 +00:00
|
|
|
/*
|
|
|
|
* "RTT as Real Time Clock" driver for AT91SAM9 SoC family
|
|
|
|
*
|
|
|
|
* (C) 2007 Michel Benoit
|
|
|
|
*
|
|
|
|
* Based on rtc-at91rm9200.c by Rick Bronson
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/time.h>
|
|
|
|
#include <linux/rtc.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/ioctl.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2012-10-29 21:12:23 +00:00
|
|
|
#include <linux/platform_data/atmel.h>
|
2013-04-29 23:20:35 +00:00
|
|
|
#include <linux/io.h>
|
2014-09-23 11:14:44 +00:00
|
|
|
#include <linux/mfd/syscon.h>
|
|
|
|
#include <linux/regmap.h>
|
2015-03-02 09:18:14 +00:00
|
|
|
#include <linux/suspend.h>
|
2014-09-23 14:41:07 +00:00
|
|
|
#include <linux/clk.h>
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This driver uses two configurable hardware resources that live in the
|
|
|
|
* AT91SAM9 backup power domain (intended to be powered at all times)
|
|
|
|
* to implement the Real Time Clock interfaces
|
|
|
|
*
|
|
|
|
* - A "Real-time Timer" (RTT) counts up in seconds from a base time.
|
|
|
|
* We can't assign the counter value (CRTV) ... but we can reset it.
|
|
|
|
*
|
|
|
|
* - One of the "General Purpose Backup Registers" (GPBRs) holds the
|
|
|
|
* base time, normally an offset from the beginning of the POSIX
|
|
|
|
* epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
|
|
|
|
* local timezone's offset.
|
|
|
|
*
|
|
|
|
* The RTC's value is the RTT counter plus that offset. The RTC's alarm
|
|
|
|
* is likewise a base (ALMV) plus that offset.
|
|
|
|
*
|
|
|
|
* Not all RTTs will be used as RTCs; some systems have multiple RTTs to
|
|
|
|
* choose from, or a "real" RTC module. All systems have multiple GPBR
|
|
|
|
* registers available, likewise usable for more than "RTC" support.
|
|
|
|
*/
|
|
|
|
|
2014-09-23 11:13:29 +00:00
|
|
|
#define AT91_RTT_MR 0x00 /* Real-time Mode Register */
|
|
|
|
#define AT91_RTT_RTPRES (0xffff << 0) /* Real-time Timer Prescaler Value */
|
|
|
|
#define AT91_RTT_ALMIEN (1 << 16) /* Alarm Interrupt Enable */
|
|
|
|
#define AT91_RTT_RTTINCIEN (1 << 17) /* Real Time Timer Increment Interrupt Enable */
|
|
|
|
#define AT91_RTT_RTTRST (1 << 18) /* Real Time Timer Restart */
|
|
|
|
|
|
|
|
#define AT91_RTT_AR 0x04 /* Real-time Alarm Register */
|
|
|
|
#define AT91_RTT_ALMV (0xffffffff) /* Alarm Value */
|
|
|
|
|
|
|
|
#define AT91_RTT_VR 0x08 /* Real-time Value Register */
|
|
|
|
#define AT91_RTT_CRTV (0xffffffff) /* Current Real-time Value */
|
|
|
|
|
|
|
|
#define AT91_RTT_SR 0x0c /* Real-time Status Register */
|
|
|
|
#define AT91_RTT_ALMS (1 << 0) /* Real-time Alarm Status */
|
|
|
|
#define AT91_RTT_RTTINC (1 << 1) /* Real-time Timer Increment */
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
/*
|
|
|
|
* We store ALARM_DISABLED in ALMV to record that no alarm is set.
|
|
|
|
* It's also the reset value for that field.
|
|
|
|
*/
|
|
|
|
#define ALARM_DISABLED ((u32)~0)
|
|
|
|
|
|
|
|
|
|
|
|
struct sam9_rtc {
|
|
|
|
void __iomem *rtt;
|
|
|
|
struct rtc_device *rtcdev;
|
|
|
|
u32 imr;
|
2014-09-23 11:14:44 +00:00
|
|
|
struct regmap *gpbr;
|
|
|
|
unsigned int gpbr_offset;
|
2012-08-14 09:19:22 +00:00
|
|
|
int irq;
|
2014-09-23 14:41:07 +00:00
|
|
|
struct clk *sclk;
|
2015-03-02 09:18:14 +00:00
|
|
|
bool suspended;
|
|
|
|
unsigned long events;
|
|
|
|
spinlock_t lock;
|
2008-02-06 09:38:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define rtt_readl(rtc, field) \
|
2014-09-23 11:13:52 +00:00
|
|
|
readl((rtc)->rtt + AT91_RTT_ ## field)
|
2008-02-06 09:38:59 +00:00
|
|
|
#define rtt_writel(rtc, field, val) \
|
2014-09-23 11:13:52 +00:00
|
|
|
writel((val), (rtc)->rtt + AT91_RTT_ ## field)
|
2008-02-06 09:38:59 +00:00
|
|
|
|
2014-09-23 11:14:44 +00:00
|
|
|
static inline unsigned int gpbr_readl(struct sam9_rtc *rtc)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
regmap_read(rtc->gpbr, rtc->gpbr_offset, &val);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void gpbr_writel(struct sam9_rtc *rtc, unsigned int val)
|
|
|
|
{
|
|
|
|
regmap_write(rtc->gpbr, rtc->gpbr_offset, val);
|
|
|
|
}
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read current time and date in RTC
|
|
|
|
*/
|
|
|
|
static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
|
|
|
u32 secs, secs2;
|
|
|
|
u32 offset;
|
|
|
|
|
|
|
|
/* read current time offset */
|
|
|
|
offset = gpbr_readl(rtc);
|
|
|
|
if (offset == 0)
|
|
|
|
return -EILSEQ;
|
|
|
|
|
|
|
|
/* reread the counter to help sync the two clock domains */
|
|
|
|
secs = rtt_readl(rtc, VR);
|
|
|
|
secs2 = rtt_readl(rtc, VR);
|
|
|
|
if (secs != secs2)
|
|
|
|
secs = rtt_readl(rtc, VR);
|
|
|
|
|
|
|
|
rtc_time_to_tm(offset + secs, tm);
|
|
|
|
|
|
|
|
dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readtime",
|
|
|
|
1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
|
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set current time and date in RTC
|
|
|
|
*/
|
|
|
|
static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
|
|
|
int err;
|
|
|
|
u32 offset, alarm, mr;
|
|
|
|
unsigned long secs;
|
|
|
|
|
|
|
|
dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "settime",
|
|
|
|
1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
|
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
|
|
|
|
|
|
|
err = rtc_tm_to_time(tm, &secs);
|
|
|
|
if (err != 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
/* disable interrupts */
|
|
|
|
rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
|
|
|
|
|
|
|
|
/* read current time offset */
|
|
|
|
offset = gpbr_readl(rtc);
|
|
|
|
|
|
|
|
/* store the new base time in a battery backup register */
|
|
|
|
secs += 1;
|
|
|
|
gpbr_writel(rtc, secs);
|
|
|
|
|
|
|
|
/* adjust the alarm time for the new base */
|
|
|
|
alarm = rtt_readl(rtc, AR);
|
|
|
|
if (alarm != ALARM_DISABLED) {
|
|
|
|
if (offset > secs) {
|
|
|
|
/* time jumped backwards, increase time until alarm */
|
|
|
|
alarm += (offset - secs);
|
|
|
|
} else if ((alarm + offset) > secs) {
|
|
|
|
/* time jumped forwards, decrease time until alarm */
|
|
|
|
alarm -= (secs - offset);
|
|
|
|
} else {
|
|
|
|
/* time jumped past the alarm, disable alarm */
|
|
|
|
alarm = ALARM_DISABLED;
|
|
|
|
mr &= ~AT91_RTT_ALMIEN;
|
|
|
|
}
|
|
|
|
rtt_writel(rtc, AR, alarm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the timer, and re-enable interrupts */
|
|
|
|
rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
|
|
|
struct rtc_time *tm = &alrm->time;
|
|
|
|
u32 alarm = rtt_readl(rtc, AR);
|
|
|
|
u32 offset;
|
|
|
|
|
|
|
|
offset = gpbr_readl(rtc);
|
|
|
|
if (offset == 0)
|
|
|
|
return -EILSEQ;
|
|
|
|
|
2010-03-05 21:44:23 +00:00
|
|
|
memset(alrm, 0, sizeof(*alrm));
|
2008-02-06 09:38:59 +00:00
|
|
|
if (alarm != ALARM_DISABLED && offset != 0) {
|
|
|
|
rtc_time_to_tm(offset + alarm, tm);
|
|
|
|
|
|
|
|
dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readalarm",
|
|
|
|
1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
|
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
|
|
|
|
|
|
|
if (rtt_readl(rtc, MR) & AT91_RTT_ALMIEN)
|
|
|
|
alrm->enabled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
|
|
|
struct rtc_time *tm = &alrm->time;
|
|
|
|
unsigned long secs;
|
|
|
|
u32 offset;
|
|
|
|
u32 mr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = rtc_tm_to_time(tm, &secs);
|
|
|
|
if (err != 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
offset = gpbr_readl(rtc);
|
|
|
|
if (offset == 0) {
|
|
|
|
/* time is not set */
|
|
|
|
return -EILSEQ;
|
|
|
|
}
|
|
|
|
mr = rtt_readl(rtc, MR);
|
|
|
|
rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
|
|
|
|
|
|
|
|
/* alarm in the past? finish and leave disabled */
|
|
|
|
if (secs <= offset) {
|
|
|
|
rtt_writel(rtc, AR, ALARM_DISABLED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else set alarm and maybe enable it */
|
|
|
|
rtt_writel(rtc, AR, secs - offset);
|
|
|
|
if (alrm->enabled)
|
|
|
|
rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
|
|
|
|
|
|
|
|
dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "setalarm",
|
|
|
|
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
|
|
|
|
tm->tm_min, tm->tm_sec);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-03 01:02:41 +00:00
|
|
|
static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
|
|
|
u32 mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
dev_dbg(dev, "alarm_irq_enable: enabled=%08x, mr %08x\n", enabled, mr);
|
|
|
|
if (enabled)
|
|
|
|
rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
|
|
|
|
else
|
|
|
|
rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
/*
|
|
|
|
* Provide additional RTC information in /proc/driver/rtc
|
|
|
|
*/
|
|
|
|
static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
|
|
|
u32 mr = mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
seq_printf(seq, "update_IRQ\t: %s\n",
|
|
|
|
(mr & AT91_RTT_RTTINCIEN) ? "yes" : "no");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 09:18:14 +00:00
|
|
|
static irqreturn_t at91_rtc_cache_events(struct sam9_rtc *rtc)
|
2008-02-06 09:38:59 +00:00
|
|
|
{
|
|
|
|
u32 sr, mr;
|
|
|
|
|
|
|
|
/* Shared interrupt may be for another device. Note: reading
|
|
|
|
* SR clears it, so we must only read it in this irq handler!
|
|
|
|
*/
|
|
|
|
mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
|
2008-03-20 00:01:09 +00:00
|
|
|
sr = rtt_readl(rtc, SR) & (mr >> 16);
|
2008-02-06 09:38:59 +00:00
|
|
|
if (!sr)
|
|
|
|
return IRQ_NONE;
|
|
|
|
|
|
|
|
/* alarm status */
|
|
|
|
if (sr & AT91_RTT_ALMS)
|
2015-03-02 09:18:14 +00:00
|
|
|
rtc->events |= (RTC_AF | RTC_IRQF);
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
/* timer update/increment */
|
|
|
|
if (sr & AT91_RTT_RTTINC)
|
2015-03-02 09:18:14 +00:00
|
|
|
rtc->events |= (RTC_UF | RTC_IRQF);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at91_rtc_flush_events(struct sam9_rtc *rtc)
|
|
|
|
{
|
|
|
|
if (!rtc->events)
|
|
|
|
return;
|
2008-02-06 09:38:59 +00:00
|
|
|
|
2015-03-02 09:18:14 +00:00
|
|
|
rtc_update_irq(rtc->rtcdev, 1, rtc->events);
|
|
|
|
rtc->events = 0;
|
2008-02-06 09:38:59 +00:00
|
|
|
|
2008-04-28 09:12:00 +00:00
|
|
|
pr_debug("%s: num=%ld, events=0x%02lx\n", __func__,
|
2015-03-02 09:18:14 +00:00
|
|
|
rtc->events >> 8, rtc->events & 0x000000FF);
|
|
|
|
}
|
2008-02-06 09:38:59 +00:00
|
|
|
|
2015-03-02 09:18:14 +00:00
|
|
|
/*
|
|
|
|
* IRQ handler for the RTC
|
|
|
|
*/
|
|
|
|
static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = _rtc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
spin_lock(&rtc->lock);
|
|
|
|
|
|
|
|
ret = at91_rtc_cache_events(rtc);
|
|
|
|
|
|
|
|
/* We're called in suspended state */
|
|
|
|
if (rtc->suspended) {
|
|
|
|
/* Mask irqs coming from this peripheral */
|
|
|
|
rtt_writel(rtc, MR,
|
|
|
|
rtt_readl(rtc, MR) &
|
|
|
|
~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
|
|
|
|
/* Trigger a system wakeup */
|
|
|
|
pm_system_wakeup();
|
|
|
|
} else {
|
|
|
|
at91_rtc_flush_events(rtc);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&rtc->lock);
|
|
|
|
|
|
|
|
return ret;
|
2008-02-06 09:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rtc_class_ops at91_rtc_ops = {
|
|
|
|
.read_time = at91_rtc_readtime,
|
|
|
|
.set_time = at91_rtc_settime,
|
|
|
|
.read_alarm = at91_rtc_readalarm,
|
|
|
|
.set_alarm = at91_rtc_setalarm,
|
|
|
|
.proc = at91_rtc_proc,
|
2011-02-25 19:13:55 +00:00
|
|
|
.alarm_irq_enable = at91_rtc_alarm_irq_enable,
|
2008-02-06 09:38:59 +00:00
|
|
|
};
|
|
|
|
|
2015-02-13 22:40:48 +00:00
|
|
|
static const struct regmap_config gpbr_regmap_config = {
|
2014-09-23 11:14:44 +00:00
|
|
|
.reg_bits = 32,
|
|
|
|
.val_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
};
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
/*
|
|
|
|
* Initialize and install RTC driver
|
|
|
|
*/
|
2012-12-21 21:09:38 +00:00
|
|
|
static int at91_rtc_probe(struct platform_device *pdev)
|
2008-02-06 09:38:59 +00:00
|
|
|
{
|
2014-09-23 11:14:09 +00:00
|
|
|
struct resource *r;
|
2008-02-06 09:38:59 +00:00
|
|
|
struct sam9_rtc *rtc;
|
2012-08-14 09:19:22 +00:00
|
|
|
int ret, irq;
|
2008-02-06 09:38:59 +00:00
|
|
|
u32 mr;
|
2014-09-23 14:41:07 +00:00
|
|
|
unsigned int sclk_rate;
|
2008-02-06 09:38:59 +00:00
|
|
|
|
2012-08-14 09:19:22 +00:00
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
if (irq < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed to get interrupt resource\n");
|
|
|
|
return irq;
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:20:35 +00:00
|
|
|
rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
|
2008-02-06 09:38:59 +00:00
|
|
|
if (!rtc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-08-14 09:19:22 +00:00
|
|
|
rtc->irq = irq;
|
|
|
|
|
2008-03-20 00:01:09 +00:00
|
|
|
/* platform setup code should have handled this; sigh */
|
|
|
|
if (!device_can_wakeup(&pdev->dev))
|
|
|
|
device_init_wakeup(&pdev->dev, 1);
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
platform_set_drvdata(pdev, rtc);
|
|
|
|
|
2014-09-23 11:14:09 +00:00
|
|
|
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
rtc->rtt = devm_ioremap_resource(&pdev->dev, r);
|
|
|
|
if (IS_ERR(rtc->rtt))
|
|
|
|
return PTR_ERR(rtc->rtt);
|
|
|
|
|
2014-09-23 11:14:44 +00:00
|
|
|
if (!pdev->dev.of_node) {
|
|
|
|
/*
|
|
|
|
* TODO: Remove this code chunk when removing non DT board
|
|
|
|
* support. Remember to remove the gpbr_regmap_config
|
|
|
|
* variable too.
|
|
|
|
*/
|
|
|
|
void __iomem *gpbr;
|
|
|
|
|
|
|
|
r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
|
|
|
|
gpbr = devm_ioremap_resource(&pdev->dev, r);
|
|
|
|
if (IS_ERR(gpbr))
|
|
|
|
return PTR_ERR(gpbr);
|
|
|
|
|
|
|
|
rtc->gpbr = regmap_init_mmio(NULL, gpbr,
|
|
|
|
&gpbr_regmap_config);
|
|
|
|
} else {
|
|
|
|
struct of_phandle_args args;
|
|
|
|
|
|
|
|
ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
|
|
|
|
"atmel,rtt-rtc-time-reg", 1, 0,
|
|
|
|
&args);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
rtc->gpbr = syscon_node_to_regmap(args.np);
|
|
|
|
rtc->gpbr_offset = args.args[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ERR(rtc->gpbr)) {
|
|
|
|
dev_err(&pdev->dev, "failed to retrieve gpbr regmap, aborting.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2012-02-15 13:24:46 +00:00
|
|
|
|
2014-09-23 14:41:07 +00:00
|
|
|
rtc->sclk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(rtc->sclk))
|
|
|
|
return PTR_ERR(rtc->sclk);
|
|
|
|
|
|
|
|
sclk_rate = clk_get_rate(rtc->sclk);
|
|
|
|
if (!sclk_rate || sclk_rate > AT91_RTT_RTPRES) {
|
|
|
|
dev_err(&pdev->dev, "Invalid slow clock rate\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(rtc->sclk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&pdev->dev, "Could not enable slow clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
/* unless RTT is counting at 1 Hz, re-initialize it */
|
2014-09-23 14:41:07 +00:00
|
|
|
if ((mr & AT91_RTT_RTPRES) != sclk_rate) {
|
|
|
|
mr = AT91_RTT_RTTRST | (sclk_rate & AT91_RTT_RTPRES);
|
2008-02-06 09:38:59 +00:00
|
|
|
gpbr_writel(rtc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disable all interrupts (same as on shutdown path) */
|
|
|
|
mr &= ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
|
|
|
|
rtt_writel(rtc, MR, mr);
|
|
|
|
|
2013-04-29 23:20:35 +00:00
|
|
|
rtc->rtcdev = devm_rtc_device_register(&pdev->dev, pdev->name,
|
|
|
|
&at91_rtc_ops, THIS_MODULE);
|
2013-07-03 22:06:12 +00:00
|
|
|
if (IS_ERR(rtc->rtcdev))
|
|
|
|
return PTR_ERR(rtc->rtcdev);
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
/* register irq handler after we know what name we'll use */
|
2013-04-29 23:20:35 +00:00
|
|
|
ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt,
|
2015-03-02 09:18:14 +00:00
|
|
|
IRQF_SHARED | IRQF_COND_SUSPEND,
|
|
|
|
dev_name(&rtc->rtcdev->dev), rtc);
|
2008-02-06 09:38:59 +00:00
|
|
|
if (ret) {
|
2012-08-14 09:19:22 +00:00
|
|
|
dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq);
|
2013-07-03 22:06:12 +00:00
|
|
|
return ret;
|
2008-02-06 09:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: sam9260 rev A silicon has a ROM bug which resets the
|
|
|
|
* RTT on at least some reboots. If you have that chip, you must
|
|
|
|
* initialize the time from some external source like a GPS, wall
|
|
|
|
* clock, discrete RTC, etc
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (gpbr_readl(rtc) == 0)
|
|
|
|
dev_warn(&pdev->dev, "%s: SET TIME!\n",
|
2009-03-24 23:38:22 +00:00
|
|
|
dev_name(&rtc->rtcdev->dev));
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable and remove the RTC driver
|
|
|
|
*/
|
2012-12-21 21:09:38 +00:00
|
|
|
static int at91_rtc_remove(struct platform_device *pdev)
|
2008-02-06 09:38:59 +00:00
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = platform_get_drvdata(pdev);
|
|
|
|
u32 mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
/* disable all interrupts */
|
|
|
|
rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
|
|
|
|
|
2014-09-23 14:41:07 +00:00
|
|
|
if (!IS_ERR(rtc->sclk))
|
|
|
|
clk_disable_unprepare(rtc->sclk);
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at91_rtc_shutdown(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct sam9_rtc *rtc = platform_get_drvdata(pdev);
|
|
|
|
u32 mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
|
|
|
|
rtt_writel(rtc, MR, mr & ~rtc->imr);
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:20:58 +00:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
/* AT91SAM9 RTC Power management control */
|
|
|
|
|
2013-04-29 23:20:58 +00:00
|
|
|
static int at91_rtc_suspend(struct device *dev)
|
2008-02-06 09:38:59 +00:00
|
|
|
{
|
2013-04-29 23:20:58 +00:00
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
2008-02-06 09:38:59 +00:00
|
|
|
u32 mr = rtt_readl(rtc, MR);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This IRQ is shared with DBGU and other hardware which isn't
|
|
|
|
* necessarily a wakeup event source.
|
|
|
|
*/
|
|
|
|
rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
|
|
|
|
if (rtc->imr) {
|
2013-04-29 23:20:58 +00:00
|
|
|
if (device_may_wakeup(dev) && (mr & AT91_RTT_ALMIEN)) {
|
2015-03-02 09:18:14 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2012-08-14 09:19:22 +00:00
|
|
|
enable_irq_wake(rtc->irq);
|
2015-03-02 09:18:14 +00:00
|
|
|
spin_lock_irqsave(&rtc->lock, flags);
|
|
|
|
rtc->suspended = true;
|
|
|
|
spin_unlock_irqrestore(&rtc->lock, flags);
|
2008-02-06 09:38:59 +00:00
|
|
|
/* don't let RTTINC cause wakeups */
|
|
|
|
if (mr & AT91_RTT_RTTINCIEN)
|
|
|
|
rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
|
|
|
|
} else
|
|
|
|
rtt_writel(rtc, MR, mr & ~rtc->imr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:20:58 +00:00
|
|
|
static int at91_rtc_resume(struct device *dev)
|
2008-02-06 09:38:59 +00:00
|
|
|
{
|
2013-04-29 23:20:58 +00:00
|
|
|
struct sam9_rtc *rtc = dev_get_drvdata(dev);
|
2008-02-06 09:38:59 +00:00
|
|
|
u32 mr;
|
|
|
|
|
|
|
|
if (rtc->imr) {
|
2015-03-02 09:18:14 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2013-04-29 23:20:58 +00:00
|
|
|
if (device_may_wakeup(dev))
|
2012-08-14 09:19:22 +00:00
|
|
|
disable_irq_wake(rtc->irq);
|
2008-02-06 09:38:59 +00:00
|
|
|
mr = rtt_readl(rtc, MR);
|
|
|
|
rtt_writel(rtc, MR, mr | rtc->imr);
|
2015-03-02 09:18:14 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&rtc->lock, flags);
|
|
|
|
rtc->suspended = false;
|
|
|
|
at91_rtc_cache_events(rtc);
|
|
|
|
at91_rtc_flush_events(rtc);
|
|
|
|
spin_unlock_irqrestore(&rtc->lock, flags);
|
2008-02-06 09:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-29 23:20:58 +00:00
|
|
|
static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
|
|
|
|
|
2014-09-23 11:14:24 +00:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static const struct of_device_id at91_rtc_dt_ids[] = {
|
|
|
|
{ .compatible = "atmel,at91sam9260-rtt" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
|
|
|
|
#endif
|
|
|
|
|
2008-02-06 09:38:59 +00:00
|
|
|
static struct platform_driver at91_rtc_driver = {
|
2012-02-15 12:51:37 +00:00
|
|
|
.probe = at91_rtc_probe,
|
2012-12-21 21:09:38 +00:00
|
|
|
.remove = at91_rtc_remove,
|
2008-02-06 09:38:59 +00:00
|
|
|
.shutdown = at91_rtc_shutdown,
|
2012-02-15 12:51:37 +00:00
|
|
|
.driver = {
|
|
|
|
.name = "rtc-at91sam9",
|
2013-04-29 23:20:58 +00:00
|
|
|
.pm = &at91_rtc_pm_ops,
|
2014-09-23 11:14:24 +00:00
|
|
|
.of_match_table = of_match_ptr(at91_rtc_dt_ids),
|
2012-02-15 12:51:37 +00:00
|
|
|
},
|
2008-02-06 09:38:59 +00:00
|
|
|
};
|
|
|
|
|
2012-10-05 00:13:54 +00:00
|
|
|
module_platform_driver(at91_rtc_driver);
|
2008-02-06 09:38:59 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Michel Benoit");
|
|
|
|
MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");
|
|
|
|
MODULE_LICENSE("GPL");
|