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>
21c8a6046eSSachin Kamat #include <linux/of.h>
221a71fb84SWolfram Sang #include <linux/stmp_device.h>
231a71fb84SWolfram Sang #include <linux/stmp3xxx_rtc_wdt.h>
24df17f631Sdmitry pervushin
2547eac337SWolfram Sang #define STMP3XXX_RTC_CTRL 0x0
2647eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001
2747eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002
2847eac337SWolfram Sang #define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004
291a71fb84SWolfram Sang #define STMP3XXX_RTC_CTRL_WATCHDOGEN 0x00000010
3047eac337SWolfram Sang
3147eac337SWolfram Sang #define STMP3XXX_RTC_STAT 0x10
3247eac337SWolfram Sang #define STMP3XXX_RTC_STAT_STALE_SHIFT 16
3347eac337SWolfram Sang #define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000
347f48b21bSUwe Kleine-König #define STMP3XXX_RTC_STAT_XTAL32000_PRESENT 0x10000000
357f48b21bSUwe Kleine-König #define STMP3XXX_RTC_STAT_XTAL32768_PRESENT 0x08000000
3647eac337SWolfram Sang
3747eac337SWolfram Sang #define STMP3XXX_RTC_SECONDS 0x30
3847eac337SWolfram Sang
3947eac337SWolfram Sang #define STMP3XXX_RTC_ALARM 0x40
4047eac337SWolfram Sang
411a71fb84SWolfram Sang #define STMP3XXX_RTC_WATCHDOG 0x50
421a71fb84SWolfram Sang
4347eac337SWolfram Sang #define STMP3XXX_RTC_PERSISTENT0 0x60
447f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE (1 << 0)
457f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN (1 << 1)
467f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN (1 << 2)
477f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP (1 << 4)
487f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP (1 << 5)
497f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ (1 << 6)
507f48b21bSUwe Kleine-König #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE (1 << 7)
51df17f631Sdmitry pervushin
521a71fb84SWolfram Sang #define STMP3XXX_RTC_PERSISTENT1 0x70
531a71fb84SWolfram Sang /* missing bitmask in headers */
541a71fb84SWolfram Sang #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER 0x80000000
551a71fb84SWolfram Sang
56df17f631Sdmitry pervushin struct stmp3xxx_rtc_data {
57df17f631Sdmitry pervushin struct rtc_device *rtc;
58df17f631Sdmitry pervushin void __iomem *io;
597e794cb7SWolfram Sang int irq_alarm;
60df17f631Sdmitry pervushin };
61df17f631Sdmitry pervushin
621a71fb84SWolfram Sang #if IS_ENABLED(CONFIG_STMP3XXX_RTC_WATCHDOG)
631a71fb84SWolfram Sang /**
641a71fb84SWolfram Sang * stmp3xxx_wdt_set_timeout - configure the watchdog inside the STMP3xxx RTC
651a71fb84SWolfram Sang * @dev: the parent device of the watchdog (= the RTC)
661a71fb84SWolfram Sang * @timeout: the desired value for the timeout register of the watchdog.
671a71fb84SWolfram Sang * 0 disables the watchdog
681a71fb84SWolfram Sang *
691a71fb84SWolfram Sang * The watchdog needs one register and two bits which are in the RTC domain.
701a71fb84SWolfram Sang * To handle the resource conflict, the RTC driver will create another
711a71fb84SWolfram Sang * platform_device for the watchdog driver as a child of the RTC device.
721a71fb84SWolfram Sang * The watchdog driver is passed the below accessor function via platform_data
731a71fb84SWolfram Sang * to configure the watchdog. Locking is not needed because accessing SET/CLR
741a71fb84SWolfram Sang * registers is atomic.
751a71fb84SWolfram Sang */
761a71fb84SWolfram Sang
stmp3xxx_wdt_set_timeout(struct device * dev,u32 timeout)771a71fb84SWolfram Sang static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout)
781a71fb84SWolfram Sang {
791a71fb84SWolfram Sang struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
801a71fb84SWolfram Sang
811a71fb84SWolfram Sang if (timeout) {
821a71fb84SWolfram Sang writel(timeout, rtc_data->io + STMP3XXX_RTC_WATCHDOG);
831a71fb84SWolfram Sang writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
841a71fb84SWolfram Sang rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
851a71fb84SWolfram Sang writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
861a71fb84SWolfram Sang rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_SET);
871a71fb84SWolfram Sang } else {
881a71fb84SWolfram Sang writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
891a71fb84SWolfram Sang rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
901a71fb84SWolfram Sang writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
911a71fb84SWolfram Sang rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_CLR);
921a71fb84SWolfram Sang }
931a71fb84SWolfram Sang }
941a71fb84SWolfram Sang
951a71fb84SWolfram Sang static struct stmp3xxx_wdt_pdata wdt_pdata = {
961a71fb84SWolfram Sang .wdt_set_timeout = stmp3xxx_wdt_set_timeout,
971a71fb84SWolfram Sang };
981a71fb84SWolfram Sang
stmp3xxx_wdt_register(struct platform_device * rtc_pdev)991a71fb84SWolfram Sang static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
1001a71fb84SWolfram Sang {
1013497610aSSudip Mukherjee int rc = -1;
1021a71fb84SWolfram Sang struct platform_device *wdt_pdev =
1031a71fb84SWolfram Sang platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
1041a71fb84SWolfram Sang
1051a71fb84SWolfram Sang if (wdt_pdev) {
1061a71fb84SWolfram Sang wdt_pdev->dev.parent = &rtc_pdev->dev;
1071a71fb84SWolfram Sang wdt_pdev->dev.platform_data = &wdt_pdata;
1083497610aSSudip Mukherjee rc = platform_device_add(wdt_pdev);
10907590111SLin Yujun if (rc)
11007590111SLin Yujun platform_device_put(wdt_pdev);
1111a71fb84SWolfram Sang }
1123497610aSSudip Mukherjee
1133497610aSSudip Mukherjee if (rc)
1143497610aSSudip Mukherjee dev_err(&rtc_pdev->dev,
1153497610aSSudip Mukherjee "failed to register stmp3xxx_rtc_wdt\n");
1161a71fb84SWolfram Sang }
1171a71fb84SWolfram Sang #else
stmp3xxx_wdt_register(struct platform_device * rtc_pdev)1181a71fb84SWolfram Sang static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
1191a71fb84SWolfram Sang {
1201a71fb84SWolfram Sang }
1211a71fb84SWolfram Sang #endif /* CONFIG_STMP3XXX_RTC_WATCHDOG */
1221a71fb84SWolfram Sang
stmp3xxx_wait_time(struct stmp3xxx_rtc_data * rtc_data)12328a0c883SLothar Waßmann static int stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
124df17f631Sdmitry pervushin {
12528a0c883SLothar Waßmann int timeout = 5000; /* 3ms according to i.MX28 Ref Manual */
126df17f631Sdmitry pervushin /*
12728a0c883SLothar Waßmann * The i.MX28 Applications Processor Reference Manual, Rev. 1, 2010
12828a0c883SLothar Waßmann * states:
12928a0c883SLothar Waßmann * | The order in which registers are updated is
13028a0c883SLothar Waßmann * | Persistent 0, 1, 2, 3, 4, 5, Alarm, Seconds.
13128a0c883SLothar Waßmann * | (This list is in bitfield order, from LSB to MSB, as they would
13228a0c883SLothar Waßmann * | appear in the STALE_REGS and NEW_REGS bitfields of the HW_RTC_STAT
13328a0c883SLothar Waßmann * | register. For example, the Seconds register corresponds to
13428a0c883SLothar Waßmann * | STALE_REGS or NEW_REGS containing 0x80.)
135df17f631Sdmitry pervushin */
13628a0c883SLothar Waßmann do {
13728a0c883SLothar Waßmann if (!(readl(rtc_data->io + STMP3XXX_RTC_STAT) &
13828a0c883SLothar Waßmann (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)))
13928a0c883SLothar Waßmann return 0;
14028a0c883SLothar Waßmann udelay(1);
14128a0c883SLothar Waßmann } while (--timeout > 0);
14228a0c883SLothar Waßmann return (readl(rtc_data->io + STMP3XXX_RTC_STAT) &
14328a0c883SLothar Waßmann (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)) ? -ETIME : 0;
144df17f631Sdmitry pervushin }
145df17f631Sdmitry pervushin
146df17f631Sdmitry pervushin /* Time read/write */
stmp3xxx_rtc_gettime(struct device * dev,struct rtc_time * rtc_tm)147df17f631Sdmitry pervushin static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
148df17f631Sdmitry pervushin {
14928a0c883SLothar Waßmann int ret;
150df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
151df17f631Sdmitry pervushin
15228a0c883SLothar Waßmann ret = stmp3xxx_wait_time(rtc_data);
15328a0c883SLothar Waßmann if (ret)
15428a0c883SLothar Waßmann return ret;
15528a0c883SLothar Waßmann
156a659a081SAlexandre Belloni rtc_time64_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
157df17f631Sdmitry pervushin return 0;
158df17f631Sdmitry pervushin }
159df17f631Sdmitry pervushin
stmp3xxx_rtc_settime(struct device * dev,struct rtc_time * rtc_tm)160622eb9b4SAlexandre Belloni static int stmp3xxx_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
161df17f631Sdmitry pervushin {
162df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
163df17f631Sdmitry pervushin
164622eb9b4SAlexandre Belloni writel(rtc_tm_to_time64(rtc_tm), rtc_data->io + STMP3XXX_RTC_SECONDS);
16528a0c883SLothar Waßmann return stmp3xxx_wait_time(rtc_data);
166df17f631Sdmitry pervushin }
167df17f631Sdmitry pervushin
168df17f631Sdmitry pervushin /* interrupt(s) handler */
stmp3xxx_rtc_interrupt(int irq,void * dev_id)169df17f631Sdmitry pervushin static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
170df17f631Sdmitry pervushin {
171df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
1727e794cb7SWolfram Sang u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL);
173df17f631Sdmitry pervushin
17447eac337SWolfram Sang if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
175b5167159SWolfram Sang writel(STMP3XXX_RTC_CTRL_ALARM_IRQ,
17624417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
1777e794cb7SWolfram Sang rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF);
178df17f631Sdmitry pervushin return IRQ_HANDLED;
179df17f631Sdmitry pervushin }
180df17f631Sdmitry pervushin
1817e794cb7SWolfram Sang return IRQ_NONE;
1827e794cb7SWolfram Sang }
1837e794cb7SWolfram Sang
stmp3xxx_alarm_irq_enable(struct device * dev,unsigned int enabled)184df17f631Sdmitry pervushin static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
185df17f631Sdmitry pervushin {
186df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
187df17f631Sdmitry pervushin
188df17f631Sdmitry pervushin if (enabled) {
189b5167159SWolfram Sang writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
190b5167159SWolfram Sang STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
19124417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
19224417829SHarald Geyer STMP_OFFSET_REG_SET);
193b5167159SWolfram Sang writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
19424417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
195df17f631Sdmitry pervushin } else {
196b5167159SWolfram Sang writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
197b5167159SWolfram Sang STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
19824417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
19924417829SHarald Geyer STMP_OFFSET_REG_CLR);
200b5167159SWolfram Sang writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
20124417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
202df17f631Sdmitry pervushin }
203df17f631Sdmitry pervushin return 0;
204df17f631Sdmitry pervushin }
205df17f631Sdmitry pervushin
stmp3xxx_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alm)206df17f631Sdmitry pervushin static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
207df17f631Sdmitry pervushin {
208df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
209df17f631Sdmitry pervushin
210a659a081SAlexandre Belloni rtc_time64_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
211df17f631Sdmitry pervushin return 0;
212df17f631Sdmitry pervushin }
213df17f631Sdmitry pervushin
stmp3xxx_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alm)214df17f631Sdmitry pervushin static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
215df17f631Sdmitry pervushin {
216df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
217df17f631Sdmitry pervushin
218a659a081SAlexandre Belloni writel(rtc_tm_to_time64(&alm->time), rtc_data->io + STMP3XXX_RTC_ALARM);
2197e794cb7SWolfram Sang
2207e794cb7SWolfram Sang stmp3xxx_alarm_irq_enable(dev, alm->enabled);
2217e794cb7SWolfram Sang
222df17f631Sdmitry pervushin return 0;
223df17f631Sdmitry pervushin }
224df17f631Sdmitry pervushin
22534c7b3acSJulia Lawall static const struct rtc_class_ops stmp3xxx_rtc_ops = {
226df17f631Sdmitry pervushin .alarm_irq_enable =
227df17f631Sdmitry pervushin stmp3xxx_alarm_irq_enable,
228df17f631Sdmitry pervushin .read_time = stmp3xxx_rtc_gettime,
229622eb9b4SAlexandre Belloni .set_time = stmp3xxx_rtc_settime,
230df17f631Sdmitry pervushin .read_alarm = stmp3xxx_rtc_read_alarm,
231df17f631Sdmitry pervushin .set_alarm = stmp3xxx_rtc_set_alarm,
232df17f631Sdmitry pervushin };
233df17f631Sdmitry pervushin
stmp3xxx_rtc_remove(struct platform_device * pdev)234*16324165SUwe Kleine-König static void stmp3xxx_rtc_remove(struct platform_device *pdev)
235df17f631Sdmitry pervushin {
236df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
237df17f631Sdmitry pervushin
238df17f631Sdmitry pervushin if (!rtc_data)
239*16324165SUwe Kleine-König return;
240df17f631Sdmitry pervushin
2417e794cb7SWolfram Sang writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
24224417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
243df17f631Sdmitry pervushin }
244df17f631Sdmitry pervushin
stmp3xxx_rtc_probe(struct platform_device * pdev)245df17f631Sdmitry pervushin static int stmp3xxx_rtc_probe(struct platform_device *pdev)
246df17f631Sdmitry pervushin {
247df17f631Sdmitry pervushin struct stmp3xxx_rtc_data *rtc_data;
248df17f631Sdmitry pervushin struct resource *r;
2497f48b21bSUwe Kleine-König u32 rtc_stat;
2507f48b21bSUwe Kleine-König u32 pers0_set, pers0_clr;
2517f48b21bSUwe Kleine-König u32 crystalfreq = 0;
252df17f631Sdmitry pervushin int err;
253df17f631Sdmitry pervushin
25487a81420SJingoo Han rtc_data = devm_kzalloc(&pdev->dev, sizeof(*rtc_data), GFP_KERNEL);
255df17f631Sdmitry pervushin if (!rtc_data)
256df17f631Sdmitry pervushin return -ENOMEM;
257df17f631Sdmitry pervushin
258df17f631Sdmitry pervushin r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
259df17f631Sdmitry pervushin if (!r) {
260df17f631Sdmitry pervushin dev_err(&pdev->dev, "failed to get resource\n");
26187a81420SJingoo Han return -ENXIO;
262df17f631Sdmitry pervushin }
263df17f631Sdmitry pervushin
26487a81420SJingoo Han rtc_data->io = devm_ioremap(&pdev->dev, r->start, resource_size(r));
265df17f631Sdmitry pervushin if (!rtc_data->io) {
266df17f631Sdmitry pervushin dev_err(&pdev->dev, "ioremap failed\n");
26787a81420SJingoo Han return -EIO;
268df17f631Sdmitry pervushin }
269df17f631Sdmitry pervushin
270df17f631Sdmitry pervushin rtc_data->irq_alarm = platform_get_irq(pdev, 0);
271df17f631Sdmitry pervushin
2727f48b21bSUwe Kleine-König rtc_stat = readl(rtc_data->io + STMP3XXX_RTC_STAT);
2737f48b21bSUwe Kleine-König if (!(rtc_stat & STMP3XXX_RTC_STAT_RTC_PRESENT)) {
274df17f631Sdmitry pervushin dev_err(&pdev->dev, "no device onboard\n");
27587a81420SJingoo Han return -ENODEV;
276df17f631Sdmitry pervushin }
277df17f631Sdmitry pervushin
278a91d2babSWolfram Sang platform_set_drvdata(pdev, rtc_data);
279a91d2babSWolfram Sang
280dff700faSUwe Kleine-König /*
281dff700faSUwe Kleine-König * Resetting the rtc stops the watchdog timer that is potentially
282dff700faSUwe Kleine-König * running. So (assuming it is running on purpose) don't reset if the
283dff700faSUwe Kleine-König * watchdog is enabled.
284dff700faSUwe Kleine-König */
285dff700faSUwe Kleine-König if (readl(rtc_data->io + STMP3XXX_RTC_CTRL) &
286dff700faSUwe Kleine-König STMP3XXX_RTC_CTRL_WATCHDOGEN) {
287dff700faSUwe Kleine-König dev_info(&pdev->dev,
288dff700faSUwe Kleine-König "Watchdog is running, skip resetting rtc\n");
289dff700faSUwe Kleine-König } else {
2904e80b188SFabio Estevam err = stmp_reset_block(rtc_data->io);
2914e80b188SFabio Estevam if (err) {
292dff700faSUwe Kleine-König dev_err(&pdev->dev, "stmp_reset_block failed: %d\n",
293dff700faSUwe Kleine-König err);
2944e80b188SFabio Estevam return err;
2954e80b188SFabio Estevam }
296dff700faSUwe Kleine-König }
2974e80b188SFabio Estevam
2987f48b21bSUwe Kleine-König /*
2997f48b21bSUwe Kleine-König * Obviously the rtc needs a clock input to be able to run.
3007f48b21bSUwe Kleine-König * This clock can be provided by an external 32k crystal. If that one is
3017f48b21bSUwe Kleine-König * missing XTAL must not be disabled in suspend which consumes a
3027f48b21bSUwe Kleine-König * lot of power. Normally the presence and exact frequency (supported
3037f48b21bSUwe Kleine-König * are 32000 Hz and 32768 Hz) is detectable from fuses, but as reality
3047f48b21bSUwe Kleine-König * proves these fuses are not blown correctly on all machines, so the
3057f48b21bSUwe Kleine-König * frequency can be overridden in the device tree.
3067f48b21bSUwe Kleine-König */
3077f48b21bSUwe Kleine-König if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32000_PRESENT)
3087f48b21bSUwe Kleine-König crystalfreq = 32000;
3097f48b21bSUwe Kleine-König else if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32768_PRESENT)
3107f48b21bSUwe Kleine-König crystalfreq = 32768;
3117f48b21bSUwe Kleine-König
3127f48b21bSUwe Kleine-König of_property_read_u32(pdev->dev.of_node, "stmp,crystal-freq",
3137f48b21bSUwe Kleine-König &crystalfreq);
3147f48b21bSUwe Kleine-König
3157f48b21bSUwe Kleine-König switch (crystalfreq) {
3167f48b21bSUwe Kleine-König case 32000:
3177f48b21bSUwe Kleine-König /* keep 32kHz crystal running in low-power mode */
3187f48b21bSUwe Kleine-König pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ |
3197f48b21bSUwe Kleine-König 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 break;
3237f48b21bSUwe Kleine-König case 32768:
3247f48b21bSUwe Kleine-König /* keep 32.768kHz crystal running in low-power mode */
3257f48b21bSUwe Kleine-König pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3267f48b21bSUwe Kleine-König STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3277f48b21bSUwe Kleine-König pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP |
3287f48b21bSUwe Kleine-König STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ;
3297f48b21bSUwe Kleine-König break;
3307f48b21bSUwe Kleine-König default:
3317f48b21bSUwe Kleine-König dev_warn(&pdev->dev,
3327f48b21bSUwe Kleine-König "invalid crystal-freq specified in device-tree. Assuming no crystal\n");
333df561f66SGustavo A. R. Silva fallthrough;
3347f48b21bSUwe Kleine-König case 0:
3357f48b21bSUwe Kleine-König /* keep XTAL on in low-power mode */
3367f48b21bSUwe Kleine-König pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
3377f48b21bSUwe Kleine-König pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
3387f48b21bSUwe Kleine-König STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
3397f48b21bSUwe Kleine-König }
3407f48b21bSUwe Kleine-König
34124417829SHarald Geyer writel(pers0_set, rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
34224417829SHarald Geyer STMP_OFFSET_REG_SET);
3437f48b21bSUwe Kleine-König
344b5167159SWolfram Sang writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
34547eac337SWolfram Sang STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
3467f48b21bSUwe Kleine-König STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE | pers0_clr,
34724417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
348a91d2babSWolfram Sang
3497e794cb7SWolfram Sang writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
3507e794cb7SWolfram Sang STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
35124417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
3527e794cb7SWolfram Sang
3530d823abdSAlexandre Belloni rtc_data->rtc = devm_rtc_allocate_device(&pdev->dev);
354dfd2a178SJingoo Han if (IS_ERR(rtc_data->rtc))
355dfd2a178SJingoo Han return PTR_ERR(rtc_data->rtc);
356df17f631Sdmitry pervushin
35787a81420SJingoo Han err = devm_request_irq(&pdev->dev, rtc_data->irq_alarm,
35887a81420SJingoo Han stmp3xxx_rtc_interrupt, 0, "RTC alarm", &pdev->dev);
359df17f631Sdmitry pervushin if (err) {
360df17f631Sdmitry pervushin dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
361df17f631Sdmitry pervushin rtc_data->irq_alarm);
362dfd2a178SJingoo Han return err;
363df17f631Sdmitry pervushin }
364df17f631Sdmitry pervushin
3650d823abdSAlexandre Belloni rtc_data->rtc->ops = &stmp3xxx_rtc_ops;
3660d823abdSAlexandre Belloni rtc_data->rtc->range_max = U32_MAX;
3670d823abdSAlexandre Belloni
368fdcfd854SBartosz Golaszewski err = devm_rtc_register_device(rtc_data->rtc);
3690d823abdSAlexandre Belloni if (err)
3700d823abdSAlexandre Belloni return err;
3710d823abdSAlexandre Belloni
3721a71fb84SWolfram Sang stmp3xxx_wdt_register(pdev);
373df17f631Sdmitry pervushin return 0;
374df17f631Sdmitry pervushin }
375df17f631Sdmitry pervushin
376ef69a7f0SJingoo Han #ifdef CONFIG_PM_SLEEP
stmp3xxx_rtc_suspend(struct device * dev)377ef69a7f0SJingoo Han static int stmp3xxx_rtc_suspend(struct device *dev)
378df17f631Sdmitry pervushin {
379df17f631Sdmitry pervushin return 0;
380df17f631Sdmitry pervushin }
381df17f631Sdmitry pervushin
stmp3xxx_rtc_resume(struct device * dev)382ef69a7f0SJingoo Han static int stmp3xxx_rtc_resume(struct device *dev)
383df17f631Sdmitry pervushin {
384ef69a7f0SJingoo Han struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
385df17f631Sdmitry pervushin
38636d1da1dSShawn Guo stmp_reset_block(rtc_data->io);
387b5167159SWolfram Sang writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
38847eac337SWolfram Sang STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
38947eac337SWolfram Sang STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
39024417829SHarald Geyer rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
391df17f631Sdmitry pervushin return 0;
392df17f631Sdmitry pervushin }
393df17f631Sdmitry pervushin #endif
394df17f631Sdmitry pervushin
395ef69a7f0SJingoo Han static SIMPLE_DEV_PM_OPS(stmp3xxx_rtc_pm_ops, stmp3xxx_rtc_suspend,
396ef69a7f0SJingoo Han stmp3xxx_rtc_resume);
397ef69a7f0SJingoo Han
398dd8d20a3SMarek Vasut static const struct of_device_id rtc_dt_ids[] = {
399dd8d20a3SMarek Vasut { .compatible = "fsl,stmp3xxx-rtc", },
400dd8d20a3SMarek Vasut { /* sentinel */ }
401dd8d20a3SMarek Vasut };
402dd8d20a3SMarek Vasut MODULE_DEVICE_TABLE(of, rtc_dt_ids);
403dd8d20a3SMarek Vasut
404df17f631Sdmitry pervushin static struct platform_driver stmp3xxx_rtcdrv = {
405df17f631Sdmitry pervushin .probe = stmp3xxx_rtc_probe,
406*16324165SUwe Kleine-König .remove_new = stmp3xxx_rtc_remove,
407df17f631Sdmitry pervushin .driver = {
408df17f631Sdmitry pervushin .name = "stmp3xxx-rtc",
409ef69a7f0SJingoo Han .pm = &stmp3xxx_rtc_pm_ops,
410462a465bSSachin Kamat .of_match_table = rtc_dt_ids,
411df17f631Sdmitry pervushin },
412df17f631Sdmitry pervushin };
413df17f631Sdmitry pervushin
4140c4eae66SAxel Lin module_platform_driver(stmp3xxx_rtcdrv);
415df17f631Sdmitry pervushin
416df17f631Sdmitry pervushin MODULE_DESCRIPTION("STMP3xxx RTC Driver");
4177e794cb7SWolfram Sang MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
41832271efdSWolfram Sang "Wolfram Sang <kernel@pengutronix.de>");
419df17f631Sdmitry pervushin MODULE_LICENSE("GPL");
420