rtc/spear: fix for RTC_AIE_ON and RTC_AIE_OFF ioctl errors
[cascardo/linux.git] / drivers / rtc / rtc-spear.c
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
79 struct spear_rtc_config {
80         struct clk *clk;
81         spinlock_t lock;
82         void __iomem *ioaddr;
83         unsigned int irq_wake;
84 };
85
86 static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
87 {
88         unsigned int val;
89         unsigned long flags;
90
91         spin_lock_irqsave(&config->lock, flags);
92         val = readl(config->ioaddr + STATUS_REG);
93         val |= RTC_INT_MASK;
94         writel(val, config->ioaddr + STATUS_REG);
95         spin_unlock_irqrestore(&config->lock, flags);
96 }
97
98 static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
99 {
100         unsigned int val;
101
102         val = readl(config->ioaddr + CTRL_REG);
103         if (!(val & INT_ENABLE)) {
104                 spear_rtc_clear_interrupt(config);
105                 val |= INT_ENABLE;
106                 writel(val, config->ioaddr + CTRL_REG);
107         }
108 }
109
110 static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
111 {
112         unsigned int val;
113
114         val = readl(config->ioaddr + CTRL_REG);
115         if (val & INT_ENABLE) {
116                 val &= ~INT_ENABLE;
117                 writel(val, config->ioaddr + CTRL_REG);
118         }
119 }
120
121 static inline int is_write_complete(struct spear_rtc_config *config)
122 {
123         int ret = 0;
124         unsigned long flags;
125
126         spin_lock_irqsave(&config->lock, flags);
127         if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
128                 ret = -EIO;
129         spin_unlock_irqrestore(&config->lock, flags);
130
131         return ret;
132 }
133
134 static void rtc_wait_not_busy(struct spear_rtc_config *config)
135 {
136         int status, count = 0;
137         unsigned long flags;
138
139         /* Assuming BUSY may stay active for 80 msec) */
140         for (count = 0; count < 80; count++) {
141                 spin_lock_irqsave(&config->lock, flags);
142                 status = readl(config->ioaddr + STATUS_REG);
143                 spin_unlock_irqrestore(&config->lock, flags);
144                 if ((status & STATUS_BUSY) == 0)
145                         break;
146                 /* check status busy, after each msec */
147                 msleep(1);
148         }
149 }
150
151 static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
152 {
153         struct rtc_device *rtc = (struct rtc_device *)dev_id;
154         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
155         unsigned long flags, events = 0;
156         unsigned int irq_data;
157
158         spin_lock_irqsave(&config->lock, flags);
159         irq_data = readl(config->ioaddr + STATUS_REG);
160         spin_unlock_irqrestore(&config->lock, flags);
161
162         if ((irq_data & RTC_INT_MASK)) {
163                 spear_rtc_clear_interrupt(config);
164                 events = RTC_IRQF | RTC_AF;
165                 rtc_update_irq(rtc, 1, events);
166                 return IRQ_HANDLED;
167         } else
168                 return IRQ_NONE;
169
170 }
171
172 static int tm2bcd(struct rtc_time *tm)
173 {
174         if (rtc_valid_tm(tm) != 0)
175                 return -EINVAL;
176         tm->tm_sec = bin2bcd(tm->tm_sec);
177         tm->tm_min = bin2bcd(tm->tm_min);
178         tm->tm_hour = bin2bcd(tm->tm_hour);
179         tm->tm_mday = bin2bcd(tm->tm_mday);
180         tm->tm_mon = bin2bcd(tm->tm_mon + 1);
181         tm->tm_year = bin2bcd(tm->tm_year);
182
183         return 0;
184 }
185
186 static void bcd2tm(struct rtc_time *tm)
187 {
188         tm->tm_sec = bcd2bin(tm->tm_sec);
189         tm->tm_min = bcd2bin(tm->tm_min);
190         tm->tm_hour = bcd2bin(tm->tm_hour);
191         tm->tm_mday = bcd2bin(tm->tm_mday);
192         tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
193         /* epoch == 1900 */
194         tm->tm_year = bcd2bin(tm->tm_year);
195 }
196
197 /*
198  * spear_rtc_read_time - set the time
199  * @dev: rtc device in use
200  * @tm: holds date and time
201  *
202  * This function read time and date. On success it will return 0
203  * otherwise -ve error is returned.
204  */
205 static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
206 {
207         struct platform_device *pdev = to_platform_device(dev);
208         struct rtc_device *rtc = platform_get_drvdata(pdev);
209         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
210         unsigned int time, date;
211
212         /* we don't report wday/yday/isdst ... */
213         rtc_wait_not_busy(config);
214
215         time = readl(config->ioaddr + TIME_REG);
216         date = readl(config->ioaddr + DATE_REG);
217         tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
218         tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
219         tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
220         tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
221         tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
222         tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
223
224         bcd2tm(tm);
225         return 0;
226 }
227
228 /*
229  * spear_rtc_set_time - set the time
230  * @dev: rtc device in use
231  * @tm: holds date and time
232  *
233  * This function set time and date. On success it will return 0
234  * otherwise -ve error is returned.
235  */
236 static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
237 {
238         struct platform_device *pdev = to_platform_device(dev);
239         struct rtc_device *rtc = platform_get_drvdata(pdev);
240         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
241         unsigned int time, date, err = 0;
242
243         if (tm2bcd(tm) < 0)
244                 return -EINVAL;
245
246         rtc_wait_not_busy(config);
247         time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
248                 (tm->tm_hour << HOUR_SHIFT);
249         date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
250                 (tm->tm_year << YEAR_SHIFT);
251         writel(time, config->ioaddr + TIME_REG);
252         writel(date, config->ioaddr + DATE_REG);
253         err = is_write_complete(config);
254         if (err < 0)
255                 return err;
256
257         return 0;
258 }
259
260 /*
261  * spear_rtc_read_alarm - read the alarm time
262  * @dev: rtc device in use
263  * @alm: holds alarm date and time
264  *
265  * This function read alarm time and date. On success it will return 0
266  * otherwise -ve error is returned.
267  */
268 static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
269 {
270         struct platform_device *pdev = to_platform_device(dev);
271         struct rtc_device *rtc = platform_get_drvdata(pdev);
272         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
273         unsigned int time, date;
274
275         rtc_wait_not_busy(config);
276
277         time = readl(config->ioaddr + ALARM_TIME_REG);
278         date = readl(config->ioaddr + ALARM_DATE_REG);
279         alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
280         alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
281         alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
282         alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
283         alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
284         alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
285
286         bcd2tm(&alm->time);
287         alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
288
289         return 0;
290 }
291
292 /*
293  * spear_rtc_set_alarm - set the alarm time
294  * @dev: rtc device in use
295  * @alm: holds alarm date and time
296  *
297  * This function set alarm time and date. On success it will return 0
298  * otherwise -ve error is returned.
299  */
300 static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
301 {
302         struct platform_device *pdev = to_platform_device(dev);
303         struct rtc_device *rtc = platform_get_drvdata(pdev);
304         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
305         unsigned int time, date, err = 0;
306
307         if (tm2bcd(&alm->time) < 0)
308                 return -EINVAL;
309
310         rtc_wait_not_busy(config);
311
312         time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
313                         MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
314         date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
315                         MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
316
317         writel(time, config->ioaddr + ALARM_TIME_REG);
318         writel(date, config->ioaddr + ALARM_DATE_REG);
319         err = is_write_complete(config);
320         if (err < 0)
321                 return err;
322
323         if (alm->enabled)
324                 spear_rtc_enable_interrupt(config);
325         else
326                 spear_rtc_disable_interrupt(config);
327
328         return 0;
329 }
330
331 static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
332 {
333         struct platform_device *pdev = to_platform_device(dev);
334         struct rtc_device *rtc = platform_get_drvdata(pdev);
335         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
336         int ret = 0;
337
338         spear_rtc_clear_interrupt(config);
339
340         switch (enabled) {
341         case 0:
342                 /* alarm off */
343                 spear_rtc_disable_interrupt(config);
344                 break;
345         case 1:
346                 /* alarm on */
347                 spear_rtc_enable_interrupt(config);
348                 break;
349         default:
350                 ret = -EINVAL;
351                 break;
352         }
353
354         return ret;
355 }
356
357 static struct rtc_class_ops spear_rtc_ops = {
358         .read_time = spear_rtc_read_time,
359         .set_time = spear_rtc_set_time,
360         .read_alarm = spear_rtc_read_alarm,
361         .set_alarm = spear_rtc_set_alarm,
362         .alarm_irq_enable = spear_alarm_irq_enable,
363 };
364
365 static int __devinit spear_rtc_probe(struct platform_device *pdev)
366 {
367         struct resource *res;
368         struct rtc_device *rtc;
369         struct spear_rtc_config *config;
370         unsigned int status = 0;
371         int irq;
372
373         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
374         if (!res) {
375                 dev_err(&pdev->dev, "no resource defined\n");
376                 return -EBUSY;
377         }
378         if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
379                 dev_err(&pdev->dev, "rtc region already claimed\n");
380                 return -EBUSY;
381         }
382
383         config = kzalloc(sizeof(*config), GFP_KERNEL);
384         if (!config) {
385                 dev_err(&pdev->dev, "out of memory\n");
386                 status = -ENOMEM;
387                 goto err_release_region;
388         }
389
390         config->clk = clk_get(&pdev->dev, NULL);
391         if (IS_ERR(config->clk)) {
392                 status = PTR_ERR(config->clk);
393                 goto err_kfree;
394         }
395
396         status = clk_enable(config->clk);
397         if (status < 0)
398                 goto err_clk_put;
399
400         config->ioaddr = ioremap(res->start, resource_size(res));
401         if (!config->ioaddr) {
402                 dev_err(&pdev->dev, "ioremap fail\n");
403                 status = -ENOMEM;
404                 goto err_disable_clock;
405         }
406
407         spin_lock_init(&config->lock);
408
409         rtc = rtc_device_register(pdev->name, &pdev->dev, &spear_rtc_ops,
410                         THIS_MODULE);
411         if (IS_ERR(rtc)) {
412                 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
413                                 PTR_ERR(rtc));
414                 status = PTR_ERR(rtc);
415                 goto err_iounmap;
416         }
417
418         platform_set_drvdata(pdev, rtc);
419         dev_set_drvdata(&rtc->dev, config);
420
421         /* alarm irqs */
422         irq = platform_get_irq(pdev, 0);
423         if (irq < 0) {
424                 dev_err(&pdev->dev, "no update irq?\n");
425                 status = irq;
426                 goto err_clear_platdata;
427         }
428
429         status = request_irq(irq, spear_rtc_irq, 0, pdev->name, rtc);
430         if (status) {
431                 dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
432                                 claimed\n", irq);
433                 goto err_clear_platdata;
434         }
435
436         if (!device_can_wakeup(&pdev->dev))
437                 device_init_wakeup(&pdev->dev, 1);
438
439         return 0;
440
441 err_clear_platdata:
442         platform_set_drvdata(pdev, NULL);
443         dev_set_drvdata(&rtc->dev, NULL);
444         rtc_device_unregister(rtc);
445 err_iounmap:
446         iounmap(config->ioaddr);
447 err_disable_clock:
448         clk_disable(config->clk);
449 err_clk_put:
450         clk_put(config->clk);
451 err_kfree:
452         kfree(config);
453 err_release_region:
454         release_mem_region(res->start, resource_size(res));
455
456         return status;
457 }
458
459 static int __devexit spear_rtc_remove(struct platform_device *pdev)
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         struct resource *res;
465
466         /* leave rtc running, but disable irqs */
467         spear_rtc_disable_interrupt(config);
468         device_init_wakeup(&pdev->dev, 0);
469         irq = platform_get_irq(pdev, 0);
470         if (irq)
471                 free_irq(irq, pdev);
472         clk_disable(config->clk);
473         clk_put(config->clk);
474         iounmap(config->ioaddr);
475         kfree(config);
476         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
477         if (res)
478                 release_mem_region(res->start, resource_size(res));
479         platform_set_drvdata(pdev, NULL);
480         dev_set_drvdata(&rtc->dev, NULL);
481         rtc_device_unregister(rtc);
482
483         return 0;
484 }
485
486 #ifdef CONFIG_PM
487
488 static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
489 {
490         struct rtc_device *rtc = platform_get_drvdata(pdev);
491         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
492         int irq;
493
494         irq = platform_get_irq(pdev, 0);
495         if (device_may_wakeup(&pdev->dev)) {
496                 if (!enable_irq_wake(irq))
497                         config->irq_wake = 1;
498         } else {
499                 spear_rtc_disable_interrupt(config);
500                 clk_disable(config->clk);
501         }
502
503         return 0;
504 }
505
506 static int spear_rtc_resume(struct platform_device *pdev)
507 {
508         struct rtc_device *rtc = platform_get_drvdata(pdev);
509         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
510         int irq;
511
512         irq = platform_get_irq(pdev, 0);
513
514         if (device_may_wakeup(&pdev->dev)) {
515                 if (config->irq_wake) {
516                         disable_irq_wake(irq);
517                         config->irq_wake = 0;
518                 }
519         } else {
520                 clk_enable(config->clk);
521                 spear_rtc_enable_interrupt(config);
522         }
523
524         return 0;
525 }
526
527 #else
528 #define spear_rtc_suspend       NULL
529 #define spear_rtc_resume        NULL
530 #endif
531
532 static void spear_rtc_shutdown(struct platform_device *pdev)
533 {
534         struct rtc_device *rtc = platform_get_drvdata(pdev);
535         struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
536
537         spear_rtc_disable_interrupt(config);
538         clk_disable(config->clk);
539 }
540
541 static struct platform_driver spear_rtc_driver = {
542         .probe = spear_rtc_probe,
543         .remove = __devexit_p(spear_rtc_remove),
544         .suspend = spear_rtc_suspend,
545         .resume = spear_rtc_resume,
546         .shutdown = spear_rtc_shutdown,
547         .driver = {
548                 .name = "rtc-spear",
549         },
550 };
551
552 module_platform_driver(spear_rtc_driver);
553
554 MODULE_ALIAS("platform:rtc-spear");
555 MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
556 MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
557 MODULE_LICENSE("GPL");