xref: /openbmc/linux/drivers/rtc/rtc-pcf50633.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eae854b2SBalaji Rao /* NXP PCF50633 RTC Driver
3eae854b2SBalaji Rao  *
4eae854b2SBalaji Rao  * (C) 2006-2008 by Openmoko, Inc.
5eae854b2SBalaji Rao  * Author: Balaji Rao <balajirrao@openmoko.org>
6eae854b2SBalaji Rao  * All rights reserved.
7eae854b2SBalaji Rao  *
8eae854b2SBalaji Rao  * Broken down from monstrous PCF50633 driver mainly by
9eae854b2SBalaji Rao  * Harald Welte, Andy Green and Werner Almesberger
10eae854b2SBalaji Rao  */
11eae854b2SBalaji Rao 
12eae854b2SBalaji Rao #include <linux/kernel.h>
13eae854b2SBalaji Rao #include <linux/module.h>
14eae854b2SBalaji Rao #include <linux/init.h>
15eae854b2SBalaji Rao #include <linux/device.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
17eae854b2SBalaji Rao #include <linux/platform_device.h>
18eae854b2SBalaji Rao #include <linux/rtc.h>
19eae854b2SBalaji Rao #include <linux/bcd.h>
20eae854b2SBalaji Rao #include <linux/err.h>
21eae854b2SBalaji Rao 
22eae854b2SBalaji Rao #include <linux/mfd/pcf50633/core.h>
23eae854b2SBalaji Rao 
24eae854b2SBalaji Rao #define PCF50633_REG_RTCSC	0x59 /* Second */
25eae854b2SBalaji Rao #define PCF50633_REG_RTCMN	0x5a /* Minute */
26eae854b2SBalaji Rao #define PCF50633_REG_RTCHR	0x5b /* Hour */
27eae854b2SBalaji Rao #define PCF50633_REG_RTCWD	0x5c /* Weekday */
28eae854b2SBalaji Rao #define PCF50633_REG_RTCDT	0x5d /* Day */
29eae854b2SBalaji Rao #define PCF50633_REG_RTCMT	0x5e /* Month */
30eae854b2SBalaji Rao #define PCF50633_REG_RTCYR	0x5f /* Year */
31eae854b2SBalaji Rao #define PCF50633_REG_RTCSCA	0x60 /* Alarm Second */
32eae854b2SBalaji Rao #define PCF50633_REG_RTCMNA	0x61 /* Alarm Minute */
33eae854b2SBalaji Rao #define PCF50633_REG_RTCHRA	0x62 /* Alarm Hour */
34eae854b2SBalaji Rao #define PCF50633_REG_RTCWDA	0x63 /* Alarm Weekday */
35eae854b2SBalaji Rao #define PCF50633_REG_RTCDTA	0x64 /* Alarm Day */
36eae854b2SBalaji Rao #define PCF50633_REG_RTCMTA	0x65 /* Alarm Month */
37eae854b2SBalaji Rao #define PCF50633_REG_RTCYRA	0x66 /* Alarm Year */
38eae854b2SBalaji Rao 
39eae854b2SBalaji Rao enum pcf50633_time_indexes {
40eae854b2SBalaji Rao 	PCF50633_TI_SEC,
41eae854b2SBalaji Rao 	PCF50633_TI_MIN,
42eae854b2SBalaji Rao 	PCF50633_TI_HOUR,
43eae854b2SBalaji Rao 	PCF50633_TI_WKDAY,
44eae854b2SBalaji Rao 	PCF50633_TI_DAY,
45eae854b2SBalaji Rao 	PCF50633_TI_MONTH,
46eae854b2SBalaji Rao 	PCF50633_TI_YEAR,
47eae854b2SBalaji Rao 	PCF50633_TI_EXTENT /* always last */
48eae854b2SBalaji Rao };
49eae854b2SBalaji Rao 
50eae854b2SBalaji Rao struct pcf50633_time {
51eae854b2SBalaji Rao 	u_int8_t time[PCF50633_TI_EXTENT];
52eae854b2SBalaji Rao };
53eae854b2SBalaji Rao 
54eae854b2SBalaji Rao struct pcf50633_rtc {
55eae854b2SBalaji Rao 	int alarm_enabled;
56a766ae3eSWerner Almesberger 	int alarm_pending;
57eae854b2SBalaji Rao 
58eae854b2SBalaji Rao 	struct pcf50633 *pcf;
59eae854b2SBalaji Rao 	struct rtc_device *rtc_dev;
60eae854b2SBalaji Rao };
61eae854b2SBalaji Rao 
pcf2rtc_time(struct rtc_time * rtc,struct pcf50633_time * pcf)62eae854b2SBalaji Rao static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
63eae854b2SBalaji Rao {
64eae854b2SBalaji Rao 	rtc->tm_sec = bcd2bin(pcf->time[PCF50633_TI_SEC]);
65eae854b2SBalaji Rao 	rtc->tm_min = bcd2bin(pcf->time[PCF50633_TI_MIN]);
66eae854b2SBalaji Rao 	rtc->tm_hour = bcd2bin(pcf->time[PCF50633_TI_HOUR]);
67eae854b2SBalaji Rao 	rtc->tm_wday = bcd2bin(pcf->time[PCF50633_TI_WKDAY]);
68eae854b2SBalaji Rao 	rtc->tm_mday = bcd2bin(pcf->time[PCF50633_TI_DAY]);
69265f2a05SRask Ingemann Lambertsen 	rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
70eae854b2SBalaji Rao 	rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
71eae854b2SBalaji Rao }
72eae854b2SBalaji Rao 
rtc2pcf_time(struct pcf50633_time * pcf,struct rtc_time * rtc)73eae854b2SBalaji Rao static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
74eae854b2SBalaji Rao {
75eae854b2SBalaji Rao 	pcf->time[PCF50633_TI_SEC] = bin2bcd(rtc->tm_sec);
76eae854b2SBalaji Rao 	pcf->time[PCF50633_TI_MIN] = bin2bcd(rtc->tm_min);
77eae854b2SBalaji Rao 	pcf->time[PCF50633_TI_HOUR] = bin2bcd(rtc->tm_hour);
78eae854b2SBalaji Rao 	pcf->time[PCF50633_TI_WKDAY] = bin2bcd(rtc->tm_wday);
79eae854b2SBalaji Rao 	pcf->time[PCF50633_TI_DAY] = bin2bcd(rtc->tm_mday);
80265f2a05SRask Ingemann Lambertsen 	pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
81eae854b2SBalaji Rao 	pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
82eae854b2SBalaji Rao }
83eae854b2SBalaji Rao 
84eae854b2SBalaji Rao static int
pcf50633_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)85eae854b2SBalaji Rao pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
86eae854b2SBalaji Rao {
87eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
88eae854b2SBalaji Rao 	int err;
89eae854b2SBalaji Rao 
90eae854b2SBalaji Rao 	if (enabled)
91eae854b2SBalaji Rao 		err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
92eae854b2SBalaji Rao 	else
93eae854b2SBalaji Rao 		err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
94eae854b2SBalaji Rao 
95eae854b2SBalaji Rao 	if (err < 0)
96eae854b2SBalaji Rao 		return err;
97eae854b2SBalaji Rao 
98eae854b2SBalaji Rao 	rtc->alarm_enabled = enabled;
99eae854b2SBalaji Rao 
100eae854b2SBalaji Rao 	return 0;
101eae854b2SBalaji Rao }
102eae854b2SBalaji Rao 
pcf50633_rtc_read_time(struct device * dev,struct rtc_time * tm)103eae854b2SBalaji Rao static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
104eae854b2SBalaji Rao {
105eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc;
106eae854b2SBalaji Rao 	struct pcf50633_time pcf_tm;
107eae854b2SBalaji Rao 	int ret;
108eae854b2SBalaji Rao 
109eae854b2SBalaji Rao 	rtc = dev_get_drvdata(dev);
110eae854b2SBalaji Rao 
111eae854b2SBalaji Rao 	ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
112eae854b2SBalaji Rao 					    PCF50633_TI_EXTENT,
113eae854b2SBalaji Rao 					    &pcf_tm.time[0]);
114eae854b2SBalaji Rao 	if (ret != PCF50633_TI_EXTENT) {
115eae854b2SBalaji Rao 		dev_err(dev, "Failed to read time\n");
116eae854b2SBalaji Rao 		return -EIO;
117eae854b2SBalaji Rao 	}
118eae854b2SBalaji Rao 
119eae854b2SBalaji Rao 	dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
120eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_DAY],
121eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_MONTH],
122eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_YEAR],
123eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_HOUR],
124eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_MIN],
125eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_SEC]);
126eae854b2SBalaji Rao 
127eae854b2SBalaji Rao 	pcf2rtc_time(tm, &pcf_tm);
128eae854b2SBalaji Rao 
1298d2e6b22SAndy Shevchenko 	dev_dbg(dev, "RTC_TIME: %ptRr\n", tm);
130eae854b2SBalaji Rao 
13122652ba7SAlexandre Belloni 	return 0;
132eae854b2SBalaji Rao }
133eae854b2SBalaji Rao 
pcf50633_rtc_set_time(struct device * dev,struct rtc_time * tm)134eae854b2SBalaji Rao static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
135eae854b2SBalaji Rao {
136eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc;
137eae854b2SBalaji Rao 	struct pcf50633_time pcf_tm;
1383d62b8f5SLars-Peter Clausen 	int alarm_masked, ret = 0;
139eae854b2SBalaji Rao 
140eae854b2SBalaji Rao 	rtc = dev_get_drvdata(dev);
141eae854b2SBalaji Rao 
1428d2e6b22SAndy Shevchenko 	dev_dbg(dev, "RTC_TIME: %ptRr\n", tm);
143eae854b2SBalaji Rao 
144eae854b2SBalaji Rao 	rtc2pcf_time(&pcf_tm, tm);
145eae854b2SBalaji Rao 
146eae854b2SBalaji Rao 	dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
147eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_DAY],
148eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_MONTH],
149eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_YEAR],
150eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_HOUR],
151eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_MIN],
152eae854b2SBalaji Rao 		pcf_tm.time[PCF50633_TI_SEC]);
153eae854b2SBalaji Rao 
154eae854b2SBalaji Rao 
155eae854b2SBalaji Rao 	alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
156eae854b2SBalaji Rao 
157eae854b2SBalaji Rao 	if (!alarm_masked)
158eae854b2SBalaji Rao 		pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
159eae854b2SBalaji Rao 
160eae854b2SBalaji Rao 	/* Returns 0 on success */
161eae854b2SBalaji Rao 	ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
162eae854b2SBalaji Rao 					     PCF50633_TI_EXTENT,
163eae854b2SBalaji Rao 					     &pcf_tm.time[0]);
164eae854b2SBalaji Rao 
165eae854b2SBalaji Rao 	if (!alarm_masked)
166eae854b2SBalaji Rao 		pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
167eae854b2SBalaji Rao 
168eae854b2SBalaji Rao 	return ret;
169eae854b2SBalaji Rao }
170eae854b2SBalaji Rao 
pcf50633_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)171eae854b2SBalaji Rao static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
172eae854b2SBalaji Rao {
173eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc;
174eae854b2SBalaji Rao 	struct pcf50633_time pcf_tm;
175eae854b2SBalaji Rao 	int ret = 0;
176eae854b2SBalaji Rao 
177eae854b2SBalaji Rao 	rtc = dev_get_drvdata(dev);
178eae854b2SBalaji Rao 
179eae854b2SBalaji Rao 	alrm->enabled = rtc->alarm_enabled;
180a766ae3eSWerner Almesberger 	alrm->pending = rtc->alarm_pending;
181eae854b2SBalaji Rao 
182eae854b2SBalaji Rao 	ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
183eae854b2SBalaji Rao 				PCF50633_TI_EXTENT, &pcf_tm.time[0]);
184eae854b2SBalaji Rao 	if (ret != PCF50633_TI_EXTENT) {
185eae854b2SBalaji Rao 		dev_err(dev, "Failed to read time\n");
186eae854b2SBalaji Rao 		return -EIO;
187eae854b2SBalaji Rao 	}
188eae854b2SBalaji Rao 
189eae854b2SBalaji Rao 	pcf2rtc_time(&alrm->time, &pcf_tm);
190eae854b2SBalaji Rao 
191eae854b2SBalaji Rao 	return rtc_valid_tm(&alrm->time);
192eae854b2SBalaji Rao }
193eae854b2SBalaji Rao 
pcf50633_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)194eae854b2SBalaji Rao static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195eae854b2SBalaji Rao {
196eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc;
197eae854b2SBalaji Rao 	struct pcf50633_time pcf_tm;
198eae854b2SBalaji Rao 	int alarm_masked, ret = 0;
199eae854b2SBalaji Rao 
200eae854b2SBalaji Rao 	rtc = dev_get_drvdata(dev);
201eae854b2SBalaji Rao 
202eae854b2SBalaji Rao 	rtc2pcf_time(&pcf_tm, &alrm->time);
203eae854b2SBalaji Rao 
204eae854b2SBalaji Rao 	/* do like mktime does and ignore tm_wday */
205eae854b2SBalaji Rao 	pcf_tm.time[PCF50633_TI_WKDAY] = 7;
206eae854b2SBalaji Rao 
207eae854b2SBalaji Rao 	alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
208eae854b2SBalaji Rao 
209eae854b2SBalaji Rao 	/* disable alarm interrupt */
210eae854b2SBalaji Rao 	if (!alarm_masked)
211eae854b2SBalaji Rao 		pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
212eae854b2SBalaji Rao 
213eae854b2SBalaji Rao 	/* Returns 0 on success */
214eae854b2SBalaji Rao 	ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
215eae854b2SBalaji Rao 				PCF50633_TI_EXTENT, &pcf_tm.time[0]);
216a766ae3eSWerner Almesberger 	if (!alrm->enabled)
217a766ae3eSWerner Almesberger 		rtc->alarm_pending = 0;
218eae854b2SBalaji Rao 
21905f45d7dSWerner Almesberger 	if (!alarm_masked || alrm->enabled)
220eae854b2SBalaji Rao 		pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
22105f45d7dSWerner Almesberger 	rtc->alarm_enabled = alrm->enabled;
222eae854b2SBalaji Rao 
223eae854b2SBalaji Rao 	return ret;
224eae854b2SBalaji Rao }
225eae854b2SBalaji Rao 
22634c7b3acSJulia Lawall static const struct rtc_class_ops pcf50633_rtc_ops = {
227eae854b2SBalaji Rao 	.read_time		= pcf50633_rtc_read_time,
228eae854b2SBalaji Rao 	.set_time		= pcf50633_rtc_set_time,
229eae854b2SBalaji Rao 	.read_alarm		= pcf50633_rtc_read_alarm,
230eae854b2SBalaji Rao 	.set_alarm		= pcf50633_rtc_set_alarm,
231eae854b2SBalaji Rao 	.alarm_irq_enable	= pcf50633_rtc_alarm_irq_enable,
232eae854b2SBalaji Rao };
233eae854b2SBalaji Rao 
pcf50633_rtc_irq(int irq,void * data)234eae854b2SBalaji Rao static void pcf50633_rtc_irq(int irq, void *data)
235eae854b2SBalaji Rao {
236eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc = data;
237eae854b2SBalaji Rao 
238eae854b2SBalaji Rao 	rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
239a766ae3eSWerner Almesberger 	rtc->alarm_pending = 1;
240eae854b2SBalaji Rao }
241eae854b2SBalaji Rao 
pcf50633_rtc_probe(struct platform_device * pdev)2425a167f45SGreg Kroah-Hartman static int pcf50633_rtc_probe(struct platform_device *pdev)
243eae854b2SBalaji Rao {
244eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc;
245eae854b2SBalaji Rao 
24676753cafSJingoo Han 	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
247eae854b2SBalaji Rao 	if (!rtc)
248eae854b2SBalaji Rao 		return -ENOMEM;
249eae854b2SBalaji Rao 
25068d641efSLars-Peter Clausen 	rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
251eae854b2SBalaji Rao 	platform_set_drvdata(pdev, rtc);
25276753cafSJingoo Han 	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "pcf50633-rtc",
253eae854b2SBalaji Rao 				&pcf50633_rtc_ops, THIS_MODULE);
254eae854b2SBalaji Rao 
25576753cafSJingoo Han 	if (IS_ERR(rtc->rtc_dev))
25676753cafSJingoo Han 		return PTR_ERR(rtc->rtc_dev);
257eae854b2SBalaji Rao 
258eae854b2SBalaji Rao 	pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
259eae854b2SBalaji Rao 					pcf50633_rtc_irq, rtc);
260eae854b2SBalaji Rao 	return 0;
261eae854b2SBalaji Rao }
262eae854b2SBalaji Rao 
pcf50633_rtc_remove(struct platform_device * pdev)263*d25f0ba9SUwe Kleine-König static void pcf50633_rtc_remove(struct platform_device *pdev)
264eae854b2SBalaji Rao {
265eae854b2SBalaji Rao 	struct pcf50633_rtc *rtc;
266eae854b2SBalaji Rao 
267eae854b2SBalaji Rao 	rtc = platform_get_drvdata(pdev);
268eae854b2SBalaji Rao 	pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
269eae854b2SBalaji Rao }
270eae854b2SBalaji Rao 
271eae854b2SBalaji Rao static struct platform_driver pcf50633_rtc_driver = {
272eae854b2SBalaji Rao 	.driver = {
273eae854b2SBalaji Rao 		.name = "pcf50633-rtc",
274eae854b2SBalaji Rao 	},
275eae854b2SBalaji Rao 	.probe = pcf50633_rtc_probe,
276*d25f0ba9SUwe Kleine-König 	.remove_new = pcf50633_rtc_remove,
277eae854b2SBalaji Rao };
278eae854b2SBalaji Rao 
2790c4eae66SAxel Lin module_platform_driver(pcf50633_rtc_driver);
280eae854b2SBalaji Rao 
281eae854b2SBalaji Rao MODULE_DESCRIPTION("PCF50633 RTC driver");
282eae854b2SBalaji Rao MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
283eae854b2SBalaji Rao MODULE_LICENSE("GPL");
284eae854b2SBalaji Rao 
285