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/types.h>
25 #include <linux/rtc.h>
26 #include <linux/bcd.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
30 #include <linux/i2c/twl4030.h>
34 * RTC block register offsets (use TWL_MODULE_RTC)
36 #define REG_SECONDS_REG 0x00
37 #define REG_MINUTES_REG 0x01
38 #define REG_HOURS_REG 0x02
39 #define REG_DAYS_REG 0x03
40 #define REG_MONTHS_REG 0x04
41 #define REG_YEARS_REG 0x05
42 #define REG_WEEKS_REG 0x06
44 #define REG_ALARM_SECONDS_REG 0x07
45 #define REG_ALARM_MINUTES_REG 0x08
46 #define REG_ALARM_HOURS_REG 0x09
47 #define REG_ALARM_DAYS_REG 0x0A
48 #define REG_ALARM_MONTHS_REG 0x0B
49 #define REG_ALARM_YEARS_REG 0x0C
51 #define REG_RTC_CTRL_REG 0x0D
52 #define REG_RTC_STATUS_REG 0x0E
53 #define REG_RTC_INTERRUPTS_REG 0x0F
55 #define REG_RTC_COMP_LSB_REG 0x10
56 #define REG_RTC_COMP_MSB_REG 0x11
58 /* RTC_CTRL_REG bitfields */
59 #define BIT_RTC_CTRL_REG_STOP_RTC_M 0x01
60 #define BIT_RTC_CTRL_REG_ROUND_30S_M 0x02
61 #define BIT_RTC_CTRL_REG_AUTO_COMP_M 0x04
62 #define BIT_RTC_CTRL_REG_MODE_12_24_M 0x08
63 #define BIT_RTC_CTRL_REG_TEST_MODE_M 0x10
64 #define BIT_RTC_CTRL_REG_SET_32_COUNTER_M 0x20
65 #define BIT_RTC_CTRL_REG_GET_TIME_M 0x40
67 /* RTC_STATUS_REG bitfields */
68 #define BIT_RTC_STATUS_REG_RUN_M 0x02
69 #define BIT_RTC_STATUS_REG_1S_EVENT_M 0x04
70 #define BIT_RTC_STATUS_REG_1M_EVENT_M 0x08
71 #define BIT_RTC_STATUS_REG_1H_EVENT_M 0x10
72 #define BIT_RTC_STATUS_REG_1D_EVENT_M 0x20
73 #define BIT_RTC_STATUS_REG_ALARM_M 0x40
74 #define BIT_RTC_STATUS_REG_POWER_UP_M 0x80
76 /* RTC_INTERRUPTS_REG bitfields */
77 #define BIT_RTC_INTERRUPTS_REG_EVERY_M 0x03
78 #define BIT_RTC_INTERRUPTS_REG_IT_TIMER_M 0x04
79 #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M 0x08
82 /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
83 #define ALL_TIME_REGS 6
85 /*----------------------------------------------------------------------*/
88 * Supports 1 byte read from TWL4030 RTC register.
90 static int twl4030_rtc_read_u8(u8
*data
, u8 reg
)
94 ret
= twl4030_i2c_read_u8(TWL4030_MODULE_RTC
, data
, reg
);
96 pr_err("twl4030_rtc: Could not read TWL4030"
97 "register %X - error %d\n", reg
, ret
);
102 * Supports 1 byte write to TWL4030 RTC registers.
104 static int twl4030_rtc_write_u8(u8 data
, u8 reg
)
108 ret
= twl4030_i2c_write_u8(TWL4030_MODULE_RTC
, data
, reg
);
110 pr_err("twl4030_rtc: Could not write TWL4030"
111 "register %X - error %d\n", reg
, ret
);
116 * Cache the value for timer/alarm interrupts register; this is
117 * only changed by callers holding rtc ops lock (or resume).
119 static unsigned char rtc_irq_bits
;
122 * Enable timer and/or alarm interrupts.
124 static int set_rtc_irq_bit(unsigned char bit
)
129 val
= rtc_irq_bits
| bit
;
130 ret
= twl4030_rtc_write_u8(val
, REG_RTC_INTERRUPTS_REG
);
138 * Disable timer and/or alarm interrupts.
140 static int mask_rtc_irq_bit(unsigned char bit
)
145 val
= rtc_irq_bits
& ~bit
;
146 ret
= twl4030_rtc_write_u8(val
, REG_RTC_INTERRUPTS_REG
);
153 static inline int twl4030_rtc_alarm_irq_set_state(int enabled
)
158 ret
= set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M
);
160 ret
= mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M
);
165 static inline int twl4030_rtc_irq_set_state(int enabled
)
170 ret
= set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M
);
172 ret
= mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M
);
178 * Gets current TWL4030 RTC time and date parameters.
180 * The RTC's time/alarm representation is not what gmtime(3) requires
183 * - Months are 1..12 vs Linux 0-11
184 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
186 static int twl4030_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
188 unsigned char rtc_data
[ALL_TIME_REGS
+ 1];
192 ret
= twl4030_rtc_read_u8(&save_control
, REG_RTC_CTRL_REG
);
196 save_control
|= BIT_RTC_CTRL_REG_GET_TIME_M
;
198 ret
= twl4030_rtc_write_u8(save_control
, REG_RTC_CTRL_REG
);
202 ret
= twl4030_i2c_read(TWL4030_MODULE_RTC
, rtc_data
,
203 REG_SECONDS_REG
, ALL_TIME_REGS
);
206 dev_err(dev
, "rtc_read_time error %d\n", ret
);
210 tm
->tm_sec
= bcd2bin(rtc_data
[0]);
211 tm
->tm_min
= bcd2bin(rtc_data
[1]);
212 tm
->tm_hour
= bcd2bin(rtc_data
[2]);
213 tm
->tm_mday
= bcd2bin(rtc_data
[3]);
214 tm
->tm_mon
= bcd2bin(rtc_data
[4]) - 1;
215 tm
->tm_year
= bcd2bin(rtc_data
[5]) + 100;
220 static int twl4030_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
222 unsigned char save_control
;
223 unsigned char rtc_data
[ALL_TIME_REGS
+ 1];
226 rtc_data
[1] = bin2bcd(tm
->tm_sec
);
227 rtc_data
[2] = bin2bcd(tm
->tm_min
);
228 rtc_data
[3] = bin2bcd(tm
->tm_hour
);
229 rtc_data
[4] = bin2bcd(tm
->tm_mday
);
230 rtc_data
[5] = bin2bcd(tm
->tm_mon
+ 1);
231 rtc_data
[6] = bin2bcd(tm
->tm_year
- 100);
233 /* Stop RTC while updating the TC registers */
234 ret
= twl4030_rtc_read_u8(&save_control
, REG_RTC_CTRL_REG
);
238 save_control
&= ~BIT_RTC_CTRL_REG_STOP_RTC_M
;
239 twl4030_rtc_write_u8(save_control
, REG_RTC_CTRL_REG
);
243 /* update all the time registers in one shot */
244 ret
= twl4030_i2c_write(TWL4030_MODULE_RTC
, rtc_data
,
245 REG_SECONDS_REG
, ALL_TIME_REGS
);
247 dev_err(dev
, "rtc_set_time error %d\n", ret
);
252 save_control
|= BIT_RTC_CTRL_REG_STOP_RTC_M
;
253 ret
= twl4030_rtc_write_u8(save_control
, REG_RTC_CTRL_REG
);
260 * Gets current TWL4030 RTC alarm time.
262 static int twl4030_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
264 unsigned char rtc_data
[ALL_TIME_REGS
+ 1];
267 ret
= twl4030_i2c_read(TWL4030_MODULE_RTC
, rtc_data
,
268 REG_ALARM_SECONDS_REG
, ALL_TIME_REGS
);
270 dev_err(dev
, "rtc_read_alarm error %d\n", ret
);
274 /* some of these fields may be wildcard/"match all" */
275 alm
->time
.tm_sec
= bcd2bin(rtc_data
[0]);
276 alm
->time
.tm_min
= bcd2bin(rtc_data
[1]);
277 alm
->time
.tm_hour
= bcd2bin(rtc_data
[2]);
278 alm
->time
.tm_mday
= bcd2bin(rtc_data
[3]);
279 alm
->time
.tm_mon
= bcd2bin(rtc_data
[4]) - 1;
280 alm
->time
.tm_year
= bcd2bin(rtc_data
[5]) + 100;
282 /* report cached alarm enable state */
283 if (rtc_irq_bits
& BIT_RTC_INTERRUPTS_REG_IT_ALARM_M
)
289 static int twl4030_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
291 unsigned char alarm_data
[ALL_TIME_REGS
+ 1];
294 ret
= twl4030_rtc_alarm_irq_set_state(0);
298 alarm_data
[1] = bin2bcd(alm
->time
.tm_sec
);
299 alarm_data
[2] = bin2bcd(alm
->time
.tm_min
);
300 alarm_data
[3] = bin2bcd(alm
->time
.tm_hour
);
301 alarm_data
[4] = bin2bcd(alm
->time
.tm_mday
);
302 alarm_data
[5] = bin2bcd(alm
->time
.tm_mon
+ 1);
303 alarm_data
[6] = bin2bcd(alm
->time
.tm_year
- 100);
305 /* update all the alarm registers in one shot */
306 ret
= twl4030_i2c_write(TWL4030_MODULE_RTC
, alarm_data
,
307 REG_ALARM_SECONDS_REG
, ALL_TIME_REGS
);
309 dev_err(dev
, "rtc_set_alarm error %d\n", ret
);
314 ret
= twl4030_rtc_alarm_irq_set_state(1);
319 #ifdef CONFIG_RTC_INTF_DEV
321 static int twl4030_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
326 return twl4030_rtc_alarm_irq_set_state(0);
328 return twl4030_rtc_alarm_irq_set_state(1);
330 return twl4030_rtc_irq_set_state(0);
332 return twl4030_rtc_irq_set_state(1);
340 #define twl4030_rtc_ioctl NULL
343 static irqreturn_t
twl4030_rtc_interrupt(int irq
, void *rtc
)
345 unsigned long events
= 0;
350 #ifdef CONFIG_LOCKDEP
351 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
352 * we don't want and can't tolerate. Although it might be
353 * friendlier not to borrow this thread context...
358 res
= twl4030_rtc_read_u8(&rd_reg
, REG_RTC_STATUS_REG
);
362 * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
363 * only one (ALARM or RTC) interrupt source may be enabled
364 * at time, we also could check our results
365 * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
367 if (rd_reg
& BIT_RTC_STATUS_REG_ALARM_M
)
368 events
|= RTC_IRQF
| RTC_AF
;
370 events
|= RTC_IRQF
| RTC_UF
;
372 res
= twl4030_rtc_write_u8(rd_reg
| BIT_RTC_STATUS_REG_ALARM_M
,
377 /* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1
378 * needs 2 reads to clear the interrupt. One read is done in
379 * do_twl4030_pwrirq(). Doing the second read, to clear
382 * FIXME the reason PWR_ISR1 needs an extra read is that
383 * RTC_IF retriggered until we cleared REG_ALARM_M above.
384 * But re-reading like this is a bad hack; by doing so we
385 * risk wrongly clearing status for some other IRQ (losing
386 * the interrupt). Be smarter about handling RTC_UF ...
388 res
= twl4030_i2c_read_u8(TWL4030_MODULE_INT
,
389 &rd_reg
, TWL4030_INT_PWR_ISR1
);
393 /* Notify RTC core on event */
394 rtc_update_irq(rtc
, 1, events
);
401 static struct rtc_class_ops twl4030_rtc_ops
= {
402 .ioctl
= twl4030_rtc_ioctl
,
403 .read_time
= twl4030_rtc_read_time
,
404 .set_time
= twl4030_rtc_set_time
,
405 .read_alarm
= twl4030_rtc_read_alarm
,
406 .set_alarm
= twl4030_rtc_set_alarm
,
409 /*----------------------------------------------------------------------*/
411 static int __devinit
twl4030_rtc_probe(struct platform_device
*pdev
)
413 struct rtc_device
*rtc
;
415 int irq
= platform_get_irq(pdev
, 0);
421 rtc
= rtc_device_register(pdev
->name
,
422 &pdev
->dev
, &twl4030_rtc_ops
, THIS_MODULE
);
425 dev_err(&pdev
->dev
, "can't register RTC device, err %ld\n",
431 platform_set_drvdata(pdev
, rtc
);
433 ret
= twl4030_rtc_read_u8(&rd_reg
, REG_RTC_STATUS_REG
);
438 if (rd_reg
& BIT_RTC_STATUS_REG_POWER_UP_M
)
439 dev_warn(&pdev
->dev
, "Power up reset detected.\n");
441 if (rd_reg
& BIT_RTC_STATUS_REG_ALARM_M
)
442 dev_warn(&pdev
->dev
, "Pending Alarm interrupt detected.\n");
444 /* Clear RTC Power up reset and pending alarm interrupts */
445 ret
= twl4030_rtc_write_u8(rd_reg
, REG_RTC_STATUS_REG
);
449 ret
= request_irq(irq
, twl4030_rtc_interrupt
,
451 rtc
->dev
.bus_id
, rtc
);
453 dev_err(&pdev
->dev
, "IRQ is not free.\n");
457 /* Check RTC module status, Enable if it is off */
458 ret
= twl4030_rtc_read_u8(&rd_reg
, REG_RTC_CTRL_REG
);
462 if (!(rd_reg
& BIT_RTC_CTRL_REG_STOP_RTC_M
)) {
463 dev_info(&pdev
->dev
, "Enabling TWL4030-RTC.\n");
464 rd_reg
= BIT_RTC_CTRL_REG_STOP_RTC_M
;
465 ret
= twl4030_rtc_write_u8(rd_reg
, REG_RTC_CTRL_REG
);
470 /* init cached IRQ enable bits */
471 ret
= twl4030_rtc_read_u8(&rtc_irq_bits
, REG_RTC_INTERRUPTS_REG
);
481 rtc_device_unregister(rtc
);
487 * Disable all TWL4030 RTC module interrupts.
488 * Sets status flag to free.
490 static int __devexit
twl4030_rtc_remove(struct platform_device
*pdev
)
492 /* leave rtc running, but disable irqs */
493 struct rtc_device
*rtc
= platform_get_drvdata(pdev
);
494 int irq
= platform_get_irq(pdev
, 0);
496 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M
);
497 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M
);
501 rtc_device_unregister(rtc
);
502 platform_set_drvdata(pdev
, NULL
);
506 static void twl4030_rtc_shutdown(struct platform_device
*pdev
)
508 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M
|
509 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M
);
514 static unsigned char irqstat
;
516 static int twl4030_rtc_suspend(struct platform_device
*pdev
, pm_message_t state
)
518 irqstat
= rtc_irq_bits
;
520 /* REVISIT alarm may need to wake us from sleep */
521 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M
|
522 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M
);
526 static int twl4030_rtc_resume(struct platform_device
*pdev
)
528 set_rtc_irq_bit(irqstat
);
533 #define twl4030_rtc_suspend NULL
534 #define twl4030_rtc_resume NULL
537 MODULE_ALIAS("platform:twl4030_rtc");
539 static struct platform_driver twl4030rtc_driver
= {
540 .probe
= twl4030_rtc_probe
,
541 .remove
= __devexit_p(twl4030_rtc_remove
),
542 .shutdown
= twl4030_rtc_shutdown
,
543 .suspend
= twl4030_rtc_suspend
,
544 .resume
= twl4030_rtc_resume
,
546 .owner
= THIS_MODULE
,
547 .name
= "twl4030_rtc",
551 static int __init
twl4030_rtc_init(void)
553 return platform_driver_register(&twl4030rtc_driver
);
555 module_init(twl4030_rtc_init
);
557 static void __exit
twl4030_rtc_exit(void)
559 platform_driver_unregister(&twl4030rtc_driver
);
561 module_exit(twl4030_rtc_exit
);
563 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
564 MODULE_LICENSE("GPL");