blob: 16edf94ab42f2826f20024ef3a57778f8e15a0c6 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Balaji Raoeae854b2009-01-09 01:50:51 +010022#include <linux/platform_device.h>
23#include <linux/rtc.h>
24#include <linux/bcd.h>
25#include <linux/err.h>
26
27#include <linux/mfd/pcf50633/core.h>
28
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 */
43
44enum 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 */
53};
54
55struct pcf50633_time {
56 u_int8_t time[PCF50633_TI_EXTENT];
57};
58
59struct pcf50633_rtc {
60 int alarm_enabled;
61 int second_enabled;
Werner Almesbergera766ae32009-12-15 16:46:07 -080062 int alarm_pending;
Balaji Raoeae854b2009-01-09 01:50:51 +010063
64 struct pcf50633 *pcf;
65 struct rtc_device *rtc_dev;
66};
67
68static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
69{
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]);
Rask Ingemann Lambertsen265f2a02009-11-11 14:26:36 -080075 rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
Balaji Raoeae854b2009-01-09 01:50:51 +010076 rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
77}
78
79static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
80{
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);
Rask Ingemann Lambertsen265f2a02009-11-11 14:26:36 -080086 pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
Balaji Raoeae854b2009-01-09 01:50:51 +010087 pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
88}
89
90static int
91pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
92{
93 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
94 int err;
95
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);
100
101 if (err < 0)
102 return err;
103
104 rtc->alarm_enabled = enabled;
105
106 return 0;
107}
108
109static int
110pcf50633_rtc_update_irq_enable(struct device *dev, unsigned int enabled)
111{
112 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
113 int err;
114
115 if (enabled)
116 err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_SECOND);
117 else
118 err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_SECOND);
119
120 if (err < 0)
121 return err;
122
123 rtc->second_enabled = enabled;
124
125 return 0;
126}
127
128static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
129{
130 struct pcf50633_rtc *rtc;
131 struct pcf50633_time pcf_tm;
132 int ret;
133
134 rtc = dev_get_drvdata(dev);
135
136 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
137 PCF50633_TI_EXTENT,
138 &pcf_tm.time[0]);
139 if (ret != PCF50633_TI_EXTENT) {
140 dev_err(dev, "Failed to read time\n");
141 return -EIO;
142 }
143
144 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
145 pcf_tm.time[PCF50633_TI_DAY],
146 pcf_tm.time[PCF50633_TI_MONTH],
147 pcf_tm.time[PCF50633_TI_YEAR],
148 pcf_tm.time[PCF50633_TI_HOUR],
149 pcf_tm.time[PCF50633_TI_MIN],
150 pcf_tm.time[PCF50633_TI_SEC]);
151
152 pcf2rtc_time(tm, &pcf_tm);
153
154 dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
155 tm->tm_mday, tm->tm_mon, tm->tm_year,
156 tm->tm_hour, tm->tm_min, tm->tm_sec);
157
158 return rtc_valid_tm(tm);
159}
160
161static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
162{
163 struct pcf50633_rtc *rtc;
164 struct pcf50633_time pcf_tm;
165 int second_masked, alarm_masked, ret = 0;
166
167 rtc = dev_get_drvdata(dev);
168
169 dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
170 tm->tm_mday, tm->tm_mon, tm->tm_year,
171 tm->tm_hour, tm->tm_min, tm->tm_sec);
172
173 rtc2pcf_time(&pcf_tm, tm);
174
175 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
176 pcf_tm.time[PCF50633_TI_DAY],
177 pcf_tm.time[PCF50633_TI_MONTH],
178 pcf_tm.time[PCF50633_TI_YEAR],
179 pcf_tm.time[PCF50633_TI_HOUR],
180 pcf_tm.time[PCF50633_TI_MIN],
181 pcf_tm.time[PCF50633_TI_SEC]);
182
183
184 second_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_SECOND);
185 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
186
187 if (!second_masked)
188 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_SECOND);
189 if (!alarm_masked)
190 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
191
192 /* Returns 0 on success */
193 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
194 PCF50633_TI_EXTENT,
195 &pcf_tm.time[0]);
196
197 if (!second_masked)
198 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_SECOND);
199 if (!alarm_masked)
200 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
201
202 return ret;
203}
204
205static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
206{
207 struct pcf50633_rtc *rtc;
208 struct pcf50633_time pcf_tm;
209 int ret = 0;
210
211 rtc = dev_get_drvdata(dev);
212
213 alrm->enabled = rtc->alarm_enabled;
Werner Almesbergera766ae32009-12-15 16:46:07 -0800214 alrm->pending = rtc->alarm_pending;
Balaji Raoeae854b2009-01-09 01:50:51 +0100215
216 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
217 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
218 if (ret != PCF50633_TI_EXTENT) {
219 dev_err(dev, "Failed to read time\n");
220 return -EIO;
221 }
222
223 pcf2rtc_time(&alrm->time, &pcf_tm);
224
225 return rtc_valid_tm(&alrm->time);
226}
227
228static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
229{
230 struct pcf50633_rtc *rtc;
231 struct pcf50633_time pcf_tm;
232 int alarm_masked, ret = 0;
233
234 rtc = dev_get_drvdata(dev);
235
236 rtc2pcf_time(&pcf_tm, &alrm->time);
237
238 /* do like mktime does and ignore tm_wday */
239 pcf_tm.time[PCF50633_TI_WKDAY] = 7;
240
241 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
242
243 /* disable alarm interrupt */
244 if (!alarm_masked)
245 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
246
247 /* Returns 0 on success */
248 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
249 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
Werner Almesbergera766ae32009-12-15 16:46:07 -0800250 if (!alrm->enabled)
251 rtc->alarm_pending = 0;
Balaji Raoeae854b2009-01-09 01:50:51 +0100252
Werner Almesberger05f45d72009-11-11 14:26:40 -0800253 if (!alarm_masked || alrm->enabled)
Balaji Raoeae854b2009-01-09 01:50:51 +0100254 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
Werner Almesberger05f45d72009-11-11 14:26:40 -0800255 rtc->alarm_enabled = alrm->enabled;
Balaji Raoeae854b2009-01-09 01:50:51 +0100256
257 return ret;
258}
259
260static struct rtc_class_ops pcf50633_rtc_ops = {
261 .read_time = pcf50633_rtc_read_time,
262 .set_time = pcf50633_rtc_set_time,
263 .read_alarm = pcf50633_rtc_read_alarm,
264 .set_alarm = pcf50633_rtc_set_alarm,
265 .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable,
266 .update_irq_enable = pcf50633_rtc_update_irq_enable,
267};
268
269static void pcf50633_rtc_irq(int irq, void *data)
270{
271 struct pcf50633_rtc *rtc = data;
272
273 switch (irq) {
274 case PCF50633_IRQ_ALARM:
275 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
Werner Almesbergera766ae32009-12-15 16:46:07 -0800276 rtc->alarm_pending = 1;
Balaji Raoeae854b2009-01-09 01:50:51 +0100277 break;
278 case PCF50633_IRQ_SECOND:
279 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
280 break;
281 }
282}
283
284static int __devinit pcf50633_rtc_probe(struct platform_device *pdev)
285{
Balaji Raoeae854b2009-01-09 01:50:51 +0100286 struct pcf50633_rtc *rtc;
287
Balaji Raoeae854b2009-01-09 01:50:51 +0100288 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
289 if (!rtc)
290 return -ENOMEM;
291
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400292 rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
Balaji Raoeae854b2009-01-09 01:50:51 +0100293 platform_set_drvdata(pdev, rtc);
294 rtc->rtc_dev = rtc_device_register("pcf50633-rtc", &pdev->dev,
295 &pcf50633_rtc_ops, THIS_MODULE);
296
297 if (IS_ERR(rtc->rtc_dev)) {
Dan Carpenterfa00e102009-12-01 13:17:41 -0800298 int ret = PTR_ERR(rtc->rtc_dev);
Balaji Raoeae854b2009-01-09 01:50:51 +0100299 kfree(rtc);
Dan Carpenterfa00e102009-12-01 13:17:41 -0800300 return ret;
Balaji Raoeae854b2009-01-09 01:50:51 +0100301 }
302
303 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
304 pcf50633_rtc_irq, rtc);
305 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_SECOND,
306 pcf50633_rtc_irq, rtc);
307
308 return 0;
309}
310
311static int __devexit pcf50633_rtc_remove(struct platform_device *pdev)
312{
313 struct pcf50633_rtc *rtc;
314
315 rtc = platform_get_drvdata(pdev);
316
317 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
318 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_SECOND);
319
320 rtc_device_unregister(rtc->rtc_dev);
321 kfree(rtc);
322
323 return 0;
324}
325
326static struct platform_driver pcf50633_rtc_driver = {
327 .driver = {
328 .name = "pcf50633-rtc",
329 },
330 .probe = pcf50633_rtc_probe,
331 .remove = __devexit_p(pcf50633_rtc_remove),
332};
333
334static int __init pcf50633_rtc_init(void)
335{
336 return platform_driver_register(&pcf50633_rtc_driver);
337}
338module_init(pcf50633_rtc_init);
339
340static void __exit pcf50633_rtc_exit(void)
341{
342 platform_driver_unregister(&pcf50633_rtc_driver);
343}
344module_exit(pcf50633_rtc_exit);
345
346MODULE_DESCRIPTION("PCF50633 RTC driver");
347MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
348MODULE_LICENSE("GPL");
349