blob: 854c3cb365a1919d0c6a92dad726f2a648ed4a5e [file] [log] [blame]
Balaji Raoeae854b2009-01-09 01:50:51 +01001/* NXP PCF50633 RTC Driver
2 *
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
5 * All rights reserved.
6 *
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte, Andy Green and Werner Almesberger
9 *
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.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/platform_device.h>
22#include <linux/rtc.h>
23#include <linux/bcd.h>
24#include <linux/err.h>
25
26#include <linux/mfd/pcf50633/core.h>
27
28#define PCF50633_REG_RTCSC 0x59 /* Second */
29#define PCF50633_REG_RTCMN 0x5a /* Minute */
30#define PCF50633_REG_RTCHR 0x5b /* Hour */
31#define PCF50633_REG_RTCWD 0x5c /* Weekday */
32#define PCF50633_REG_RTCDT 0x5d /* Day */
33#define PCF50633_REG_RTCMT 0x5e /* Month */
34#define PCF50633_REG_RTCYR 0x5f /* Year */
35#define PCF50633_REG_RTCSCA 0x60 /* Alarm Second */
36#define PCF50633_REG_RTCMNA 0x61 /* Alarm Minute */
37#define PCF50633_REG_RTCHRA 0x62 /* Alarm Hour */
38#define PCF50633_REG_RTCWDA 0x63 /* Alarm Weekday */
39#define PCF50633_REG_RTCDTA 0x64 /* Alarm Day */
40#define PCF50633_REG_RTCMTA 0x65 /* Alarm Month */
41#define PCF50633_REG_RTCYRA 0x66 /* Alarm Year */
42
43enum pcf50633_time_indexes {
44 PCF50633_TI_SEC,
45 PCF50633_TI_MIN,
46 PCF50633_TI_HOUR,
47 PCF50633_TI_WKDAY,
48 PCF50633_TI_DAY,
49 PCF50633_TI_MONTH,
50 PCF50633_TI_YEAR,
51 PCF50633_TI_EXTENT /* always last */
52};
53
54struct pcf50633_time {
55 u_int8_t time[PCF50633_TI_EXTENT];
56};
57
58struct pcf50633_rtc {
59 int alarm_enabled;
60 int second_enabled;
Werner Almesbergera766ae32009-12-15 16:46:07 -080061 int alarm_pending;
Balaji Raoeae854b2009-01-09 01:50:51 +010062
63 struct pcf50633 *pcf;
64 struct rtc_device *rtc_dev;
65};
66
67static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
68{
69 rtc->tm_sec = bcd2bin(pcf->time[PCF50633_TI_SEC]);
70 rtc->tm_min = bcd2bin(pcf->time[PCF50633_TI_MIN]);
71 rtc->tm_hour = bcd2bin(pcf->time[PCF50633_TI_HOUR]);
72 rtc->tm_wday = bcd2bin(pcf->time[PCF50633_TI_WKDAY]);
73 rtc->tm_mday = bcd2bin(pcf->time[PCF50633_TI_DAY]);
Rask Ingemann Lambertsen265f2a02009-11-11 14:26:36 -080074 rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
Balaji Raoeae854b2009-01-09 01:50:51 +010075 rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
76}
77
78static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
79{
80 pcf->time[PCF50633_TI_SEC] = bin2bcd(rtc->tm_sec);
81 pcf->time[PCF50633_TI_MIN] = bin2bcd(rtc->tm_min);
82 pcf->time[PCF50633_TI_HOUR] = bin2bcd(rtc->tm_hour);
83 pcf->time[PCF50633_TI_WKDAY] = bin2bcd(rtc->tm_wday);
84 pcf->time[PCF50633_TI_DAY] = bin2bcd(rtc->tm_mday);
Rask Ingemann Lambertsen265f2a02009-11-11 14:26:36 -080085 pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
Balaji Raoeae854b2009-01-09 01:50:51 +010086 pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
87}
88
89static int
90pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
91{
92 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
93 int err;
94
95 if (enabled)
96 err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
97 else
98 err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
99
100 if (err < 0)
101 return err;
102
103 rtc->alarm_enabled = enabled;
104
105 return 0;
106}
107
108static int
109pcf50633_rtc_update_irq_enable(struct device *dev, unsigned int enabled)
110{
111 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
112 int err;
113
114 if (enabled)
115 err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_SECOND);
116 else
117 err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_SECOND);
118
119 if (err < 0)
120 return err;
121
122 rtc->second_enabled = enabled;
123
124 return 0;
125}
126
127static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
128{
129 struct pcf50633_rtc *rtc;
130 struct pcf50633_time pcf_tm;
131 int ret;
132
133 rtc = dev_get_drvdata(dev);
134
135 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
136 PCF50633_TI_EXTENT,
137 &pcf_tm.time[0]);
138 if (ret != PCF50633_TI_EXTENT) {
139 dev_err(dev, "Failed to read time\n");
140 return -EIO;
141 }
142
143 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
144 pcf_tm.time[PCF50633_TI_DAY],
145 pcf_tm.time[PCF50633_TI_MONTH],
146 pcf_tm.time[PCF50633_TI_YEAR],
147 pcf_tm.time[PCF50633_TI_HOUR],
148 pcf_tm.time[PCF50633_TI_MIN],
149 pcf_tm.time[PCF50633_TI_SEC]);
150
151 pcf2rtc_time(tm, &pcf_tm);
152
153 dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
154 tm->tm_mday, tm->tm_mon, tm->tm_year,
155 tm->tm_hour, tm->tm_min, tm->tm_sec);
156
157 return rtc_valid_tm(tm);
158}
159
160static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
161{
162 struct pcf50633_rtc *rtc;
163 struct pcf50633_time pcf_tm;
164 int second_masked, alarm_masked, ret = 0;
165
166 rtc = dev_get_drvdata(dev);
167
168 dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
169 tm->tm_mday, tm->tm_mon, tm->tm_year,
170 tm->tm_hour, tm->tm_min, tm->tm_sec);
171
172 rtc2pcf_time(&pcf_tm, tm);
173
174 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
175 pcf_tm.time[PCF50633_TI_DAY],
176 pcf_tm.time[PCF50633_TI_MONTH],
177 pcf_tm.time[PCF50633_TI_YEAR],
178 pcf_tm.time[PCF50633_TI_HOUR],
179 pcf_tm.time[PCF50633_TI_MIN],
180 pcf_tm.time[PCF50633_TI_SEC]);
181
182
183 second_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_SECOND);
184 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
185
186 if (!second_masked)
187 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_SECOND);
188 if (!alarm_masked)
189 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
190
191 /* Returns 0 on success */
192 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
193 PCF50633_TI_EXTENT,
194 &pcf_tm.time[0]);
195
196 if (!second_masked)
197 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_SECOND);
198 if (!alarm_masked)
199 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
200
201 return ret;
202}
203
204static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
205{
206 struct pcf50633_rtc *rtc;
207 struct pcf50633_time pcf_tm;
208 int ret = 0;
209
210 rtc = dev_get_drvdata(dev);
211
212 alrm->enabled = rtc->alarm_enabled;
Werner Almesbergera766ae32009-12-15 16:46:07 -0800213 alrm->pending = rtc->alarm_pending;
Balaji Raoeae854b2009-01-09 01:50:51 +0100214
215 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
216 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
217 if (ret != PCF50633_TI_EXTENT) {
218 dev_err(dev, "Failed to read time\n");
219 return -EIO;
220 }
221
222 pcf2rtc_time(&alrm->time, &pcf_tm);
223
224 return rtc_valid_tm(&alrm->time);
225}
226
227static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
228{
229 struct pcf50633_rtc *rtc;
230 struct pcf50633_time pcf_tm;
231 int alarm_masked, ret = 0;
232
233 rtc = dev_get_drvdata(dev);
234
235 rtc2pcf_time(&pcf_tm, &alrm->time);
236
237 /* do like mktime does and ignore tm_wday */
238 pcf_tm.time[PCF50633_TI_WKDAY] = 7;
239
240 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
241
242 /* disable alarm interrupt */
243 if (!alarm_masked)
244 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
245
246 /* Returns 0 on success */
247 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
248 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
Werner Almesbergera766ae32009-12-15 16:46:07 -0800249 if (!alrm->enabled)
250 rtc->alarm_pending = 0;
Balaji Raoeae854b2009-01-09 01:50:51 +0100251
Werner Almesberger05f45d72009-11-11 14:26:40 -0800252 if (!alarm_masked || alrm->enabled)
Balaji Raoeae854b2009-01-09 01:50:51 +0100253 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
Werner Almesberger05f45d72009-11-11 14:26:40 -0800254 rtc->alarm_enabled = alrm->enabled;
Balaji Raoeae854b2009-01-09 01:50:51 +0100255
256 return ret;
257}
258
259static struct rtc_class_ops pcf50633_rtc_ops = {
260 .read_time = pcf50633_rtc_read_time,
261 .set_time = pcf50633_rtc_set_time,
262 .read_alarm = pcf50633_rtc_read_alarm,
263 .set_alarm = pcf50633_rtc_set_alarm,
264 .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable,
265 .update_irq_enable = pcf50633_rtc_update_irq_enable,
266};
267
268static void pcf50633_rtc_irq(int irq, void *data)
269{
270 struct pcf50633_rtc *rtc = data;
271
272 switch (irq) {
273 case PCF50633_IRQ_ALARM:
274 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
Werner Almesbergera766ae32009-12-15 16:46:07 -0800275 rtc->alarm_pending = 1;
Balaji Raoeae854b2009-01-09 01:50:51 +0100276 break;
277 case PCF50633_IRQ_SECOND:
278 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
279 break;
280 }
281}
282
283static int __devinit pcf50633_rtc_probe(struct platform_device *pdev)
284{
Balaji Raoeae854b2009-01-09 01:50:51 +0100285 struct pcf50633_rtc *rtc;
286
Balaji Raoeae854b2009-01-09 01:50:51 +0100287 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
288 if (!rtc)
289 return -ENOMEM;
290
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400291 rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
Balaji Raoeae854b2009-01-09 01:50:51 +0100292 platform_set_drvdata(pdev, rtc);
293 rtc->rtc_dev = rtc_device_register("pcf50633-rtc", &pdev->dev,
294 &pcf50633_rtc_ops, THIS_MODULE);
295
296 if (IS_ERR(rtc->rtc_dev)) {
Dan Carpenterfa00e102009-12-01 13:17:41 -0800297 int ret = PTR_ERR(rtc->rtc_dev);
Balaji Raoeae854b2009-01-09 01:50:51 +0100298 kfree(rtc);
Dan Carpenterfa00e102009-12-01 13:17:41 -0800299 return ret;
Balaji Raoeae854b2009-01-09 01:50:51 +0100300 }
301
302 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
303 pcf50633_rtc_irq, rtc);
304 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_SECOND,
305 pcf50633_rtc_irq, rtc);
306
307 return 0;
308}
309
310static int __devexit pcf50633_rtc_remove(struct platform_device *pdev)
311{
312 struct pcf50633_rtc *rtc;
313
314 rtc = platform_get_drvdata(pdev);
315
316 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
317 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_SECOND);
318
319 rtc_device_unregister(rtc->rtc_dev);
320 kfree(rtc);
321
322 return 0;
323}
324
325static struct platform_driver pcf50633_rtc_driver = {
326 .driver = {
327 .name = "pcf50633-rtc",
328 },
329 .probe = pcf50633_rtc_probe,
330 .remove = __devexit_p(pcf50633_rtc_remove),
331};
332
333static int __init pcf50633_rtc_init(void)
334{
335 return platform_driver_register(&pcf50633_rtc_driver);
336}
337module_init(pcf50633_rtc_init);
338
339static void __exit pcf50633_rtc_exit(void)
340{
341 platform_driver_unregister(&pcf50633_rtc_driver);
342}
343module_exit(pcf50633_rtc_exit);
344
345MODULE_DESCRIPTION("PCF50633 RTC driver");
346MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
347MODULE_LICENSE("GPL");
348