]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/rtc/rtc-twl4030.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / rtc / rtc-twl4030.c
1 /*
2  * drivers/rtc/rtc-twl4030.c
3  * 
4  * TWL4030 Real Time Clock interface
5  * 
6  * Copyright (C) 2007 MontaVista Software, Inc
7  * Author: Alexandre Rusev <source@mvista.com>
8  *
9  * Based on original TI driver twl4030-rtc.c
10  *   Copyright (C) 2006 Texas Instruments, Inc.
11  * 
12  * Based on rtc-omap.c
13  *   Copyright (C) 2003 MontaVista Software, Inc.
14  *   Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
15  *
16  *   Copyright (C) 2006 David Brownell
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version
21  * 2 of the License, or (at your option) any later version.
22  * 
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/ioport.h>
29 #include <linux/delay.h>
30 #include <linux/types.h>
31 #include <linux/rtc.h>
32 #include <linux/bcd.h>
33 #include <linux/platform_device.h>
34 #include <linux/spinlock.h>
35 #include <linux/interrupt.h>
36 #include <linux/device.h>
37
38 #include <asm/io.h>
39 #include <asm/mach/time.h>
40 #include <asm/system.h>
41 #include <asm/hardware.h>
42 #include <asm/irq.h>
43 #include <asm/arch/twl4030.h>
44 #include <asm/arch/twl4030-rtc.h>
45
46 #define ALL_TIME_REGS           6
47
48 /*
49  * If this driver ever becomes modularised, it will be really nice
50  * to make the epoch retain its value across module reload...
51  */
52 static int epoch = 1900;        /* year corresponding to 0x00   */
53
54 /* 
55  * Supports 1 byte read from TWL4030 RTC register. 
56  */
57 static int twl4030_rtc_read_u8(u8 * data, u8 reg)
58 {
59         int ret;
60
61         ret = twl4030_i2c_read_u8(TWL4030_MODULE_RTC, data, reg);
62         if (ret < 0) {
63                 printk(KERN_WARNING "twl4030_rtc: Could not read TWL4030"
64                        "register %X - returned %d[%x]\n", reg, ret, ret);
65         }
66         return ret;
67 }
68
69 /* 
70  * Supports 1 byte write to TWL4030 RTC registers.
71  */
72 static int twl4030_rtc_write_u8(u8 data, u8 reg)
73 {
74         int ret;
75
76         ret = twl4030_i2c_write_u8(TWL4030_MODULE_RTC, data, reg);
77         if (ret < 0) {
78                 printk(KERN_WARNING "twl4030_rtc: Could not write TWL4030" 
79                        "register %X - returned %d[%x]\n", reg, ret, ret);
80         }
81         return ret;
82 }
83
84 /* 
85  * Enables timer or alarm interrupts.
86  */
87 static int set_rtc_irq_bit(unsigned char bit)
88 {
89         unsigned char val;
90         int ret;
91
92         ret = twl4030_rtc_read_u8(&val, REG_RTC_INTERRUPTS_REG);
93         if (ret < 0)
94                 goto set_irq_out;
95
96         val |= bit;
97         ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
98
99 set_irq_out:
100         return ret;
101 }
102
103 #ifdef CONFIG_PM
104 /* 
105  * Read timer or alarm interrupts register.
106  */
107 static int get_rtc_irq_bit(unsigned char *val)
108 {
109         int ret;
110
111         ret = twl4030_rtc_read_u8(val, REG_RTC_INTERRUPTS_REG);
112         return ret;
113 }
114 #endif
115 /* 
116  * Disables timer or alarm interrupts.
117  */
118 static int mask_rtc_irq_bit(unsigned char bit)
119 {
120         unsigned char val;
121         int ret;
122
123         ret = twl4030_rtc_read_u8(&val, REG_RTC_INTERRUPTS_REG);
124         if (ret < 0)
125                 goto mask_irq_out;
126         
127         val &= ~bit;
128         ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
129
130 mask_irq_out:
131         return ret;
132 }
133
134 static int twl4030_rtc_alarm_irq_set_state(struct device *dev, int enabled)
135 {
136         int ret;
137
138         /* Allow ints for RTC ALARM updates.  */
139         if (enabled) 
140                 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
141         else 
142                 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
143
144         return ret;
145 }
146
147 /* 
148  * Gets current TWL4030 RTC time and date parameters.
149  */
150 static int get_rtc_time(struct rtc_time *rtc_tm)
151 {
152         unsigned char rtc_data[ALL_TIME_REGS + 1];
153         int ret;
154         u8 save_control;
155
156         ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
157         if (ret < 0)
158                 return ret;
159
160         save_control |= BIT_RTC_CTRL_REG_GET_TIME_M;
161
162         ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
163         if (ret < 0)
164                 return ret;
165
166         ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
167                                REG_SECONDS_REG, ALL_TIME_REGS);
168
169         if (ret < 0) {
170                 printk(KERN_ERR "twl4030_rtc: twl4030_i2c_read error.\n");
171                 return ret;
172         }
173
174         rtc_tm->tm_sec = BCD2BIN(rtc_data[0]);
175         rtc_tm->tm_min = BCD2BIN(rtc_data[1]);
176         rtc_tm->tm_hour = BCD2BIN(rtc_data[2]);
177         rtc_tm->tm_mday = BCD2BIN(rtc_data[3]);
178         rtc_tm->tm_mon = BCD2BIN(rtc_data[4]);
179         rtc_tm->tm_year = BCD2BIN(rtc_data[5]);
180
181         /*
182          * Account for differences between how the RTC uses the values
183          * and how they are defined in a struct rtc_time;
184          */
185         if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
186                 rtc_tm->tm_year += 100;
187
188         rtc_tm->tm_mon--;
189
190         return ret;
191 }
192
193 static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
194 {
195         unsigned char save_control;
196         unsigned char rtc_data[ALL_TIME_REGS + 1];
197         int ret;
198
199         /* Month range is 01..12 */
200         tm->tm_mon++;
201
202         rtc_data[1] = BIN2BCD(tm->tm_sec);
203         rtc_data[2] = BIN2BCD(tm->tm_min);
204         rtc_data[3] = BIN2BCD(tm->tm_hour);
205         rtc_data[4] = BIN2BCD(tm->tm_mday);
206         rtc_data[5] = BIN2BCD(tm->tm_mon);
207         rtc_data[6] = BIN2BCD(tm->tm_year);
208
209         /* Stop RTC while updating the TC registers */
210         ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
211         if (ret < 0)
212                 goto out;
213
214         save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
215         twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
216         if (ret < 0)
217                 goto out;
218
219         /* update all the alarm registers in one shot */
220         ret = twl4030_i2c_write(TWL4030_MODULE_RTC, rtc_data,
221                                 REG_SECONDS_REG, ALL_TIME_REGS);
222         if (ret < 0) {
223                 printk(KERN_ERR "twl4030: twl4030_i2c_write error.\n");
224                 goto out;
225         }
226
227         /* Start back RTC */
228         save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
229         ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
230
231 out:
232         return ret;
233 }
234
235 /* 
236  * Gets current TWL4030 RTC alarm time.
237  */
238 static int get_rtc_alm_time(struct rtc_time *alm_tm)
239 {
240         unsigned char rtc_data[ALL_TIME_REGS + 1];
241         int ret;
242
243         ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
244                                REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
245         if (ret < 0) {
246                 printk(KERN_ERR "twl4030_rtc: twl4030_i2c_read error.\n");
247                 return ret;
248         }
249
250         alm_tm->tm_sec = BCD2BIN(rtc_data[0]);
251         alm_tm->tm_min = BCD2BIN(rtc_data[1]);
252         alm_tm->tm_hour = BCD2BIN(rtc_data[2]);
253         alm_tm->tm_mday = BCD2BIN(rtc_data[3]);
254         alm_tm->tm_mon = BCD2BIN(rtc_data[4]);
255         alm_tm->tm_year = BCD2BIN(rtc_data[5]);
256
257         /*
258          * Account for differences between how the RTC uses the values
259          * and how they are defined in a struct rtc_time;
260          */
261         if ((alm_tm->tm_year += (epoch - 1900)) <= 69)
262                 alm_tm->tm_year += 100;
263
264         alm_tm->tm_mon--;
265
266         return ret;
267 }
268
269 static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
270 {
271         int ret;
272
273         memset(tm, 0, sizeof(struct rtc_time));
274         ret = get_rtc_time(tm);
275
276         return ret;
277 }
278
279 /* 
280  * Gets current TWL4030 RTC alarm time.
281  */
282 static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
283 {
284         int ret;
285         u8 rtc_interrupts_reg = 0;
286
287         /*
288          * This returns a struct rtc_time. Reading >= 0xc0
289          * means "don't care" or "match all". Only the tm_hour,
290          * tm_min, and tm_sec values are filled in.
291          */
292         memset(&alm->time, 0, sizeof(struct rtc_time));
293         ret = get_rtc_alm_time(&alm->time);
294
295         if (ret)
296                 goto out;
297
298         /* Check alarm enabled flag state */
299         ret =
300             ret | twl4030_i2c_read_u8(TWL4030_MODULE_RTC, &rtc_interrupts_reg,
301                                       REG_RTC_INTERRUPTS_REG);
302
303         if (ret)
304                 goto out;
305
306         if ((rtc_interrupts_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) != 0)
307                 alm->enabled = 1;
308         else
309                 alm->enabled = 0;
310
311 out:
312         return ret;
313 }
314
315 static int twl4030_rtc_irq_set_state(struct device *dev, int enabled)
316 {
317         int ret;
318
319         /* Allow ints for RTC updates.  */
320         if (enabled) 
321                 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
322         else 
323                 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
324         
325         return ret;
326 }
327
328 static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
329 {
330         unsigned char alarm_data[ALL_TIME_REGS + 1];
331         int ret;
332
333         /* Month range is 01..12 */
334         alm->time.tm_mon++;
335
336         alarm_data[1] = BIN2BCD(alm->time.tm_sec);
337         alarm_data[2] = BIN2BCD(alm->time.tm_min);
338         alarm_data[3] = BIN2BCD(alm->time.tm_hour);
339         alarm_data[4] = BIN2BCD(alm->time.tm_mday);
340         alarm_data[5] = BIN2BCD(alm->time.tm_mon);
341         alarm_data[6] = BIN2BCD(alm->time.tm_year);
342
343         /* update all the alarm registers in one shot */
344         ret = twl4030_i2c_write(TWL4030_MODULE_RTC, alarm_data,
345                                 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
346         if (ret) {
347                 printk(KERN_ERR "twl4030: twl4030_i2c_write error.\n");
348                 goto out;
349         }
350
351         ret = twl4030_rtc_alarm_irq_set_state(dev, alm->enabled);
352 out:
353         return ret;
354 }
355
356 /*
357  * We will just handle setting the frequency and make use the framework for
358  * reading the periodic interupts.
359  * @freq: Current periodic IRQ freq
360  */
361 static int twl4030_rtc_irq_set_freq(struct device *dev, int freq)
362 {
363         struct rtc_device *rtc = dev_get_drvdata(dev);
364
365         if (freq < 0 || freq > 3)
366                 return -EINVAL;
367
368         rtc->irq_freq = freq;
369
370         /* set rtc irq freq to user defined value */
371         set_rtc_irq_bit(freq);
372
373         return 0;
374 }
375
376 #ifdef  CONFIG_RTC_INTF_DEV
377
378 static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
379                              unsigned long arg)
380 {
381
382         switch (cmd) {
383         case RTC_AIE_OFF:
384                 return twl4030_rtc_alarm_irq_set_state(dev, 0);
385         case RTC_AIE_ON:
386                 return twl4030_rtc_alarm_irq_set_state(dev, 1);
387
388         case RTC_UIE_OFF:
389                 /* Fall Through */
390         case RTC_PIE_OFF:
391                 /* Mask ints from RTC updates.  */
392                 return twl4030_rtc_irq_set_state(dev, 0);
393         case RTC_UIE_ON:
394                 /* Fall Through */
395         case RTC_PIE_ON:
396                 /* Allow ints for RTC updates.  */
397                 return twl4030_rtc_irq_set_state(dev, 1);
398
399         case RTC_EPOCH_READ:
400                 return put_user(epoch, (unsigned long *)arg);
401         case RTC_EPOCH_SET:     
402                 /*
403                  * There were no RTC clocks before 1900.
404                  */
405                 if (arg < 1900)
406                         return -EINVAL;
407
408                 if (!capable(CAP_SYS_TIME))
409                         return -EACCES;
410
411                 epoch = arg;
412                 return 0;
413         default:
414                 return -ENOIOCTLCMD;
415         }
416 }
417
418 #else
419 #define omap_rtc_ioctl  NULL
420 #endif
421
422 static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
423 {
424         unsigned long events = 0;
425         int ret = IRQ_NONE;
426         int res;
427         u8 rd_reg;
428         
429         /* clear the RTC interrupt in TWL4030 power module */
430         res = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_ISR1);
431         if (res)
432                 goto out;
433
434         /* Check if interrupt is sourced by RTC */
435         if (!(rd_reg & PWR_RTC_INT_CLR))
436                 goto out;
437
438         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, PWR_RTC_INT_CLR, REG_PWR_ISR1);
439         if (res)
440                 goto out;
441
442         res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
443         if (res)
444                 goto out;
445         /*
446          * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
447          * only one (ALARM or RTC) interrupt source may be enabled
448          * at time, we also could check our results
449          * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
450          */
451         if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
452                 events |= RTC_IRQF | RTC_AF;
453         else
454                 events |= RTC_IRQF | RTC_UF;
455
456         res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
457                                    REG_RTC_STATUS_REG);
458         if (res)
459                 goto out;
460         /*
461          * Workaround for strange behaviour with T2. Need to write into ISR 
462          * register one more time to clear the interrupt. Otherwise, the same
463          * RTC event generates 2 interrupts in a row.
464          * (no errata document available)
465          */
466         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, PWR_RTC_INT_CLR, REG_PWR_ISR1);
467         if (res)
468                 goto out;
469
470         /* Notify RTC core on event */
471         rtc_update_irq(rtc, 1, events);
472
473         ret = IRQ_HANDLED;
474 out:
475         return ret;
476 }
477
478 static struct rtc_class_ops twl4030_rtc_ops = {
479         .ioctl          = twl4030_rtc_ioctl,
480         .read_time      = twl4030_rtc_read_time,
481         .set_time       = twl4030_rtc_set_time,
482         .read_alarm     = twl4030_rtc_read_alarm,
483         .set_alarm      = twl4030_rtc_set_alarm,
484         .irq_set_freq   = twl4030_rtc_irq_set_freq,
485 };
486
487 static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
488 {
489         struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
490         struct rtc_device *rtc;
491         int ret = 0;
492         u8 rd_reg;
493         
494         if (pdata != NULL && pdata->init != NULL) {
495                 ret = pdata->init();
496                 if (ret < 0)
497                         goto out;
498         }
499
500         rtc = rtc_device_register(pdev->name,
501                                   &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
502         if (IS_ERR(rtc)) {
503                 ret = -EINVAL;
504                 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
505                         PTR_ERR(rtc));
506                 goto out0;
507
508         }
509
510         /* Set the irq freq to every second */
511         rtc->irq_freq = 0;
512
513         platform_set_drvdata(pdev, rtc);
514
515         ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
516
517         if (ret < 0)
518                 goto out1;
519
520         if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
521                 dev_warn(&pdev->dev, "Power up reset detected.\n");
522
523         if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
524                 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
525
526         /* Clear RTC Power up reset and pending alarm interrupts */
527         ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
528         if (ret < 0)
529                 goto out1;
530
531         ret = request_irq(TWL4030_MODIRQ_PWR, twl4030_rtc_interrupt,
532                           IRQF_DISABLED | IRQF_SHARED, rtc->dev.bus_id, rtc);
533         if (ret < 0) {
534                 dev_err(&pdev->dev, "IRQ is not free.\n");
535                 goto out1;
536         } 
537
538         /* Check RTC module status, Enable if it is off */
539         ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
540         if (ret < 0)
541                 goto out2;
542
543         if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
544                 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
545                 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
546                 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
547                 if (ret < 0)
548                         goto out2;
549         }
550
551         ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_IMR1);
552         if (ret < 0)
553                 goto out2;
554
555         rd_reg &= PWR_RTC_IT_UNMASK;
556         /* MASK PWR - we will need this */
557         ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_IMR1);
558         if (ret < 0)
559                 goto out2;
560
561         ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_EDR1);
562         if (ret < 0)
563                 goto out2;
564
565         /* Rising edge detection enabled, needed for RTC alarm */
566         rd_reg |= 0x80;         
567         ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_EDR1);
568         if (ret < 0)
569                 goto out2;
570
571         return ret;
572
573
574 out2:
575         free_irq(TWL4030_MODIRQ_PWR, rtc);
576 out1:
577         rtc_device_unregister(rtc);
578 out0:
579         if (pdata != NULL && pdata->exit != NULL)
580                 pdata->exit();
581 out:
582         return ret;
583 }
584
585 /*
586  * Disable all TWL4030 RTC module interrupts.
587  * Sets status flag to free.
588  */
589 static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
590 {
591         /* leave rtc running, but disable irqs */
592         struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
593         struct rtc_device *rtc = platform_get_drvdata(pdev);
594
595         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
596         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
597
598         free_irq(TWL4030_MODIRQ_PWR, rtc);
599
600         if (pdata != NULL && pdata->exit != NULL) 
601                 pdata->exit();
602
603         rtc_device_unregister(rtc);
604         platform_set_drvdata(pdev, NULL);
605         return 0;
606 }
607
608 static void twl4030_rtc_shutdown(struct platform_device *pdev)
609 {
610         twl4030_rtc_alarm_irq_set_state(&pdev->dev, 0);
611         twl4030_rtc_irq_set_state(&pdev->dev, 0);
612 }
613
614 #ifdef CONFIG_PM
615
616 static unsigned char irqstat = 0;
617
618 static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
619 {
620         get_rtc_irq_bit(&irqstat);
621
622         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
623                          BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
624         return 0;
625 }
626
627 static int twl4030_rtc_resume(struct platform_device *pdev)
628 {
629         set_rtc_irq_bit(irqstat);
630         return 0;
631 }
632 #else
633 #define twl4030_rtc_suspend NULL
634 #define twl4030_rtc_resume  NULL
635 #endif
636
637 MODULE_ALIAS("twl4030_rtc");
638 static struct platform_driver twl4030rtc_driver = {
639         .probe          = twl4030_rtc_probe,
640         .remove         = __devexit_p(twl4030_rtc_remove),
641         .shutdown       = twl4030_rtc_shutdown,
642         .suspend        = twl4030_rtc_suspend,
643         .resume         = twl4030_rtc_resume,
644         .driver         = {
645                 .owner  = THIS_MODULE,
646                 .name   = "twl4030_rtc",
647         },
648 };
649
650 static int __init twl4030_rtc_init(void)
651 {
652         return platform_driver_register(&twl4030rtc_driver);
653 }
654
655 static void __exit twl4030_rtc_exit(void)
656 {
657         platform_driver_unregister(&twl4030rtc_driver);
658 }
659
660 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
661 MODULE_LICENSE("GPL");;
662
663 module_init(twl4030_rtc_init);
664 module_exit(twl4030_rtc_exit);