xref: /openbmc/linux/drivers/rtc/rtc-stmp3xxx.c (revision 24417829936d82b03b156e0d036c3b8f25aa93fd)
1df17f631Sdmitry pervushin /*
2df17f631Sdmitry pervushin  * Freescale STMP37XX/STMP378X Real Time Clock driver
3df17f631Sdmitry pervushin  *
4df17f631Sdmitry pervushin  * Copyright (c) 2007 Sigmatel, Inc.
5df17f631Sdmitry pervushin  * Peter Hartley, <peter.hartley@sigmatel.com>
6df17f631Sdmitry pervushin  *
7df17f631Sdmitry pervushin  * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
8df17f631Sdmitry pervushin  * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
97e794cb7SWolfram Sang  * Copyright 2011 Wolfram Sang, Pengutronix e.K.
10df17f631Sdmitry pervushin  */
11df17f631Sdmitry pervushin 
12df17f631Sdmitry pervushin /*
13df17f631Sdmitry pervushin  * The code contained herein is licensed under the GNU General Public
14df17f631Sdmitry pervushin  * License. You may obtain a copy of the GNU General Public License
15df17f631Sdmitry pervushin  * Version 2 or later at the following locations:
16df17f631Sdmitry pervushin  *
17df17f631Sdmitry pervushin  * http://www.opensource.org/licenses/gpl-license.html
18df17f631Sdmitry pervushin  * http://www.gnu.org/copyleft/gpl.html
19df17f631Sdmitry pervushin  */
20df17f631Sdmitry pervushin #include <linux/kernel.h>
21df17f631Sdmitry pervushin #include <linux/module.h>
22b5167159SWolfram Sang #include <linux/io.h>
23df17f631Sdmitry pervushin #include <linux/init.h>
24df17f631Sdmitry pervushin #include <linux/platform_device.h>
25df17f631Sdmitry pervushin #include <linux/interrupt.h>
2628a0c883SLothar Waßmann #include <linux/delay.h>
27df17f631Sdmitry pervushin #include <linux/rtc.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
29dd8d20a3SMarek Vasut #include <linux/of_device.h>
30c8a6046eSSachin Kamat #include <linux/of.h>
311a71fb84SWolfram Sang #include <linux/stmp_device.h>
321a71fb84SWolfram Sang #include <linux/stmp3xxx_rtc_wdt.h>
33df17f631Sdmitry pervushin 
3447eac337SWolfram Sang #define STMP3XXX_RTC_CTRL			0x0
3547eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN		0x00000001
3647eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN	0x00000002
3747eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ALARM_IRQ		0x00000004
381a71fb84SWolfram Sang #define STMP3XXX_RTC_CTRL_WATCHDOGEN		0x00000010
3947eac337SWolfram Sang 
4047eac337SWolfram Sang #define STMP3XXX_RTC_STAT			0x10
4147eac337SWolfram Sang #define STMP3XXX_RTC_STAT_STALE_SHIFT		16
4247eac337SWolfram Sang #define STMP3XXX_RTC_STAT_RTC_PRESENT		0x80000000
437f48b21bSUwe Kleine-König #define STMP3XXX_RTC_STAT_XTAL32000_PRESENT	0x10000000
447f48b21bSUwe Kleine-König #define STMP3XXX_RTC_STAT_XTAL32768_PRESENT	0x08000000
4547eac337SWolfram Sang 
4647eac337SWolfram Sang #define STMP3XXX_RTC_SECONDS			0x30
4747eac337SWolfram Sang 
4847eac337SWolfram Sang #define STMP3XXX_RTC_ALARM			0x40
4947eac337SWolfram Sang 
501a71fb84SWolfram Sang #define STMP3XXX_RTC_WATCHDOG			0x50
511a71fb84SWolfram Sang 
5247eac337SWolfram Sang #define STMP3XXX_RTC_PERSISTENT0		0x60
537f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE		(1 << 0)
547f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN		(1 << 1)
557f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN		(1 << 2)
567f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP	(1 << 4)
577f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP	(1 << 5)
587f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ		(1 << 6)
597f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE		(1 << 7)
60df17f631Sdmitry pervushin 
611a71fb84SWolfram Sang #define STMP3XXX_RTC_PERSISTENT1		0x70
621a71fb84SWolfram Sang /* missing bitmask in headers */
631a71fb84SWolfram Sang #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER	0x80000000
641a71fb84SWolfram Sang 
65df17f631Sdmitry pervushin struct stmp3xxx_rtc_data {
66df17f631Sdmitry pervushin 	struct rtc_device *rtc;
67df17f631Sdmitry pervushin 	void __iomem *io;
687e794cb7SWolfram Sang 	int irq_alarm;
69df17f631Sdmitry pervushin };
70df17f631Sdmitry pervushin 
711a71fb84SWolfram Sang #if IS_ENABLED(CONFIG_STMP3XXX_RTC_WATCHDOG)
721a71fb84SWolfram Sang /**
731a71fb84SWolfram Sang  * stmp3xxx_wdt_set_timeout - configure the watchdog inside the STMP3xxx RTC
741a71fb84SWolfram Sang  * @dev: the parent device of the watchdog (= the RTC)
751a71fb84SWolfram Sang  * @timeout: the desired value for the timeout register of the watchdog.
761a71fb84SWolfram Sang  *           0 disables the watchdog
771a71fb84SWolfram Sang  *
781a71fb84SWolfram Sang  * The watchdog needs one register and two bits which are in the RTC domain.
791a71fb84SWolfram Sang  * To handle the resource conflict, the RTC driver will create another
801a71fb84SWolfram Sang  * platform_device for the watchdog driver as a child of the RTC device.
811a71fb84SWolfram Sang  * The watchdog driver is passed the below accessor function via platform_data
821a71fb84SWolfram Sang  * to configure the watchdog. Locking is not needed because accessing SET/CLR
831a71fb84SWolfram Sang  * registers is atomic.
841a71fb84SWolfram Sang  */
851a71fb84SWolfram Sang 
861a71fb84SWolfram Sang static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout)
871a71fb84SWolfram Sang {
881a71fb84SWolfram Sang 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
891a71fb84SWolfram Sang 
901a71fb84SWolfram Sang 	if (timeout) {
911a71fb84SWolfram Sang 		writel(timeout, rtc_data->io + STMP3XXX_RTC_WATCHDOG);
921a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
931a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
941a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
951a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_SET);
961a71fb84SWolfram Sang 	} else {
971a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
981a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
991a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
1001a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_CLR);
1011a71fb84SWolfram Sang 	}
1021a71fb84SWolfram Sang }
1031a71fb84SWolfram Sang 
1041a71fb84SWolfram Sang static struct stmp3xxx_wdt_pdata wdt_pdata = {
1051a71fb84SWolfram Sang 	.wdt_set_timeout = stmp3xxx_wdt_set_timeout,
1061a71fb84SWolfram Sang };
1071a71fb84SWolfram Sang 
1081a71fb84SWolfram Sang static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
1091a71fb84SWolfram Sang {
1101a71fb84SWolfram Sang 	struct platform_device *wdt_pdev =
1111a71fb84SWolfram Sang 		platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
1121a71fb84SWolfram Sang 
1131a71fb84SWolfram Sang 	if (wdt_pdev) {
1141a71fb84SWolfram Sang 		wdt_pdev->dev.parent = &rtc_pdev->dev;
1151a71fb84SWolfram Sang 		wdt_pdev->dev.platform_data = &wdt_pdata;
1161a71fb84SWolfram Sang 		platform_device_add(wdt_pdev);
1171a71fb84SWolfram Sang 	}
1181a71fb84SWolfram Sang }
1191a71fb84SWolfram Sang #else
1201a71fb84SWolfram Sang static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
1211a71fb84SWolfram Sang {
1221a71fb84SWolfram Sang }
1231a71fb84SWolfram Sang #endif /* CONFIG_STMP3XXX_RTC_WATCHDOG */
1241a71fb84SWolfram Sang 
12528a0c883SLothar Waßmann static int stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
126df17f631Sdmitry pervushin {
12728a0c883SLothar Waßmann 	int timeout = 5000; /* 3ms according to i.MX28 Ref Manual */
128df17f631Sdmitry pervushin 	/*
12928a0c883SLothar Waßmann 	 * The i.MX28 Applications Processor Reference Manual, Rev. 1, 2010
13028a0c883SLothar Waßmann 	 * states:
13128a0c883SLothar Waßmann 	 * | The order in which registers are updated is
13228a0c883SLothar Waßmann 	 * | Persistent 0, 1, 2, 3, 4, 5, Alarm, Seconds.
13328a0c883SLothar Waßmann 	 * | (This list is in bitfield order, from LSB to MSB, as they would
13428a0c883SLothar Waßmann 	 * | appear in the STALE_REGS and NEW_REGS bitfields of the HW_RTC_STAT
13528a0c883SLothar Waßmann 	 * | register. For example, the Seconds register corresponds to
13628a0c883SLothar Waßmann 	 * | STALE_REGS or NEW_REGS containing 0x80.)
137df17f631Sdmitry pervushin 	 */
13828a0c883SLothar Waßmann 	do {
13928a0c883SLothar Waßmann 		if (!(readl(rtc_data->io + STMP3XXX_RTC_STAT) &
14028a0c883SLothar Waßmann 				(0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)))
14128a0c883SLothar Waßmann 			return 0;
14228a0c883SLothar Waßmann 		udelay(1);
14328a0c883SLothar Waßmann 	} while (--timeout > 0);
14428a0c883SLothar Waßmann 	return (readl(rtc_data->io + STMP3XXX_RTC_STAT) &
14528a0c883SLothar Waßmann 		(0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)) ? -ETIME : 0;
146df17f631Sdmitry pervushin }
147df17f631Sdmitry pervushin 
148df17f631Sdmitry pervushin /* Time read/write */
149df17f631Sdmitry pervushin static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
150df17f631Sdmitry pervushin {
15128a0c883SLothar Waßmann 	int ret;
152df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
153df17f631Sdmitry pervushin 
15428a0c883SLothar Waßmann 	ret = stmp3xxx_wait_time(rtc_data);
15528a0c883SLothar Waßmann 	if (ret)
15628a0c883SLothar Waßmann 		return ret;
15728a0c883SLothar Waßmann 
158b5167159SWolfram Sang 	rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
159df17f631Sdmitry pervushin 	return 0;
160df17f631Sdmitry pervushin }
161df17f631Sdmitry pervushin 
162df17f631Sdmitry pervushin static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
163df17f631Sdmitry pervushin {
164df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
165df17f631Sdmitry pervushin 
166b5167159SWolfram Sang 	writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
16728a0c883SLothar Waßmann 	return stmp3xxx_wait_time(rtc_data);
168df17f631Sdmitry pervushin }
169df17f631Sdmitry pervushin 
170df17f631Sdmitry pervushin /* interrupt(s) handler */
171df17f631Sdmitry pervushin static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
172df17f631Sdmitry pervushin {
173df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
1747e794cb7SWolfram Sang 	u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL);
175df17f631Sdmitry pervushin 
17647eac337SWolfram Sang 	if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
177b5167159SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_ALARM_IRQ,
178*24417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
1797e794cb7SWolfram Sang 		rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF);
180df17f631Sdmitry pervushin 		return IRQ_HANDLED;
181df17f631Sdmitry pervushin 	}
182df17f631Sdmitry pervushin 
1837e794cb7SWolfram Sang 	return IRQ_NONE;
1847e794cb7SWolfram Sang }
1857e794cb7SWolfram Sang 
186df17f631Sdmitry pervushin static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
187df17f631Sdmitry pervushin {
188df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
189df17f631Sdmitry pervushin 
190df17f631Sdmitry pervushin 	if (enabled) {
191b5167159SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
192b5167159SWolfram Sang 				STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
193*24417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
194*24417829SHarald Geyer 				STMP_OFFSET_REG_SET);
195b5167159SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
196*24417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
197df17f631Sdmitry pervushin 	} else {
198b5167159SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
199b5167159SWolfram Sang 				STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
200*24417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
201*24417829SHarald Geyer 				STMP_OFFSET_REG_CLR);
202b5167159SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
203*24417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
204df17f631Sdmitry pervushin 	}
205df17f631Sdmitry pervushin 	return 0;
206df17f631Sdmitry pervushin }
207df17f631Sdmitry pervushin 
208df17f631Sdmitry pervushin static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
209df17f631Sdmitry pervushin {
210df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
211df17f631Sdmitry pervushin 
212b5167159SWolfram Sang 	rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
213df17f631Sdmitry pervushin 	return 0;
214df17f631Sdmitry pervushin }
215df17f631Sdmitry pervushin 
216df17f631Sdmitry pervushin static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
217df17f631Sdmitry pervushin {
218df17f631Sdmitry pervushin 	unsigned long t;
219df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
220df17f631Sdmitry pervushin 
221df17f631Sdmitry pervushin 	rtc_tm_to_time(&alm->time, &t);
222b5167159SWolfram Sang 	writel(t, rtc_data->io + STMP3XXX_RTC_ALARM);
2237e794cb7SWolfram Sang 
2247e794cb7SWolfram Sang 	stmp3xxx_alarm_irq_enable(dev, alm->enabled);
2257e794cb7SWolfram Sang 
226df17f631Sdmitry pervushin 	return 0;
227df17f631Sdmitry pervushin }
228df17f631Sdmitry pervushin 
229df17f631Sdmitry pervushin static struct rtc_class_ops stmp3xxx_rtc_ops = {
230df17f631Sdmitry pervushin 	.alarm_irq_enable =
231df17f631Sdmitry pervushin 			  stmp3xxx_alarm_irq_enable,
232df17f631Sdmitry pervushin 	.read_time	= stmp3xxx_rtc_gettime,
233df17f631Sdmitry pervushin 	.set_mmss	= stmp3xxx_rtc_set_mmss,
234df17f631Sdmitry pervushin 	.read_alarm	= stmp3xxx_rtc_read_alarm,
235df17f631Sdmitry pervushin 	.set_alarm	= stmp3xxx_rtc_set_alarm,
236df17f631Sdmitry pervushin };
237df17f631Sdmitry pervushin 
238df17f631Sdmitry pervushin static int stmp3xxx_rtc_remove(struct platform_device *pdev)
239df17f631Sdmitry pervushin {
240df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
241df17f631Sdmitry pervushin 
242df17f631Sdmitry pervushin 	if (!rtc_data)
243df17f631Sdmitry pervushin 		return 0;
244df17f631Sdmitry pervushin 
2457e794cb7SWolfram Sang 	writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
246*24417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
247df17f631Sdmitry pervushin 
248df17f631Sdmitry pervushin 	return 0;
249df17f631Sdmitry pervushin }
250df17f631Sdmitry pervushin 
251df17f631Sdmitry pervushin static int stmp3xxx_rtc_probe(struct platform_device *pdev)
252df17f631Sdmitry pervushin {
253df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data;
254df17f631Sdmitry pervushin 	struct resource *r;
2557f48b21bSUwe Kleine-König 	u32 rtc_stat;
2567f48b21bSUwe Kleine-König 	u32 pers0_set, pers0_clr;
2577f48b21bSUwe Kleine-König 	u32 crystalfreq = 0;
258df17f631Sdmitry pervushin 	int err;
259df17f631Sdmitry pervushin 
26087a81420SJingoo Han 	rtc_data = devm_kzalloc(&pdev->dev, sizeof(*rtc_data), GFP_KERNEL);
261df17f631Sdmitry pervushin 	if (!rtc_data)
262df17f631Sdmitry pervushin 		return -ENOMEM;
263df17f631Sdmitry pervushin 
264df17f631Sdmitry pervushin 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265df17f631Sdmitry pervushin 	if (!r) {
266df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "failed to get resource\n");
26787a81420SJingoo Han 		return -ENXIO;
268df17f631Sdmitry pervushin 	}
269df17f631Sdmitry pervushin 
27087a81420SJingoo Han 	rtc_data->io = devm_ioremap(&pdev->dev, r->start, resource_size(r));
271df17f631Sdmitry pervushin 	if (!rtc_data->io) {
272df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "ioremap failed\n");
27387a81420SJingoo Han 		return -EIO;
274df17f631Sdmitry pervushin 	}
275df17f631Sdmitry pervushin 
276df17f631Sdmitry pervushin 	rtc_data->irq_alarm = platform_get_irq(pdev, 0);
277df17f631Sdmitry pervushin 
2787f48b21bSUwe Kleine-König 	rtc_stat = readl(rtc_data->io + STMP3XXX_RTC_STAT);
2797f48b21bSUwe Kleine-König 	if (!(rtc_stat & STMP3XXX_RTC_STAT_RTC_PRESENT)) {
280df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "no device onboard\n");
28187a81420SJingoo Han 		return -ENODEV;
282df17f631Sdmitry pervushin 	}
283df17f631Sdmitry pervushin 
284a91d2babSWolfram Sang 	platform_set_drvdata(pdev, rtc_data);
285a91d2babSWolfram Sang 
2864e80b188SFabio Estevam 	err = stmp_reset_block(rtc_data->io);
2874e80b188SFabio Estevam 	if (err) {
2884e80b188SFabio Estevam 		dev_err(&pdev->dev, "stmp_reset_block failed: %d\n", err);
2894e80b188SFabio Estevam 		return err;
2904e80b188SFabio Estevam 	}
2914e80b188SFabio Estevam 
2927f48b21bSUwe Kleine-König 	/*
2937f48b21bSUwe Kleine-König 	 * Obviously the rtc needs a clock input to be able to run.
2947f48b21bSUwe Kleine-König 	 * This clock can be provided by an external 32k crystal. If that one is
2957f48b21bSUwe Kleine-König 	 * missing XTAL must not be disabled in suspend which consumes a
2967f48b21bSUwe Kleine-König 	 * lot of power. Normally the presence and exact frequency (supported
2977f48b21bSUwe Kleine-König 	 * are 32000 Hz and 32768 Hz) is detectable from fuses, but as reality
2987f48b21bSUwe Kleine-König 	 * proves these fuses are not blown correctly on all machines, so the
2997f48b21bSUwe Kleine-König 	 * frequency can be overridden in the device tree.
3007f48b21bSUwe Kleine-König 	 */
3017f48b21bSUwe Kleine-König 	if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32000_PRESENT)
3027f48b21bSUwe Kleine-König 		crystalfreq = 32000;
3037f48b21bSUwe Kleine-König 	else if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32768_PRESENT)
3047f48b21bSUwe Kleine-König 		crystalfreq = 32768;
3057f48b21bSUwe Kleine-König 
3067f48b21bSUwe Kleine-König 	of_property_read_u32(pdev->dev.of_node, "stmp,crystal-freq",
3077f48b21bSUwe Kleine-König 			     &crystalfreq);
3087f48b21bSUwe Kleine-König 
3097f48b21bSUwe Kleine-König 	switch (crystalfreq) {
3107f48b21bSUwe Kleine-König 	case 32000:
3117f48b21bSUwe Kleine-König 		/* keep 32kHz crystal running in low-power mode */
3127f48b21bSUwe Kleine-König 		pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ |
3137f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3147f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3157f48b21bSUwe Kleine-König 		pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
3167f48b21bSUwe Kleine-König 		break;
3177f48b21bSUwe Kleine-König 	case 32768:
3187f48b21bSUwe Kleine-König 		/* keep 32.768kHz crystal running in low-power mode */
3197f48b21bSUwe Kleine-König 		pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3207f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3217f48b21bSUwe Kleine-König 		pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP |
3227f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ;
3237f48b21bSUwe Kleine-König 		break;
3247f48b21bSUwe Kleine-König 	default:
3257f48b21bSUwe Kleine-König 		dev_warn(&pdev->dev,
3267f48b21bSUwe Kleine-König 			 "invalid crystal-freq specified in device-tree. Assuming no crystal\n");
3277f48b21bSUwe Kleine-König 		/* fall-through */
3287f48b21bSUwe Kleine-König 	case 0:
3297f48b21bSUwe Kleine-König 		/* keep XTAL on in low-power mode */
3307f48b21bSUwe Kleine-König 		pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
3317f48b21bSUwe Kleine-König 		pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3327f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3337f48b21bSUwe Kleine-König 	}
3347f48b21bSUwe Kleine-König 
335*24417829SHarald Geyer 	writel(pers0_set, rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
336*24417829SHarald Geyer 			STMP_OFFSET_REG_SET);
3377f48b21bSUwe Kleine-König 
338b5167159SWolfram Sang 	writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
33947eac337SWolfram Sang 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
3407f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE | pers0_clr,
341*24417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
342a91d2babSWolfram Sang 
3437e794cb7SWolfram Sang 	writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
3447e794cb7SWolfram Sang 			STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
345*24417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
3467e794cb7SWolfram Sang 
34787a81420SJingoo Han 	rtc_data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
348df17f631Sdmitry pervushin 				&stmp3xxx_rtc_ops, THIS_MODULE);
349dfd2a178SJingoo Han 	if (IS_ERR(rtc_data->rtc))
350dfd2a178SJingoo Han 		return PTR_ERR(rtc_data->rtc);
351df17f631Sdmitry pervushin 
35287a81420SJingoo Han 	err = devm_request_irq(&pdev->dev, rtc_data->irq_alarm,
35387a81420SJingoo Han 			stmp3xxx_rtc_interrupt, 0, "RTC alarm", &pdev->dev);
354df17f631Sdmitry pervushin 	if (err) {
355df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
356df17f631Sdmitry pervushin 			rtc_data->irq_alarm);
357dfd2a178SJingoo Han 		return err;
358df17f631Sdmitry pervushin 	}
359df17f631Sdmitry pervushin 
3601a71fb84SWolfram Sang 	stmp3xxx_wdt_register(pdev);
361df17f631Sdmitry pervushin 	return 0;
362df17f631Sdmitry pervushin }
363df17f631Sdmitry pervushin 
364ef69a7f0SJingoo Han #ifdef CONFIG_PM_SLEEP
365ef69a7f0SJingoo Han static int stmp3xxx_rtc_suspend(struct device *dev)
366df17f631Sdmitry pervushin {
367df17f631Sdmitry pervushin 	return 0;
368df17f631Sdmitry pervushin }
369df17f631Sdmitry pervushin 
370ef69a7f0SJingoo Han static int stmp3xxx_rtc_resume(struct device *dev)
371df17f631Sdmitry pervushin {
372ef69a7f0SJingoo Han 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
373df17f631Sdmitry pervushin 
37436d1da1dSShawn Guo 	stmp_reset_block(rtc_data->io);
375b5167159SWolfram Sang 	writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
37647eac337SWolfram Sang 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
37747eac337SWolfram Sang 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
378*24417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
379df17f631Sdmitry pervushin 	return 0;
380df17f631Sdmitry pervushin }
381df17f631Sdmitry pervushin #endif
382df17f631Sdmitry pervushin 
383ef69a7f0SJingoo Han static SIMPLE_DEV_PM_OPS(stmp3xxx_rtc_pm_ops, stmp3xxx_rtc_suspend,
384ef69a7f0SJingoo Han 			stmp3xxx_rtc_resume);
385ef69a7f0SJingoo Han 
386dd8d20a3SMarek Vasut static const struct of_device_id rtc_dt_ids[] = {
387dd8d20a3SMarek Vasut 	{ .compatible = "fsl,stmp3xxx-rtc", },
388dd8d20a3SMarek Vasut 	{ /* sentinel */ }
389dd8d20a3SMarek Vasut };
390dd8d20a3SMarek Vasut MODULE_DEVICE_TABLE(of, rtc_dt_ids);
391dd8d20a3SMarek Vasut 
392df17f631Sdmitry pervushin static struct platform_driver stmp3xxx_rtcdrv = {
393df17f631Sdmitry pervushin 	.probe		= stmp3xxx_rtc_probe,
394df17f631Sdmitry pervushin 	.remove		= stmp3xxx_rtc_remove,
395df17f631Sdmitry pervushin 	.driver		= {
396df17f631Sdmitry pervushin 		.name	= "stmp3xxx-rtc",
397ef69a7f0SJingoo Han 		.pm	= &stmp3xxx_rtc_pm_ops,
398462a465bSSachin Kamat 		.of_match_table = rtc_dt_ids,
399df17f631Sdmitry pervushin 	},
400df17f631Sdmitry pervushin };
401df17f631Sdmitry pervushin 
4020c4eae66SAxel Lin module_platform_driver(stmp3xxx_rtcdrv);
403df17f631Sdmitry pervushin 
404df17f631Sdmitry pervushin MODULE_DESCRIPTION("STMP3xxx RTC Driver");
4057e794cb7SWolfram Sang MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
4067e794cb7SWolfram Sang 		"Wolfram Sang <w.sang@pengutronix.de>");
407df17f631Sdmitry pervushin MODULE_LICENSE("GPL");
408