2 * drivers/rtc/rtc-twl4030.c
4 * TWL4030 Real Time Clock interface
6 * Copyright (C) 2007 MontaVista Software, Inc
7 * Author: Alexandre Rusev <source@mvista.com>
9 * Based on original TI driver twl4030-rtc.c
10 * Copyright (C) 2006 Texas Instruments, Inc.
13 * Copyright (C) 2003 MontaVista Software, Inc.
14 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
16 * Copyright (C) 2006 David Brownell
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.
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>
39 #include <asm/mach/time.h>
40 #include <asm/system.h>
41 #include <asm/hardware.h>
43 #include <asm/arch/twl4030.h>
44 #include <asm/arch/twl4030-rtc.h>
46 #define ALL_TIME_REGS 6
49 * If this driver ever becomes modularised, it will be really nice
50 * to make the epoch retain its value across module reload...
52 static int epoch = 1900; /* year corresponding to 0x00 */
55 * Supports 1 byte read from TWL4030 RTC register.
57 static int twl4030_rtc_read_u8(u8 * data, u8 reg)
61 ret = twl4030_i2c_read_u8(TWL4030_MODULE_RTC, data, reg);
63 printk(KERN_WARNING "twl4030_rtc: Could not read TWL4030"
64 "register %X - returned %d[%x]\n", reg, ret, ret);
70 * Supports 1 byte write to TWL4030 RTC registers.
72 static int twl4030_rtc_write_u8(u8 data, u8 reg)
76 ret = twl4030_i2c_write_u8(TWL4030_MODULE_RTC, data, reg);
78 printk(KERN_WARNING "twl4030_rtc: Could not write TWL4030"
79 "register %X - returned %d[%x]\n", reg, ret, ret);
85 * Enables timer or alarm interrupts.
87 static int set_rtc_irq_bit(unsigned char bit)
92 ret = twl4030_rtc_read_u8(&val, REG_RTC_INTERRUPTS_REG);
97 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
105 * Read timer or alarm interrupts register.
107 static int get_rtc_irq_bit(unsigned char *val)
111 ret = twl4030_rtc_read_u8(val, REG_RTC_INTERRUPTS_REG);
116 * Disables timer or alarm interrupts.
118 static int mask_rtc_irq_bit(unsigned char bit)
123 ret = twl4030_rtc_read_u8(&val, REG_RTC_INTERRUPTS_REG);
128 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
134 static int twl4030_rtc_alarm_irq_set_state(struct device *dev, int enabled)
138 /* Allow ints for RTC ALARM updates. */
140 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
142 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
148 * Gets current TWL4030 RTC time and date parameters.
150 static int get_rtc_time(struct rtc_time *rtc_tm)
152 unsigned char rtc_data[ALL_TIME_REGS + 1];
156 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
160 save_control |= BIT_RTC_CTRL_REG_GET_TIME_M;
162 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
166 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
167 REG_SECONDS_REG, ALL_TIME_REGS);
170 printk(KERN_ERR "twl4030_rtc: twl4030_i2c_read error.\n");
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]);
182 * Account for differences between how the RTC uses the values
183 * and how they are defined in a struct rtc_time;
185 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
186 rtc_tm->tm_year += 100;
193 static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
195 unsigned char save_control;
196 unsigned char rtc_data[ALL_TIME_REGS + 1];
199 /* Month range is 01..12 */
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);
209 /* Stop RTC while updating the TC registers */
210 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
214 save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
215 twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
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);
223 printk(KERN_ERR "twl4030: twl4030_i2c_write error.\n");
228 save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
229 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
236 * Gets current TWL4030 RTC alarm time.
238 static int get_rtc_alm_time(struct rtc_time *alm_tm)
240 unsigned char rtc_data[ALL_TIME_REGS + 1];
243 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
244 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
246 printk(KERN_ERR "twl4030_rtc: twl4030_i2c_read error.\n");
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]);
258 * Account for differences between how the RTC uses the values
259 * and how they are defined in a struct rtc_time;
261 if ((alm_tm->tm_year += (epoch - 1900)) <= 69)
262 alm_tm->tm_year += 100;
269 static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
273 memset(tm, 0, sizeof(struct rtc_time));
274 ret = get_rtc_time(tm);
280 * Gets current TWL4030 RTC alarm time.
282 static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
285 u8 rtc_interrupts_reg = 0;
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.
292 memset(&alm->time, 0, sizeof(struct rtc_time));
293 ret = get_rtc_alm_time(&alm->time);
298 /* Check alarm enabled flag state */
300 ret | twl4030_i2c_read_u8(TWL4030_MODULE_RTC, &rtc_interrupts_reg,
301 REG_RTC_INTERRUPTS_REG);
306 if ((rtc_interrupts_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) != 0)
315 static int twl4030_rtc_irq_set_state(struct device *dev, int enabled)
319 /* Allow ints for RTC updates. */
321 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
323 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
328 static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
330 unsigned char alarm_data[ALL_TIME_REGS + 1];
333 /* Month range is 01..12 */
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);
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);
347 printk(KERN_ERR "twl4030: twl4030_i2c_write error.\n");
351 ret = twl4030_rtc_alarm_irq_set_state(dev, alm->enabled);
356 #ifdef CONFIG_RTC_INTF_DEV
358 static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
364 return twl4030_rtc_alarm_irq_set_state(dev, 0);
366 return twl4030_rtc_alarm_irq_set_state(dev, 1);
368 /* Mask ints from RTC updates. */
369 return twl4030_rtc_irq_set_state(dev, 0);
371 /* Allow ints for RTC updates. */
372 return twl4030_rtc_irq_set_state(dev, 1);
374 return put_user(epoch, (unsigned long *)arg);
377 * There were no RTC clocks before 1900.
382 if (!capable(CAP_SYS_TIME))
393 #define omap_rtc_ioctl NULL
396 static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
398 unsigned long events = 0;
403 /* clear the RTC interrupt in TWL4030 power module */
404 res = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_ISR1);
408 /* Check if interrupt is sourced by RTC */
409 if (!(rd_reg & PWR_RTC_INT_CLR))
412 rd_reg |= PWR_RTC_INT_CLR;
413 res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_ISR1);
417 res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
421 * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
422 * only one (ALARM or RTC) interrupt source may be enabled
423 * at time, we also could check our results
424 * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
426 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
427 events |= RTC_IRQF | RTC_AF;
429 events |= RTC_IRQF | RTC_UF;
431 res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
436 * Workaround for strange behaviour with T2. Need to write into ISR
437 * register one more time to clear the interrupt. Otherwise, the same
438 * RTC event generates 2 interrupts in a row.
439 * (no errata document available)
441 res = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_ISR1);
445 rd_reg |= PWR_RTC_INT_CLR;
446 res = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_ISR1);
450 /* Notify RTC core on event */
451 rtc_update_irq(rtc, 1, events);
458 static struct rtc_class_ops twl4030_rtc_ops = {
459 .ioctl = twl4030_rtc_ioctl,
460 .read_time = twl4030_rtc_read_time,
461 .set_time = twl4030_rtc_set_time,
462 .read_alarm = twl4030_rtc_read_alarm,
463 .set_alarm = twl4030_rtc_set_alarm,
466 static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
468 struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
469 struct rtc_device *rtc;
473 if (pdata != NULL && pdata->init != NULL) {
479 rtc = rtc_device_register(pdev->name,
480 &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
483 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
489 platform_set_drvdata(pdev, rtc);
491 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
496 if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
497 dev_warn(&pdev->dev, "Power up reset detected.\n");
499 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
500 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
502 /* Clear RTC Power up reset and pending alarm interrupts */
503 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
507 ret = request_irq(TWL4030_MODIRQ_PWR, twl4030_rtc_interrupt,
508 IRQF_DISABLED | IRQF_SHARED, rtc->dev.bus_id, rtc);
510 dev_err(&pdev->dev, "IRQ is not free.\n");
514 /* Check RTC module status, Enable if it is off */
515 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
519 if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
520 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
521 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
522 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
527 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_IMR1);
531 rd_reg &= PWR_RTC_IT_UNMASK;
532 /* MASK PWR - we will need this */
533 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_IMR1);
537 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_EDR1);
541 /* Rising edge detection enabled, needed for RTC alarm */
543 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_EDR1);
551 free_irq(TWL4030_MODIRQ_PWR, rtc);
553 rtc_device_unregister(rtc);
555 if (pdata != NULL && pdata->exit != NULL)
562 * Disable all TWL4030 RTC module interrupts.
563 * Sets status flag to free.
565 static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
567 /* leave rtc running, but disable irqs */
568 struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
569 struct rtc_device *rtc = platform_get_drvdata(pdev);
571 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
572 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
574 free_irq(TWL4030_MODIRQ_PWR, rtc);
576 if (pdata != NULL && pdata->exit != NULL)
579 rtc_device_unregister(rtc);
583 static void twl4030_rtc_shutdown(struct platform_device *pdev)
585 twl4030_rtc_alarm_irq_set_state(&pdev->dev, 0);
586 twl4030_rtc_irq_set_state(&pdev->dev, 0);
591 static unsigned char irqstat = 0;
593 static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
595 get_rtc_irq_bit(&irqstat);
597 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
598 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
602 static int twl4030_rtc_resume(struct platform_device *pdev)
604 set_rtc_irq_bit(irqstat);
608 #define twl4030_rtc_suspend NULL
609 #define twl4030_rtc_resume NULL
612 MODULE_ALIAS("twl4030_rtc");
613 static struct platform_driver twl4030rtc_driver = {
614 .probe = twl4030_rtc_probe,
615 .remove = __devexit_p(twl4030_rtc_remove),
616 .shutdown = twl4030_rtc_shutdown,
617 .suspend = twl4030_rtc_suspend,
618 .resume = twl4030_rtc_resume,
620 .owner = THIS_MODULE,
621 .name = "twl4030_rtc",
625 static int __init twl4030_rtc_init(void)
627 return platform_driver_register(&twl4030rtc_driver);
630 static void __exit twl4030_rtc_exit(void)
632 platform_driver_unregister(&twl4030rtc_driver);
635 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
636 MODULE_LICENSE("GPL");;
638 module_init(twl4030_rtc_init);
639 module_exit(twl4030_rtc_exit);