Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/rtc/rtc-pl031.c |
| 3 | * |
| 4 | * Real Time Clock interface for ARM AMBA PrimeCell 031 RTC |
| 5 | * |
| 6 | * Author: Deepak Saxena <dsaxena@plexity.net> |
| 7 | * |
| 8 | * Copyright 2006 (c) MontaVista Software, Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/rtc.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/pm.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 25 | |
| 26 | #include <linux/amba/bus.h> |
| 27 | |
| 28 | #include <asm/io.h> |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 29 | #include <asm/hardware.h> |
| 30 | #include <asm/irq.h> |
| 31 | #include <asm/rtc.h> |
| 32 | |
| 33 | /* |
| 34 | * Register definitions |
| 35 | */ |
| 36 | #define RTC_DR 0x00 /* Data read register */ |
| 37 | #define RTC_MR 0x04 /* Match register */ |
| 38 | #define RTC_LR 0x08 /* Data load register */ |
| 39 | #define RTC_CR 0x0c /* Control register */ |
| 40 | #define RTC_IMSC 0x10 /* Interrupt mask and set register */ |
| 41 | #define RTC_RIS 0x14 /* Raw interrupt status register */ |
| 42 | #define RTC_MIS 0x18 /* Masked interrupt status register */ |
| 43 | #define RTC_ICR 0x1c /* Interrupt clear register */ |
| 44 | |
| 45 | struct pl031_local { |
| 46 | struct rtc_device *rtc; |
| 47 | void __iomem *base; |
| 48 | }; |
| 49 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 50 | static irqreturn_t pl031_interrupt(int irq, void *dev_id) |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 51 | { |
| 52 | struct rtc_device *rtc = dev_id; |
| 53 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 54 | rtc_update_irq(rtc, 1, RTC_AF); |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 55 | |
| 56 | return IRQ_HANDLED; |
| 57 | } |
| 58 | |
| 59 | static int pl031_open(struct device *dev) |
| 60 | { |
| 61 | /* |
| 62 | * We request IRQ in pl031_probe, so nothing to do here... |
| 63 | */ |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static void pl031_release(struct device *dev) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 72 | { |
| 73 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 74 | |
| 75 | switch (cmd) { |
| 76 | case RTC_AIE_OFF: |
| 77 | __raw_writel(1, ldata->base + RTC_MIS); |
| 78 | return 0; |
| 79 | case RTC_AIE_ON: |
| 80 | __raw_writel(0, ldata->base + RTC_MIS); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | return -ENOIOCTLCMD; |
| 85 | } |
| 86 | |
| 87 | static int pl031_read_time(struct device *dev, struct rtc_time *tm) |
| 88 | { |
| 89 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 90 | |
| 91 | rtc_time_to_tm(__raw_readl(ldata->base + RTC_DR), tm); |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int pl031_set_time(struct device *dev, struct rtc_time *tm) |
| 97 | { |
| 98 | unsigned long time; |
| 99 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 100 | |
| 101 | rtc_tm_to_time(tm, &time); |
| 102 | __raw_writel(time, ldata->base + RTC_LR); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 108 | { |
| 109 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 110 | |
| 111 | rtc_time_to_tm(__raw_readl(ldata->base + RTC_MR), &alarm->time); |
| 112 | alarm->pending = __raw_readl(ldata->base + RTC_RIS); |
| 113 | alarm->enabled = __raw_readl(ldata->base + RTC_IMSC); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 119 | { |
| 120 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 121 | unsigned long time; |
| 122 | |
| 123 | rtc_tm_to_time(&alarm->time, &time); |
| 124 | |
| 125 | __raw_writel(time, ldata->base + RTC_MR); |
| 126 | __raw_writel(!alarm->enabled, ldata->base + RTC_MIS); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 131 | static const struct rtc_class_ops pl031_ops = { |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 132 | .open = pl031_open, |
| 133 | .release = pl031_release, |
| 134 | .ioctl = pl031_ioctl, |
| 135 | .read_time = pl031_read_time, |
| 136 | .set_time = pl031_set_time, |
| 137 | .read_alarm = pl031_read_alarm, |
| 138 | .set_alarm = pl031_set_alarm, |
| 139 | }; |
| 140 | |
| 141 | static int pl031_remove(struct amba_device *adev) |
| 142 | { |
| 143 | struct pl031_local *ldata = dev_get_drvdata(&adev->dev); |
| 144 | |
| 145 | if (ldata) { |
| 146 | dev_set_drvdata(&adev->dev, NULL); |
| 147 | free_irq(adev->irq[0], ldata->rtc); |
| 148 | rtc_device_unregister(ldata->rtc); |
| 149 | iounmap(ldata->base); |
| 150 | kfree(ldata); |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int pl031_probe(struct amba_device *adev, void *id) |
| 157 | { |
| 158 | int ret; |
| 159 | struct pl031_local *ldata; |
| 160 | |
| 161 | |
| 162 | ldata = kmalloc(sizeof(struct pl031_local), GFP_KERNEL); |
| 163 | if (!ldata) { |
| 164 | ret = -ENOMEM; |
| 165 | goto out; |
| 166 | } |
| 167 | dev_set_drvdata(&adev->dev, ldata); |
| 168 | |
| 169 | ldata->base = ioremap(adev->res.start, |
| 170 | adev->res.end - adev->res.start + 1); |
| 171 | if (!ldata->base) { |
| 172 | ret = -ENOMEM; |
| 173 | goto out_no_remap; |
| 174 | } |
| 175 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 176 | if (request_irq(adev->irq[0], pl031_interrupt, IRQF_DISABLED, |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 177 | "rtc-pl031", ldata->rtc)) { |
| 178 | ret = -EIO; |
| 179 | goto out_no_irq; |
| 180 | } |
| 181 | |
| 182 | ldata->rtc = rtc_device_register("pl031", &adev->dev, &pl031_ops, |
| 183 | THIS_MODULE); |
| 184 | if (IS_ERR(ldata->rtc)) { |
| 185 | ret = PTR_ERR(ldata->rtc); |
| 186 | goto out_no_rtc; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | |
| 191 | out_no_rtc: |
| 192 | free_irq(adev->irq[0], ldata->rtc); |
| 193 | out_no_irq: |
| 194 | iounmap(ldata->base); |
| 195 | out_no_remap: |
| 196 | dev_set_drvdata(&adev->dev, NULL); |
| 197 | kfree(ldata); |
| 198 | out: |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static struct amba_id pl031_ids[] __initdata = { |
| 203 | { |
| 204 | .id = 0x00041031, |
| 205 | .mask = 0x000fffff, }, |
| 206 | {0, 0}, |
| 207 | }; |
| 208 | |
| 209 | static struct amba_driver pl031_driver = { |
| 210 | .drv = { |
| 211 | .name = "rtc-pl031", |
| 212 | }, |
| 213 | .id_table = pl031_ids, |
| 214 | .probe = pl031_probe, |
| 215 | .remove = pl031_remove, |
| 216 | }; |
| 217 | |
| 218 | static int __init pl031_init(void) |
| 219 | { |
| 220 | return amba_driver_register(&pl031_driver); |
| 221 | } |
| 222 | |
| 223 | static void __exit pl031_exit(void) |
| 224 | { |
| 225 | amba_driver_unregister(&pl031_driver); |
| 226 | } |
| 227 | |
| 228 | module_init(pl031_init); |
| 229 | module_exit(pl031_exit); |
| 230 | |
| 231 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net"); |
| 232 | MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver"); |
| 233 | MODULE_LICENSE("GPL"); |