WIP FPC-III support
[linux/fpc-iii.git] / drivers / rtc / rtc-pcf50633.c
blob48951a16d65dc2cffeaeffb8d4d661461351af92
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* NXP PCF50633 RTC Driver
4 * (C) 2006-2008 by Openmoko, Inc.
5 * Author: Balaji Rao <balajirrao@openmoko.org>
6 * All rights reserved.
8 * Broken down from monstrous PCF50633 driver mainly by
9 * Harald Welte, Andy Green and Werner Almesberger
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/rtc.h>
19 #include <linux/bcd.h>
20 #include <linux/err.h>
22 #include <linux/mfd/pcf50633/core.h>
24 #define PCF50633_REG_RTCSC 0x59 /* Second */
25 #define PCF50633_REG_RTCMN 0x5a /* Minute */
26 #define PCF50633_REG_RTCHR 0x5b /* Hour */
27 #define PCF50633_REG_RTCWD 0x5c /* Weekday */
28 #define PCF50633_REG_RTCDT 0x5d /* Day */
29 #define PCF50633_REG_RTCMT 0x5e /* Month */
30 #define PCF50633_REG_RTCYR 0x5f /* Year */
31 #define PCF50633_REG_RTCSCA 0x60 /* Alarm Second */
32 #define PCF50633_REG_RTCMNA 0x61 /* Alarm Minute */
33 #define PCF50633_REG_RTCHRA 0x62 /* Alarm Hour */
34 #define PCF50633_REG_RTCWDA 0x63 /* Alarm Weekday */
35 #define PCF50633_REG_RTCDTA 0x64 /* Alarm Day */
36 #define PCF50633_REG_RTCMTA 0x65 /* Alarm Month */
37 #define PCF50633_REG_RTCYRA 0x66 /* Alarm Year */
39 enum pcf50633_time_indexes {
40 PCF50633_TI_SEC,
41 PCF50633_TI_MIN,
42 PCF50633_TI_HOUR,
43 PCF50633_TI_WKDAY,
44 PCF50633_TI_DAY,
45 PCF50633_TI_MONTH,
46 PCF50633_TI_YEAR,
47 PCF50633_TI_EXTENT /* always last */
50 struct pcf50633_time {
51 u_int8_t time[PCF50633_TI_EXTENT];
54 struct pcf50633_rtc {
55 int alarm_enabled;
56 int alarm_pending;
58 struct pcf50633 *pcf;
59 struct rtc_device *rtc_dev;
62 static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
64 rtc->tm_sec = bcd2bin(pcf->time[PCF50633_TI_SEC]);
65 rtc->tm_min = bcd2bin(pcf->time[PCF50633_TI_MIN]);
66 rtc->tm_hour = bcd2bin(pcf->time[PCF50633_TI_HOUR]);
67 rtc->tm_wday = bcd2bin(pcf->time[PCF50633_TI_WKDAY]);
68 rtc->tm_mday = bcd2bin(pcf->time[PCF50633_TI_DAY]);
69 rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
70 rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
73 static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
75 pcf->time[PCF50633_TI_SEC] = bin2bcd(rtc->tm_sec);
76 pcf->time[PCF50633_TI_MIN] = bin2bcd(rtc->tm_min);
77 pcf->time[PCF50633_TI_HOUR] = bin2bcd(rtc->tm_hour);
78 pcf->time[PCF50633_TI_WKDAY] = bin2bcd(rtc->tm_wday);
79 pcf->time[PCF50633_TI_DAY] = bin2bcd(rtc->tm_mday);
80 pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
81 pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
84 static int
85 pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
87 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
88 int err;
90 if (enabled)
91 err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
92 else
93 err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
95 if (err < 0)
96 return err;
98 rtc->alarm_enabled = enabled;
100 return 0;
103 static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
105 struct pcf50633_rtc *rtc;
106 struct pcf50633_time pcf_tm;
107 int ret;
109 rtc = dev_get_drvdata(dev);
111 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
112 PCF50633_TI_EXTENT,
113 &pcf_tm.time[0]);
114 if (ret != PCF50633_TI_EXTENT) {
115 dev_err(dev, "Failed to read time\n");
116 return -EIO;
119 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
120 pcf_tm.time[PCF50633_TI_DAY],
121 pcf_tm.time[PCF50633_TI_MONTH],
122 pcf_tm.time[PCF50633_TI_YEAR],
123 pcf_tm.time[PCF50633_TI_HOUR],
124 pcf_tm.time[PCF50633_TI_MIN],
125 pcf_tm.time[PCF50633_TI_SEC]);
127 pcf2rtc_time(tm, &pcf_tm);
129 dev_dbg(dev, "RTC_TIME: %ptRr\n", tm);
131 return 0;
134 static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
136 struct pcf50633_rtc *rtc;
137 struct pcf50633_time pcf_tm;
138 int alarm_masked, ret = 0;
140 rtc = dev_get_drvdata(dev);
142 dev_dbg(dev, "RTC_TIME: %ptRr\n", tm);
144 rtc2pcf_time(&pcf_tm, tm);
146 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
147 pcf_tm.time[PCF50633_TI_DAY],
148 pcf_tm.time[PCF50633_TI_MONTH],
149 pcf_tm.time[PCF50633_TI_YEAR],
150 pcf_tm.time[PCF50633_TI_HOUR],
151 pcf_tm.time[PCF50633_TI_MIN],
152 pcf_tm.time[PCF50633_TI_SEC]);
155 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
157 if (!alarm_masked)
158 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
160 /* Returns 0 on success */
161 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
162 PCF50633_TI_EXTENT,
163 &pcf_tm.time[0]);
165 if (!alarm_masked)
166 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
168 return ret;
171 static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
173 struct pcf50633_rtc *rtc;
174 struct pcf50633_time pcf_tm;
175 int ret = 0;
177 rtc = dev_get_drvdata(dev);
179 alrm->enabled = rtc->alarm_enabled;
180 alrm->pending = rtc->alarm_pending;
182 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
183 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
184 if (ret != PCF50633_TI_EXTENT) {
185 dev_err(dev, "Failed to read time\n");
186 return -EIO;
189 pcf2rtc_time(&alrm->time, &pcf_tm);
191 return rtc_valid_tm(&alrm->time);
194 static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
196 struct pcf50633_rtc *rtc;
197 struct pcf50633_time pcf_tm;
198 int alarm_masked, ret = 0;
200 rtc = dev_get_drvdata(dev);
202 rtc2pcf_time(&pcf_tm, &alrm->time);
204 /* do like mktime does and ignore tm_wday */
205 pcf_tm.time[PCF50633_TI_WKDAY] = 7;
207 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
209 /* disable alarm interrupt */
210 if (!alarm_masked)
211 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
213 /* Returns 0 on success */
214 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
215 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
216 if (!alrm->enabled)
217 rtc->alarm_pending = 0;
219 if (!alarm_masked || alrm->enabled)
220 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
221 rtc->alarm_enabled = alrm->enabled;
223 return ret;
226 static const struct rtc_class_ops pcf50633_rtc_ops = {
227 .read_time = pcf50633_rtc_read_time,
228 .set_time = pcf50633_rtc_set_time,
229 .read_alarm = pcf50633_rtc_read_alarm,
230 .set_alarm = pcf50633_rtc_set_alarm,
231 .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable,
234 static void pcf50633_rtc_irq(int irq, void *data)
236 struct pcf50633_rtc *rtc = data;
238 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
239 rtc->alarm_pending = 1;
242 static int pcf50633_rtc_probe(struct platform_device *pdev)
244 struct pcf50633_rtc *rtc;
246 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
247 if (!rtc)
248 return -ENOMEM;
250 rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
251 platform_set_drvdata(pdev, rtc);
252 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "pcf50633-rtc",
253 &pcf50633_rtc_ops, THIS_MODULE);
255 if (IS_ERR(rtc->rtc_dev))
256 return PTR_ERR(rtc->rtc_dev);
258 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
259 pcf50633_rtc_irq, rtc);
260 return 0;
263 static int pcf50633_rtc_remove(struct platform_device *pdev)
265 struct pcf50633_rtc *rtc;
267 rtc = platform_get_drvdata(pdev);
268 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
270 return 0;
273 static struct platform_driver pcf50633_rtc_driver = {
274 .driver = {
275 .name = "pcf50633-rtc",
277 .probe = pcf50633_rtc_probe,
278 .remove = pcf50633_rtc_remove,
281 module_platform_driver(pcf50633_rtc_driver);
283 MODULE_DESCRIPTION("PCF50633 RTC driver");
284 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
285 MODULE_LICENSE("GPL");