1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Andreas Färber
10 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/platform_device.h>
14 #include <linux/rtc.h>
15 #include <linux/spinlock.h>
17 #define RTD_RTCSEC 0x00
18 #define RTD_RTCMIN 0x04
19 #define RTD_RTCHR 0x08
20 #define RTD_RTCDATE1 0x0c
21 #define RTD_RTCDATE2 0x10
22 #define RTD_RTCACR 0x28
23 #define RTD_RTCEN 0x2c
24 #define RTD_RTCCR 0x30
26 #define RTD_RTCSEC_RTCSEC_MASK 0x7f
28 #define RTD_RTCMIN_RTCMIN_MASK 0x3f
30 #define RTD_RTCHR_RTCHR_MASK 0x1f
32 #define RTD_RTCDATE1_RTCDATE1_MASK 0xff
34 #define RTD_RTCDATE2_RTCDATE2_MASK 0x7f
36 #define RTD_RTCACR_RTCPWR BIT(7)
38 #define RTD_RTCEN_RTCEN_MASK 0xff
40 #define RTD_RTCCR_RTCRST BIT(6)
45 struct rtc_device *rtcdev;
46 unsigned int base_year;
49 static inline int rtd119x_rtc_days_in_year(int year)
51 return 365 + (is_leap_year(year) ? 1 : 0);
54 static void rtd119x_rtc_reset(struct device *dev)
56 struct rtd119x_rtc *data = dev_get_drvdata(dev);
59 val = readl_relaxed(data->base + RTD_RTCCR);
60 val |= RTD_RTCCR_RTCRST;
61 writel_relaxed(val, data->base + RTD_RTCCR);
63 val &= ~RTD_RTCCR_RTCRST;
64 writel(val, data->base + RTD_RTCCR);
67 static void rtd119x_rtc_set_enabled(struct device *dev, bool enable)
69 struct rtd119x_rtc *data = dev_get_drvdata(dev);
72 val = readl_relaxed(data->base + RTD_RTCEN);
74 if ((val & RTD_RTCEN_RTCEN_MASK) == 0x5a)
76 writel_relaxed(0x5a, data->base + RTD_RTCEN);
78 writel_relaxed(0, data->base + RTD_RTCEN);
82 static int rtd119x_rtc_read_time(struct device *dev, struct rtc_time *tm)
84 struct rtd119x_rtc *data = dev_get_drvdata(dev);
91 tm->tm_sec = (readl_relaxed(data->base + RTD_RTCSEC) & RTD_RTCSEC_RTCSEC_MASK) >> 1;
92 tm->tm_min = readl_relaxed(data->base + RTD_RTCMIN) & RTD_RTCMIN_RTCMIN_MASK;
93 tm->tm_hour = readl_relaxed(data->base + RTD_RTCHR) & RTD_RTCHR_RTCHR_MASK;
94 day = readl_relaxed(data->base + RTD_RTCDATE1) & RTD_RTCDATE1_RTCDATE1_MASK;
95 day |= (readl_relaxed(data->base + RTD_RTCDATE2) & RTD_RTCDATE2_RTCDATE2_MASK) << 8;
96 sec = (readl_relaxed(data->base + RTD_RTCSEC) & RTD_RTCSEC_RTCSEC_MASK) >> 1;
99 if (sec == tm->tm_sec)
106 dev_dbg(dev, "%s: needed %i tries\n", __func__, tries);
108 year = data->base_year;
109 while (day >= rtd119x_rtc_days_in_year(year)) {
110 day -= rtd119x_rtc_days_in_year(year);
113 tm->tm_year = year - 1900;
117 while (day >= rtc_month_days(tm->tm_mon, year)) {
118 day -= rtc_month_days(tm->tm_mon, year);
121 tm->tm_mday = day + 1;
126 static int rtd119x_rtc_set_time(struct device *dev, struct rtc_time *tm)
128 struct rtd119x_rtc *data = dev_get_drvdata(dev);
132 if (1900 + tm->tm_year < data->base_year)
136 for (i = data->base_year; i < 1900 + tm->tm_year; i++)
137 day += rtd119x_rtc_days_in_year(i);
143 rtd119x_rtc_set_enabled(dev, false);
145 writel_relaxed((tm->tm_sec << 1) & RTD_RTCSEC_RTCSEC_MASK, data->base + RTD_RTCSEC);
146 writel_relaxed(tm->tm_min & RTD_RTCMIN_RTCMIN_MASK, data->base + RTD_RTCMIN);
147 writel_relaxed(tm->tm_hour & RTD_RTCHR_RTCHR_MASK, data->base + RTD_RTCHR);
148 writel_relaxed(day & RTD_RTCDATE1_RTCDATE1_MASK, data->base + RTD_RTCDATE1);
149 writel_relaxed((day >> 8) & RTD_RTCDATE2_RTCDATE2_MASK, data->base + RTD_RTCDATE2);
151 rtd119x_rtc_set_enabled(dev, true);
156 static const struct rtc_class_ops rtd119x_rtc_ops = {
157 .read_time = rtd119x_rtc_read_time,
158 .set_time = rtd119x_rtc_set_time,
161 static const struct of_device_id rtd119x_rtc_dt_ids[] = {
162 { .compatible = "realtek,rtd1295-rtc" },
166 static int rtd119x_rtc_probe(struct platform_device *pdev)
168 struct rtd119x_rtc *data;
172 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
176 platform_set_drvdata(pdev, data);
177 data->base_year = 2014;
179 data->base = devm_platform_ioremap_resource(pdev, 0);
180 if (IS_ERR(data->base))
181 return PTR_ERR(data->base);
183 data->clk = of_clk_get(pdev->dev.of_node, 0);
184 if (IS_ERR(data->clk))
185 return PTR_ERR(data->clk);
187 ret = clk_prepare_enable(data->clk);
193 val = readl_relaxed(data->base + RTD_RTCACR);
194 if (!(val & RTD_RTCACR_RTCPWR)) {
195 writel_relaxed(RTD_RTCACR_RTCPWR, data->base + RTD_RTCACR);
197 rtd119x_rtc_reset(&pdev->dev);
199 writel_relaxed(0, data->base + RTD_RTCMIN);
200 writel_relaxed(0, data->base + RTD_RTCHR);
201 writel_relaxed(0, data->base + RTD_RTCDATE1);
202 writel_relaxed(0, data->base + RTD_RTCDATE2);
205 rtd119x_rtc_set_enabled(&pdev->dev, true);
207 data->rtcdev = devm_rtc_device_register(&pdev->dev, "rtc",
208 &rtd119x_rtc_ops, THIS_MODULE);
209 if (IS_ERR(data->rtcdev)) {
210 dev_err(&pdev->dev, "failed to register rtc device");
211 clk_disable_unprepare(data->clk);
213 return PTR_ERR(data->rtcdev);
219 static void rtd119x_rtc_remove(struct platform_device *pdev)
221 struct rtd119x_rtc *data = platform_get_drvdata(pdev);
223 rtd119x_rtc_set_enabled(&pdev->dev, false);
225 clk_disable_unprepare(data->clk);
229 static struct platform_driver rtd119x_rtc_driver = {
230 .probe = rtd119x_rtc_probe,
231 .remove_new = rtd119x_rtc_remove,
233 .name = "rtd1295-rtc",
234 .of_match_table = rtd119x_rtc_dt_ids,
237 builtin_platform_driver(rtd119x_rtc_driver);