Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An RTC driver for the AVR32 AT32AP700x processor series. |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
| 5 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 15 | #include <linux/rtc.h> |
| 16 | #include <linux/io.h> |
| 17 | |
| 18 | /* |
| 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 |
| 22 | * much use. |
| 23 | * |
| 24 | * The alarm functionality is limited by the hardware, not supporting |
| 25 | * periodic interrupts. |
| 26 | */ |
| 27 | |
| 28 | #define RTC_CTRL 0x00 |
| 29 | #define RTC_CTRL_EN 0 |
| 30 | #define RTC_CTRL_PCLR 1 |
| 31 | #define RTC_CTRL_TOPEN 2 |
| 32 | #define RTC_CTRL_PSEL 8 |
| 33 | |
| 34 | #define RTC_VAL 0x04 |
| 35 | |
| 36 | #define RTC_TOP 0x08 |
| 37 | |
| 38 | #define RTC_IER 0x10 |
| 39 | #define RTC_IER_TOPI 0 |
| 40 | |
| 41 | #define RTC_IDR 0x14 |
| 42 | #define RTC_IDR_TOPI 0 |
| 43 | |
| 44 | #define RTC_IMR 0x18 |
| 45 | #define RTC_IMR_TOPI 0 |
| 46 | |
| 47 | #define RTC_ISR 0x1c |
| 48 | #define RTC_ISR_TOPI 0 |
| 49 | |
| 50 | #define RTC_ICR 0x20 |
| 51 | #define RTC_ICR_TOPI 0 |
| 52 | |
| 53 | #define RTC_BIT(name) (1 << RTC_##name) |
| 54 | #define RTC_BF(name, value) ((value) << RTC_##name) |
| 55 | |
| 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) |
| 60 | |
| 61 | struct rtc_at32ap700x { |
| 62 | struct rtc_device *rtc; |
| 63 | void __iomem *regs; |
| 64 | unsigned long alarm_time; |
| 65 | unsigned long irq; |
| 66 | /* Protect against concurrent register access. */ |
| 67 | spinlock_t lock; |
| 68 | }; |
| 69 | |
| 70 | static int at32_rtc_readtime(struct device *dev, struct rtc_time *tm) |
| 71 | { |
| 72 | struct rtc_at32ap700x *rtc = dev_get_drvdata(dev); |
| 73 | unsigned long now; |
| 74 | |
| 75 | now = rtc_readl(rtc, VAL); |
| 76 | rtc_time_to_tm(now, tm); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int at32_rtc_settime(struct device *dev, struct rtc_time *tm) |
| 82 | { |
| 83 | struct rtc_at32ap700x *rtc = dev_get_drvdata(dev); |
| 84 | unsigned long now; |
| 85 | int ret; |
| 86 | |
| 87 | ret = rtc_tm_to_time(tm, &now); |
| 88 | if (ret == 0) |
| 89 | rtc_writel(rtc, VAL, now); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static int at32_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 95 | { |
| 96 | struct rtc_at32ap700x *rtc = dev_get_drvdata(dev); |
| 97 | |
Haavard Skinnemoen | 529a4f4 | 2008-06-12 15:21:38 -0700 | [diff] [blame] | 98 | spin_lock_irq(&rtc->lock); |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 99 | rtc_time_to_tm(rtc->alarm_time, &alrm->time); |
Haavard Skinnemoen | 529a4f4 | 2008-06-12 15:21:38 -0700 | [diff] [blame] | 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); |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int at32_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 108 | { |
| 109 | struct rtc_at32ap700x *rtc = dev_get_drvdata(dev); |
| 110 | unsigned long rtc_unix_time; |
| 111 | unsigned long alarm_unix_time; |
| 112 | int ret; |
| 113 | |
| 114 | rtc_unix_time = rtc_readl(rtc, VAL); |
| 115 | |
| 116 | ret = rtc_tm_to_time(&alrm->time, &alarm_unix_time); |
| 117 | if (ret) |
| 118 | return ret; |
| 119 | |
| 120 | if (alarm_unix_time < rtc_unix_time) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | spin_lock_irq(&rtc->lock); |
| 124 | rtc->alarm_time = alarm_unix_time; |
| 125 | rtc_writel(rtc, TOP, rtc->alarm_time); |
Haavard Skinnemoen | 529a4f4 | 2008-06-12 15:21:38 -0700 | [diff] [blame] | 126 | if (alrm->enabled) |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 127 | rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL) |
| 128 | | RTC_BIT(CTRL_TOPEN)); |
| 129 | else |
| 130 | rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL) |
| 131 | & ~RTC_BIT(CTRL_TOPEN)); |
| 132 | spin_unlock_irq(&rtc->lock); |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static int at32_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 138 | unsigned long arg) |
| 139 | { |
| 140 | struct rtc_at32ap700x *rtc = dev_get_drvdata(dev); |
| 141 | int ret = 0; |
| 142 | |
| 143 | spin_lock_irq(&rtc->lock); |
| 144 | |
| 145 | switch (cmd) { |
| 146 | case RTC_AIE_ON: |
| 147 | if (rtc_readl(rtc, VAL) > rtc->alarm_time) { |
| 148 | ret = -EINVAL; |
| 149 | break; |
| 150 | } |
| 151 | rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL) |
| 152 | | RTC_BIT(CTRL_TOPEN)); |
| 153 | rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI)); |
| 154 | rtc_writel(rtc, IER, RTC_BIT(IER_TOPI)); |
| 155 | break; |
| 156 | case RTC_AIE_OFF: |
| 157 | rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL) |
| 158 | & ~RTC_BIT(CTRL_TOPEN)); |
| 159 | rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI)); |
| 160 | rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI)); |
| 161 | break; |
| 162 | default: |
| 163 | ret = -ENOIOCTLCMD; |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | spin_unlock_irq(&rtc->lock); |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static irqreturn_t at32_rtc_interrupt(int irq, void *dev_id) |
| 173 | { |
| 174 | struct rtc_at32ap700x *rtc = (struct rtc_at32ap700x *)dev_id; |
| 175 | unsigned long isr = rtc_readl(rtc, ISR); |
| 176 | unsigned long events = 0; |
| 177 | int ret = IRQ_NONE; |
| 178 | |
| 179 | spin_lock(&rtc->lock); |
| 180 | |
| 181 | if (isr & RTC_BIT(ISR_TOPI)) { |
| 182 | rtc_writel(rtc, ICR, RTC_BIT(ICR_TOPI)); |
| 183 | rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI)); |
| 184 | rtc_writel(rtc, CTRL, rtc_readl(rtc, CTRL) |
| 185 | & ~RTC_BIT(CTRL_TOPEN)); |
| 186 | rtc_writel(rtc, VAL, rtc->alarm_time); |
| 187 | events = RTC_AF | RTC_IRQF; |
| 188 | rtc_update_irq(rtc->rtc, 1, events); |
| 189 | ret = IRQ_HANDLED; |
| 190 | } |
| 191 | |
| 192 | spin_unlock(&rtc->lock); |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static struct rtc_class_ops at32_rtc_ops = { |
| 198 | .ioctl = at32_rtc_ioctl, |
| 199 | .read_time = at32_rtc_readtime, |
| 200 | .set_time = at32_rtc_settime, |
| 201 | .read_alarm = at32_rtc_readalarm, |
| 202 | .set_alarm = at32_rtc_setalarm, |
| 203 | }; |
| 204 | |
| 205 | static int __init at32_rtc_probe(struct platform_device *pdev) |
| 206 | { |
| 207 | struct resource *regs; |
| 208 | struct rtc_at32ap700x *rtc; |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 209 | int irq; |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 210 | int ret; |
| 211 | |
| 212 | rtc = kzalloc(sizeof(struct rtc_at32ap700x), GFP_KERNEL); |
| 213 | if (!rtc) { |
| 214 | dev_dbg(&pdev->dev, "out of memory\n"); |
| 215 | return -ENOMEM; |
| 216 | } |
| 217 | |
| 218 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 219 | if (!regs) { |
| 220 | dev_dbg(&pdev->dev, "no mmio resource defined\n"); |
| 221 | ret = -ENXIO; |
| 222 | goto out; |
| 223 | } |
| 224 | |
| 225 | irq = platform_get_irq(pdev, 0); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 226 | if (irq <= 0) { |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 227 | dev_dbg(&pdev->dev, "could not get irq\n"); |
| 228 | ret = -ENXIO; |
| 229 | goto out; |
| 230 | } |
| 231 | |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 232 | rtc->irq = irq; |
| 233 | rtc->regs = ioremap(regs->start, regs->end - regs->start + 1); |
| 234 | if (!rtc->regs) { |
| 235 | ret = -ENOMEM; |
| 236 | dev_dbg(&pdev->dev, "could not map I/O memory\n"); |
David Brownell | 8d431db | 2007-12-17 16:19:50 -0800 | [diff] [blame] | 237 | goto out; |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 238 | } |
| 239 | spin_lock_init(&rtc->lock); |
| 240 | |
| 241 | /* |
| 242 | * Maybe init RTC: count from zero at 1 Hz, disable wrap irq. |
| 243 | * |
| 244 | * Do not reset VAL register, as it can hold an old time |
| 245 | * from last JTAG reset. |
| 246 | */ |
| 247 | if (!(rtc_readl(rtc, CTRL) & RTC_BIT(CTRL_EN))) { |
| 248 | rtc_writel(rtc, CTRL, RTC_BIT(CTRL_PCLR)); |
| 249 | rtc_writel(rtc, IDR, RTC_BIT(IDR_TOPI)); |
| 250 | rtc_writel(rtc, CTRL, RTC_BF(CTRL_PSEL, 0xe) |
| 251 | | RTC_BIT(CTRL_EN)); |
| 252 | } |
| 253 | |
David Brownell | 8d431db | 2007-12-17 16:19:50 -0800 | [diff] [blame] | 254 | ret = request_irq(irq, at32_rtc_interrupt, IRQF_SHARED, "rtc", rtc); |
| 255 | if (ret) { |
| 256 | dev_dbg(&pdev->dev, "could not request irq %d\n", irq); |
| 257 | goto out_iounmap; |
| 258 | } |
| 259 | |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 260 | platform_set_drvdata(pdev, rtc); |
| 261 | |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 262 | rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 263 | &at32_rtc_ops, THIS_MODULE); |
| 264 | if (IS_ERR(rtc->rtc)) { |
| 265 | dev_dbg(&pdev->dev, "could not register rtc device\n"); |
| 266 | ret = PTR_ERR(rtc->rtc); |
David Brownell | 8d431db | 2007-12-17 16:19:50 -0800 | [diff] [blame] | 267 | goto out_free_irq; |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Haavard Skinnemoen | f3a24e1 | 2008-06-30 10:54:31 +0200 | [diff] [blame] | 270 | device_init_wakeup(&pdev->dev, 1); |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 271 | |
| 272 | dev_info(&pdev->dev, "Atmel RTC for AT32AP700x at %08lx irq %ld\n", |
| 273 | (unsigned long)rtc->regs, rtc->irq); |
| 274 | |
| 275 | return 0; |
| 276 | |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 277 | out_free_irq: |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 278 | platform_set_drvdata(pdev, NULL); |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 279 | free_irq(irq, rtc); |
David Brownell | 8d431db | 2007-12-17 16:19:50 -0800 | [diff] [blame] | 280 | out_iounmap: |
| 281 | iounmap(rtc->regs); |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 282 | out: |
| 283 | kfree(rtc); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | static int __exit at32_rtc_remove(struct platform_device *pdev) |
| 288 | { |
| 289 | struct rtc_at32ap700x *rtc = platform_get_drvdata(pdev); |
| 290 | |
Haavard Skinnemoen | f3a24e1 | 2008-06-30 10:54:31 +0200 | [diff] [blame] | 291 | device_init_wakeup(&pdev->dev, 0); |
| 292 | |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 293 | free_irq(rtc->irq, rtc); |
| 294 | iounmap(rtc->regs); |
| 295 | rtc_device_unregister(rtc->rtc); |
| 296 | kfree(rtc); |
| 297 | platform_set_drvdata(pdev, NULL); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 302 | MODULE_ALIAS("platform:at32ap700x_rtc"); |
Hans-Christian Egtvedt | fa04e78 | 2007-07-17 04:05:00 -0700 | [diff] [blame] | 303 | |
| 304 | static struct platform_driver at32_rtc_driver = { |
| 305 | .remove = __exit_p(at32_rtc_remove), |
| 306 | .driver = { |
| 307 | .name = "at32ap700x_rtc", |
| 308 | .owner = THIS_MODULE, |
| 309 | }, |
| 310 | }; |
| 311 | |
| 312 | static int __init at32_rtc_init(void) |
| 313 | { |
| 314 | return platform_driver_probe(&at32_rtc_driver, at32_rtc_probe); |
| 315 | } |
| 316 | module_init(at32_rtc_init); |
| 317 | |
| 318 | static void __exit at32_rtc_exit(void) |
| 319 | { |
| 320 | platform_driver_unregister(&at32_rtc_driver); |
| 321 | } |
| 322 | module_exit(at32_rtc_exit); |
| 323 | |
| 324 | MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>"); |
| 325 | MODULE_DESCRIPTION("Real time clock for AVR32 AT32AP700x"); |
| 326 | MODULE_LICENSE("GPL"); |