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/errno.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/rtc.h>
27 #include <linux/bcd.h>
28 #include <linux/platform_device.h>
29 #include <linux/interrupt.h>
31 #include <linux/i2c/twl4030.h>
35 * RTC block register offsets (use TWL_MODULE_RTC)
37 #define REG_SECONDS_REG 0x00
38 #define REG_MINUTES_REG 0x01
39 #define REG_HOURS_REG 0x02
40 #define REG_DAYS_REG 0x03
41 #define REG_MONTHS_REG 0x04
42 #define REG_YEARS_REG 0x05
43 #define REG_WEEKS_REG 0x06
45 #define REG_ALARM_SECONDS_REG 0x07
46 #define REG_ALARM_MINUTES_REG 0x08
47 #define REG_ALARM_HOURS_REG 0x09
48 #define REG_ALARM_DAYS_REG 0x0A
49 #define REG_ALARM_MONTHS_REG 0x0B
50 #define REG_ALARM_YEARS_REG 0x0C
52 #define REG_RTC_CTRL_REG 0x0D
53 #define REG_RTC_STATUS_REG 0x0E
54 #define REG_RTC_INTERRUPTS_REG 0x0F
56 #define REG_RTC_COMP_LSB_REG 0x10
57 #define REG_RTC_COMP_MSB_REG 0x11
59 /* RTC_CTRL_REG bitfields */
60 #define BIT_RTC_CTRL_REG_STOP_RTC_M 0x01
61 #define BIT_RTC_CTRL_REG_ROUND_30S_M 0x02
62 #define BIT_RTC_CTRL_REG_AUTO_COMP_M 0x04
63 #define BIT_RTC_CTRL_REG_MODE_12_24_M 0x08
64 #define BIT_RTC_CTRL_REG_TEST_MODE_M 0x10
65 #define BIT_RTC_CTRL_REG_SET_32_COUNTER_M 0x20
66 #define BIT_RTC_CTRL_REG_GET_TIME_M 0x40
68 /* RTC_STATUS_REG bitfields */
69 #define BIT_RTC_STATUS_REG_RUN_M 0x02
70 #define BIT_RTC_STATUS_REG_1S_EVENT_M 0x04
71 #define BIT_RTC_STATUS_REG_1M_EVENT_M 0x08
72 #define BIT_RTC_STATUS_REG_1H_EVENT_M 0x10
73 #define BIT_RTC_STATUS_REG_1D_EVENT_M 0x20
74 #define BIT_RTC_STATUS_REG_ALARM_M 0x40
75 #define BIT_RTC_STATUS_REG_POWER_UP_M 0x80
77 /* RTC_INTERRUPTS_REG bitfields */
78 #define BIT_RTC_INTERRUPTS_REG_EVERY_M 0x03
79 #define BIT_RTC_INTERRUPTS_REG_IT_TIMER_M 0x04
80 #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M 0x08
83 /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
84 #define ALL_TIME_REGS 6
86 /*----------------------------------------------------------------------*/
89 * Supports 1 byte read from TWL4030 RTC register.
91 static int twl4030_rtc_read_u8(u8 *data, u8 reg)
95 ret = twl4030_i2c_read_u8(TWL4030_MODULE_RTC, data, reg);
97 pr_err("twl4030_rtc: Could not read TWL4030"
98 "register %X - error %d\n", reg, ret);
103 * Supports 1 byte write to TWL4030 RTC registers.
105 static int twl4030_rtc_write_u8(u8 data, u8 reg)
109 ret = twl4030_i2c_write_u8(TWL4030_MODULE_RTC, data, reg);
111 pr_err("twl4030_rtc: Could not write TWL4030"
112 "register %X - error %d\n", reg, ret);
117 * Cache the value for timer/alarm interrupts register; this is
118 * only changed by callers holding rtc ops lock (or resume).
120 static unsigned char rtc_irq_bits;
123 * Enable timer and/or alarm interrupts.
125 static int set_rtc_irq_bit(unsigned char bit)
130 val = rtc_irq_bits | bit;
131 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
139 * Disable timer and/or alarm interrupts.
141 static int mask_rtc_irq_bit(unsigned char bit)
146 val = rtc_irq_bits & ~bit;
147 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
154 static inline int twl4030_rtc_alarm_irq_set_state(int enabled)
159 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
161 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
166 static inline int twl4030_rtc_irq_set_state(int enabled)
171 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
173 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
179 * Gets current TWL4030 RTC time and date parameters.
181 * The RTC's time/alarm representation is not what gmtime(3) requires
184 * - Months are 1..12 vs Linux 0-11
185 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
187 static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
189 unsigned char rtc_data[ALL_TIME_REGS + 1];
193 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
197 save_control |= BIT_RTC_CTRL_REG_GET_TIME_M;
199 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
203 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
204 REG_SECONDS_REG, ALL_TIME_REGS);
207 dev_err(dev, "rtc_read_time error %d\n", ret);
211 tm->tm_sec = bcd2bin(rtc_data[0]);
212 tm->tm_min = bcd2bin(rtc_data[1]);
213 tm->tm_hour = bcd2bin(rtc_data[2]);
214 tm->tm_mday = bcd2bin(rtc_data[3]);
215 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
216 tm->tm_year = bcd2bin(rtc_data[5]) + 100;
221 static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
223 unsigned char save_control;
224 unsigned char rtc_data[ALL_TIME_REGS + 1];
227 rtc_data[1] = bin2bcd(tm->tm_sec);
228 rtc_data[2] = bin2bcd(tm->tm_min);
229 rtc_data[3] = bin2bcd(tm->tm_hour);
230 rtc_data[4] = bin2bcd(tm->tm_mday);
231 rtc_data[5] = bin2bcd(tm->tm_mon + 1);
232 rtc_data[6] = bin2bcd(tm->tm_year - 100);
234 /* Stop RTC while updating the TC registers */
235 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
239 save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
240 twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
244 /* update all the time registers in one shot */
245 ret = twl4030_i2c_write(TWL4030_MODULE_RTC, rtc_data,
246 REG_SECONDS_REG, ALL_TIME_REGS);
248 dev_err(dev, "rtc_set_time error %d\n", ret);
253 save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
254 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
261 * Gets current TWL4030 RTC alarm time.
263 static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
265 unsigned char rtc_data[ALL_TIME_REGS + 1];
268 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
269 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
271 dev_err(dev, "rtc_read_alarm error %d\n", ret);
275 /* some of these fields may be wildcard/"match all" */
276 alm->time.tm_sec = bcd2bin(rtc_data[0]);
277 alm->time.tm_min = bcd2bin(rtc_data[1]);
278 alm->time.tm_hour = bcd2bin(rtc_data[2]);
279 alm->time.tm_mday = bcd2bin(rtc_data[3]);
280 alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1;
281 alm->time.tm_year = bcd2bin(rtc_data[5]) + 100;
283 /* report cached alarm enable state */
284 if (rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M)
290 static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
292 unsigned char alarm_data[ALL_TIME_REGS + 1];
295 ret = twl4030_rtc_alarm_irq_set_state(0);
299 alarm_data[1] = bin2bcd(alm->time.tm_sec);
300 alarm_data[2] = bin2bcd(alm->time.tm_min);
301 alarm_data[3] = bin2bcd(alm->time.tm_hour);
302 alarm_data[4] = bin2bcd(alm->time.tm_mday);
303 alarm_data[5] = bin2bcd(alm->time.tm_mon + 1);
304 alarm_data[6] = bin2bcd(alm->time.tm_year - 100);
306 /* update all the alarm registers in one shot */
307 ret = twl4030_i2c_write(TWL4030_MODULE_RTC, alarm_data,
308 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
310 dev_err(dev, "rtc_set_alarm error %d\n", ret);
315 ret = twl4030_rtc_alarm_irq_set_state(1);
320 #ifdef CONFIG_RTC_INTF_DEV
322 static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
327 return twl4030_rtc_alarm_irq_set_state(0);
329 return twl4030_rtc_alarm_irq_set_state(1);
331 return twl4030_rtc_irq_set_state(0);
333 return twl4030_rtc_irq_set_state(1);
341 #define twl4030_rtc_ioctl NULL
344 static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
346 unsigned long events = 0;
351 #ifdef CONFIG_LOCKDEP
352 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
353 * we don't want and can't tolerate. Although it might be
354 * friendlier not to borrow this thread context...
359 res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
363 * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
364 * only one (ALARM or RTC) interrupt source may be enabled
365 * at time, we also could check our results
366 * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
368 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
369 events |= RTC_IRQF | RTC_AF;
371 events |= RTC_IRQF | RTC_UF;
373 res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
378 /* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1
379 * needs 2 reads to clear the interrupt. One read is done in
380 * do_twl4030_pwrirq(). Doing the second read, to clear
383 * FIXME the reason PWR_ISR1 needs an extra read is that
384 * RTC_IF retriggered until we cleared REG_ALARM_M above.
385 * But re-reading like this is a bad hack; by doing so we
386 * risk wrongly clearing status for some other IRQ (losing
387 * the interrupt). Be smarter about handling RTC_UF ...
389 res = twl4030_i2c_read_u8(TWL4030_MODULE_INT,
390 &rd_reg, TWL4030_INT_PWR_ISR1);
394 /* Notify RTC core on event */
395 rtc_update_irq(rtc, 1, events);
402 static struct rtc_class_ops twl4030_rtc_ops = {
403 .ioctl = twl4030_rtc_ioctl,
404 .read_time = twl4030_rtc_read_time,
405 .set_time = twl4030_rtc_set_time,
406 .read_alarm = twl4030_rtc_read_alarm,
407 .set_alarm = twl4030_rtc_set_alarm,
410 /*----------------------------------------------------------------------*/
412 static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
414 struct rtc_device *rtc;
416 int irq = platform_get_irq(pdev, 0);
422 rtc = rtc_device_register(pdev->name,
423 &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
426 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
432 platform_set_drvdata(pdev, rtc);
434 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
439 if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
440 dev_warn(&pdev->dev, "Power up reset detected.\n");
442 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
443 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
445 /* Clear RTC Power up reset and pending alarm interrupts */
446 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
450 ret = request_irq(irq, twl4030_rtc_interrupt,
452 rtc->dev.bus_id, rtc);
454 dev_err(&pdev->dev, "IRQ is not free.\n");
458 /* Check RTC module status, Enable if it is off */
459 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
463 if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
464 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
465 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
466 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
471 /* init cached IRQ enable bits */
472 ret = twl4030_rtc_read_u8(&rtc_irq_bits, REG_RTC_INTERRUPTS_REG);
482 rtc_device_unregister(rtc);
488 * Disable all TWL4030 RTC module interrupts.
489 * Sets status flag to free.
491 static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
493 /* leave rtc running, but disable irqs */
494 struct rtc_device *rtc = platform_get_drvdata(pdev);
495 int irq = platform_get_irq(pdev, 0);
497 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
498 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
502 rtc_device_unregister(rtc);
503 platform_set_drvdata(pdev, NULL);
507 static void twl4030_rtc_shutdown(struct platform_device *pdev)
509 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
510 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
515 static unsigned char irqstat;
517 static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
519 irqstat = rtc_irq_bits;
521 /* REVISIT alarm may need to wake us from sleep */
522 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
523 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
527 static int twl4030_rtc_resume(struct platform_device *pdev)
529 set_rtc_irq_bit(irqstat);
534 #define twl4030_rtc_suspend NULL
535 #define twl4030_rtc_resume NULL
538 MODULE_ALIAS("platform:twl4030_rtc");
540 static struct platform_driver twl4030rtc_driver = {
541 .probe = twl4030_rtc_probe,
542 .remove = __devexit_p(twl4030_rtc_remove),
543 .shutdown = twl4030_rtc_shutdown,
544 .suspend = twl4030_rtc_suspend,
545 .resume = twl4030_rtc_resume,
547 .owner = THIS_MODULE,
548 .name = "twl4030_rtc",
552 static int __init twl4030_rtc_init(void)
554 return platform_driver_register(&twl4030rtc_driver);
556 module_init(twl4030_rtc_init);
558 static void __exit twl4030_rtc_exit(void)
560 platform_driver_unregister(&twl4030rtc_driver);
562 module_exit(twl4030_rtc_exit);
564 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
565 MODULE_LICENSE("GPL");