1 // SPDX-License-Identifier: GPL-2.0+
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
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
{
54 enum jz4740_rtc_type type
;
56 struct rtc_device
*rtc
;
63 unsigned int min_wakeup_pin_assert_time
;
64 unsigned int reset_pin_assert_time
;
67 static struct device
*dev_for_power_off
;
69 static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc
*rtc
, size_t reg
)
71 return readl(rtc
->base
+ reg
);
74 static int jz4740_rtc_wait_write_ready(struct jz4740_rtc
*rtc
)
80 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
81 } while (!(ctrl
& JZ_RTC_CTRL_WRDY
) && --timeout
);
83 return timeout
? 0 : -EIO
;
86 static inline int jz4780_rtc_enable_write(struct jz4740_rtc
*rtc
)
89 int ret
, timeout
= 10000;
91 ret
= jz4740_rtc_wait_write_ready(rtc
);
95 writel(JZ_RTC_WENR_MAGIC
, rtc
->base
+ JZ_REG_RTC_WENR
);
98 ctrl
= readl(rtc
->base
+ JZ_REG_RTC_WENR
);
99 } while (!(ctrl
& JZ_RTC_WENR_WEN
) && --timeout
);
101 return timeout
? 0 : -EIO
;
104 static inline int jz4740_rtc_reg_write(struct jz4740_rtc
*rtc
, size_t reg
,
109 if (rtc
->type
>= ID_JZ4780
)
110 ret
= jz4780_rtc_enable_write(rtc
);
112 ret
= jz4740_rtc_wait_write_ready(rtc
);
114 writel(val
, rtc
->base
+ reg
);
119 static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc
*rtc
, uint32_t mask
,
126 spin_lock_irqsave(&rtc
->lock
, flags
);
128 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
130 /* Don't clear interrupt flags by accident */
131 ctrl
|= JZ_RTC_CTRL_1HZ
| JZ_RTC_CTRL_AF
;
138 ret
= jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_CTRL
, ctrl
);
140 spin_unlock_irqrestore(&rtc
->lock
, flags
);
145 static int jz4740_rtc_read_time(struct device
*dev
, struct rtc_time
*time
)
147 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
148 uint32_t secs
, secs2
;
151 if (jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SCRATCHPAD
) != 0x12345678)
154 /* If the seconds register is read while it is updated, it can contain a
155 * bogus value. This can be avoided by making sure that two consecutive
156 * reads have the same value.
158 secs
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC
);
159 secs2
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC
);
161 while (secs
!= secs2
&& --timeout
) {
163 secs2
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC
);
169 rtc_time64_to_tm(secs
, time
);
174 static int jz4740_rtc_set_time(struct device
*dev
, struct rtc_time
*time
)
176 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
179 ret
= jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_SEC
, rtc_tm_to_time64(time
));
183 return jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_SCRATCHPAD
, 0x12345678);
186 static int jz4740_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
188 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
192 secs
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC_ALARM
);
194 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
196 alrm
->enabled
= !!(ctrl
& JZ_RTC_CTRL_AE
);
197 alrm
->pending
= !!(ctrl
& JZ_RTC_CTRL_AF
);
199 rtc_time64_to_tm(secs
, &alrm
->time
);
204 static int jz4740_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
207 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
208 uint32_t secs
= lower_32_bits(rtc_tm_to_time64(&alrm
->time
));
210 ret
= jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_SEC_ALARM
, secs
);
212 ret
= jz4740_rtc_ctrl_set_bits(rtc
,
213 JZ_RTC_CTRL_AE
| JZ_RTC_CTRL_AF_IRQ
, alrm
->enabled
);
218 static int jz4740_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
220 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
221 return jz4740_rtc_ctrl_set_bits(rtc
, JZ_RTC_CTRL_AF_IRQ
, enable
);
224 static const struct rtc_class_ops jz4740_rtc_ops
= {
225 .read_time
= jz4740_rtc_read_time
,
226 .set_time
= jz4740_rtc_set_time
,
227 .read_alarm
= jz4740_rtc_read_alarm
,
228 .set_alarm
= jz4740_rtc_set_alarm
,
229 .alarm_irq_enable
= jz4740_rtc_alarm_irq_enable
,
232 static irqreturn_t
jz4740_rtc_irq(int irq
, void *data
)
234 struct jz4740_rtc
*rtc
= data
;
236 unsigned long events
= 0;
238 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
240 if (ctrl
& JZ_RTC_CTRL_1HZ
)
241 events
|= (RTC_UF
| RTC_IRQF
);
243 if (ctrl
& JZ_RTC_CTRL_AF
)
244 events
|= (RTC_AF
| RTC_IRQF
);
246 rtc_update_irq(rtc
->rtc
, 1, events
);
248 jz4740_rtc_ctrl_set_bits(rtc
, JZ_RTC_CTRL_1HZ
| JZ_RTC_CTRL_AF
, false);
253 static void jz4740_rtc_poweroff(struct device
*dev
)
255 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
256 jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_HIBERNATE
, 1);
259 static void jz4740_rtc_power_off(void)
261 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev_for_power_off
);
262 unsigned long rtc_rate
;
263 unsigned long wakeup_filter_ticks
;
264 unsigned long reset_counter_ticks
;
266 clk_prepare_enable(rtc
->clk
);
268 rtc_rate
= clk_get_rate(rtc
->clk
);
271 * Set minimum wakeup pin assertion time: 100 ms.
272 * Range is 0 to 2 sec if RTC is clocked at 32 kHz.
274 wakeup_filter_ticks
=
275 (rtc
->min_wakeup_pin_assert_time
* rtc_rate
) / 1000;
276 if (wakeup_filter_ticks
< JZ_RTC_WAKEUP_FILTER_MASK
)
277 wakeup_filter_ticks
&= JZ_RTC_WAKEUP_FILTER_MASK
;
279 wakeup_filter_ticks
= JZ_RTC_WAKEUP_FILTER_MASK
;
280 jz4740_rtc_reg_write(rtc
,
281 JZ_REG_RTC_WAKEUP_FILTER
, wakeup_filter_ticks
);
284 * Set reset pin low-level assertion time after wakeup: 60 ms.
285 * Range is 0 to 125 ms if RTC is clocked at 32 kHz.
287 reset_counter_ticks
= (rtc
->reset_pin_assert_time
* rtc_rate
) / 1000;
288 if (reset_counter_ticks
< JZ_RTC_RESET_COUNTER_MASK
)
289 reset_counter_ticks
&= JZ_RTC_RESET_COUNTER_MASK
;
291 reset_counter_ticks
= JZ_RTC_RESET_COUNTER_MASK
;
292 jz4740_rtc_reg_write(rtc
,
293 JZ_REG_RTC_RESET_COUNTER
, reset_counter_ticks
);
295 jz4740_rtc_poweroff(dev_for_power_off
);
299 static const struct of_device_id jz4740_rtc_of_match
[] = {
300 { .compatible
= "ingenic,jz4740-rtc", .data
= (void *)ID_JZ4740
},
301 { .compatible
= "ingenic,jz4780-rtc", .data
= (void *)ID_JZ4780
},
304 MODULE_DEVICE_TABLE(of
, jz4740_rtc_of_match
);
306 static int jz4740_rtc_probe(struct platform_device
*pdev
)
309 struct jz4740_rtc
*rtc
;
310 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
311 const struct of_device_id
*of_id
= of_match_device(
312 jz4740_rtc_of_match
, &pdev
->dev
);
313 struct device_node
*np
= pdev
->dev
.of_node
;
315 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
320 rtc
->type
= (enum jz4740_rtc_type
)of_id
->data
;
322 rtc
->type
= id
->driver_data
;
324 rtc
->irq
= platform_get_irq(pdev
, 0);
328 rtc
->base
= devm_platform_ioremap_resource(pdev
, 0);
329 if (IS_ERR(rtc
->base
))
330 return PTR_ERR(rtc
->base
);
332 rtc
->clk
= devm_clk_get(&pdev
->dev
, "rtc");
333 if (IS_ERR(rtc
->clk
)) {
334 dev_err(&pdev
->dev
, "Failed to get RTC clock\n");
335 return PTR_ERR(rtc
->clk
);
338 spin_lock_init(&rtc
->lock
);
340 platform_set_drvdata(pdev
, rtc
);
342 device_init_wakeup(&pdev
->dev
, 1);
344 ret
= dev_pm_set_wake_irq(&pdev
->dev
, rtc
->irq
);
346 dev_err(&pdev
->dev
, "Failed to set wake irq: %d\n", ret
);
350 rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
351 if (IS_ERR(rtc
->rtc
)) {
352 ret
= PTR_ERR(rtc
->rtc
);
353 dev_err(&pdev
->dev
, "Failed to allocate rtc device: %d\n", ret
);
357 rtc
->rtc
->ops
= &jz4740_rtc_ops
;
358 rtc
->rtc
->range_max
= U32_MAX
;
360 ret
= rtc_register_device(rtc
->rtc
);
364 ret
= devm_request_irq(&pdev
->dev
, rtc
->irq
, jz4740_rtc_irq
, 0,
367 dev_err(&pdev
->dev
, "Failed to request rtc irq: %d\n", ret
);
371 if (np
&& of_device_is_system_power_controller(np
)) {
374 rtc
->reset_pin_assert_time
= 60;
375 of_property_read_u32(np
, "reset-pin-assert-time-ms",
376 &rtc
->reset_pin_assert_time
);
379 rtc
->min_wakeup_pin_assert_time
= 100;
380 of_property_read_u32(np
,
381 "min-wakeup-pin-assert-time-ms",
382 &rtc
->min_wakeup_pin_assert_time
);
384 dev_for_power_off
= &pdev
->dev
;
385 pm_power_off
= jz4740_rtc_power_off
;
388 "Poweroff handler already present!\n");
395 static const struct platform_device_id jz4740_rtc_ids
[] = {
396 { "jz4740-rtc", ID_JZ4740
},
397 { "jz4780-rtc", ID_JZ4780
},
400 MODULE_DEVICE_TABLE(platform
, jz4740_rtc_ids
);
402 static struct platform_driver jz4740_rtc_driver
= {
403 .probe
= jz4740_rtc_probe
,
405 .name
= "jz4740-rtc",
406 .of_match_table
= of_match_ptr(jz4740_rtc_of_match
),
408 .id_table
= jz4740_rtc_ids
,
411 module_platform_driver(jz4740_rtc_driver
);
413 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
414 MODULE_LICENSE("GPL");
415 MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
416 MODULE_ALIAS("platform:jz4740-rtc");