cxgb3: convert to use netdev_for_each_addr
[linux-2.6/cjktty.git] / drivers / rtc / rtc-pcf50633.c
blob854c3cb365a1919d0c6a92dad726f2a648ed4a5e
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/platform_device.h>
22 #include <linux/rtc.h>
23 #include <linux/bcd.h>
24 #include <linux/err.h>
26 #include <linux/mfd/pcf50633/core.h>
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 */
43 enum 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 */
54 struct pcf50633_time {
55 u_int8_t time[PCF50633_TI_EXTENT];
58 struct pcf50633_rtc {
59 int alarm_enabled;
60 int second_enabled;
61 int alarm_pending;
63 struct pcf50633 *pcf;
64 struct rtc_device *rtc_dev;
67 static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
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]);
74 rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
75 rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
78 static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
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);
85 pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
86 pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
89 static int
90 pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
92 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
93 int err;
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);
100 if (err < 0)
101 return err;
103 rtc->alarm_enabled = enabled;
105 return 0;
108 static int
109 pcf50633_rtc_update_irq_enable(struct device *dev, unsigned int enabled)
111 struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
112 int err;
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);
119 if (err < 0)
120 return err;
122 rtc->second_enabled = enabled;
124 return 0;
127 static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
129 struct pcf50633_rtc *rtc;
130 struct pcf50633_time pcf_tm;
131 int ret;
133 rtc = dev_get_drvdata(dev);
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;
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]);
151 pcf2rtc_time(tm, &pcf_tm);
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);
157 return rtc_valid_tm(tm);
160 static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
162 struct pcf50633_rtc *rtc;
163 struct pcf50633_time pcf_tm;
164 int second_masked, alarm_masked, ret = 0;
166 rtc = dev_get_drvdata(dev);
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);
172 rtc2pcf_time(&pcf_tm, tm);
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]);
183 second_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_SECOND);
184 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
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);
191 /* Returns 0 on success */
192 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
193 PCF50633_TI_EXTENT,
194 &pcf_tm.time[0]);
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);
201 return ret;
204 static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
206 struct pcf50633_rtc *rtc;
207 struct pcf50633_time pcf_tm;
208 int ret = 0;
210 rtc = dev_get_drvdata(dev);
212 alrm->enabled = rtc->alarm_enabled;
213 alrm->pending = rtc->alarm_pending;
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;
222 pcf2rtc_time(&alrm->time, &pcf_tm);
224 return rtc_valid_tm(&alrm->time);
227 static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
229 struct pcf50633_rtc *rtc;
230 struct pcf50633_time pcf_tm;
231 int alarm_masked, ret = 0;
233 rtc = dev_get_drvdata(dev);
235 rtc2pcf_time(&pcf_tm, &alrm->time);
237 /* do like mktime does and ignore tm_wday */
238 pcf_tm.time[PCF50633_TI_WKDAY] = 7;
240 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
242 /* disable alarm interrupt */
243 if (!alarm_masked)
244 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
246 /* Returns 0 on success */
247 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
248 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
249 if (!alrm->enabled)
250 rtc->alarm_pending = 0;
252 if (!alarm_masked || alrm->enabled)
253 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
254 rtc->alarm_enabled = alrm->enabled;
256 return ret;
259 static 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,
268 static void pcf50633_rtc_irq(int irq, void *data)
270 struct pcf50633_rtc *rtc = data;
272 switch (irq) {
273 case PCF50633_IRQ_ALARM:
274 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
275 rtc->alarm_pending = 1;
276 break;
277 case PCF50633_IRQ_SECOND:
278 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
279 break;
283 static int __devinit pcf50633_rtc_probe(struct platform_device *pdev)
285 struct pcf50633_rtc *rtc;
287 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
288 if (!rtc)
289 return -ENOMEM;
291 rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
292 platform_set_drvdata(pdev, rtc);
293 rtc->rtc_dev = rtc_device_register("pcf50633-rtc", &pdev->dev,
294 &pcf50633_rtc_ops, THIS_MODULE);
296 if (IS_ERR(rtc->rtc_dev)) {
297 int ret = PTR_ERR(rtc->rtc_dev);
298 kfree(rtc);
299 return ret;
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);
307 return 0;
310 static int __devexit pcf50633_rtc_remove(struct platform_device *pdev)
312 struct pcf50633_rtc *rtc;
314 rtc = platform_get_drvdata(pdev);
316 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
317 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_SECOND);
319 rtc_device_unregister(rtc->rtc_dev);
320 kfree(rtc);
322 return 0;
325 static struct platform_driver pcf50633_rtc_driver = {
326 .driver = {
327 .name = "pcf50633-rtc",
329 .probe = pcf50633_rtc_probe,
330 .remove = __devexit_p(pcf50633_rtc_remove),
333 static int __init pcf50633_rtc_init(void)
335 return platform_driver_register(&pcf50633_rtc_driver);
337 module_init(pcf50633_rtc_init);
339 static void __exit pcf50633_rtc_exit(void)
341 platform_driver_unregister(&pcf50633_rtc_driver);
343 module_exit(pcf50633_rtc_exit);
345 MODULE_DESCRIPTION("PCF50633 RTC driver");
346 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
347 MODULE_LICENSE("GPL");