blob: 0eb05b1d0b945dcd3b0e5a343df765f58d7220a9 [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;
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
Balaji Raoeae854b2009-01-09 01:50:51 +0100108static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
109{
110 struct pcf50633_rtc *rtc;
111 struct pcf50633_time pcf_tm;
112 int ret;
113
114 rtc = dev_get_drvdata(dev);
115
116 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
117 PCF50633_TI_EXTENT,
118 &pcf_tm.time[0]);
119 if (ret != PCF50633_TI_EXTENT) {
120 dev_err(dev, "Failed to read time\n");
121 return -EIO;
122 }
123
124 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
125 pcf_tm.time[PCF50633_TI_DAY],
126 pcf_tm.time[PCF50633_TI_MONTH],
127 pcf_tm.time[PCF50633_TI_YEAR],
128 pcf_tm.time[PCF50633_TI_HOUR],
129 pcf_tm.time[PCF50633_TI_MIN],
130 pcf_tm.time[PCF50633_TI_SEC]);
131
132 pcf2rtc_time(tm, &pcf_tm);
133
Andy Shevchenko8d2e6b22018-12-04 23:23:18 +0200134 dev_dbg(dev, "RTC_TIME: %ptRr\n", tm);
Balaji Raoeae854b2009-01-09 01:50:51 +0100135
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100136 return 0;
Balaji Raoeae854b2009-01-09 01:50:51 +0100137}
138
139static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
140{
141 struct pcf50633_rtc *rtc;
142 struct pcf50633_time pcf_tm;
Lars-Peter Clausen3d62b8f2011-05-26 16:25:10 -0700143 int alarm_masked, ret = 0;
Balaji Raoeae854b2009-01-09 01:50:51 +0100144
145 rtc = dev_get_drvdata(dev);
146
Andy Shevchenko8d2e6b22018-12-04 23:23:18 +0200147 dev_dbg(dev, "RTC_TIME: %ptRr\n", tm);
Balaji Raoeae854b2009-01-09 01:50:51 +0100148
149 rtc2pcf_time(&pcf_tm, tm);
150
151 dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
152 pcf_tm.time[PCF50633_TI_DAY],
153 pcf_tm.time[PCF50633_TI_MONTH],
154 pcf_tm.time[PCF50633_TI_YEAR],
155 pcf_tm.time[PCF50633_TI_HOUR],
156 pcf_tm.time[PCF50633_TI_MIN],
157 pcf_tm.time[PCF50633_TI_SEC]);
158
159
Balaji Raoeae854b2009-01-09 01:50:51 +0100160 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
161
Balaji Raoeae854b2009-01-09 01:50:51 +0100162 if (!alarm_masked)
163 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
164
165 /* Returns 0 on success */
166 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
167 PCF50633_TI_EXTENT,
168 &pcf_tm.time[0]);
169
Balaji Raoeae854b2009-01-09 01:50:51 +0100170 if (!alarm_masked)
171 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
172
173 return ret;
174}
175
176static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
177{
178 struct pcf50633_rtc *rtc;
179 struct pcf50633_time pcf_tm;
180 int ret = 0;
181
182 rtc = dev_get_drvdata(dev);
183
184 alrm->enabled = rtc->alarm_enabled;
Werner Almesbergera766ae32009-12-15 16:46:07 -0800185 alrm->pending = rtc->alarm_pending;
Balaji Raoeae854b2009-01-09 01:50:51 +0100186
187 ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
188 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
189 if (ret != PCF50633_TI_EXTENT) {
190 dev_err(dev, "Failed to read time\n");
191 return -EIO;
192 }
193
194 pcf2rtc_time(&alrm->time, &pcf_tm);
195
196 return rtc_valid_tm(&alrm->time);
197}
198
199static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
200{
201 struct pcf50633_rtc *rtc;
202 struct pcf50633_time pcf_tm;
203 int alarm_masked, ret = 0;
204
205 rtc = dev_get_drvdata(dev);
206
207 rtc2pcf_time(&pcf_tm, &alrm->time);
208
209 /* do like mktime does and ignore tm_wday */
210 pcf_tm.time[PCF50633_TI_WKDAY] = 7;
211
212 alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
213
214 /* disable alarm interrupt */
215 if (!alarm_masked)
216 pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
217
218 /* Returns 0 on success */
219 ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
220 PCF50633_TI_EXTENT, &pcf_tm.time[0]);
Werner Almesbergera766ae32009-12-15 16:46:07 -0800221 if (!alrm->enabled)
222 rtc->alarm_pending = 0;
Balaji Raoeae854b2009-01-09 01:50:51 +0100223
Werner Almesberger05f45d72009-11-11 14:26:40 -0800224 if (!alarm_masked || alrm->enabled)
Balaji Raoeae854b2009-01-09 01:50:51 +0100225 pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
Werner Almesberger05f45d72009-11-11 14:26:40 -0800226 rtc->alarm_enabled = alrm->enabled;
Balaji Raoeae854b2009-01-09 01:50:51 +0100227
228 return ret;
229}
230
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200231static const struct rtc_class_ops pcf50633_rtc_ops = {
Balaji Raoeae854b2009-01-09 01:50:51 +0100232 .read_time = pcf50633_rtc_read_time,
233 .set_time = pcf50633_rtc_set_time,
234 .read_alarm = pcf50633_rtc_read_alarm,
235 .set_alarm = pcf50633_rtc_set_alarm,
John Stultz51ba60c2011-02-03 12:13:50 -0800236 .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable,
Balaji Raoeae854b2009-01-09 01:50:51 +0100237};
238
239static void pcf50633_rtc_irq(int irq, void *data)
240{
241 struct pcf50633_rtc *rtc = data;
242
Lars-Peter Clausen3d62b8f2011-05-26 16:25:10 -0700243 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
244 rtc->alarm_pending = 1;
Balaji Raoeae854b2009-01-09 01:50:51 +0100245}
246
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800247static int pcf50633_rtc_probe(struct platform_device *pdev)
Balaji Raoeae854b2009-01-09 01:50:51 +0100248{
Balaji Raoeae854b2009-01-09 01:50:51 +0100249 struct pcf50633_rtc *rtc;
250
Jingoo Han76753ca2013-04-29 16:20:48 -0700251 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
Balaji Raoeae854b2009-01-09 01:50:51 +0100252 if (!rtc)
253 return -ENOMEM;
254
Lars-Peter Clausen68d641e2009-10-14 02:12:33 +0400255 rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
Balaji Raoeae854b2009-01-09 01:50:51 +0100256 platform_set_drvdata(pdev, rtc);
Jingoo Han76753ca2013-04-29 16:20:48 -0700257 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "pcf50633-rtc",
Balaji Raoeae854b2009-01-09 01:50:51 +0100258 &pcf50633_rtc_ops, THIS_MODULE);
259
Jingoo Han76753ca2013-04-29 16:20:48 -0700260 if (IS_ERR(rtc->rtc_dev))
261 return PTR_ERR(rtc->rtc_dev);
Balaji Raoeae854b2009-01-09 01:50:51 +0100262
263 pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
264 pcf50633_rtc_irq, rtc);
Balaji Raoeae854b2009-01-09 01:50:51 +0100265 return 0;
266}
267
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800268static int pcf50633_rtc_remove(struct platform_device *pdev)
Balaji Raoeae854b2009-01-09 01:50:51 +0100269{
270 struct pcf50633_rtc *rtc;
271
272 rtc = platform_get_drvdata(pdev);
Balaji Raoeae854b2009-01-09 01:50:51 +0100273 pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
Balaji Raoeae854b2009-01-09 01:50:51 +0100274
Balaji Raoeae854b2009-01-09 01:50:51 +0100275 return 0;
276}
277
278static struct platform_driver pcf50633_rtc_driver = {
279 .driver = {
280 .name = "pcf50633-rtc",
281 },
282 .probe = pcf50633_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800283 .remove = pcf50633_rtc_remove,
Balaji Raoeae854b2009-01-09 01:50:51 +0100284};
285
Axel Lin0c4eae62012-01-10 15:10:48 -0800286module_platform_driver(pcf50633_rtc_driver);
Balaji Raoeae854b2009-01-09 01:50:51 +0100287
288MODULE_DESCRIPTION("PCF50633 RTC driver");
289MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
290MODULE_LICENSE("GPL");
291