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