2 * An RTC driver for the AVR32 AT32AP700x processor series.
4 * Copyright (C) 2007 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/rtc.h>
19 * This is a bare-bones RTC. It runs during most system sleep states, but has
20 * no battery backup and gets reset during system restart. It must be
21 * initialized from an external clock (network, I2C, etc) before it can be of
24 * The alarm functionality is limited by the hardware, not supporting
25 * periodic interrupts.
30 #define RTC_CTRL_PCLR 1
31 #define RTC_CTRL_TOPEN 2
32 #define RTC_CTRL_PSEL 8
39 #define RTC_IER_TOPI 0
42 #define RTC_IDR_TOPI 0
45 #define RTC_IMR_TOPI 0
48 #define RTC_ISR_TOPI 0
51 #define RTC_ICR_TOPI 0
53 #define RTC_BIT(name) (1 << RTC_##name)
54 #define RTC_BF(name, value) ((value) << RTC_##name)
56 #define rtc_readl(dev, reg) \
57 __raw_readl((dev)->regs + RTC_##reg)
58 #define rtc_writel(dev, reg, value) \
59 __raw_writel((value), (dev)->regs + RTC_##reg)
61 struct rtc_at32ap700x
{
62 struct rtc_device
*rtc
;
64 unsigned long alarm_time
;
66 /* Protect against concurrent register access. */
70 static int at32_rtc_readtime(struct device
*dev
, struct rtc_time
*tm
)
72 struct rtc_at32ap700x
*rtc
= dev_get_drvdata(dev
);
75 now
= rtc_readl(rtc
, VAL
);
76 rtc_time_to_tm(now
, tm
);
81 static int at32_rtc_settime(struct device
*dev
, struct rtc_time
*tm
)
83 struct rtc_at32ap700x
*rtc
= dev_get_drvdata(dev
);
87 ret
= rtc_tm_to_time(tm
, &now
);
89 rtc_writel(rtc
, VAL
, now
);
94 static int at32_rtc_readalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
96 struct rtc_at32ap700x
*rtc
= dev_get_drvdata(dev
);
98 spin_lock_irq(&rtc
->lock
);
99 rtc_time_to_tm(rtc
->alarm_time
, &alrm
->time
);
100 alrm
->enabled
= rtc_readl(rtc
, IMR
) & RTC_BIT(IMR_TOPI
) ? 1 : 0;
101 alrm
->pending
= rtc_readl(rtc
, ISR
) & RTC_BIT(ISR_TOPI
) ? 1 : 0;
102 spin_unlock_irq(&rtc
->lock
);
107 static int at32_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
109 struct rtc_at32ap700x
*rtc
= dev_get_drvdata(dev
);
110 unsigned long rtc_unix_time
;
111 unsigned long alarm_unix_time
;
114 rtc_unix_time
= rtc_readl(rtc
, VAL
);
116 ret
= rtc_tm_to_time(&alrm
->time
, &alarm_unix_time
);
120 if (alarm_unix_time
< rtc_unix_time
)
123 spin_lock_irq(&rtc
->lock
);
124 rtc
->alarm_time
= alarm_unix_time
;
125 rtc_writel(rtc
, TOP
, rtc
->alarm_time
);
127 rtc_writel(rtc
, CTRL
, rtc_readl(rtc
, CTRL
)
128 | RTC_BIT(CTRL_TOPEN
));
130 rtc_writel(rtc
, CTRL
, rtc_readl(rtc
, CTRL
)
131 & ~RTC_BIT(CTRL_TOPEN
));
132 spin_unlock_irq(&rtc
->lock
);
137 static int at32_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
139 struct rtc_at32ap700x
*rtc
= dev_get_drvdata(dev
);
142 spin_lock_irq(&rtc
->lock
);
145 if (rtc_readl(rtc
, VAL
) > rtc
->alarm_time
) {
149 rtc_writel(rtc
, CTRL
, rtc_readl(rtc
, CTRL
)
150 | RTC_BIT(CTRL_TOPEN
));
151 rtc_writel(rtc
, ICR
, RTC_BIT(ICR_TOPI
));
152 rtc_writel(rtc
, IER
, RTC_BIT(IER_TOPI
));
154 rtc_writel(rtc
, CTRL
, rtc_readl(rtc
, CTRL
)
155 & ~RTC_BIT(CTRL_TOPEN
));
156 rtc_writel(rtc
, IDR
, RTC_BIT(IDR_TOPI
));
157 rtc_writel(rtc
, ICR
, RTC_BIT(ICR_TOPI
));
160 spin_unlock_irq(&rtc
->lock
);
165 static irqreturn_t
at32_rtc_interrupt(int irq
, void *dev_id
)
167 struct rtc_at32ap700x
*rtc
= (struct rtc_at32ap700x
*)dev_id
;
168 unsigned long isr
= rtc_readl(rtc
, ISR
);
169 unsigned long events
= 0;
172 spin_lock(&rtc
->lock
);
174 if (isr
& RTC_BIT(ISR_TOPI
)) {
175 rtc_writel(rtc
, ICR
, RTC_BIT(ICR_TOPI
));
176 rtc_writel(rtc
, IDR
, RTC_BIT(IDR_TOPI
));
177 rtc_writel(rtc
, CTRL
, rtc_readl(rtc
, CTRL
)
178 & ~RTC_BIT(CTRL_TOPEN
));
179 rtc_writel(rtc
, VAL
, rtc
->alarm_time
);
180 events
= RTC_AF
| RTC_IRQF
;
181 rtc_update_irq(rtc
->rtc
, 1, events
);
185 spin_unlock(&rtc
->lock
);
190 static struct rtc_class_ops at32_rtc_ops
= {
191 .read_time
= at32_rtc_readtime
,
192 .set_time
= at32_rtc_settime
,
193 .read_alarm
= at32_rtc_readalarm
,
194 .set_alarm
= at32_rtc_setalarm
,
195 .alarm_irq_enable
= at32_rtc_alarm_irq_enable
,
198 static int __init
at32_rtc_probe(struct platform_device
*pdev
)
200 struct resource
*regs
;
201 struct rtc_at32ap700x
*rtc
;
205 rtc
= kzalloc(sizeof(struct rtc_at32ap700x
), GFP_KERNEL
);
207 dev_dbg(&pdev
->dev
, "out of memory\n");
211 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
213 dev_dbg(&pdev
->dev
, "no mmio resource defined\n");
218 irq
= platform_get_irq(pdev
, 0);
220 dev_dbg(&pdev
->dev
, "could not get irq\n");
226 rtc
->regs
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
229 dev_dbg(&pdev
->dev
, "could not map I/O memory\n");
232 spin_lock_init(&rtc
->lock
);
235 * Maybe init RTC: count from zero at 1 Hz, disable wrap irq.
237 * Do not reset VAL register, as it can hold an old time
238 * from last JTAG reset.
240 if (!(rtc_readl(rtc
, CTRL
) & RTC_BIT(CTRL_EN
))) {
241 rtc_writel(rtc
, CTRL
, RTC_BIT(CTRL_PCLR
));
242 rtc_writel(rtc
, IDR
, RTC_BIT(IDR_TOPI
));
243 rtc_writel(rtc
, CTRL
, RTC_BF(CTRL_PSEL
, 0xe)
247 ret
= request_irq(irq
, at32_rtc_interrupt
, IRQF_SHARED
, "rtc", rtc
);
249 dev_dbg(&pdev
->dev
, "could not request irq %d\n", irq
);
253 platform_set_drvdata(pdev
, rtc
);
255 rtc
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
256 &at32_rtc_ops
, THIS_MODULE
);
257 if (IS_ERR(rtc
->rtc
)) {
258 dev_dbg(&pdev
->dev
, "could not register rtc device\n");
259 ret
= PTR_ERR(rtc
->rtc
);
263 device_init_wakeup(&pdev
->dev
, 1);
265 dev_info(&pdev
->dev
, "Atmel RTC for AT32AP700x at %08lx irq %ld\n",
266 (unsigned long)rtc
->regs
, rtc
->irq
);
271 platform_set_drvdata(pdev
, NULL
);
280 static int __exit
at32_rtc_remove(struct platform_device
*pdev
)
282 struct rtc_at32ap700x
*rtc
= platform_get_drvdata(pdev
);
284 device_init_wakeup(&pdev
->dev
, 0);
286 free_irq(rtc
->irq
, rtc
);
288 rtc_device_unregister(rtc
->rtc
);
290 platform_set_drvdata(pdev
, NULL
);
295 MODULE_ALIAS("platform:at32ap700x_rtc");
297 static struct platform_driver at32_rtc_driver
= {
298 .remove
= __exit_p(at32_rtc_remove
),
300 .name
= "at32ap700x_rtc",
301 .owner
= THIS_MODULE
,
305 static int __init
at32_rtc_init(void)
307 return platform_driver_probe(&at32_rtc_driver
, at32_rtc_probe
);
309 module_init(at32_rtc_init
);
311 static void __exit
at32_rtc_exit(void)
313 platform_driver_unregister(&at32_rtc_driver
);
315 module_exit(at32_rtc_exit
);
317 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
318 MODULE_DESCRIPTION("Real time clock for AVR32 AT32AP700x");
319 MODULE_LICENSE("GPL");