xref: /openbmc/linux/drivers/rtc/rtc-stmp3xxx.c (revision 16324165ab1f90fa06294679e3de58cb53c0423f)
1838d2d91SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0+
2df17f631Sdmitry pervushin /*
3df17f631Sdmitry pervushin  * Freescale STMP37XX/STMP378X Real Time Clock driver
4df17f631Sdmitry pervushin  *
5df17f631Sdmitry pervushin  * Copyright (c) 2007 Sigmatel, Inc.
6df17f631Sdmitry pervushin  * Peter Hartley, <peter.hartley@sigmatel.com>
7df17f631Sdmitry pervushin  *
8df17f631Sdmitry pervushin  * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
9df17f631Sdmitry pervushin  * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
107e794cb7SWolfram Sang  * Copyright 2011 Wolfram Sang, Pengutronix e.K.
11df17f631Sdmitry pervushin  */
12df17f631Sdmitry pervushin #include <linux/kernel.h>
13df17f631Sdmitry pervushin #include <linux/module.h>
14b5167159SWolfram Sang #include <linux/io.h>
15df17f631Sdmitry pervushin #include <linux/init.h>
16df17f631Sdmitry pervushin #include <linux/platform_device.h>
17df17f631Sdmitry pervushin #include <linux/interrupt.h>
1828a0c883SLothar Waßmann #include <linux/delay.h>
19df17f631Sdmitry pervushin #include <linux/rtc.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21dd8d20a3SMarek Vasut #include <linux/of_device.h>
22c8a6046eSSachin Kamat #include <linux/of.h>
231a71fb84SWolfram Sang #include <linux/stmp_device.h>
241a71fb84SWolfram Sang #include <linux/stmp3xxx_rtc_wdt.h>
25df17f631Sdmitry pervushin 
2647eac337SWolfram Sang #define STMP3XXX_RTC_CTRL			0x0
2747eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN		0x00000001
2847eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN	0x00000002
2947eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ALARM_IRQ		0x00000004
301a71fb84SWolfram Sang #define STMP3XXX_RTC_CTRL_WATCHDOGEN		0x00000010
3147eac337SWolfram Sang 
3247eac337SWolfram Sang #define STMP3XXX_RTC_STAT			0x10
3347eac337SWolfram Sang #define STMP3XXX_RTC_STAT_STALE_SHIFT		16
3447eac337SWolfram Sang #define STMP3XXX_RTC_STAT_RTC_PRESENT		0x80000000
357f48b21bSUwe Kleine-König #define STMP3XXX_RTC_STAT_XTAL32000_PRESENT	0x10000000
367f48b21bSUwe Kleine-König #define STMP3XXX_RTC_STAT_XTAL32768_PRESENT	0x08000000
3747eac337SWolfram Sang 
3847eac337SWolfram Sang #define STMP3XXX_RTC_SECONDS			0x30
3947eac337SWolfram Sang 
4047eac337SWolfram Sang #define STMP3XXX_RTC_ALARM			0x40
4147eac337SWolfram Sang 
421a71fb84SWolfram Sang #define STMP3XXX_RTC_WATCHDOG			0x50
431a71fb84SWolfram Sang 
4447eac337SWolfram Sang #define STMP3XXX_RTC_PERSISTENT0		0x60
457f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE		(1 << 0)
467f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN		(1 << 1)
477f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN		(1 << 2)
487f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP	(1 << 4)
497f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP	(1 << 5)
507f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ		(1 << 6)
517f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE		(1 << 7)
52df17f631Sdmitry pervushin 
531a71fb84SWolfram Sang #define STMP3XXX_RTC_PERSISTENT1		0x70
541a71fb84SWolfram Sang /* missing bitmask in headers */
551a71fb84SWolfram Sang #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER	0x80000000
561a71fb84SWolfram Sang 
57df17f631Sdmitry pervushin struct stmp3xxx_rtc_data {
58df17f631Sdmitry pervushin 	struct rtc_device *rtc;
59df17f631Sdmitry pervushin 	void __iomem *io;
607e794cb7SWolfram Sang 	int irq_alarm;
61df17f631Sdmitry pervushin };
62df17f631Sdmitry pervushin 
631a71fb84SWolfram Sang #if IS_ENABLED(CONFIG_STMP3XXX_RTC_WATCHDOG)
641a71fb84SWolfram Sang /**
651a71fb84SWolfram Sang  * stmp3xxx_wdt_set_timeout - configure the watchdog inside the STMP3xxx RTC
661a71fb84SWolfram Sang  * @dev: the parent device of the watchdog (= the RTC)
671a71fb84SWolfram Sang  * @timeout: the desired value for the timeout register of the watchdog.
681a71fb84SWolfram Sang  *           0 disables the watchdog
691a71fb84SWolfram Sang  *
701a71fb84SWolfram Sang  * The watchdog needs one register and two bits which are in the RTC domain.
711a71fb84SWolfram Sang  * To handle the resource conflict, the RTC driver will create another
721a71fb84SWolfram Sang  * platform_device for the watchdog driver as a child of the RTC device.
731a71fb84SWolfram Sang  * The watchdog driver is passed the below accessor function via platform_data
741a71fb84SWolfram Sang  * to configure the watchdog. Locking is not needed because accessing SET/CLR
751a71fb84SWolfram Sang  * registers is atomic.
761a71fb84SWolfram Sang  */
771a71fb84SWolfram Sang 
781a71fb84SWolfram Sang static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout)
791a71fb84SWolfram Sang {
801a71fb84SWolfram Sang 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
811a71fb84SWolfram Sang 
821a71fb84SWolfram Sang 	if (timeout) {
831a71fb84SWolfram Sang 		writel(timeout, rtc_data->io + STMP3XXX_RTC_WATCHDOG);
841a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
851a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
861a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
871a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_SET);
881a71fb84SWolfram Sang 	} else {
891a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
901a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
911a71fb84SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
921a71fb84SWolfram Sang 		       rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_CLR);
931a71fb84SWolfram Sang 	}
941a71fb84SWolfram Sang }
951a71fb84SWolfram Sang 
961a71fb84SWolfram Sang static struct stmp3xxx_wdt_pdata wdt_pdata = {
971a71fb84SWolfram Sang 	.wdt_set_timeout = stmp3xxx_wdt_set_timeout,
981a71fb84SWolfram Sang };
991a71fb84SWolfram Sang 
1001a71fb84SWolfram Sang static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
1011a71fb84SWolfram Sang {
1023497610aSSudip Mukherjee 	int rc = -1;
1031a71fb84SWolfram Sang 	struct platform_device *wdt_pdev =
1041a71fb84SWolfram Sang 		platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
1051a71fb84SWolfram Sang 
1061a71fb84SWolfram Sang 	if (wdt_pdev) {
1071a71fb84SWolfram Sang 		wdt_pdev->dev.parent = &rtc_pdev->dev;
1081a71fb84SWolfram Sang 		wdt_pdev->dev.platform_data = &wdt_pdata;
1093497610aSSudip Mukherjee 		rc = platform_device_add(wdt_pdev);
11007590111SLin Yujun 		if (rc)
11107590111SLin Yujun 			platform_device_put(wdt_pdev);
1121a71fb84SWolfram Sang 	}
1133497610aSSudip Mukherjee 
1143497610aSSudip Mukherjee 	if (rc)
1153497610aSSudip Mukherjee 		dev_err(&rtc_pdev->dev,
1163497610aSSudip Mukherjee 			"failed to register stmp3xxx_rtc_wdt\n");
1171a71fb84SWolfram Sang }
1181a71fb84SWolfram Sang #else
1191a71fb84SWolfram Sang static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
1201a71fb84SWolfram Sang {
1211a71fb84SWolfram Sang }
1221a71fb84SWolfram Sang #endif /* CONFIG_STMP3XXX_RTC_WATCHDOG */
1231a71fb84SWolfram Sang 
12428a0c883SLothar Waßmann static int stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
125df17f631Sdmitry pervushin {
12628a0c883SLothar Waßmann 	int timeout = 5000; /* 3ms according to i.MX28 Ref Manual */
127df17f631Sdmitry pervushin 	/*
12828a0c883SLothar Waßmann 	 * The i.MX28 Applications Processor Reference Manual, Rev. 1, 2010
12928a0c883SLothar Waßmann 	 * states:
13028a0c883SLothar Waßmann 	 * | The order in which registers are updated is
13128a0c883SLothar Waßmann 	 * | Persistent 0, 1, 2, 3, 4, 5, Alarm, Seconds.
13228a0c883SLothar Waßmann 	 * | (This list is in bitfield order, from LSB to MSB, as they would
13328a0c883SLothar Waßmann 	 * | appear in the STALE_REGS and NEW_REGS bitfields of the HW_RTC_STAT
13428a0c883SLothar Waßmann 	 * | register. For example, the Seconds register corresponds to
13528a0c883SLothar Waßmann 	 * | STALE_REGS or NEW_REGS containing 0x80.)
136df17f631Sdmitry pervushin 	 */
13728a0c883SLothar Waßmann 	do {
13828a0c883SLothar Waßmann 		if (!(readl(rtc_data->io + STMP3XXX_RTC_STAT) &
13928a0c883SLothar Waßmann 				(0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)))
14028a0c883SLothar Waßmann 			return 0;
14128a0c883SLothar Waßmann 		udelay(1);
14228a0c883SLothar Waßmann 	} while (--timeout > 0);
14328a0c883SLothar Waßmann 	return (readl(rtc_data->io + STMP3XXX_RTC_STAT) &
14428a0c883SLothar Waßmann 		(0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)) ? -ETIME : 0;
145df17f631Sdmitry pervushin }
146df17f631Sdmitry pervushin 
147df17f631Sdmitry pervushin /* Time read/write */
148df17f631Sdmitry pervushin static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
149df17f631Sdmitry pervushin {
15028a0c883SLothar Waßmann 	int ret;
151df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
152df17f631Sdmitry pervushin 
15328a0c883SLothar Waßmann 	ret = stmp3xxx_wait_time(rtc_data);
15428a0c883SLothar Waßmann 	if (ret)
15528a0c883SLothar Waßmann 		return ret;
15628a0c883SLothar Waßmann 
157a659a081SAlexandre Belloni 	rtc_time64_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
158df17f631Sdmitry pervushin 	return 0;
159df17f631Sdmitry pervushin }
160df17f631Sdmitry pervushin 
161622eb9b4SAlexandre Belloni static int stmp3xxx_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
162df17f631Sdmitry pervushin {
163df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
164df17f631Sdmitry pervushin 
165622eb9b4SAlexandre Belloni 	writel(rtc_tm_to_time64(rtc_tm), rtc_data->io + STMP3XXX_RTC_SECONDS);
16628a0c883SLothar Waßmann 	return stmp3xxx_wait_time(rtc_data);
167df17f631Sdmitry pervushin }
168df17f631Sdmitry pervushin 
169df17f631Sdmitry pervushin /* interrupt(s) handler */
170df17f631Sdmitry pervushin static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
171df17f631Sdmitry pervushin {
172df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
1737e794cb7SWolfram Sang 	u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL);
174df17f631Sdmitry pervushin 
17547eac337SWolfram Sang 	if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
176b5167159SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_ALARM_IRQ,
17724417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
1787e794cb7SWolfram Sang 		rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF);
179df17f631Sdmitry pervushin 		return IRQ_HANDLED;
180df17f631Sdmitry pervushin 	}
181df17f631Sdmitry pervushin 
1827e794cb7SWolfram Sang 	return IRQ_NONE;
1837e794cb7SWolfram Sang }
1847e794cb7SWolfram Sang 
185df17f631Sdmitry pervushin static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
186df17f631Sdmitry pervushin {
187df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
188df17f631Sdmitry pervushin 
189df17f631Sdmitry pervushin 	if (enabled) {
190b5167159SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
191b5167159SWolfram Sang 				STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
19224417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
19324417829SHarald Geyer 				STMP_OFFSET_REG_SET);
194b5167159SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
19524417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
196df17f631Sdmitry pervushin 	} else {
197b5167159SWolfram Sang 		writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
198b5167159SWolfram Sang 				STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
19924417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
20024417829SHarald Geyer 				STMP_OFFSET_REG_CLR);
201b5167159SWolfram Sang 		writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
20224417829SHarald Geyer 			rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
203df17f631Sdmitry pervushin 	}
204df17f631Sdmitry pervushin 	return 0;
205df17f631Sdmitry pervushin }
206df17f631Sdmitry pervushin 
207df17f631Sdmitry pervushin static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
208df17f631Sdmitry pervushin {
209df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
210df17f631Sdmitry pervushin 
211a659a081SAlexandre Belloni 	rtc_time64_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
212df17f631Sdmitry pervushin 	return 0;
213df17f631Sdmitry pervushin }
214df17f631Sdmitry pervushin 
215df17f631Sdmitry pervushin static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
216df17f631Sdmitry pervushin {
217df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
218df17f631Sdmitry pervushin 
219a659a081SAlexandre Belloni 	writel(rtc_tm_to_time64(&alm->time), rtc_data->io + STMP3XXX_RTC_ALARM);
2207e794cb7SWolfram Sang 
2217e794cb7SWolfram Sang 	stmp3xxx_alarm_irq_enable(dev, alm->enabled);
2227e794cb7SWolfram Sang 
223df17f631Sdmitry pervushin 	return 0;
224df17f631Sdmitry pervushin }
225df17f631Sdmitry pervushin 
22634c7b3acSJulia Lawall static const struct rtc_class_ops stmp3xxx_rtc_ops = {
227df17f631Sdmitry pervushin 	.alarm_irq_enable =
228df17f631Sdmitry pervushin 			  stmp3xxx_alarm_irq_enable,
229df17f631Sdmitry pervushin 	.read_time	= stmp3xxx_rtc_gettime,
230622eb9b4SAlexandre Belloni 	.set_time	= stmp3xxx_rtc_settime,
231df17f631Sdmitry pervushin 	.read_alarm	= stmp3xxx_rtc_read_alarm,
232df17f631Sdmitry pervushin 	.set_alarm	= stmp3xxx_rtc_set_alarm,
233df17f631Sdmitry pervushin };
234df17f631Sdmitry pervushin 
235*16324165SUwe Kleine-König static void stmp3xxx_rtc_remove(struct platform_device *pdev)
236df17f631Sdmitry pervushin {
237df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
238df17f631Sdmitry pervushin 
239df17f631Sdmitry pervushin 	if (!rtc_data)
240*16324165SUwe Kleine-König 		return;
241df17f631Sdmitry pervushin 
2427e794cb7SWolfram Sang 	writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
24324417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
244df17f631Sdmitry pervushin }
245df17f631Sdmitry pervushin 
246df17f631Sdmitry pervushin static int stmp3xxx_rtc_probe(struct platform_device *pdev)
247df17f631Sdmitry pervushin {
248df17f631Sdmitry pervushin 	struct stmp3xxx_rtc_data *rtc_data;
249df17f631Sdmitry pervushin 	struct resource *r;
2507f48b21bSUwe Kleine-König 	u32 rtc_stat;
2517f48b21bSUwe Kleine-König 	u32 pers0_set, pers0_clr;
2527f48b21bSUwe Kleine-König 	u32 crystalfreq = 0;
253df17f631Sdmitry pervushin 	int err;
254df17f631Sdmitry pervushin 
25587a81420SJingoo Han 	rtc_data = devm_kzalloc(&pdev->dev, sizeof(*rtc_data), GFP_KERNEL);
256df17f631Sdmitry pervushin 	if (!rtc_data)
257df17f631Sdmitry pervushin 		return -ENOMEM;
258df17f631Sdmitry pervushin 
259df17f631Sdmitry pervushin 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
260df17f631Sdmitry pervushin 	if (!r) {
261df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "failed to get resource\n");
26287a81420SJingoo Han 		return -ENXIO;
263df17f631Sdmitry pervushin 	}
264df17f631Sdmitry pervushin 
26587a81420SJingoo Han 	rtc_data->io = devm_ioremap(&pdev->dev, r->start, resource_size(r));
266df17f631Sdmitry pervushin 	if (!rtc_data->io) {
267df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "ioremap failed\n");
26887a81420SJingoo Han 		return -EIO;
269df17f631Sdmitry pervushin 	}
270df17f631Sdmitry pervushin 
271df17f631Sdmitry pervushin 	rtc_data->irq_alarm = platform_get_irq(pdev, 0);
272df17f631Sdmitry pervushin 
2737f48b21bSUwe Kleine-König 	rtc_stat = readl(rtc_data->io + STMP3XXX_RTC_STAT);
2747f48b21bSUwe Kleine-König 	if (!(rtc_stat & STMP3XXX_RTC_STAT_RTC_PRESENT)) {
275df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "no device onboard\n");
27687a81420SJingoo Han 		return -ENODEV;
277df17f631Sdmitry pervushin 	}
278df17f631Sdmitry pervushin 
279a91d2babSWolfram Sang 	platform_set_drvdata(pdev, rtc_data);
280a91d2babSWolfram Sang 
281dff700faSUwe Kleine-König 	/*
282dff700faSUwe Kleine-König 	 * Resetting the rtc stops the watchdog timer that is potentially
283dff700faSUwe Kleine-König 	 * running. So (assuming it is running on purpose) don't reset if the
284dff700faSUwe Kleine-König 	 * watchdog is enabled.
285dff700faSUwe Kleine-König 	 */
286dff700faSUwe Kleine-König 	if (readl(rtc_data->io + STMP3XXX_RTC_CTRL) &
287dff700faSUwe Kleine-König 	    STMP3XXX_RTC_CTRL_WATCHDOGEN) {
288dff700faSUwe Kleine-König 		dev_info(&pdev->dev,
289dff700faSUwe Kleine-König 			 "Watchdog is running, skip resetting rtc\n");
290dff700faSUwe Kleine-König 	} else {
2914e80b188SFabio Estevam 		err = stmp_reset_block(rtc_data->io);
2924e80b188SFabio Estevam 		if (err) {
293dff700faSUwe Kleine-König 			dev_err(&pdev->dev, "stmp_reset_block failed: %d\n",
294dff700faSUwe Kleine-König 				err);
2954e80b188SFabio Estevam 			return err;
2964e80b188SFabio Estevam 		}
297dff700faSUwe Kleine-König 	}
2984e80b188SFabio Estevam 
2997f48b21bSUwe Kleine-König 	/*
3007f48b21bSUwe Kleine-König 	 * Obviously the rtc needs a clock input to be able to run.
3017f48b21bSUwe Kleine-König 	 * This clock can be provided by an external 32k crystal. If that one is
3027f48b21bSUwe Kleine-König 	 * missing XTAL must not be disabled in suspend which consumes a
3037f48b21bSUwe Kleine-König 	 * lot of power. Normally the presence and exact frequency (supported
3047f48b21bSUwe Kleine-König 	 * are 32000 Hz and 32768 Hz) is detectable from fuses, but as reality
3057f48b21bSUwe Kleine-König 	 * proves these fuses are not blown correctly on all machines, so the
3067f48b21bSUwe Kleine-König 	 * frequency can be overridden in the device tree.
3077f48b21bSUwe Kleine-König 	 */
3087f48b21bSUwe Kleine-König 	if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32000_PRESENT)
3097f48b21bSUwe Kleine-König 		crystalfreq = 32000;
3107f48b21bSUwe Kleine-König 	else if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32768_PRESENT)
3117f48b21bSUwe Kleine-König 		crystalfreq = 32768;
3127f48b21bSUwe Kleine-König 
3137f48b21bSUwe Kleine-König 	of_property_read_u32(pdev->dev.of_node, "stmp,crystal-freq",
3147f48b21bSUwe Kleine-König 			     &crystalfreq);
3157f48b21bSUwe Kleine-König 
3167f48b21bSUwe Kleine-König 	switch (crystalfreq) {
3177f48b21bSUwe Kleine-König 	case 32000:
3187f48b21bSUwe Kleine-König 		/* keep 32kHz crystal running in low-power mode */
3197f48b21bSUwe Kleine-König 		pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ |
3207f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3217f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3227f48b21bSUwe Kleine-König 		pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
3237f48b21bSUwe Kleine-König 		break;
3247f48b21bSUwe Kleine-König 	case 32768:
3257f48b21bSUwe Kleine-König 		/* keep 32.768kHz crystal running in low-power mode */
3267f48b21bSUwe Kleine-König 		pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3277f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3287f48b21bSUwe Kleine-König 		pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP |
3297f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ;
3307f48b21bSUwe Kleine-König 		break;
3317f48b21bSUwe Kleine-König 	default:
3327f48b21bSUwe Kleine-König 		dev_warn(&pdev->dev,
3337f48b21bSUwe Kleine-König 			 "invalid crystal-freq specified in device-tree. Assuming no crystal\n");
334df561f66SGustavo A. R. Silva 		fallthrough;
3357f48b21bSUwe Kleine-König 	case 0:
3367f48b21bSUwe Kleine-König 		/* keep XTAL on in low-power mode */
3377f48b21bSUwe Kleine-König 		pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
3387f48b21bSUwe Kleine-König 		pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3397f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3407f48b21bSUwe Kleine-König 	}
3417f48b21bSUwe Kleine-König 
34224417829SHarald Geyer 	writel(pers0_set, rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
34324417829SHarald Geyer 			STMP_OFFSET_REG_SET);
3447f48b21bSUwe Kleine-König 
345b5167159SWolfram Sang 	writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
34647eac337SWolfram Sang 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
3477f48b21bSUwe Kleine-König 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE | pers0_clr,
34824417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
349a91d2babSWolfram Sang 
3507e794cb7SWolfram Sang 	writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
3517e794cb7SWolfram Sang 			STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
35224417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
3537e794cb7SWolfram Sang 
3540d823abdSAlexandre Belloni 	rtc_data->rtc = devm_rtc_allocate_device(&pdev->dev);
355dfd2a178SJingoo Han 	if (IS_ERR(rtc_data->rtc))
356dfd2a178SJingoo Han 		return PTR_ERR(rtc_data->rtc);
357df17f631Sdmitry pervushin 
35887a81420SJingoo Han 	err = devm_request_irq(&pdev->dev, rtc_data->irq_alarm,
35987a81420SJingoo Han 			stmp3xxx_rtc_interrupt, 0, "RTC alarm", &pdev->dev);
360df17f631Sdmitry pervushin 	if (err) {
361df17f631Sdmitry pervushin 		dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
362df17f631Sdmitry pervushin 			rtc_data->irq_alarm);
363dfd2a178SJingoo Han 		return err;
364df17f631Sdmitry pervushin 	}
365df17f631Sdmitry pervushin 
3660d823abdSAlexandre Belloni 	rtc_data->rtc->ops = &stmp3xxx_rtc_ops;
3670d823abdSAlexandre Belloni 	rtc_data->rtc->range_max = U32_MAX;
3680d823abdSAlexandre Belloni 
369fdcfd854SBartosz Golaszewski 	err = devm_rtc_register_device(rtc_data->rtc);
3700d823abdSAlexandre Belloni 	if (err)
3710d823abdSAlexandre Belloni 		return err;
3720d823abdSAlexandre Belloni 
3731a71fb84SWolfram Sang 	stmp3xxx_wdt_register(pdev);
374df17f631Sdmitry pervushin 	return 0;
375df17f631Sdmitry pervushin }
376df17f631Sdmitry pervushin 
377ef69a7f0SJingoo Han #ifdef CONFIG_PM_SLEEP
378ef69a7f0SJingoo Han static int stmp3xxx_rtc_suspend(struct device *dev)
379df17f631Sdmitry pervushin {
380df17f631Sdmitry pervushin 	return 0;
381df17f631Sdmitry pervushin }
382df17f631Sdmitry pervushin 
383ef69a7f0SJingoo Han static int stmp3xxx_rtc_resume(struct device *dev)
384df17f631Sdmitry pervushin {
385ef69a7f0SJingoo Han 	struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
386df17f631Sdmitry pervushin 
38736d1da1dSShawn Guo 	stmp_reset_block(rtc_data->io);
388b5167159SWolfram Sang 	writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
38947eac337SWolfram Sang 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
39047eac337SWolfram Sang 			STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
39124417829SHarald Geyer 		rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
392df17f631Sdmitry pervushin 	return 0;
393df17f631Sdmitry pervushin }
394df17f631Sdmitry pervushin #endif
395df17f631Sdmitry pervushin 
396ef69a7f0SJingoo Han static SIMPLE_DEV_PM_OPS(stmp3xxx_rtc_pm_ops, stmp3xxx_rtc_suspend,
397ef69a7f0SJingoo Han 			stmp3xxx_rtc_resume);
398ef69a7f0SJingoo Han 
399dd8d20a3SMarek Vasut static const struct of_device_id rtc_dt_ids[] = {
400dd8d20a3SMarek Vasut 	{ .compatible = "fsl,stmp3xxx-rtc", },
401dd8d20a3SMarek Vasut 	{ /* sentinel */ }
402dd8d20a3SMarek Vasut };
403dd8d20a3SMarek Vasut MODULE_DEVICE_TABLE(of, rtc_dt_ids);
404dd8d20a3SMarek Vasut 
405df17f631Sdmitry pervushin static struct platform_driver stmp3xxx_rtcdrv = {
406df17f631Sdmitry pervushin 	.probe		= stmp3xxx_rtc_probe,
407*16324165SUwe Kleine-König 	.remove_new	= stmp3xxx_rtc_remove,
408df17f631Sdmitry pervushin 	.driver		= {
409df17f631Sdmitry pervushin 		.name	= "stmp3xxx-rtc",
410ef69a7f0SJingoo Han 		.pm	= &stmp3xxx_rtc_pm_ops,
411462a465bSSachin Kamat 		.of_match_table = rtc_dt_ids,
412df17f631Sdmitry pervushin 	},
413df17f631Sdmitry pervushin };
414df17f631Sdmitry pervushin 
4150c4eae66SAxel Lin module_platform_driver(stmp3xxx_rtcdrv);
416df17f631Sdmitry pervushin 
417df17f631Sdmitry pervushin MODULE_DESCRIPTION("STMP3xxx RTC Driver");
4187e794cb7SWolfram Sang MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
41932271efdSWolfram Sang 		"Wolfram Sang <kernel@pengutronix.de>");
420df17f631Sdmitry pervushin MODULE_LICENSE("GPL");
421