Linux 2.6.34-rc3
[pohmelfs.git] / drivers / rtc / rtc-mv.c
blobdc052ce6e63a8af6525cd067d0748927298c0c41
1 /*
2 * Driver for the RTC in Marvell SoCs.
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
13 #include <linux/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
18 #define RTC_TIME_REG_OFFS 0
19 #define RTC_SECONDS_OFFS 0
20 #define RTC_MINUTES_OFFS 8
21 #define RTC_HOURS_OFFS 16
22 #define RTC_WDAY_OFFS 24
23 #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
25 #define RTC_DATE_REG_OFFS 4
26 #define RTC_MDAY_OFFS 0
27 #define RTC_MONTH_OFFS 8
28 #define RTC_YEAR_OFFS 16
30 #define RTC_ALARM_TIME_REG_OFFS 8
31 #define RTC_ALARM_DATE_REG_OFFS 0xc
32 #define RTC_ALARM_VALID (1 << 7)
34 #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
35 #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
37 struct rtc_plat_data {
38 struct rtc_device *rtc;
39 void __iomem *ioaddr;
40 int irq;
43 static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
45 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
46 void __iomem *ioaddr = pdata->ioaddr;
47 u32 rtc_reg;
49 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
50 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
51 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
52 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
53 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
55 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
56 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
57 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
58 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
60 return 0;
63 static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
65 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
66 void __iomem *ioaddr = pdata->ioaddr;
67 u32 rtc_time, rtc_date;
68 unsigned int year, month, day, hour, minute, second, wday;
70 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
71 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
73 second = rtc_time & 0x7f;
74 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
75 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
76 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
78 day = rtc_date & 0x3f;
79 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
80 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
82 tm->tm_sec = bcd2bin(second);
83 tm->tm_min = bcd2bin(minute);
84 tm->tm_hour = bcd2bin(hour);
85 tm->tm_mday = bcd2bin(day);
86 tm->tm_wday = bcd2bin(wday);
87 tm->tm_mon = bcd2bin(month) - 1;
88 /* hw counts from year 2000, but tm_year is relative to 1900 */
89 tm->tm_year = bcd2bin(year) + 100;
91 return rtc_valid_tm(tm);
94 static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
96 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
97 void __iomem *ioaddr = pdata->ioaddr;
98 u32 rtc_time, rtc_date;
99 unsigned int year, month, day, hour, minute, second, wday;
101 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
102 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
104 second = rtc_time & 0x7f;
105 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
106 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
107 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
109 day = rtc_date & 0x3f;
110 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
111 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
113 alm->time.tm_sec = bcd2bin(second);
114 alm->time.tm_min = bcd2bin(minute);
115 alm->time.tm_hour = bcd2bin(hour);
116 alm->time.tm_mday = bcd2bin(day);
117 alm->time.tm_wday = bcd2bin(wday);
118 alm->time.tm_mon = bcd2bin(month) - 1;
119 /* hw counts from year 2000, but tm_year is relative to 1900 */
120 alm->time.tm_year = bcd2bin(year) + 100;
122 if (rtc_valid_tm(&alm->time) < 0) {
123 dev_err(dev, "retrieved alarm date/time is not valid.\n");
124 rtc_time_to_tm(0, &alm->time);
127 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
128 return 0;
131 static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
133 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
134 void __iomem *ioaddr = pdata->ioaddr;
135 u32 rtc_reg = 0;
137 if (alm->time.tm_sec >= 0)
138 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
139 << RTC_SECONDS_OFFS;
140 if (alm->time.tm_min >= 0)
141 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
142 << RTC_MINUTES_OFFS;
143 if (alm->time.tm_hour >= 0)
144 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
145 << RTC_HOURS_OFFS;
147 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
149 if (alm->time.tm_mday >= 0)
150 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
151 << RTC_MDAY_OFFS;
152 else
153 rtc_reg = 0;
155 if (alm->time.tm_mon >= 0)
156 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
157 << RTC_MONTH_OFFS;
159 if (alm->time.tm_year >= 0)
160 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
161 << RTC_YEAR_OFFS;
163 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
164 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
165 writel(alm->enabled ? 1 : 0,
166 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
168 return 0;
171 static int mv_rtc_ioctl(struct device *dev, unsigned int cmd,
172 unsigned long arg)
174 struct platform_device *pdev = to_platform_device(dev);
175 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
176 void __iomem *ioaddr = pdata->ioaddr;
178 if (pdata->irq < 0)
179 return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */
180 switch (cmd) {
181 case RTC_AIE_OFF:
182 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
183 break;
184 case RTC_AIE_ON:
185 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
186 break;
187 default:
188 return -ENOIOCTLCMD;
190 return 0;
193 static irqreturn_t mv_rtc_interrupt(int irq, void *data)
195 struct rtc_plat_data *pdata = data;
196 void __iomem *ioaddr = pdata->ioaddr;
198 /* alarm irq? */
199 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
200 return IRQ_NONE;
202 /* clear interrupt */
203 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
204 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
205 return IRQ_HANDLED;
208 static const struct rtc_class_ops mv_rtc_ops = {
209 .read_time = mv_rtc_read_time,
210 .set_time = mv_rtc_set_time,
213 static const struct rtc_class_ops mv_rtc_alarm_ops = {
214 .read_time = mv_rtc_read_time,
215 .set_time = mv_rtc_set_time,
216 .read_alarm = mv_rtc_read_alarm,
217 .set_alarm = mv_rtc_set_alarm,
218 .ioctl = mv_rtc_ioctl,
221 static int __devinit mv_rtc_probe(struct platform_device *pdev)
223 struct resource *res;
224 struct rtc_plat_data *pdata;
225 resource_size_t size;
226 u32 rtc_time;
228 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229 if (!res)
230 return -ENODEV;
232 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
233 if (!pdata)
234 return -ENOMEM;
236 size = resource_size(res);
237 if (!devm_request_mem_region(&pdev->dev, res->start, size,
238 pdev->name))
239 return -EBUSY;
241 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
242 if (!pdata->ioaddr)
243 return -ENOMEM;
245 /* make sure the 24 hours mode is enabled */
246 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
247 if (rtc_time & RTC_HOURS_12H_MODE) {
248 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
249 return -EINVAL;
252 /* make sure it is actually functional */
253 if (rtc_time == 0x01000000) {
254 ssleep(1);
255 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
256 if (rtc_time == 0x01000000) {
257 dev_err(&pdev->dev, "internal RTC not ticking\n");
258 return -ENODEV;
262 pdata->irq = platform_get_irq(pdev, 0);
264 platform_set_drvdata(pdev, pdata);
266 if (pdata->irq >= 0) {
267 device_init_wakeup(&pdev->dev, 1);
268 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
269 &mv_rtc_alarm_ops,
270 THIS_MODULE);
271 } else
272 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
273 &mv_rtc_ops, THIS_MODULE);
274 if (IS_ERR(pdata->rtc))
275 return PTR_ERR(pdata->rtc);
277 if (pdata->irq >= 0) {
278 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
279 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
280 IRQF_DISABLED | IRQF_SHARED,
281 pdev->name, pdata) < 0) {
282 dev_warn(&pdev->dev, "interrupt not available.\n");
283 pdata->irq = -1;
287 return 0;
290 static int __exit mv_rtc_remove(struct platform_device *pdev)
292 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
294 if (pdata->irq >= 0)
295 device_init_wakeup(&pdev->dev, 0);
297 rtc_device_unregister(pdata->rtc);
298 return 0;
301 static struct platform_driver mv_rtc_driver = {
302 .remove = __exit_p(mv_rtc_remove),
303 .driver = {
304 .name = "rtc-mv",
305 .owner = THIS_MODULE,
309 static __init int mv_init(void)
311 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
314 static __exit void mv_exit(void)
316 platform_driver_unregister(&mv_rtc_driver);
319 module_init(mv_init);
320 module_exit(mv_exit);
322 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
323 MODULE_DESCRIPTION("Marvell RTC driver");
324 MODULE_LICENSE("GPL");
325 MODULE_ALIAS("platform:rtc-mv");