1 // SPDX-License-Identifier: GPL-2.0
3 * pcap rtc code for Motorola EZX phones
5 * Copyright (c) 2008 guiming zhuo <gmzhuo@gmail.com>
6 * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
8 * Based on Motorola's rtc.c Copyright (c) 2003-2005 Motorola
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/mfd/ezx-pcap.h>
15 #include <linux/rtc.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
20 struct pcap_chip *pcap;
21 struct rtc_device *rtc;
24 static irqreturn_t pcap_rtc_irq(int irq, void *_pcap_rtc)
26 struct pcap_rtc *pcap_rtc = _pcap_rtc;
27 unsigned long rtc_events;
29 if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ))
30 rtc_events = RTC_IRQF | RTC_UF;
31 else if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA))
32 rtc_events = RTC_IRQF | RTC_AF;
36 rtc_update_irq(pcap_rtc->rtc, 1, rtc_events);
40 static int pcap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
42 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
43 struct rtc_time *tm = &alrm->time;
45 u32 tod; /* time of day, seconds since midnight */
46 u32 days; /* days since 1/1/1970 */
48 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TODA, &tod);
49 secs = tod & PCAP_RTC_TOD_MASK;
51 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, &days);
52 secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
54 rtc_time64_to_tm(secs, tm);
59 static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
61 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
62 unsigned long secs = rtc_tm_to_time64(&alrm->time);
65 tod = secs % SEC_PER_DAY;
66 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TODA, tod);
68 days = secs / SEC_PER_DAY;
69 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, days);
74 static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm)
76 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
80 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TOD, &tod);
81 secs = tod & PCAP_RTC_TOD_MASK;
83 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAY, &days);
84 secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
86 rtc_time64_to_tm(secs, tm);
91 static int pcap_rtc_set_time(struct device *dev, struct rtc_time *tm)
93 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
94 unsigned long secs = rtc_tm_to_time64(tm);
97 tod = secs % SEC_PER_DAY;
98 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TOD, tod);
100 days = secs / SEC_PER_DAY;
101 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAY, days);
106 static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en)
108 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
111 enable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
113 disable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
118 static int pcap_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
120 return pcap_rtc_irq_enable(dev, PCAP_IRQ_TODA, en);
123 static const struct rtc_class_ops pcap_rtc_ops = {
124 .read_time = pcap_rtc_read_time,
125 .set_time = pcap_rtc_set_time,
126 .read_alarm = pcap_rtc_read_alarm,
127 .set_alarm = pcap_rtc_set_alarm,
128 .alarm_irq_enable = pcap_rtc_alarm_irq_enable,
131 static int __init pcap_rtc_probe(struct platform_device *pdev)
133 struct pcap_rtc *pcap_rtc;
134 int timer_irq, alarm_irq;
137 pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc),
142 pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent);
144 platform_set_drvdata(pdev, pcap_rtc);
146 pcap_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
147 if (IS_ERR(pcap_rtc->rtc))
148 return PTR_ERR(pcap_rtc->rtc);
150 pcap_rtc->rtc->ops = &pcap_rtc_ops;
151 pcap_rtc->rtc->range_max = (1 << 14) * 86400ULL - 1;
153 timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ);
154 alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA);
156 err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0,
157 "RTC Timer", pcap_rtc);
161 err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0,
162 "RTC Alarm", pcap_rtc);
166 return devm_rtc_register_device(pcap_rtc->rtc);
169 static int __exit pcap_rtc_remove(struct platform_device *pdev)
174 static struct platform_driver pcap_rtc_driver = {
175 .remove = __exit_p(pcap_rtc_remove),
181 module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe);
183 MODULE_DESCRIPTION("Motorola pcap rtc driver");
184 MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
185 MODULE_LICENSE("GPL");