Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-pcf50633.c
blobf90c574f9d055a8cffb8398c03b51c83327c0d50
1 /* NXP PCF50633 RTC Driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
5 * All rights reserved.
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte, Andy Green and Werner Almesberger
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/platform_device.h>
23 #include <linux/rtc.h>
24 #include <linux/bcd.h>
25 #include <linux/err.h>
27 #include <linux/mfd/pcf50633/core.h>
29 #define PCF50633_REG_RTCSC 0x59 /* Second */
30 #define PCF50633_REG_RTCMN 0x5a /* Minute */
31 #define PCF50633_REG_RTCHR 0x5b /* Hour */
32 #define PCF50633_REG_RTCWD 0x5c /* Weekday */
33 #define PCF50633_REG_RTCDT 0x5d /* Day */
34 #define PCF50633_REG_RTCMT 0x5e /* Month */
35 #define PCF50633_REG_RTCYR 0x5f /* Year */
36 #define PCF50633_REG_RTCSCA 0x60 /* Alarm Second */
37 #define PCF50633_REG_RTCMNA 0x61 /* Alarm Minute */
38 #define PCF50633_REG_RTCHRA 0x62 /* Alarm Hour */
39 #define PCF50633_REG_RTCWDA 0x63 /* Alarm Weekday */
40 #define PCF50633_REG_RTCDTA 0x64 /* Alarm Day */
41 #define PCF50633_REG_RTCMTA 0x65 /* Alarm Month */
42 #define PCF50633_REG_RTCYRA 0x66 /* Alarm Year */
44 enum pcf50633_time_indexes {
45 PCF50633_TI_SEC,
46 PCF50633_TI_MIN,
47 PCF50633_TI_HOUR,
48 PCF50633_TI_WKDAY,
49 PCF50633_TI_DAY,
50 PCF50633_TI_MONTH,
51 PCF50633_TI_YEAR,
52 PCF50633_TI_EXTENT /* always last */
55 struct pcf50633_time {
56 u_int8_t time[PCF50633_TI_EXTENT];
59 struct pcf50633_rtc {
60 int alarm_enabled;
61 int second_enabled;
62 int alarm_pending;
64 struct pcf50633 *pcf;
65 struct rtc_device *rtc_dev;
68 static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
70 rtc->tm_sec = bcd2bin(pcf->time[PCF50633_TI_SEC]);
71 rtc->tm_min = bcd2bin(pcf->time[PCF50633_TI_MIN]);
72 rtc->tm_hour = bcd2bin(pcf->time[PCF50633_TI_HOUR]);
73 rtc->tm_wday = bcd2bin(pcf->time[PCF50633_TI_WKDAY]);
74 rtc->tm_mday = bcd2bin(pcf->time[PCF50633_TI_DAY]);
75 rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
76 rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
79 static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
81 pcf->time[PCF50633_TI_SEC] = bin2bcd(rtc->tm_sec);
82 pcf->time[PCF50633_TI_MIN] = bin2bcd(rtc->tm_min);
83 pcf->time[PCF50633_TI_HOUR] = bin2bcd(rtc->tm_hour);
84 pcf->time[PCF50633_TI_WKDAY] = bin2bcd(rtc->tm_wday);
85 pcf->time[PCF50633_TI_DAY] = bin2bcd(rtc->tm_mday);
86 pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
87 pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
90 static int
91 pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
93 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
94 int err;
96 if (enabled)
97 err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
98 else
99 err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
101 if (err < 0)
102 return err;
104 rtc->alarm_enabled = enabled;
106 return 0;
109 static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
111 struct pcf50633_rtc *rtc;
112 struct pcf50633_time pcf_tm;
113 int ret;
115 rtc = dev_get_drvdata(dev);
117 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
118 PCF50633_TI_EXTENT,
119 &pcf_tm.time[0]);
120 if (ret != PCF50633_TI_EXTENT) {
121 dev_err(dev, "Failed to read time\n");
122 return -EIO;
125 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
126 pcf_tm.time[PCF50633_TI_DAY],
127 pcf_tm.time[PCF50633_TI_MONTH],
128 pcf_tm.time[PCF50633_TI_YEAR],
129 pcf_tm.time[PCF50633_TI_HOUR],
130 pcf_tm.time[PCF50633_TI_MIN],
131 pcf_tm.time[PCF50633_TI_SEC]);
133 pcf2rtc_time(tm, &pcf_tm);
135 dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
136 tm->tm_mday, tm->tm_mon, tm->tm_year,
137 tm->tm_hour, tm->tm_min, tm->tm_sec);
139 return rtc_valid_tm(tm);
142 static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
144 struct pcf50633_rtc *rtc;
145 struct pcf50633_time pcf_tm;
146 int second_masked, alarm_masked, ret = 0;
148 rtc = dev_get_drvdata(dev);
150 dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
151 tm->tm_mday, tm->tm_mon, tm->tm_year,
152 tm->tm_hour, tm->tm_min, tm->tm_sec);
154 rtc2pcf_time(&pcf_tm, tm);
156 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
157 pcf_tm.time[PCF50633_TI_DAY],
158 pcf_tm.time[PCF50633_TI_MONTH],
159 pcf_tm.time[PCF50633_TI_YEAR],
160 pcf_tm.time[PCF50633_TI_HOUR],
161 pcf_tm.time[PCF50633_TI_MIN],
162 pcf_tm.time[PCF50633_TI_SEC]);
165 second_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_SECOND);
166 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
168 if (!second_masked)
169 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_SECOND);
170 if (!alarm_masked)
171 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
173 /* Returns 0 on success */
174 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
175 PCF50633_TI_EXTENT,
176 &pcf_tm.time[0]);
178 if (!second_masked)
179 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_SECOND);
180 if (!alarm_masked)
181 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
183 return ret;
186 static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
188 struct pcf50633_rtc *rtc;
189 struct pcf50633_time pcf_tm;
190 int ret = 0;
192 rtc = dev_get_drvdata(dev);
194 alrm->enabled = rtc->alarm_enabled;
195 alrm->pending = rtc->alarm_pending;
197 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
198 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
199 if (ret != PCF50633_TI_EXTENT) {
200 dev_err(dev, "Failed to read time\n");
201 return -EIO;
204 pcf2rtc_time(&alrm->time, &pcf_tm);
206 return rtc_valid_tm(&alrm->time);
209 static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
211 struct pcf50633_rtc *rtc;
212 struct pcf50633_time pcf_tm;
213 int alarm_masked, ret = 0;
215 rtc = dev_get_drvdata(dev);
217 rtc2pcf_time(&pcf_tm, &alrm->time);
219 /* do like mktime does and ignore tm_wday */
220 pcf_tm.time[PCF50633_TI_WKDAY] = 7;
222 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
224 /* disable alarm interrupt */
225 if (!alarm_masked)
226 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
228 /* Returns 0 on success */
229 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
230 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
231 if (!alrm->enabled)
232 rtc->alarm_pending = 0;
234 if (!alarm_masked || alrm->enabled)
235 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
236 rtc->alarm_enabled = alrm->enabled;
238 return ret;
241 static struct rtc_class_ops pcf50633_rtc_ops = {
242 .read_time = pcf50633_rtc_read_time,
243 .set_time = pcf50633_rtc_set_time,
244 .read_alarm = pcf50633_rtc_read_alarm,
245 .set_alarm = pcf50633_rtc_set_alarm,
246 .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable,
249 static void pcf50633_rtc_irq(int irq, void *data)
251 struct pcf50633_rtc *rtc = data;
253 switch (irq) {
254 case PCF50633_IRQ_ALARM:
255 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
256 rtc->alarm_pending = 1;
257 break;
258 case PCF50633_IRQ_SECOND:
259 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
260 break;
264 static int __devinit pcf50633_rtc_probe(struct platform_device *pdev)
266 struct pcf50633_rtc *rtc;
268 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
269 if (!rtc)
270 return -ENOMEM;
272 rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
273 platform_set_drvdata(pdev, rtc);
274 rtc->rtc_dev = rtc_device_register("pcf50633-rtc", &pdev->dev,
275 &pcf50633_rtc_ops, THIS_MODULE);
277 if (IS_ERR(rtc->rtc_dev)) {
278 int ret = PTR_ERR(rtc->rtc_dev);
279 kfree(rtc);
280 return ret;
283 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
284 pcf50633_rtc_irq, rtc);
285 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_SECOND,
286 pcf50633_rtc_irq, rtc);
288 return 0;
291 static int __devexit pcf50633_rtc_remove(struct platform_device *pdev)
293 struct pcf50633_rtc *rtc;
295 rtc = platform_get_drvdata(pdev);
297 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
298 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_SECOND);
300 rtc_device_unregister(rtc->rtc_dev);
301 kfree(rtc);
303 return 0;
306 static struct platform_driver pcf50633_rtc_driver = {
307 .driver = {
308 .name = "pcf50633-rtc",
310 .probe = pcf50633_rtc_probe,
311 .remove = __devexit_p(pcf50633_rtc_remove),
314 static int __init pcf50633_rtc_init(void)
316 return platform_driver_register(&pcf50633_rtc_driver);
318 module_init(pcf50633_rtc_init);
320 static void __exit pcf50633_rtc_exit(void)
322 platform_driver_unregister(&pcf50633_rtc_driver);
324 module_exit(pcf50633_rtc_exit);
326 MODULE_DESCRIPTION("PCF50633 RTC driver");
327 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
328 MODULE_LICENSE("GPL");