2 * rtc-twl4030.c -- TWL4030 Real Time Clock interface
4 * Copyright (C) 2007 MontaVista Software, Inc
5 * Author: Alexandre Rusev <source@mvista.com>
7 * Based on original TI driver twl4030-rtc.c
8 * Copyright (C) 2006 Texas Instruments, Inc.
11 * Copyright (C) 2003 MontaVista Software, Inc.
12 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
13 * Copyright (C) 2006 David Brownell
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/ioport.h>
25 #include <linux/delay.h>
26 #include <linux/types.h>
27 #include <linux/rtc.h>
28 #include <linux/bcd.h>
29 #include <linux/platform_device.h>
30 #include <linux/spinlock.h>
31 #include <linux/interrupt.h>
32 #include <linux/device.h>
33 #include <linux/i2c/twl4030.h>
34 #include <linux/i2c/twl4030-rtc.h>
36 #include <linux/irq.h>
38 #include <asm/mach/time.h>
39 #include <asm/system.h>
40 #include <mach/hardware.h>
42 #define ALL_TIME_REGS 6
45 * Supports 1 byte read from TWL4030 RTC register.
47 static int twl4030_rtc_read_u8(u8 *data, u8 reg)
51 ret = twl4030_i2c_read_u8(TWL4030_MODULE_RTC, data, reg);
53 pr_err("twl4030_rtc: Could not read TWL4030"
54 "register %X - error %d\n", reg, ret);
59 * Supports 1 byte write to TWL4030 RTC registers.
61 static int twl4030_rtc_write_u8(u8 data, u8 reg)
65 ret = twl4030_i2c_write_u8(TWL4030_MODULE_RTC, data, reg);
67 pr_err("twl4030_rtc: Could not write TWL4030"
68 "register %X - error %d\n", reg, ret);
73 * Cache the value for timer/alarm interrupts register; this is
74 * only changed by callers holding rtc ops lock (or resume).
76 static unsigned char rtc_irq_bits;
79 * Enable timer and/or alarm interrupts.
81 static int set_rtc_irq_bit(unsigned char bit)
86 val = rtc_irq_bits | bit;
87 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
95 * Disable timer and/or alarm interrupts.
97 static int mask_rtc_irq_bit(unsigned char bit)
102 val = rtc_irq_bits & ~bit;
103 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
110 static inline int twl4030_rtc_alarm_irq_set_state(int enabled)
115 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
117 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
122 static inline int twl4030_rtc_irq_set_state(int enabled)
127 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
129 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
135 * Gets current TWL4030 RTC time and date parameters.
137 * The RTC's time/alarm representation is not what gmtime(3) requires
140 * - Months are 1..12 vs Linux 0-11
141 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
143 static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
145 unsigned char rtc_data[ALL_TIME_REGS + 1];
149 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
153 save_control |= BIT_RTC_CTRL_REG_GET_TIME_M;
155 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
159 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
160 REG_SECONDS_REG, ALL_TIME_REGS);
163 dev_err(dev, "rtc_read_time error %d\n", ret);
167 tm->tm_sec = bcd2bin(rtc_data[0]);
168 tm->tm_min = bcd2bin(rtc_data[1]);
169 tm->tm_hour = bcd2bin(rtc_data[2]);
170 tm->tm_mday = bcd2bin(rtc_data[3]);
171 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
172 tm->tm_year = bcd2bin(rtc_data[5]) + 100;
177 static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
179 unsigned char save_control;
180 unsigned char rtc_data[ALL_TIME_REGS + 1];
183 rtc_data[1] = bin2bcd(tm->tm_sec);
184 rtc_data[2] = bin2bcd(tm->tm_min);
185 rtc_data[3] = bin2bcd(tm->tm_hour);
186 rtc_data[4] = bin2bcd(tm->tm_mday);
187 rtc_data[5] = bin2bcd(tm->tm_mon + 1);
188 rtc_data[6] = bin2bcd(tm->tm_year - 100);
190 /* Stop RTC while updating the TC registers */
191 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
195 save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
196 twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
200 /* update all the time registers in one shot */
201 ret = twl4030_i2c_write(TWL4030_MODULE_RTC, rtc_data,
202 REG_SECONDS_REG, ALL_TIME_REGS);
204 dev_err(dev, "rtc_set_time error %d\n", ret);
209 save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
210 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
217 * Gets current TWL4030 RTC alarm time.
219 static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
221 unsigned char rtc_data[ALL_TIME_REGS + 1];
224 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
225 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
227 dev_err(dev, "rtc_read_alarm error %d\n", ret);
231 /* some of these fields may be wildcard/"match all" */
232 alm->time.tm_sec = bcd2bin(rtc_data[0]);
233 alm->time.tm_min = bcd2bin(rtc_data[1]);
234 alm->time.tm_hour = bcd2bin(rtc_data[2]);
235 alm->time.tm_mday = bcd2bin(rtc_data[3]);
236 alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1;
237 alm->time.tm_year = bcd2bin(rtc_data[5]) + 100;
239 /* report cached alarm enable state */
240 if (rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M)
246 static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
248 unsigned char alarm_data[ALL_TIME_REGS + 1];
251 ret = twl4030_rtc_alarm_irq_set_state(0);
255 alarm_data[1] = bin2bcd(alm->time.tm_sec);
256 alarm_data[2] = bin2bcd(alm->time.tm_min);
257 alarm_data[3] = bin2bcd(alm->time.tm_hour);
258 alarm_data[4] = bin2bcd(alm->time.tm_mday);
259 alarm_data[5] = bin2bcd(alm->time.tm_mon + 1);
260 alarm_data[6] = bin2bcd(alm->time.tm_year - 100);
262 /* update all the alarm registers in one shot */
263 ret = twl4030_i2c_write(TWL4030_MODULE_RTC, alarm_data,
264 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
266 dev_err(dev, "rtc_set_alarm error %d\n", ret);
271 ret = twl4030_rtc_alarm_irq_set_state(1);
276 #ifdef CONFIG_RTC_INTF_DEV
278 static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
283 return twl4030_rtc_alarm_irq_set_state(0);
285 return twl4030_rtc_alarm_irq_set_state(1);
287 return twl4030_rtc_irq_set_state(0);
289 return twl4030_rtc_irq_set_state(1);
297 #define omap_rtc_ioctl NULL
300 static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
302 unsigned long events = 0;
307 res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
311 * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
312 * only one (ALARM or RTC) interrupt source may be enabled
313 * at time, we also could check our results
314 * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
316 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
317 events |= RTC_IRQF | RTC_AF;
319 events |= RTC_IRQF | RTC_UF;
321 res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
326 /* Clear on Read enabled. RTC_IT bit of REG_PWR_ISR1 needs
327 * 2 reads to clear the interrupt. One read is done in
328 * do_twl4030_pwrirq(). Doing the second read, to clear
331 res = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_ISR1);
335 /* Notify RTC core on event */
336 rtc_update_irq(rtc, 1, events);
343 static struct rtc_class_ops twl4030_rtc_ops = {
344 .ioctl = twl4030_rtc_ioctl,
345 .read_time = twl4030_rtc_read_time,
346 .set_time = twl4030_rtc_set_time,
347 .read_alarm = twl4030_rtc_read_alarm,
348 .set_alarm = twl4030_rtc_set_alarm,
351 static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
353 struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
354 struct rtc_device *rtc;
356 int irq = platform_get_irq(pdev, 0);
362 if (pdata != NULL && pdata->init != NULL) {
368 rtc = rtc_device_register(pdev->name,
369 &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
372 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
378 platform_set_drvdata(pdev, rtc);
380 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
385 if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
386 dev_warn(&pdev->dev, "Power up reset detected.\n");
388 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
389 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
391 /* Clear RTC Power up reset and pending alarm interrupts */
392 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
396 ret = request_irq(irq, twl4030_rtc_interrupt,
397 0, rtc->dev.bus_id, rtc);
399 dev_err(&pdev->dev, "IRQ is not free.\n");
403 /* Check RTC module status, Enable if it is off */
404 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
408 if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
409 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
410 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
411 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
416 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_IMR1);
420 rd_reg &= PWR_RTC_IT_UNMASK;
421 /* MASK PWR - we will need this */
422 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_IMR1);
426 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT, &rd_reg, REG_PWR_EDR1);
430 /* Rising edge detection enabled, needed for RTC alarm */
432 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT, rd_reg, REG_PWR_EDR1);
436 /* init cached IRQ enable bits */
437 ret = twl4030_rtc_read_u8(&rtc_irq_bits, REG_RTC_INTERRUPTS_REG);
447 rtc_device_unregister(rtc);
449 if (pdata != NULL && pdata->exit != NULL)
456 * Disable all TWL4030 RTC module interrupts.
457 * Sets status flag to free.
459 static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
461 /* leave rtc running, but disable irqs */
462 struct twl4030rtc_platform_data *pdata = pdev->dev.platform_data;
463 struct rtc_device *rtc = platform_get_drvdata(pdev);
464 int irq = platform_get_irq(pdev, 0);
466 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
467 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
471 if (pdata != NULL && pdata->exit != NULL)
474 rtc_device_unregister(rtc);
475 platform_set_drvdata(pdev, NULL);
479 static void twl4030_rtc_shutdown(struct platform_device *pdev)
481 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
482 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
487 static unsigned char irqstat;
489 static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
491 irqstat = rtc_irq_bits;
493 /* REVISIT alarm may need to wake us from sleep */
494 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
495 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
499 static int twl4030_rtc_resume(struct platform_device *pdev)
501 set_rtc_irq_bit(irqstat);
506 #define twl4030_rtc_suspend NULL
507 #define twl4030_rtc_resume NULL
510 MODULE_ALIAS("platform:twl4030_rtc");
512 static struct platform_driver twl4030rtc_driver = {
513 .probe = twl4030_rtc_probe,
514 .remove = __devexit_p(twl4030_rtc_remove),
515 .shutdown = twl4030_rtc_shutdown,
516 .suspend = twl4030_rtc_suspend,
517 .resume = twl4030_rtc_resume,
519 .owner = THIS_MODULE,
520 .name = "twl4030_rtc",
524 static int __init twl4030_rtc_init(void)
526 return platform_driver_register(&twl4030rtc_driver);
528 module_init(twl4030_rtc_init);
530 static void __exit twl4030_rtc_exit(void)
532 platform_driver_unregister(&twl4030rtc_driver);
534 module_exit(twl4030_rtc_exit);
536 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
537 MODULE_LICENSE("GPL");