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 | */ |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/rtc.h> |
| 17 | #include <linux/init.h> |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 19 | #include <linux/amba/bus.h> |
Russell King | 2dba851 | 2008-04-20 12:08:04 +0100 | [diff] [blame] | 20 | #include <linux/io.h> |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Register definitions |
| 24 | */ |
| 25 | #define RTC_DR 0x00 /* Data read register */ |
| 26 | #define RTC_MR 0x04 /* Match register */ |
| 27 | #define RTC_LR 0x08 /* Data load register */ |
| 28 | #define RTC_CR 0x0c /* Control register */ |
| 29 | #define RTC_IMSC 0x10 /* Interrupt mask and set register */ |
| 30 | #define RTC_RIS 0x14 /* Raw interrupt status register */ |
| 31 | #define RTC_MIS 0x18 /* Masked interrupt status register */ |
| 32 | #define RTC_ICR 0x1c /* Interrupt clear register */ |
| 33 | |
| 34 | struct pl031_local { |
| 35 | struct rtc_device *rtc; |
| 36 | void __iomem *base; |
| 37 | }; |
| 38 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 39 | static irqreturn_t pl031_interrupt(int irq, void *dev_id) |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 40 | { |
| 41 | struct rtc_device *rtc = dev_id; |
| 42 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 43 | rtc_update_irq(rtc, 1, RTC_AF); |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 44 | |
| 45 | return IRQ_HANDLED; |
| 46 | } |
| 47 | |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 48 | static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 49 | { |
| 50 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 51 | |
| 52 | switch (cmd) { |
| 53 | case RTC_AIE_OFF: |
| 54 | __raw_writel(1, ldata->base + RTC_MIS); |
| 55 | return 0; |
| 56 | case RTC_AIE_ON: |
| 57 | __raw_writel(0, ldata->base + RTC_MIS); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | return -ENOIOCTLCMD; |
| 62 | } |
| 63 | |
| 64 | static int pl031_read_time(struct device *dev, struct rtc_time *tm) |
| 65 | { |
| 66 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 67 | |
| 68 | rtc_time_to_tm(__raw_readl(ldata->base + RTC_DR), tm); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int pl031_set_time(struct device *dev, struct rtc_time *tm) |
| 74 | { |
| 75 | unsigned long time; |
| 76 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 77 | |
| 78 | rtc_tm_to_time(tm, &time); |
| 79 | __raw_writel(time, ldata->base + RTC_LR); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 85 | { |
| 86 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 87 | |
| 88 | rtc_time_to_tm(__raw_readl(ldata->base + RTC_MR), &alarm->time); |
| 89 | alarm->pending = __raw_readl(ldata->base + RTC_RIS); |
| 90 | alarm->enabled = __raw_readl(ldata->base + RTC_IMSC); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 96 | { |
| 97 | struct pl031_local *ldata = dev_get_drvdata(dev); |
| 98 | unsigned long time; |
| 99 | |
| 100 | rtc_tm_to_time(&alarm->time, &time); |
| 101 | |
| 102 | __raw_writel(time, ldata->base + RTC_MR); |
| 103 | __raw_writel(!alarm->enabled, ldata->base + RTC_MIS); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 108 | static const struct rtc_class_ops pl031_ops = { |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 109 | .ioctl = pl031_ioctl, |
| 110 | .read_time = pl031_read_time, |
| 111 | .set_time = pl031_set_time, |
| 112 | .read_alarm = pl031_read_alarm, |
| 113 | .set_alarm = pl031_set_alarm, |
| 114 | }; |
| 115 | |
| 116 | static int pl031_remove(struct amba_device *adev) |
| 117 | { |
| 118 | struct pl031_local *ldata = dev_get_drvdata(&adev->dev); |
| 119 | |
Russell King | 2dba851 | 2008-04-20 12:08:04 +0100 | [diff] [blame] | 120 | amba_set_drvdata(adev, NULL); |
| 121 | free_irq(adev->irq[0], ldata->rtc); |
| 122 | rtc_device_unregister(ldata->rtc); |
| 123 | iounmap(ldata->base); |
| 124 | kfree(ldata); |
| 125 | amba_release_regions(adev); |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Alessandro Rubini | 03fbdb1 | 2009-05-20 22:39:08 +0100 | [diff] [blame] | 130 | static int pl031_probe(struct amba_device *adev, struct amba_id *id) |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 131 | { |
| 132 | int ret; |
| 133 | struct pl031_local *ldata; |
| 134 | |
Russell King | 2dba851 | 2008-04-20 12:08:04 +0100 | [diff] [blame] | 135 | ret = amba_request_regions(adev, NULL); |
| 136 | if (ret) |
| 137 | goto err_req; |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 138 | |
| 139 | ldata = kmalloc(sizeof(struct pl031_local), GFP_KERNEL); |
| 140 | if (!ldata) { |
| 141 | ret = -ENOMEM; |
| 142 | goto out; |
| 143 | } |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 144 | |
Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 145 | ldata->base = ioremap(adev->res.start, resource_size(&adev->res)); |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 146 | if (!ldata->base) { |
| 147 | ret = -ENOMEM; |
| 148 | goto out_no_remap; |
| 149 | } |
| 150 | |
Russell King | 2dba851 | 2008-04-20 12:08:04 +0100 | [diff] [blame] | 151 | amba_set_drvdata(adev, ldata); |
| 152 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 153 | if (request_irq(adev->irq[0], pl031_interrupt, IRQF_DISABLED, |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 154 | "rtc-pl031", ldata->rtc)) { |
| 155 | ret = -EIO; |
| 156 | goto out_no_irq; |
| 157 | } |
| 158 | |
| 159 | ldata->rtc = rtc_device_register("pl031", &adev->dev, &pl031_ops, |
| 160 | THIS_MODULE); |
| 161 | if (IS_ERR(ldata->rtc)) { |
| 162 | ret = PTR_ERR(ldata->rtc); |
| 163 | goto out_no_rtc; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | |
| 168 | out_no_rtc: |
| 169 | free_irq(adev->irq[0], ldata->rtc); |
| 170 | out_no_irq: |
| 171 | iounmap(ldata->base); |
Russell King | 2dba851 | 2008-04-20 12:08:04 +0100 | [diff] [blame] | 172 | amba_set_drvdata(adev, NULL); |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 173 | out_no_remap: |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 174 | kfree(ldata); |
| 175 | out: |
Russell King | 2dba851 | 2008-04-20 12:08:04 +0100 | [diff] [blame] | 176 | amba_release_regions(adev); |
| 177 | err_req: |
Deepak Saxena | 8ae6e16 | 2006-06-25 05:47:38 -0700 | [diff] [blame] | 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static struct amba_id pl031_ids[] __initdata = { |
| 182 | { |
| 183 | .id = 0x00041031, |
| 184 | .mask = 0x000fffff, }, |
| 185 | {0, 0}, |
| 186 | }; |
| 187 | |
| 188 | static struct amba_driver pl031_driver = { |
| 189 | .drv = { |
| 190 | .name = "rtc-pl031", |
| 191 | }, |
| 192 | .id_table = pl031_ids, |
| 193 | .probe = pl031_probe, |
| 194 | .remove = pl031_remove, |
| 195 | }; |
| 196 | |
| 197 | static int __init pl031_init(void) |
| 198 | { |
| 199 | return amba_driver_register(&pl031_driver); |
| 200 | } |
| 201 | |
| 202 | static void __exit pl031_exit(void) |
| 203 | { |
| 204 | amba_driver_unregister(&pl031_driver); |
| 205 | } |
| 206 | |
| 207 | module_init(pl031_init); |
| 208 | module_exit(pl031_exit); |
| 209 | |
| 210 | MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net"); |
| 211 | MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver"); |
| 212 | MODULE_LICENSE("GPL"); |