1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * rtc-st-lpc.c - ST's LPC RTC, powered by the Low Power Timer
5 * Copyright (C) 2014 STMicroelectronics Limited
7 * Author: David Paris <david.paris@st.com> for STMicroelectronics
8 * Lee Jones <lee.jones@linaro.org> for STMicroelectronics
10 * Based on the original driver written by Stuart Menefy.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 #include <linux/rtc.h>
25 #include <dt-bindings/mfd/st-lpc.h>
28 #define LPC_LPT_LSB_OFF 0x400
29 #define LPC_LPT_MSB_OFF 0x404
30 #define LPC_LPT_START_OFF 0x408
33 #define LPC_LPA_LSB_OFF 0x410
34 #define LPC_LPA_MSB_OFF 0x414
35 #define LPC_LPA_START_OFF 0x418
38 #define LPC_WDT_OFF 0x510
39 #define LPC_WDT_FLAG_OFF 0x514
42 struct rtc_device *rtc_dev;
43 struct rtc_wkalrm alarm;
45 unsigned long clkrate;
52 static void st_rtc_set_hw_alarm(struct st_rtc *rtc,
53 unsigned long msb, unsigned long lsb)
57 spin_lock_irqsave(&rtc->lock, flags);
59 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
61 writel_relaxed(msb, rtc->ioaddr + LPC_LPA_MSB_OFF);
62 writel_relaxed(lsb, rtc->ioaddr + LPC_LPA_LSB_OFF);
63 writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
65 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
67 spin_unlock_irqrestore(&rtc->lock, flags);
70 static irqreturn_t st_rtc_handler(int this_irq, void *data)
72 struct st_rtc *rtc = (struct st_rtc *)data;
74 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF);
79 static int st_rtc_read_time(struct device *dev, struct rtc_time *tm)
81 struct st_rtc *rtc = dev_get_drvdata(dev);
82 unsigned long lpt_lsb, lpt_msb;
83 unsigned long long lpt;
86 spin_lock_irqsave(&rtc->lock, flags);
89 lpt_msb = readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF);
90 lpt_lsb = readl_relaxed(rtc->ioaddr + LPC_LPT_LSB_OFF);
91 } while (readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF) != lpt_msb);
93 spin_unlock_irqrestore(&rtc->lock, flags);
95 lpt = ((unsigned long long)lpt_msb << 32) | lpt_lsb;
96 do_div(lpt, rtc->clkrate);
97 rtc_time64_to_tm(lpt, tm);
102 static int st_rtc_set_time(struct device *dev, struct rtc_time *tm)
104 struct st_rtc *rtc = dev_get_drvdata(dev);
105 unsigned long long lpt, secs;
108 secs = rtc_tm_to_time64(tm);
110 lpt = (unsigned long long)secs * rtc->clkrate;
112 spin_lock_irqsave(&rtc->lock, flags);
114 writel_relaxed(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF);
115 writel_relaxed(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF);
116 writel_relaxed(1, rtc->ioaddr + LPC_LPT_START_OFF);
118 spin_unlock_irqrestore(&rtc->lock, flags);
123 static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
125 struct st_rtc *rtc = dev_get_drvdata(dev);
128 spin_lock_irqsave(&rtc->lock, flags);
130 memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm));
132 spin_unlock_irqrestore(&rtc->lock, flags);
137 static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
139 struct st_rtc *rtc = dev_get_drvdata(dev);
141 if (enabled && !rtc->irq_enabled) {
142 enable_irq(rtc->irq);
143 rtc->irq_enabled = true;
144 } else if (!enabled && rtc->irq_enabled) {
145 disable_irq(rtc->irq);
146 rtc->irq_enabled = false;
152 static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
154 struct st_rtc *rtc = dev_get_drvdata(dev);
156 unsigned long long now_secs;
157 unsigned long long alarm_secs;
158 unsigned long long lpa;
160 st_rtc_read_time(dev, &now);
161 now_secs = rtc_tm_to_time64(&now);
162 alarm_secs = rtc_tm_to_time64(&t->time);
164 memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm));
166 /* Now many secs to fire */
167 alarm_secs -= now_secs;
168 lpa = (unsigned long long)alarm_secs * rtc->clkrate;
170 st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa);
171 st_rtc_alarm_irq_enable(dev, t->enabled);
176 static const struct rtc_class_ops st_rtc_ops = {
177 .read_time = st_rtc_read_time,
178 .set_time = st_rtc_set_time,
179 .read_alarm = st_rtc_read_alarm,
180 .set_alarm = st_rtc_set_alarm,
181 .alarm_irq_enable = st_rtc_alarm_irq_enable,
184 static int st_rtc_probe(struct platform_device *pdev)
186 struct device_node *np = pdev->dev.of_node;
191 ret = of_property_read_u32(np, "st,lpc-mode", &mode);
193 dev_err(&pdev->dev, "An LPC mode must be provided\n");
197 /* LPC can either run as a Clocksource or in RTC or WDT mode */
198 if (mode != ST_LPC_MODE_RTC)
201 rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL);
205 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
206 if (IS_ERR(rtc->rtc_dev))
207 return PTR_ERR(rtc->rtc_dev);
209 spin_lock_init(&rtc->lock);
211 rtc->ioaddr = devm_platform_ioremap_resource(pdev, 0);
212 if (IS_ERR(rtc->ioaddr))
213 return PTR_ERR(rtc->ioaddr);
215 rtc->irq = irq_of_parse_and_map(np, 0);
217 dev_err(&pdev->dev, "IRQ missing or invalid\n");
221 ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0,
224 dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq);
228 enable_irq_wake(rtc->irq);
229 disable_irq(rtc->irq);
231 rtc->clk = devm_clk_get_enabled(&pdev->dev, NULL);
232 if (IS_ERR(rtc->clk))
233 return dev_err_probe(&pdev->dev, PTR_ERR(rtc->clk),
234 "Unable to request clock\n");
236 rtc->clkrate = clk_get_rate(rtc->clk);
238 dev_err(&pdev->dev, "Unable to fetch clock rate\n");
242 device_set_wakeup_capable(&pdev->dev, 1);
244 platform_set_drvdata(pdev, rtc);
246 rtc->rtc_dev->ops = &st_rtc_ops;
247 rtc->rtc_dev->range_max = U64_MAX;
248 do_div(rtc->rtc_dev->range_max, rtc->clkrate);
250 ret = devm_rtc_register_device(rtc->rtc_dev);
257 #ifdef CONFIG_PM_SLEEP
258 static int st_rtc_suspend(struct device *dev)
260 struct st_rtc *rtc = dev_get_drvdata(dev);
262 if (device_may_wakeup(dev))
265 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
266 writel_relaxed(0, rtc->ioaddr + LPC_LPA_START_OFF);
267 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
272 static int st_rtc_resume(struct device *dev)
274 struct st_rtc *rtc = dev_get_drvdata(dev);
276 rtc_alarm_irq_enable(rtc->rtc_dev, 0);
279 * clean 'rtc->alarm' to allow a new
280 * .set_alarm to the upper RTC layer
282 memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm));
284 writel_relaxed(0, rtc->ioaddr + LPC_LPA_MSB_OFF);
285 writel_relaxed(0, rtc->ioaddr + LPC_LPA_LSB_OFF);
286 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
287 writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
288 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
294 static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume);
296 static const struct of_device_id st_rtc_match[] = {
297 { .compatible = "st,stih407-lpc" },
300 MODULE_DEVICE_TABLE(of, st_rtc_match);
302 static struct platform_driver st_rtc_platform_driver = {
304 .name = "st-lpc-rtc",
305 .pm = &st_rtc_pm_ops,
306 .of_match_table = st_rtc_match,
308 .probe = st_rtc_probe,
311 module_platform_driver(st_rtc_platform_driver);
313 MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver");
314 MODULE_AUTHOR("David Paris <david.paris@st.com>");
315 MODULE_LICENSE("GPL");