WIP FPC-III support
[linux/fpc-iii.git] / drivers / rtc / rtc-jz4740.c
blob6e51df72fd658147f5e3b033ea1c331962492809
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
4 * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
5 * JZ4740 SoC RTC driver
6 */
8 #include <linux/clk.h>
9 #include <linux/io.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_wakeirq.h>
15 #include <linux/reboot.h>
16 #include <linux/rtc.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #define JZ_REG_RTC_CTRL 0x00
21 #define JZ_REG_RTC_SEC 0x04
22 #define JZ_REG_RTC_SEC_ALARM 0x08
23 #define JZ_REG_RTC_REGULATOR 0x0C
24 #define JZ_REG_RTC_HIBERNATE 0x20
25 #define JZ_REG_RTC_WAKEUP_FILTER 0x24
26 #define JZ_REG_RTC_RESET_COUNTER 0x28
27 #define JZ_REG_RTC_SCRATCHPAD 0x34
29 /* The following are present on the jz4780 */
30 #define JZ_REG_RTC_WENR 0x3C
31 #define JZ_RTC_WENR_WEN BIT(31)
33 #define JZ_RTC_CTRL_WRDY BIT(7)
34 #define JZ_RTC_CTRL_1HZ BIT(6)
35 #define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
36 #define JZ_RTC_CTRL_AF BIT(4)
37 #define JZ_RTC_CTRL_AF_IRQ BIT(3)
38 #define JZ_RTC_CTRL_AE BIT(2)
39 #define JZ_RTC_CTRL_ENABLE BIT(0)
41 /* Magic value to enable writes on jz4780 */
42 #define JZ_RTC_WENR_MAGIC 0xA55A
44 #define JZ_RTC_WAKEUP_FILTER_MASK 0x0000FFE0
45 #define JZ_RTC_RESET_COUNTER_MASK 0x00000FE0
47 enum jz4740_rtc_type {
48 ID_JZ4740,
49 ID_JZ4760,
50 ID_JZ4780,
53 struct jz4740_rtc {
54 void __iomem *base;
55 enum jz4740_rtc_type type;
57 struct rtc_device *rtc;
59 spinlock_t lock;
62 static struct device *dev_for_power_off;
64 static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
66 return readl(rtc->base + reg);
69 static int jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
71 uint32_t ctrl;
72 int timeout = 10000;
74 do {
75 ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
76 } while (!(ctrl & JZ_RTC_CTRL_WRDY) && --timeout);
78 return timeout ? 0 : -EIO;
81 static inline int jz4780_rtc_enable_write(struct jz4740_rtc *rtc)
83 uint32_t ctrl;
84 int ret, timeout = 10000;
86 ret = jz4740_rtc_wait_write_ready(rtc);
87 if (ret != 0)
88 return ret;
90 writel(JZ_RTC_WENR_MAGIC, rtc->base + JZ_REG_RTC_WENR);
92 do {
93 ctrl = readl(rtc->base + JZ_REG_RTC_WENR);
94 } while (!(ctrl & JZ_RTC_WENR_WEN) && --timeout);
96 return timeout ? 0 : -EIO;
99 static inline int jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
100 uint32_t val)
102 int ret = 0;
104 if (rtc->type >= ID_JZ4760)
105 ret = jz4780_rtc_enable_write(rtc);
106 if (ret == 0)
107 ret = jz4740_rtc_wait_write_ready(rtc);
108 if (ret == 0)
109 writel(val, rtc->base + reg);
111 return ret;
114 static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
115 bool set)
117 int ret;
118 unsigned long flags;
119 uint32_t ctrl;
121 spin_lock_irqsave(&rtc->lock, flags);
123 ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
125 /* Don't clear interrupt flags by accident */
126 ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
128 if (set)
129 ctrl |= mask;
130 else
131 ctrl &= ~mask;
133 ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
135 spin_unlock_irqrestore(&rtc->lock, flags);
137 return ret;
140 static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
142 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
143 uint32_t secs, secs2;
144 int timeout = 5;
146 if (jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD) != 0x12345678)
147 return -EINVAL;
149 /* If the seconds register is read while it is updated, it can contain a
150 * bogus value. This can be avoided by making sure that two consecutive
151 * reads have the same value.
153 secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
154 secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
156 while (secs != secs2 && --timeout) {
157 secs = secs2;
158 secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
161 if (timeout == 0)
162 return -EIO;
164 rtc_time64_to_tm(secs, time);
166 return 0;
169 static int jz4740_rtc_set_time(struct device *dev, struct rtc_time *time)
171 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
172 int ret;
174 ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, rtc_tm_to_time64(time));
175 if (ret)
176 return ret;
178 return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
181 static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
183 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
184 uint32_t secs;
185 uint32_t ctrl;
187 secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
189 ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
191 alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
192 alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
194 rtc_time64_to_tm(secs, &alrm->time);
196 return 0;
199 static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
201 int ret;
202 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
203 uint32_t secs = lower_32_bits(rtc_tm_to_time64(&alrm->time));
205 ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs);
206 if (!ret)
207 ret = jz4740_rtc_ctrl_set_bits(rtc,
208 JZ_RTC_CTRL_AE | JZ_RTC_CTRL_AF_IRQ, alrm->enabled);
210 return ret;
213 static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
215 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
216 return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ, enable);
219 static const struct rtc_class_ops jz4740_rtc_ops = {
220 .read_time = jz4740_rtc_read_time,
221 .set_time = jz4740_rtc_set_time,
222 .read_alarm = jz4740_rtc_read_alarm,
223 .set_alarm = jz4740_rtc_set_alarm,
224 .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
227 static irqreturn_t jz4740_rtc_irq(int irq, void *data)
229 struct jz4740_rtc *rtc = data;
230 uint32_t ctrl;
231 unsigned long events = 0;
233 ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
235 if (ctrl & JZ_RTC_CTRL_1HZ)
236 events |= (RTC_UF | RTC_IRQF);
238 if (ctrl & JZ_RTC_CTRL_AF)
239 events |= (RTC_AF | RTC_IRQF);
241 rtc_update_irq(rtc->rtc, 1, events);
243 jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, false);
245 return IRQ_HANDLED;
248 static void jz4740_rtc_poweroff(struct device *dev)
250 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
251 jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
254 static void jz4740_rtc_power_off(void)
256 jz4740_rtc_poweroff(dev_for_power_off);
257 kernel_halt();
260 static void jz4740_rtc_clk_disable(void *data)
262 clk_disable_unprepare(data);
265 static const struct of_device_id jz4740_rtc_of_match[] = {
266 { .compatible = "ingenic,jz4740-rtc", .data = (void *)ID_JZ4740 },
267 { .compatible = "ingenic,jz4760-rtc", .data = (void *)ID_JZ4760 },
268 { .compatible = "ingenic,jz4780-rtc", .data = (void *)ID_JZ4780 },
271 MODULE_DEVICE_TABLE(of, jz4740_rtc_of_match);
273 static void jz4740_rtc_set_wakeup_params(struct jz4740_rtc *rtc,
274 struct device_node *np,
275 unsigned long rate)
277 unsigned long wakeup_ticks, reset_ticks;
278 unsigned int min_wakeup_pin_assert_time = 60; /* Default: 60ms */
279 unsigned int reset_pin_assert_time = 100; /* Default: 100ms */
281 of_property_read_u32(np, "ingenic,reset-pin-assert-time-ms",
282 &reset_pin_assert_time);
283 of_property_read_u32(np, "ingenic,min-wakeup-pin-assert-time-ms",
284 &min_wakeup_pin_assert_time);
287 * Set minimum wakeup pin assertion time: 100 ms.
288 * Range is 0 to 2 sec if RTC is clocked at 32 kHz.
290 wakeup_ticks = (min_wakeup_pin_assert_time * rate) / 1000;
291 if (wakeup_ticks < JZ_RTC_WAKEUP_FILTER_MASK)
292 wakeup_ticks &= JZ_RTC_WAKEUP_FILTER_MASK;
293 else
294 wakeup_ticks = JZ_RTC_WAKEUP_FILTER_MASK;
295 jz4740_rtc_reg_write(rtc, JZ_REG_RTC_WAKEUP_FILTER, wakeup_ticks);
298 * Set reset pin low-level assertion time after wakeup: 60 ms.
299 * Range is 0 to 125 ms if RTC is clocked at 32 kHz.
301 reset_ticks = (reset_pin_assert_time * rate) / 1000;
302 if (reset_ticks < JZ_RTC_RESET_COUNTER_MASK)
303 reset_ticks &= JZ_RTC_RESET_COUNTER_MASK;
304 else
305 reset_ticks = JZ_RTC_RESET_COUNTER_MASK;
306 jz4740_rtc_reg_write(rtc, JZ_REG_RTC_RESET_COUNTER, reset_ticks);
309 static int jz4740_rtc_probe(struct platform_device *pdev)
311 struct device *dev = &pdev->dev;
312 struct device_node *np = dev->of_node;
313 struct jz4740_rtc *rtc;
314 unsigned long rate;
315 struct clk *clk;
316 int ret, irq;
318 rtc = devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL);
319 if (!rtc)
320 return -ENOMEM;
322 rtc->type = (enum jz4740_rtc_type)device_get_match_data(dev);
324 irq = platform_get_irq(pdev, 0);
325 if (irq < 0)
326 return irq;
328 rtc->base = devm_platform_ioremap_resource(pdev, 0);
329 if (IS_ERR(rtc->base))
330 return PTR_ERR(rtc->base);
332 clk = devm_clk_get(dev, "rtc");
333 if (IS_ERR(clk)) {
334 dev_err(dev, "Failed to get RTC clock\n");
335 return PTR_ERR(clk);
338 ret = clk_prepare_enable(clk);
339 if (ret) {
340 dev_err(dev, "Failed to enable clock\n");
341 return ret;
344 ret = devm_add_action_or_reset(dev, jz4740_rtc_clk_disable, clk);
345 if (ret) {
346 dev_err(dev, "Failed to register devm action\n");
347 return ret;
350 spin_lock_init(&rtc->lock);
352 platform_set_drvdata(pdev, rtc);
354 device_init_wakeup(dev, 1);
356 ret = dev_pm_set_wake_irq(dev, irq);
357 if (ret) {
358 dev_err(dev, "Failed to set wake irq: %d\n", ret);
359 return ret;
362 rtc->rtc = devm_rtc_allocate_device(dev);
363 if (IS_ERR(rtc->rtc)) {
364 ret = PTR_ERR(rtc->rtc);
365 dev_err(dev, "Failed to allocate rtc device: %d\n", ret);
366 return ret;
369 rtc->rtc->ops = &jz4740_rtc_ops;
370 rtc->rtc->range_max = U32_MAX;
372 rate = clk_get_rate(clk);
373 jz4740_rtc_set_wakeup_params(rtc, np, rate);
375 /* Each 1 Hz pulse should happen after (rate) ticks */
376 jz4740_rtc_reg_write(rtc, JZ_REG_RTC_REGULATOR, rate - 1);
378 ret = devm_rtc_register_device(rtc->rtc);
379 if (ret)
380 return ret;
382 ret = devm_request_irq(dev, irq, jz4740_rtc_irq, 0,
383 pdev->name, rtc);
384 if (ret) {
385 dev_err(dev, "Failed to request rtc irq: %d\n", ret);
386 return ret;
389 if (of_device_is_system_power_controller(np)) {
390 dev_for_power_off = dev;
392 if (!pm_power_off)
393 pm_power_off = jz4740_rtc_power_off;
394 else
395 dev_warn(dev, "Poweroff handler already present!\n");
398 return 0;
401 static struct platform_driver jz4740_rtc_driver = {
402 .probe = jz4740_rtc_probe,
403 .driver = {
404 .name = "jz4740-rtc",
405 .of_match_table = jz4740_rtc_of_match,
409 module_platform_driver(jz4740_rtc_driver);
411 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
412 MODULE_LICENSE("GPL");
413 MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
414 MODULE_ALIAS("platform:jz4740-rtc");