blob: 539a90b98bc5e1f8bb6f0dd39086c111110aafd9 [file] [log] [blame]
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -07001/*
2 * pcap rtc code for Motorola EZX phones
3 *
4 * Copyright (c) 2008 guiming zhuo <gmzhuo@gmail.com>
5 * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
6 *
7 * Based on Motorola's rtc.c Copyright (c) 2003-2005 Motorola
8 *
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.
12 *
13 */
14
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070021#include <linux/platform_device.h>
22
23struct pcap_rtc {
24 struct pcap_chip *pcap;
25 struct rtc_device *rtc;
26};
27
28static irqreturn_t pcap_rtc_irq(int irq, void *_pcap_rtc)
29{
30 struct pcap_rtc *pcap_rtc = _pcap_rtc;
31 unsigned long rtc_events;
32
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;
37 else
38 rtc_events = 0;
39
40 rtc_update_irq(pcap_rtc->rtc, 1, rtc_events);
41 return IRQ_HANDLED;
42}
43
44static int pcap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
45{
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;
49 unsigned long secs;
50 u32 tod; /* time of day, seconds since midnight */
51 u32 days; /* days since 1/1/1970 */
52
53 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TODA, &tod);
54 secs = tod & PCAP_RTC_TOD_MASK;
55
56 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, &days);
57 secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
58
59 rtc_time_to_tm(secs, tm);
60
61 return 0;
62}
63
64static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
65{
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;
69 unsigned long secs;
70 u32 tod, days;
71
72 rtc_tm_to_time(tm, &secs);
73
74 tod = secs % SEC_PER_DAY;
75 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TODA, tod);
76
77 days = secs / SEC_PER_DAY;
78 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, days);
79
80 return 0;
81}
82
83static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm)
84{
85 struct platform_device *pdev = to_platform_device(dev);
86 struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
87 unsigned long secs;
88 u32 tod, days;
89
90 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TOD, &tod);
91 secs = tod & PCAP_RTC_TOD_MASK;
92
93 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAY, &days);
94 secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
95
96 rtc_time_to_tm(secs, tm);
97
98 return rtc_valid_tm(tm);
99}
100
101static int pcap_rtc_set_mmss(struct device *dev, unsigned long secs)
102{
103 struct platform_device *pdev = to_platform_device(dev);
104 struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
105 u32 tod, days;
106
107 tod = secs % SEC_PER_DAY;
108 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TOD, tod);
109
110 days = secs / SEC_PER_DAY;
111 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAY, days);
112
113 return 0;
114}
115
116static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en)
117{
118 struct platform_device *pdev = to_platform_device(dev);
119 struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
120
121 if (en)
122 enable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
123 else
124 disable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
125
126 return 0;
127}
128
129static int pcap_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
130{
131 return pcap_rtc_irq_enable(dev, PCAP_IRQ_TODA, en);
132}
133
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700134static const struct rtc_class_ops pcap_rtc_ops = {
135 .read_time = pcap_rtc_read_time,
136 .read_alarm = pcap_rtc_read_alarm,
137 .set_alarm = pcap_rtc_set_alarm,
138 .set_mmss = pcap_rtc_set_mmss,
139 .alarm_irq_enable = pcap_rtc_alarm_irq_enable,
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700140};
141
Jingoo Han5cc2b9c2013-04-29 16:18:26 -0700142static int __init pcap_rtc_probe(struct platform_device *pdev)
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700143{
144 struct pcap_rtc *pcap_rtc;
145 int timer_irq, alarm_irq;
146 int err = -ENOMEM;
147
Jingoo Han6b5f4862013-04-29 16:20:47 -0700148 pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc),
149 GFP_KERNEL);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700150 if (!pcap_rtc)
151 return err;
152
153 pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent);
154
John Stultz4b3687f2011-05-06 17:30:57 -0700155 platform_set_drvdata(pdev, pcap_rtc);
156
Jingoo Han6b5f4862013-04-29 16:20:47 -0700157 pcap_rtc->rtc = devm_rtc_device_register(&pdev->dev, "pcap",
158 &pcap_rtc_ops, THIS_MODULE);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700159 if (IS_ERR(pcap_rtc->rtc)) {
160 err = PTR_ERR(pcap_rtc->rtc);
Jingoo Han6b5f4862013-04-29 16:20:47 -0700161 goto fail;
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700162 }
163
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700164 timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ);
165 alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA);
166
Jingoo Han6b5f4862013-04-29 16:20:47 -0700167 err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0,
168 "RTC Timer", pcap_rtc);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700169 if (err)
Jingoo Han6b5f4862013-04-29 16:20:47 -0700170 goto fail;
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700171
Jingoo Han6b5f4862013-04-29 16:20:47 -0700172 err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0,
173 "RTC Alarm", pcap_rtc);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700174 if (err)
Jingoo Han6b5f4862013-04-29 16:20:47 -0700175 goto fail;
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700176
177 return 0;
Jingoo Han6b5f4862013-04-29 16:20:47 -0700178fail:
John Stultz4b3687f2011-05-06 17:30:57 -0700179 platform_set_drvdata(pdev, NULL);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700180 return err;
181}
182
Jingoo Han5cc2b9c2013-04-29 16:18:26 -0700183static int __exit pcap_rtc_remove(struct platform_device *pdev)
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700184{
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700185 return 0;
186}
187
188static struct platform_driver pcap_rtc_driver = {
Jingoo Han5cc2b9c2013-04-29 16:18:26 -0700189 .remove = __exit_p(pcap_rtc_remove),
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700190 .driver = {
191 .name = "pcap-rtc",
192 .owner = THIS_MODULE,
193 },
194};
195
Jingoo Han0de4c7c2013-04-29 16:18:47 -0700196module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700197
198MODULE_DESCRIPTION("Motorola pcap rtc driver");
199MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
200MODULE_LICENSE("GPL");