Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / rtc / rtc-st-lpc.c
blob5fe021821831d95f93088ee156dd1c7b84f9b0fb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of.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>
27 /* Low Power Timer */
28 #define LPC_LPT_LSB_OFF 0x400
29 #define LPC_LPT_MSB_OFF 0x404
30 #define LPC_LPT_START_OFF 0x408
32 /* Low Power Alarm */
33 #define LPC_LPA_LSB_OFF 0x410
34 #define LPC_LPA_MSB_OFF 0x414
35 #define LPC_LPA_START_OFF 0x418
37 /* LPC as WDT */
38 #define LPC_WDT_OFF 0x510
39 #define LPC_WDT_FLAG_OFF 0x514
41 struct st_rtc {
42 struct rtc_device *rtc_dev;
43 struct rtc_wkalrm alarm;
44 struct resource *res;
45 struct clk *clk;
46 unsigned long clkrate;
47 void __iomem *ioaddr;
48 bool irq_enabled:1;
49 spinlock_t lock;
50 short irq;
53 static void st_rtc_set_hw_alarm(struct st_rtc *rtc,
54 unsigned long msb, unsigned long lsb)
56 unsigned long flags;
58 spin_lock_irqsave(&rtc->lock, flags);
60 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
62 writel_relaxed(msb, rtc->ioaddr + LPC_LPA_MSB_OFF);
63 writel_relaxed(lsb, rtc->ioaddr + LPC_LPA_LSB_OFF);
64 writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
66 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
68 spin_unlock_irqrestore(&rtc->lock, flags);
71 static irqreturn_t st_rtc_handler(int this_irq, void *data)
73 struct st_rtc *rtc = (struct st_rtc *)data;
75 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF);
77 return IRQ_HANDLED;
80 static int st_rtc_read_time(struct device *dev, struct rtc_time *tm)
82 struct st_rtc *rtc = dev_get_drvdata(dev);
83 unsigned long lpt_lsb, lpt_msb;
84 unsigned long long lpt;
85 unsigned long flags;
87 spin_lock_irqsave(&rtc->lock, flags);
89 do {
90 lpt_msb = readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF);
91 lpt_lsb = readl_relaxed(rtc->ioaddr + LPC_LPT_LSB_OFF);
92 } while (readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF) != lpt_msb);
94 spin_unlock_irqrestore(&rtc->lock, flags);
96 lpt = ((unsigned long long)lpt_msb << 32) | lpt_lsb;
97 do_div(lpt, rtc->clkrate);
98 rtc_time64_to_tm(lpt, tm);
100 return 0;
103 static int st_rtc_set_time(struct device *dev, struct rtc_time *tm)
105 struct st_rtc *rtc = dev_get_drvdata(dev);
106 unsigned long long lpt, secs;
107 unsigned long flags;
109 secs = rtc_tm_to_time64(tm);
111 lpt = (unsigned long long)secs * rtc->clkrate;
113 spin_lock_irqsave(&rtc->lock, flags);
115 writel_relaxed(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF);
116 writel_relaxed(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF);
117 writel_relaxed(1, rtc->ioaddr + LPC_LPT_START_OFF);
119 spin_unlock_irqrestore(&rtc->lock, flags);
121 return 0;
124 static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
126 struct st_rtc *rtc = dev_get_drvdata(dev);
127 unsigned long flags;
129 spin_lock_irqsave(&rtc->lock, flags);
131 memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm));
133 spin_unlock_irqrestore(&rtc->lock, flags);
135 return 0;
138 static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
140 struct st_rtc *rtc = dev_get_drvdata(dev);
142 if (enabled && !rtc->irq_enabled) {
143 enable_irq(rtc->irq);
144 rtc->irq_enabled = true;
145 } else if (!enabled && rtc->irq_enabled) {
146 disable_irq(rtc->irq);
147 rtc->irq_enabled = false;
150 return 0;
153 static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
155 struct st_rtc *rtc = dev_get_drvdata(dev);
156 struct rtc_time now;
157 unsigned long long now_secs;
158 unsigned long long alarm_secs;
159 unsigned long long lpa;
161 st_rtc_read_time(dev, &now);
162 now_secs = rtc_tm_to_time64(&now);
163 alarm_secs = rtc_tm_to_time64(&t->time);
165 /* Invalid alarm time */
166 if (now_secs > alarm_secs)
167 return -EINVAL;
169 memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm));
171 /* Now many secs to fire */
172 alarm_secs -= now_secs;
173 lpa = (unsigned long long)alarm_secs * rtc->clkrate;
175 st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa);
176 st_rtc_alarm_irq_enable(dev, t->enabled);
178 return 0;
181 static struct rtc_class_ops st_rtc_ops = {
182 .read_time = st_rtc_read_time,
183 .set_time = st_rtc_set_time,
184 .read_alarm = st_rtc_read_alarm,
185 .set_alarm = st_rtc_set_alarm,
186 .alarm_irq_enable = st_rtc_alarm_irq_enable,
189 static int st_rtc_probe(struct platform_device *pdev)
191 struct device_node *np = pdev->dev.of_node;
192 struct st_rtc *rtc;
193 struct resource *res;
194 uint32_t mode;
195 int ret = 0;
197 ret = of_property_read_u32(np, "st,lpc-mode", &mode);
198 if (ret) {
199 dev_err(&pdev->dev, "An LPC mode must be provided\n");
200 return -EINVAL;
203 /* LPC can either run as a Clocksource or in RTC or WDT mode */
204 if (mode != ST_LPC_MODE_RTC)
205 return -ENODEV;
207 rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL);
208 if (!rtc)
209 return -ENOMEM;
211 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
212 if (IS_ERR(rtc->rtc_dev))
213 return PTR_ERR(rtc->rtc_dev);
215 spin_lock_init(&rtc->lock);
217 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218 rtc->ioaddr = devm_ioremap_resource(&pdev->dev, res);
219 if (IS_ERR(rtc->ioaddr))
220 return PTR_ERR(rtc->ioaddr);
222 rtc->irq = irq_of_parse_and_map(np, 0);
223 if (!rtc->irq) {
224 dev_err(&pdev->dev, "IRQ missing or invalid\n");
225 return -EINVAL;
228 ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0,
229 pdev->name, rtc);
230 if (ret) {
231 dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq);
232 return ret;
235 enable_irq_wake(rtc->irq);
236 disable_irq(rtc->irq);
238 rtc->clk = clk_get(&pdev->dev, NULL);
239 if (IS_ERR(rtc->clk)) {
240 dev_err(&pdev->dev, "Unable to request clock\n");
241 return PTR_ERR(rtc->clk);
244 clk_prepare_enable(rtc->clk);
246 rtc->clkrate = clk_get_rate(rtc->clk);
247 if (!rtc->clkrate) {
248 dev_err(&pdev->dev, "Unable to fetch clock rate\n");
249 return -EINVAL;
252 device_set_wakeup_capable(&pdev->dev, 1);
254 platform_set_drvdata(pdev, rtc);
256 rtc->rtc_dev->ops = &st_rtc_ops;
257 rtc->rtc_dev->range_max = U64_MAX;
258 do_div(rtc->rtc_dev->range_max, rtc->clkrate);
260 ret = rtc_register_device(rtc->rtc_dev);
261 if (ret) {
262 clk_disable_unprepare(rtc->clk);
263 return ret;
266 return 0;
269 #ifdef CONFIG_PM_SLEEP
270 static int st_rtc_suspend(struct device *dev)
272 struct st_rtc *rtc = dev_get_drvdata(dev);
274 if (device_may_wakeup(dev))
275 return 0;
277 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
278 writel_relaxed(0, rtc->ioaddr + LPC_LPA_START_OFF);
279 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
281 return 0;
284 static int st_rtc_resume(struct device *dev)
286 struct st_rtc *rtc = dev_get_drvdata(dev);
288 rtc_alarm_irq_enable(rtc->rtc_dev, 0);
291 * clean 'rtc->alarm' to allow a new
292 * .set_alarm to the upper RTC layer
294 memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm));
296 writel_relaxed(0, rtc->ioaddr + LPC_LPA_MSB_OFF);
297 writel_relaxed(0, rtc->ioaddr + LPC_LPA_LSB_OFF);
298 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF);
299 writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF);
300 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF);
302 return 0;
304 #endif
306 static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume);
308 static const struct of_device_id st_rtc_match[] = {
309 { .compatible = "st,stih407-lpc" },
312 MODULE_DEVICE_TABLE(of, st_rtc_match);
314 static struct platform_driver st_rtc_platform_driver = {
315 .driver = {
316 .name = "st-lpc-rtc",
317 .pm = &st_rtc_pm_ops,
318 .of_match_table = st_rtc_match,
320 .probe = st_rtc_probe,
323 module_platform_driver(st_rtc_platform_driver);
325 MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver");
326 MODULE_AUTHOR("David Paris <david.paris@st.com>");
327 MODULE_LICENSE("GPL");