2 * pcap rtc code for Motorola EZX phones
4 * Copyright (c) 2008 guiming zhuo <gmzhuo@gmail.com>
5 * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
7 * Based on Motorola's rtc.c Copyright (c) 2003-2005 Motorola
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/mfd/ezx-pcap.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
24 struct pcap_chip
*pcap
;
25 struct rtc_device
*rtc
;
28 static irqreturn_t
pcap_rtc_irq(int irq
, void *_pcap_rtc
)
30 struct pcap_rtc
*pcap_rtc
= _pcap_rtc
;
31 unsigned long rtc_events
;
33 if (irq
== pcap_to_irq(pcap_rtc
->pcap
, PCAP_IRQ_1HZ
))
34 rtc_events
= RTC_IRQF
| RTC_UF
;
35 else if (irq
== pcap_to_irq(pcap_rtc
->pcap
, PCAP_IRQ_TODA
))
36 rtc_events
= RTC_IRQF
| RTC_AF
;
40 rtc_update_irq(pcap_rtc
->rtc
, 1, rtc_events
);
44 static int pcap_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
46 struct platform_device
*pdev
= to_platform_device(dev
);
47 struct pcap_rtc
*pcap_rtc
= platform_get_drvdata(pdev
);
48 struct rtc_time
*tm
= &alrm
->time
;
50 u32 tod
; /* time of day, seconds since midnight */
51 u32 days
; /* days since 1/1/1970 */
53 ezx_pcap_read(pcap_rtc
->pcap
, PCAP_REG_RTC_TODA
, &tod
);
54 secs
= tod
& PCAP_RTC_TOD_MASK
;
56 ezx_pcap_read(pcap_rtc
->pcap
, PCAP_REG_RTC_DAYA
, &days
);
57 secs
+= (days
& PCAP_RTC_DAY_MASK
) * SEC_PER_DAY
;
59 rtc_time_to_tm(secs
, tm
);
64 static int pcap_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
66 struct platform_device
*pdev
= to_platform_device(dev
);
67 struct pcap_rtc
*pcap_rtc
= platform_get_drvdata(pdev
);
68 struct rtc_time
*tm
= &alrm
->time
;
72 rtc_tm_to_time(tm
, &secs
);
74 tod
= secs
% SEC_PER_DAY
;
75 ezx_pcap_write(pcap_rtc
->pcap
, PCAP_REG_RTC_TODA
, tod
);
77 days
= secs
/ SEC_PER_DAY
;
78 ezx_pcap_write(pcap_rtc
->pcap
, PCAP_REG_RTC_DAYA
, days
);
83 static int pcap_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
85 struct platform_device
*pdev
= to_platform_device(dev
);
86 struct pcap_rtc
*pcap_rtc
= platform_get_drvdata(pdev
);
90 ezx_pcap_read(pcap_rtc
->pcap
, PCAP_REG_RTC_TOD
, &tod
);
91 secs
= tod
& PCAP_RTC_TOD_MASK
;
93 ezx_pcap_read(pcap_rtc
->pcap
, PCAP_REG_RTC_DAY
, &days
);
94 secs
+= (days
& PCAP_RTC_DAY_MASK
) * SEC_PER_DAY
;
96 rtc_time_to_tm(secs
, tm
);
98 return rtc_valid_tm(tm
);
101 static int pcap_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
103 struct platform_device
*pdev
= to_platform_device(dev
);
104 struct pcap_rtc
*pcap_rtc
= platform_get_drvdata(pdev
);
107 tod
= secs
% SEC_PER_DAY
;
108 ezx_pcap_write(pcap_rtc
->pcap
, PCAP_REG_RTC_TOD
, tod
);
110 days
= secs
/ SEC_PER_DAY
;
111 ezx_pcap_write(pcap_rtc
->pcap
, PCAP_REG_RTC_DAY
, days
);
116 static int pcap_rtc_irq_enable(struct device
*dev
, int pirq
, unsigned int en
)
118 struct platform_device
*pdev
= to_platform_device(dev
);
119 struct pcap_rtc
*pcap_rtc
= platform_get_drvdata(pdev
);
122 enable_irq(pcap_to_irq(pcap_rtc
->pcap
, pirq
));
124 disable_irq(pcap_to_irq(pcap_rtc
->pcap
, pirq
));
129 static int pcap_rtc_alarm_irq_enable(struct device
*dev
, unsigned int en
)
131 return pcap_rtc_irq_enable(dev
, PCAP_IRQ_TODA
, en
);
134 static int pcap_rtc_update_irq_enable(struct device
*dev
, unsigned int en
)
136 return pcap_rtc_irq_enable(dev
, PCAP_IRQ_1HZ
, en
);
139 static const struct rtc_class_ops pcap_rtc_ops
= {
140 .read_time
= pcap_rtc_read_time
,
141 .read_alarm
= pcap_rtc_read_alarm
,
142 .set_alarm
= pcap_rtc_set_alarm
,
143 .set_mmss
= pcap_rtc_set_mmss
,
144 .alarm_irq_enable
= pcap_rtc_alarm_irq_enable
,
145 .update_irq_enable
= pcap_rtc_update_irq_enable
,
148 static int __devinit
pcap_rtc_probe(struct platform_device
*pdev
)
150 struct pcap_rtc
*pcap_rtc
;
151 int timer_irq
, alarm_irq
;
154 pcap_rtc
= kmalloc(sizeof(struct pcap_rtc
), GFP_KERNEL
);
158 pcap_rtc
->pcap
= dev_get_drvdata(pdev
->dev
.parent
);
160 pcap_rtc
->rtc
= rtc_device_register("pcap", &pdev
->dev
,
161 &pcap_rtc_ops
, THIS_MODULE
);
162 if (IS_ERR(pcap_rtc
->rtc
)) {
163 err
= PTR_ERR(pcap_rtc
->rtc
);
167 platform_set_drvdata(pdev
, pcap_rtc
);
169 timer_irq
= pcap_to_irq(pcap_rtc
->pcap
, PCAP_IRQ_1HZ
);
170 alarm_irq
= pcap_to_irq(pcap_rtc
->pcap
, PCAP_IRQ_TODA
);
172 err
= request_irq(timer_irq
, pcap_rtc_irq
, 0, "RTC Timer", pcap_rtc
);
176 err
= request_irq(alarm_irq
, pcap_rtc_irq
, 0, "RTC Alarm", pcap_rtc
);
182 free_irq(timer_irq
, pcap_rtc
);
184 rtc_device_unregister(pcap_rtc
->rtc
);
190 static int __devexit
pcap_rtc_remove(struct platform_device
*pdev
)
192 struct pcap_rtc
*pcap_rtc
= platform_get_drvdata(pdev
);
194 free_irq(pcap_to_irq(pcap_rtc
->pcap
, PCAP_IRQ_1HZ
), pcap_rtc
);
195 free_irq(pcap_to_irq(pcap_rtc
->pcap
, PCAP_IRQ_TODA
), pcap_rtc
);
196 rtc_device_unregister(pcap_rtc
->rtc
);
202 static struct platform_driver pcap_rtc_driver
= {
203 .remove
= __devexit_p(pcap_rtc_remove
),
206 .owner
= THIS_MODULE
,
210 static int __init
rtc_pcap_init(void)
212 return platform_driver_probe(&pcap_rtc_driver
, pcap_rtc_probe
);
215 static void __exit
rtc_pcap_exit(void)
217 platform_driver_unregister(&pcap_rtc_driver
);
220 module_init(rtc_pcap_init
);
221 module_exit(rtc_pcap_exit
);
223 MODULE_DESCRIPTION("Motorola pcap rtc driver");
224 MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
225 MODULE_LICENSE("GPL");