aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-spear.c
diff options
context:
space:
mode:
authorRajeev Kumar <rajeev-dlh.kumar@st.com>2011-05-26 19:25:09 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-05-26 20:12:33 -0400
commit0942a71e435f5198921e8f072540f208f0176542 (patch)
treea1d40dc3e84acccf48276d5a6fcfc5b469db84ff /drivers/rtc/rtc-spear.c
parentc258f9a0aab09366070f3c9283070edead23d4cf (diff)
rtc: add support for spear rtc
Signed-off-by: Rajeev Kumar <rajeev-dlh.kumar@st.com> Signed-off-by: Viresh Kumar <viresh.kumar@st.com> Cc: Alessandro Zummo <a.zummo@towertech.it> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/rtc/rtc-spear.c')
-rw-r--r--drivers/rtc/rtc-spear.c534
1 files changed, 534 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-spear.c b/drivers/rtc/rtc-spear.c
new file mode 100644
index 000000000000..893bac2bb21b
--- /dev/null
+++ b/drivers/rtc/rtc-spear.c
@@ -0,0 +1,534 @@
1/*
2 * drivers/rtc/rtc-spear.c
3 *
4 * Copyright (C) 2010 ST Microelectronics
5 * Rajeev Kumar<rajeev-dlh.kumar@st.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/bcd.h>
13#include <linux/clk.h>
14#include <linux/delay.h>
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/irq.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/rtc.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23
24/* RTC registers */
25#define TIME_REG 0x00
26#define DATE_REG 0x04
27#define ALARM_TIME_REG 0x08
28#define ALARM_DATE_REG 0x0C
29#define CTRL_REG 0x10
30#define STATUS_REG 0x14
31
32/* TIME_REG & ALARM_TIME_REG */
33#define SECONDS_UNITS (0xf<<0) /* seconds units position */
34#define SECONDS_TENS (0x7<<4) /* seconds tens position */
35#define MINUTES_UNITS (0xf<<8) /* minutes units position */
36#define MINUTES_TENS (0x7<<12) /* minutes tens position */
37#define HOURS_UNITS (0xf<<16) /* hours units position */
38#define HOURS_TENS (0x3<<20) /* hours tens position */
39
40/* DATE_REG & ALARM_DATE_REG */
41#define DAYS_UNITS (0xf<<0) /* days units position */
42#define DAYS_TENS (0x3<<4) /* days tens position */
43#define MONTHS_UNITS (0xf<<8) /* months units position */
44#define MONTHS_TENS (0x1<<12) /* months tens position */
45#define YEARS_UNITS (0xf<<16) /* years units position */
46#define YEARS_TENS (0xf<<20) /* years tens position */
47#define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */
48#define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */
49
50/* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
51#define SECOND_SHIFT 0x00 /* seconds units */
52#define MINUTE_SHIFT 0x08 /* minutes units position */
53#define HOUR_SHIFT 0x10 /* hours units position */
54#define MDAY_SHIFT 0x00 /* Month day shift */
55#define MONTH_SHIFT 0x08 /* Month shift */
56#define YEAR_SHIFT 0x10 /* Year shift */
57
58#define SECOND_MASK 0x7F
59#define MIN_MASK 0x7F
60#define HOUR_MASK 0x3F
61#define DAY_MASK 0x3F
62#define MONTH_MASK 0x7F
63#define YEAR_MASK 0xFFFF
64
65/* date reg equal to time reg, for debug only */
66#define TIME_BYP (1<<9)
67#define INT_ENABLE (1<<31) /* interrupt enable */
68
69/* STATUS_REG */
70#define CLK_UNCONNECTED (1<<0)
71#define PEND_WR_TIME (1<<2)
72#define PEND_WR_DATE (1<<3)
73#define LOST_WR_TIME (1<<4)
74#define LOST_WR_DATE (1<<5)
75#define RTC_INT_MASK (1<<31)
76#define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE)
77#define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE)
78
79struct spear_rtc_config {
80 struct clk *clk;
81 spinlock_t lock;
82 void __iomem *ioaddr;
83};
84
85static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
86{
87 unsigned int val;
88 unsigned long flags;
89
90 spin_lock_irqsave(&config->lock, flags);
91 val = readl(config->ioaddr + STATUS_REG);
92 val |= RTC_INT_MASK;
93 writel(val, config->ioaddr + STATUS_REG);
94 spin_unlock_irqrestore(&config->lock, flags);
95}
96
97static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
98{
99 unsigned int val;
100
101 val = readl(config->ioaddr + CTRL_REG);
102 if (!(val & INT_ENABLE)) {
103 spear_rtc_clear_interrupt(config);
104 val |= INT_ENABLE;
105 writel(val, config->ioaddr + CTRL_REG);
106 }
107}
108
109static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
110{
111 unsigned int val;
112
113 val = readl(config->ioaddr + CTRL_REG);
114 if (val & INT_ENABLE) {
115 val &= ~INT_ENABLE;
116 writel(val, config->ioaddr + CTRL_REG);
117 }
118}
119
120static inline int is_write_complete(struct spear_rtc_config *config)
121{
122 int ret = 0;
123 unsigned long flags;
124
125 spin_lock_irqsave(&config->lock, flags);
126 if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
127 ret = -EIO;
128 spin_unlock_irqrestore(&config->lock, flags);
129
130 return ret;
131}
132
133static void rtc_wait_not_busy(struct spear_rtc_config *config)
134{
135 int status, count = 0;
136 unsigned long flags;
137
138 /* Assuming BUSY may stay active for 80 msec) */
139 for (count = 0; count < 80; count++) {
140 spin_lock_irqsave(&config->lock, flags);
141 status = readl(config->ioaddr + STATUS_REG);
142 spin_unlock_irqrestore(&config->lock, flags);
143 if ((status & STATUS_BUSY) == 0)
144 break;
145 /* check status busy, after each msec */
146 msleep(1);
147 }
148}
149
150static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
151{
152 struct rtc_device *rtc = (struct rtc_device *)dev_id;
153 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
154 unsigned long flags, events = 0;
155 unsigned int irq_data;
156
157 spin_lock_irqsave(&config->lock, flags);
158 irq_data = readl(config->ioaddr + STATUS_REG);
159 spin_unlock_irqrestore(&config->lock, flags);
160
161 if ((irq_data & RTC_INT_MASK)) {
162 spear_rtc_clear_interrupt(config);
163 events = RTC_IRQF | RTC_AF;
164 rtc_update_irq(rtc, 1, events);
165 return IRQ_HANDLED;
166 } else
167 return IRQ_NONE;
168
169}
170
171static int tm2bcd(struct rtc_time *tm)
172{
173 if (rtc_valid_tm(tm) != 0)
174 return -EINVAL;
175 tm->tm_sec = bin2bcd(tm->tm_sec);
176 tm->tm_min = bin2bcd(tm->tm_min);
177 tm->tm_hour = bin2bcd(tm->tm_hour);
178 tm->tm_mday = bin2bcd(tm->tm_mday);
179 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
180 tm->tm_year = bin2bcd(tm->tm_year);
181
182 return 0;
183}
184
185static void bcd2tm(struct rtc_time *tm)
186{
187 tm->tm_sec = bcd2bin(tm->tm_sec);
188 tm->tm_min = bcd2bin(tm->tm_min);
189 tm->tm_hour = bcd2bin(tm->tm_hour);
190 tm->tm_mday = bcd2bin(tm->tm_mday);
191 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
192 /* epoch == 1900 */
193 tm->tm_year = bcd2bin(tm->tm_year);
194}
195
196/*
197 * spear_rtc_read_time - set the time
198 * @dev: rtc device in use
199 * @tm: holds date and time
200 *
201 * This function read time and date. On success it will return 0
202 * otherwise -ve error is returned.
203 */
204static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
205{
206 struct platform_device *pdev = to_platform_device(dev);
207 struct rtc_device *rtc = platform_get_drvdata(pdev);
208 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
209 unsigned int time, date;
210
211 /* we don't report wday/yday/isdst ... */
212 rtc_wait_not_busy(config);
213
214 time = readl(config->ioaddr + TIME_REG);
215 date = readl(config->ioaddr + DATE_REG);
216 tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
217 tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
218 tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
219 tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
220 tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
221 tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
222
223 bcd2tm(tm);
224 return 0;
225}
226
227/*
228 * spear_rtc_set_time - set the time
229 * @dev: rtc device in use
230 * @tm: holds date and time
231 *
232 * This function set time and date. On success it will return 0
233 * otherwise -ve error is returned.
234 */
235static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
236{
237 struct platform_device *pdev = to_platform_device(dev);
238 struct rtc_device *rtc = platform_get_drvdata(pdev);
239 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
240 unsigned int time, date, err = 0;
241
242 if (tm2bcd(tm) < 0)
243 return -EINVAL;
244
245 rtc_wait_not_busy(config);
246 time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
247 (tm->tm_hour << HOUR_SHIFT);
248 date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
249 (tm->tm_year << YEAR_SHIFT);
250 writel(time, config->ioaddr + TIME_REG);
251 writel(date, config->ioaddr + DATE_REG);
252 err = is_write_complete(config);
253 if (err < 0)
254 return err;
255
256 return 0;
257}
258
259/*
260 * spear_rtc_read_alarm - read the alarm time
261 * @dev: rtc device in use
262 * @alm: holds alarm date and time
263 *
264 * This function read alarm time and date. On success it will return 0
265 * otherwise -ve error is returned.
266 */
267static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
268{
269 struct platform_device *pdev = to_platform_device(dev);
270 struct rtc_device *rtc = platform_get_drvdata(pdev);
271 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
272 unsigned int time, date;
273
274 rtc_wait_not_busy(config);
275
276 time = readl(config->ioaddr + ALARM_TIME_REG);
277 date = readl(config->ioaddr + ALARM_DATE_REG);
278 alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
279 alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
280 alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
281 alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
282 alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
283 alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
284
285 bcd2tm(&alm->time);
286 alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
287
288 return 0;
289}
290
291/*
292 * spear_rtc_set_alarm - set the alarm time
293 * @dev: rtc device in use
294 * @alm: holds alarm date and time
295 *
296 * This function set alarm time and date. On success it will return 0
297 * otherwise -ve error is returned.
298 */
299static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
300{
301 struct platform_device *pdev = to_platform_device(dev);
302 struct rtc_device *rtc = platform_get_drvdata(pdev);
303 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
304 unsigned int time, date, err = 0;
305
306 if (tm2bcd(&alm->time) < 0)
307 return -EINVAL;
308
309 rtc_wait_not_busy(config);
310
311 time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
312 MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
313 date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
314 MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
315
316 writel(time, config->ioaddr + ALARM_TIME_REG);
317 writel(date, config->ioaddr + ALARM_DATE_REG);
318 err = is_write_complete(config);
319 if (err < 0)
320 return err;
321
322 if (alm->enabled)
323 spear_rtc_enable_interrupt(config);
324 else
325 spear_rtc_disable_interrupt(config);
326
327 return 0;
328}
329static struct rtc_class_ops spear_rtc_ops = {
330 .read_time = spear_rtc_read_time,
331 .set_time = spear_rtc_set_time,
332 .read_alarm = spear_rtc_read_alarm,
333 .set_alarm = spear_rtc_set_alarm,
334};
335
336static int __devinit spear_rtc_probe(struct platform_device *pdev)
337{
338 struct resource *res;
339 struct rtc_device *rtc;
340 struct spear_rtc_config *config;
341 unsigned int status = 0;
342 int irq;
343
344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
345 if (!res) {
346 dev_err(&pdev->dev, "no resource defined\n");
347 return -EBUSY;
348 }
349 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
350 dev_err(&pdev->dev, "rtc region already claimed\n");
351 return -EBUSY;
352 }
353
354 config = kzalloc(sizeof(*config), GFP_KERNEL);
355 if (!config) {
356 dev_err(&pdev->dev, "out of memory\n");
357 status = -ENOMEM;
358 goto err_release_region;
359 }
360
361 config->clk = clk_get(&pdev->dev, NULL);
362 if (IS_ERR(config->clk)) {
363 status = PTR_ERR(config->clk);
364 goto err_kfree;
365 }
366
367 status = clk_enable(config->clk);
368 if (status < 0)
369 goto err_clk_put;
370
371 config->ioaddr = ioremap(res->start, resource_size(res));
372 if (!config->ioaddr) {
373 dev_err(&pdev->dev, "ioremap fail\n");
374 status = -ENOMEM;
375 goto err_disable_clock;
376 }
377
378 spin_lock_init(&config->lock);
379
380 rtc = rtc_device_register(pdev->name, &pdev->dev, &spear_rtc_ops,
381 THIS_MODULE);
382 if (IS_ERR(rtc)) {
383 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
384 PTR_ERR(rtc));
385 status = PTR_ERR(rtc);
386 goto err_iounmap;
387 }
388
389 platform_set_drvdata(pdev, rtc);
390 dev_set_drvdata(&rtc->dev, config);
391
392 /* alarm irqs */
393 irq = platform_get_irq(pdev, 0);
394 if (irq < 0) {
395 dev_err(&pdev->dev, "no update irq?\n");
396 status = irq;
397 goto err_clear_platdata;
398 }
399
400 status = request_irq(irq, spear_rtc_irq, 0, pdev->name, rtc);
401 if (status) {
402 dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
403 claimed\n", irq);
404 goto err_clear_platdata;
405 }
406
407 if (!device_can_wakeup(&pdev->dev))
408 device_init_wakeup(&pdev->dev, 1);
409
410 return 0;
411
412err_clear_platdata:
413 platform_set_drvdata(pdev, NULL);
414 dev_set_drvdata(&rtc->dev, NULL);
415 rtc_device_unregister(rtc);
416err_iounmap:
417 iounmap(config->ioaddr);
418err_disable_clock:
419 clk_disable(config->clk);
420err_clk_put:
421 clk_put(config->clk);
422err_kfree:
423 kfree(config);
424err_release_region:
425 release_mem_region(res->start, resource_size(res));
426
427 return status;
428}
429
430static int __devexit spear_rtc_remove(struct platform_device *pdev)
431{
432 struct rtc_device *rtc = platform_get_drvdata(pdev);
433 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
434 int irq;
435 struct resource *res;
436
437 /* leave rtc running, but disable irqs */
438 spear_rtc_disable_interrupt(config);
439 device_init_wakeup(&pdev->dev, 0);
440 irq = platform_get_irq(pdev, 0);
441 if (irq)
442 free_irq(irq, pdev);
443 clk_disable(config->clk);
444 clk_put(config->clk);
445 iounmap(config->ioaddr);
446 kfree(config);
447 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
448 if (res)
449 release_mem_region(res->start, resource_size(res));
450 platform_set_drvdata(pdev, NULL);
451 dev_set_drvdata(&rtc->dev, NULL);
452 rtc_device_unregister(rtc);
453
454 return 0;
455}
456
457#ifdef CONFIG_PM
458
459static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
460{
461 struct rtc_device *rtc = platform_get_drvdata(pdev);
462 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
463 int irq;
464
465 irq = platform_get_irq(pdev, 0);
466 if (device_may_wakeup(&pdev->dev))
467 enable_irq_wake(irq);
468 else {
469 spear_rtc_disable_interrupt(config);
470 clk_disable(config->clk);
471 }
472
473 return 0;
474}
475
476static int spear_rtc_resume(struct platform_device *pdev)
477{
478 struct rtc_device *rtc = platform_get_drvdata(pdev);
479 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
480 int irq;
481
482 irq = platform_get_irq(pdev, 0);
483
484 if (device_may_wakeup(&pdev->dev))
485 disable_irq_wake(irq);
486 else {
487 clk_enable(config->clk);
488 spear_rtc_enable_interrupt(config);
489 }
490
491 return 0;
492}
493
494#else
495#define spear_rtc_suspend NULL
496#define spear_rtc_resume NULL
497#endif
498
499static void spear_rtc_shutdown(struct platform_device *pdev)
500{
501 struct rtc_device *rtc = platform_get_drvdata(pdev);
502 struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
503
504 spear_rtc_disable_interrupt(config);
505 clk_disable(config->clk);
506}
507
508static struct platform_driver spear_rtc_driver = {
509 .probe = spear_rtc_probe,
510 .remove = __devexit_p(spear_rtc_remove),
511 .suspend = spear_rtc_suspend,
512 .resume = spear_rtc_resume,
513 .shutdown = spear_rtc_shutdown,
514 .driver = {
515 .name = "rtc-spear",
516 },
517};
518
519static int __init rtc_init(void)
520{
521 return platform_driver_register(&spear_rtc_driver);
522}
523module_init(rtc_init);
524
525static void __exit rtc_exit(void)
526{
527 platform_driver_unregister(&spear_rtc_driver);
528}
529module_exit(rtc_exit);
530
531MODULE_ALIAS("platform:rtc-spear");
532MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
533MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
534MODULE_LICENSE("GPL");