blob: 08b4610ec5a6ca218567f25c552516effa33b6fa [file] [log] [blame]
Deepak Saxena8ae6e162006-06-25 05:47:38 -07001/*
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 Saxena8ae6e162006-06-25 05:47:38 -070015#include <linux/module.h>
16#include <linux/rtc.h>
17#include <linux/init.h>
Deepak Saxena8ae6e162006-06-25 05:47:38 -070018#include <linux/interrupt.h>
Deepak Saxena8ae6e162006-06-25 05:47:38 -070019#include <linux/amba/bus.h>
Russell King2dba8512008-04-20 12:08:04 +010020#include <linux/io.h>
Deepak Saxena8ae6e162006-06-25 05:47:38 -070021
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
34struct pl031_local {
35 struct rtc_device *rtc;
36 void __iomem *base;
37};
38
David Howells7d12e782006-10-05 14:55:46 +010039static irqreturn_t pl031_interrupt(int irq, void *dev_id)
Deepak Saxena8ae6e162006-06-25 05:47:38 -070040{
41 struct rtc_device *rtc = dev_id;
42
David Brownellab6a2d72007-05-08 00:33:30 -070043 rtc_update_irq(rtc, 1, RTC_AF);
Deepak Saxena8ae6e162006-06-25 05:47:38 -070044
45 return IRQ_HANDLED;
46}
47
48static int pl031_open(struct device *dev)
49{
50 /*
51 * We request IRQ in pl031_probe, so nothing to do here...
52 */
53 return 0;
54}
55
56static void pl031_release(struct device *dev)
57{
58}
59
60static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
61{
62 struct pl031_local *ldata = dev_get_drvdata(dev);
63
64 switch (cmd) {
65 case RTC_AIE_OFF:
66 __raw_writel(1, ldata->base + RTC_MIS);
67 return 0;
68 case RTC_AIE_ON:
69 __raw_writel(0, ldata->base + RTC_MIS);
70 return 0;
71 }
72
73 return -ENOIOCTLCMD;
74}
75
76static int pl031_read_time(struct device *dev, struct rtc_time *tm)
77{
78 struct pl031_local *ldata = dev_get_drvdata(dev);
79
80 rtc_time_to_tm(__raw_readl(ldata->base + RTC_DR), tm);
81
82 return 0;
83}
84
85static int pl031_set_time(struct device *dev, struct rtc_time *tm)
86{
87 unsigned long time;
88 struct pl031_local *ldata = dev_get_drvdata(dev);
89
90 rtc_tm_to_time(tm, &time);
91 __raw_writel(time, ldata->base + RTC_LR);
92
93 return 0;
94}
95
96static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
97{
98 struct pl031_local *ldata = dev_get_drvdata(dev);
99
100 rtc_time_to_tm(__raw_readl(ldata->base + RTC_MR), &alarm->time);
101 alarm->pending = __raw_readl(ldata->base + RTC_RIS);
102 alarm->enabled = __raw_readl(ldata->base + RTC_IMSC);
103
104 return 0;
105}
106
107static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
108{
109 struct pl031_local *ldata = dev_get_drvdata(dev);
110 unsigned long time;
111
112 rtc_tm_to_time(&alarm->time, &time);
113
114 __raw_writel(time, ldata->base + RTC_MR);
115 __raw_writel(!alarm->enabled, ldata->base + RTC_MIS);
116
117 return 0;
118}
119
David Brownellff8371a2006-09-30 23:28:17 -0700120static const struct rtc_class_ops pl031_ops = {
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700121 .open = pl031_open,
122 .release = pl031_release,
123 .ioctl = pl031_ioctl,
124 .read_time = pl031_read_time,
125 .set_time = pl031_set_time,
126 .read_alarm = pl031_read_alarm,
127 .set_alarm = pl031_set_alarm,
128};
129
130static int pl031_remove(struct amba_device *adev)
131{
132 struct pl031_local *ldata = dev_get_drvdata(&adev->dev);
133
Russell King2dba8512008-04-20 12:08:04 +0100134 amba_set_drvdata(adev, NULL);
135 free_irq(adev->irq[0], ldata->rtc);
136 rtc_device_unregister(ldata->rtc);
137 iounmap(ldata->base);
138 kfree(ldata);
139 amba_release_regions(adev);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700140
141 return 0;
142}
143
144static int pl031_probe(struct amba_device *adev, void *id)
145{
146 int ret;
147 struct pl031_local *ldata;
148
Russell King2dba8512008-04-20 12:08:04 +0100149 ret = amba_request_regions(adev, NULL);
150 if (ret)
151 goto err_req;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700152
153 ldata = kmalloc(sizeof(struct pl031_local), GFP_KERNEL);
154 if (!ldata) {
155 ret = -ENOMEM;
156 goto out;
157 }
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700158
159 ldata->base = ioremap(adev->res.start,
160 adev->res.end - adev->res.start + 1);
161 if (!ldata->base) {
162 ret = -ENOMEM;
163 goto out_no_remap;
164 }
165
Russell King2dba8512008-04-20 12:08:04 +0100166 amba_set_drvdata(adev, ldata);
167
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700168 if (request_irq(adev->irq[0], pl031_interrupt, IRQF_DISABLED,
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700169 "rtc-pl031", ldata->rtc)) {
170 ret = -EIO;
171 goto out_no_irq;
172 }
173
174 ldata->rtc = rtc_device_register("pl031", &adev->dev, &pl031_ops,
175 THIS_MODULE);
176 if (IS_ERR(ldata->rtc)) {
177 ret = PTR_ERR(ldata->rtc);
178 goto out_no_rtc;
179 }
180
181 return 0;
182
183out_no_rtc:
184 free_irq(adev->irq[0], ldata->rtc);
185out_no_irq:
186 iounmap(ldata->base);
Russell King2dba8512008-04-20 12:08:04 +0100187 amba_set_drvdata(adev, NULL);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700188out_no_remap:
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700189 kfree(ldata);
190out:
Russell King2dba8512008-04-20 12:08:04 +0100191 amba_release_regions(adev);
192err_req:
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700193 return ret;
194}
195
196static struct amba_id pl031_ids[] __initdata = {
197 {
198 .id = 0x00041031,
199 .mask = 0x000fffff, },
200 {0, 0},
201};
202
203static struct amba_driver pl031_driver = {
204 .drv = {
205 .name = "rtc-pl031",
206 },
207 .id_table = pl031_ids,
208 .probe = pl031_probe,
209 .remove = pl031_remove,
210};
211
212static int __init pl031_init(void)
213{
214 return amba_driver_register(&pl031_driver);
215}
216
217static void __exit pl031_exit(void)
218{
219 amba_driver_unregister(&pl031_driver);
220}
221
222module_init(pl031_init);
223module_exit(pl031_exit);
224
225MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net");
226MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
227MODULE_LICENSE("GPL");