]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/rtc/rtc-twl4030.c
e4a36584f6beb66d6c20f2f2759e3c06599786be
[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 #include <linux/i2c/twl4030.h>
38 #include <linux/i2c/twl4030-rtc.h>
39 #include <linux/io.h>
40 #include <linux/irq.h>
41
42 #include <asm/mach/time.h>
43 #include <asm/system.h>
44 #include <mach/hardware.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         rtc_tm->tm_year += (epoch - 1900);
186         if (rtc_tm->tm_year <= 69)
187                 rtc_tm->tm_year += 100;
188
189         rtc_tm->tm_mon--;
190
191         return ret;
192 }
193
194 static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
195 {
196         unsigned char save_control;
197         unsigned char rtc_data[ALL_TIME_REGS + 1];
198         int ret;
199
200         /* Month range is 01..12 */
201         tm->tm_mon++;
202
203         rtc_data[1] = BIN2BCD(tm->tm_sec);
204         rtc_data[2] = BIN2BCD(tm->tm_min);
205         rtc_data[3] = BIN2BCD(tm->tm_hour);
206         rtc_data[4] = BIN2BCD(tm->tm_mday);
207         rtc_data[5] = BIN2BCD(tm->tm_mon);
208         rtc_data[6] = BIN2BCD(tm->tm_year);
209
210         /* Stop RTC while updating the TC registers */
211         ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
212         if (ret < 0)
213                 goto out;
214
215         save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
216         twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
217         if (ret < 0)
218                 goto out;
219
220         /* update all the alarm registers in one shot */
221         ret = twl4030_i2c_write(TWL4030_MODULE_RTC, rtc_data,
222                         REG_SECONDS_REG, ALL_TIME_REGS);
223         if (ret < 0) {
224                 printk(KERN_ERR "twl4030: twl4030_i2c_write error.\n");
225                 goto out;
226         }
227
228         /* Start back RTC */
229         save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
230         ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
231
232 out:
233         return ret;
234 }
235
236 /*
237  * Gets current TWL4030 RTC alarm time.
238  */
239 static int get_rtc_alm_time(struct rtc_time *alm_tm)
240 {
241         unsigned char rtc_data[ALL_TIME_REGS + 1];
242         int ret;
243
244         ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
245                                REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
246         if (ret < 0) {
247                 printk(KERN_ERR "twl4030_rtc: twl4030_i2c_read error.\n");
248                 return ret;
249         }
250
251         alm_tm->tm_sec = BCD2BIN(rtc_data[0]);
252         alm_tm->tm_min = BCD2BIN(rtc_data[1]);
253         alm_tm->tm_hour = BCD2BIN(rtc_data[2]);
254         alm_tm->tm_mday = BCD2BIN(rtc_data[3]);
255         alm_tm->tm_mon = BCD2BIN(rtc_data[4]);
256         alm_tm->tm_year = BCD2BIN(rtc_data[5]);
257
258         /*
259          * Account for differences between how the RTC uses the values
260          * and how they are defined in a struct rtc_time;
261          */
262         alm_tm->tm_year += (epoch - 1900);
263         if (alm_tm->tm_year <= 69)
264                 alm_tm->tm_year += 100;
265
266         alm_tm->tm_mon--;
267
268         return ret;
269 }
270
271 static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
272 {
273         int ret;
274
275         memset(tm, 0, sizeof(struct rtc_time));
276         ret = get_rtc_time(tm);
277
278         return ret;
279 }
280
281 /*
282  * Gets current TWL4030 RTC alarm time.
283  */
284 static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
285 {
286         int ret;
287         u8 rtc_interrupts_reg = 0;
288
289         /*
290          * This returns a struct rtc_time. Reading >= 0xc0
291          * means "don't care" or "match all". Only the tm_hour,
292          * tm_min, and tm_sec values are filled in.
293          */
294         memset(&alm->time, 0, sizeof(struct rtc_time));
295         ret = get_rtc_alm_time(&alm->time);
296
297         if (ret)
298                 goto out;
299
300         /* Check alarm enabled flag state */
301         ret =
302             ret | twl4030_i2c_read_u8(TWL4030_MODULE_RTC, &rtc_interrupts_reg,
303                                       REG_RTC_INTERRUPTS_REG);
304
305         if (ret)
306                 goto out;
307
308         if ((rtc_interrupts_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) != 0)
309                 alm->enabled = 1;
310         else
311                 alm->enabled = 0;
312
313 out:
314         return ret;
315 }
316
317 static int twl4030_rtc_irq_set_state(struct device *dev, int enabled)
318 {
319         int ret;
320
321         /* Allow ints for RTC updates.  */
322         if (enabled)
323                 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
324         else
325                 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
326
327         return ret;
328 }
329
330 static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
331 {
332         unsigned char alarm_data[ALL_TIME_REGS + 1];
333         int ret;
334
335         /* Month range is 01..12 */
336         alm->time.tm_mon++;
337
338         alarm_data[1] = BIN2BCD(alm->time.tm_sec);
339         alarm_data[2] = BIN2BCD(alm->time.tm_min);
340         alarm_data[3] = BIN2BCD(alm->time.tm_hour);
341         alarm_data[4] = BIN2BCD(alm->time.tm_mday);
342         alarm_data[5] = BIN2BCD(alm->time.tm_mon);
343         alarm_data[6] = BIN2BCD(alm->time.tm_year);
344
345         /* update all the alarm registers in one shot */
346         ret = twl4030_i2c_write(TWL4030_MODULE_RTC, alarm_data,
347                         REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
348         if (ret) {
349                 printk(KERN_ERR "twl4030: twl4030_i2c_write error.\n");
350                 goto out;
351         }
352
353         ret = twl4030_rtc_alarm_irq_set_state(dev, alm->enabled);
354 out:
355         return ret;
356 }
357
358 /*
359  * We will just handle setting the frequency and make use the framework for
360  * reading the periodic interupts.
361  * @freq: Current periodic IRQ freq
362  */
363 static int twl4030_rtc_irq_set_freq(struct device *dev, int freq)
364 {
365         struct rtc_device *rtc = dev_get_drvdata(dev);
366
367         if (freq < 0 || freq > 3)
368                 return -EINVAL;
369
370         rtc->irq_freq = freq;
371
372         /* set rtc irq freq to user defined value */
373         set_rtc_irq_bit(freq);
374
375         return 0;
376 }
377
378 #ifdef  CONFIG_RTC_INTF_DEV
379
380 static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
381                              unsigned long arg)
382 {
383
384         switch (cmd) {
385         case RTC_AIE_OFF:
386                 return twl4030_rtc_alarm_irq_set_state(dev, 0);
387         case RTC_AIE_ON:
388                 return twl4030_rtc_alarm_irq_set_state(dev, 1);
389
390         case RTC_UIE_OFF:
391                 /* Fall Through */
392         case RTC_PIE_OFF:
393                 /* Mask ints from RTC updates.  */
394                 return twl4030_rtc_irq_set_state(dev, 0);
395         case RTC_UIE_ON:
396                 /* Fall Through */
397         case RTC_PIE_ON:
398                 /* Allow ints for RTC updates.  */
399                 return twl4030_rtc_irq_set_state(dev, 1);
400
401         case RTC_EPOCH_READ:
402                 return put_user(epoch, (unsigned long *)arg);
403         case RTC_EPOCH_SET:
404                 /*
405                  * There were no RTC clocks before 1900.
406                  */
407                 if (arg < 1900)
408                         return -EINVAL;
409
410                 if (!capable(CAP_SYS_TIME))
411                         return -EACCES;
412
413                 epoch = arg;
414                 return 0;
415         default:
416                 return -ENOIOCTLCMD;
417         }
418 }
419
420 #else
421 #define omap_rtc_ioctl  NULL
422 #endif
423
424 static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
425 {
426         unsigned long events = 0;
427         int ret = IRQ_NONE;
428         int res;
429         u8 rd_reg;
430
431         res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
432         if (res)
433                 goto out;
434         /*
435          * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
436          * only one (ALARM or RTC) interrupt source may be enabled
437          * at time, we also could check our results
438          * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
439          */
440         if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
441                 events |= RTC_IRQF | RTC_AF;
442         else
443                 events |= RTC_IRQF | RTC_UF;
444
445         res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
446                                    REG_RTC_STATUS_REG);
447         if (res)
448                 goto out;
449         res = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
450                         PWR_RTC_INT_CLR, REG_PWR_ISR1);
451         if (res)
452                 goto out;
453
454         /* Notify RTC core on event */
455         rtc_update_irq(rtc, 1, events);
456
457         ret = IRQ_HANDLED;
458 out:
459         return ret;
460 }
461
462 static struct rtc_class_ops twl4030_rtc_ops = {
463         .ioctl          = twl4030_rtc_ioctl,
464         .read_time      = twl4030_rtc_read_time,
465         .set_time       = twl4030_rtc_set_time,
466         .read_alarm     = twl4030_rtc_read_alarm,
467         .set_alarm      = twl4030_rtc_set_alarm,
468         .irq_set_freq   = twl4030_rtc_irq_set_freq,
469 };
470
471 static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
472 {
473         struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
474         struct rtc_device *rtc;
475         int ret = 0;
476         u8 rd_reg;
477
478         if (pdata != NULL && pdata->init != NULL) {
479                 ret = pdata->init();
480                 if (ret < 0)
481                         goto out;
482         }
483
484         rtc = rtc_device_register(pdev->name,
485                                   &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
486         if (IS_ERR(rtc)) {
487                 ret = -EINVAL;
488                 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
489                         PTR_ERR(rtc));
490                 goto out0;
491
492         }
493
494         /* Set the irq freq to every second */
495         rtc->irq_freq = 0;
496
497         platform_set_drvdata(pdev, rtc);
498
499         ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
500
501         if (ret < 0)
502                 goto out1;
503
504         if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
505                 dev_warn(&pdev->dev, "Power up reset detected.\n");
506
507         if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
508                 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
509
510         /* Clear RTC Power up reset and pending alarm interrupts */
511         ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
512         if (ret < 0)
513                 goto out1;
514
515         ret = request_irq(TWL4030_PWRIRQ_RTC, twl4030_rtc_interrupt,
516                                 0, rtc->dev.bus_id, rtc);
517         if (ret < 0) {
518                 dev_err(&pdev->dev, "IRQ is not free.\n");
519                 goto out1;
520         }
521
522         /* Check RTC module status, Enable if it is off */
523         ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
524         if (ret < 0)
525                 goto out2;
526
527         if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
528                 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
529                 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
530                 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
531                 if (ret < 0)
532                         goto out2;
533         }
534
535         ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_IMR1);
536         if (ret < 0)
537                 goto out2;
538
539         rd_reg &= PWR_RTC_IT_UNMASK;
540         /* MASK PWR - we will need this */
541         ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_IMR1);
542         if (ret < 0)
543                 goto out2;
544
545         ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_EDR1);
546         if (ret < 0)
547                 goto out2;
548
549         /* Rising edge detection enabled, needed for RTC alarm */
550         rd_reg |= 0x80;
551         ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_EDR1);
552         if (ret < 0)
553                 goto out2;
554
555         return ret;
556
557
558 out2:
559         free_irq(TWL4030_MODIRQ_PWR, rtc);
560 out1:
561         rtc_device_unregister(rtc);
562 out0:
563         if (pdata != NULL && pdata->exit != NULL)
564                 pdata->exit();
565 out:
566         return ret;
567 }
568
569 /*
570  * Disable all TWL4030 RTC module interrupts.
571  * Sets status flag to free.
572  */
573 static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
574 {
575         /* leave rtc running, but disable irqs */
576         struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
577         struct rtc_device *rtc = platform_get_drvdata(pdev);
578
579         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
580         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
581
582         free_irq(TWL4030_MODIRQ_PWR, rtc);
583
584         if (pdata != NULL && pdata->exit != NULL)
585                 pdata->exit();
586
587         rtc_device_unregister(rtc);
588         platform_set_drvdata(pdev, NULL);
589         return 0;
590 }
591
592 static void twl4030_rtc_shutdown(struct platform_device *pdev)
593 {
594         twl4030_rtc_alarm_irq_set_state(&pdev->dev, 0);
595         twl4030_rtc_irq_set_state(&pdev->dev, 0);
596 }
597
598 #ifdef CONFIG_PM
599
600 static unsigned char irqstat;
601
602 static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
603 {
604         get_rtc_irq_bit(&irqstat);
605
606         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
607                          BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
608         return 0;
609 }
610
611 static int twl4030_rtc_resume(struct platform_device *pdev)
612 {
613         set_rtc_irq_bit(irqstat);
614         return 0;
615 }
616 #else
617 #define twl4030_rtc_suspend NULL
618 #define twl4030_rtc_resume  NULL
619 #endif
620
621 MODULE_ALIAS("platform:twl4030_rtc");
622 static struct platform_driver twl4030rtc_driver = {
623         .probe          = twl4030_rtc_probe,
624         .remove         = __devexit_p(twl4030_rtc_remove),
625         .shutdown       = twl4030_rtc_shutdown,
626         .suspend        = twl4030_rtc_suspend,
627         .resume         = twl4030_rtc_resume,
628         .driver         = {
629                 .owner  = THIS_MODULE,
630                 .name   = "twl4030_rtc",
631         },
632 };
633
634 static int __init twl4030_rtc_init(void)
635 {
636         return platform_driver_register(&twl4030rtc_driver);
637 }
638
639 static void __exit twl4030_rtc_exit(void)
640 {
641         platform_driver_unregister(&twl4030rtc_driver);
642 }
643
644 MODULE_ALIAS("platform:twl4030_rtc");
645 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
646 MODULE_LICENSE("GPL");;
647
648 module_init(twl4030_rtc_init);
649 module_exit(twl4030_rtc_exit);