Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / rtc / rtc-ftrtc010.c
blobcb4a5d101f537e20a685bb022e6f6516b0df8271
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Faraday Technology FTRTC010 driver
5 * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
7 * Original code for older kernel 2.6.15 are from Stormlinksemi
8 * first update from Janos Laube for > 2.6.29 kernels
10 * checkpatch fixes and usage of rtc-lib code
11 * Hans Ulli Kroll <ulli.kroll@googlemail.com>
14 #include <linux/rtc.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/clk.h>
23 #define DRV_NAME "rtc-ftrtc010"
25 MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
26 MODULE_DESCRIPTION("RTC driver for Gemini SoC");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS("platform:" DRV_NAME);
30 struct ftrtc010_rtc {
31 struct rtc_device *rtc_dev;
32 void __iomem *rtc_base;
33 int rtc_irq;
34 struct clk *pclk;
35 struct clk *extclk;
38 enum ftrtc010_rtc_offsets {
39 FTRTC010_RTC_SECOND = 0x00,
40 FTRTC010_RTC_MINUTE = 0x04,
41 FTRTC010_RTC_HOUR = 0x08,
42 FTRTC010_RTC_DAYS = 0x0C,
43 FTRTC010_RTC_ALARM_SECOND = 0x10,
44 FTRTC010_RTC_ALARM_MINUTE = 0x14,
45 FTRTC010_RTC_ALARM_HOUR = 0x18,
46 FTRTC010_RTC_RECORD = 0x1C,
47 FTRTC010_RTC_CR = 0x20,
50 static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
52 return IRQ_HANDLED;
56 * Looks like the RTC in the Gemini SoC is (totaly) broken
57 * We can't read/write directly the time from RTC registers.
58 * We must do some "offset" calculation to get the real time
60 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
61 * the same thing, without the rtc-lib.c calls.
64 static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
66 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
68 u32 days, hour, min, sec, offset;
69 timeu64_t time;
71 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
72 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
73 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
74 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
75 offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
77 time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
79 rtc_time64_to_tm(time, tm);
81 return 0;
84 static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
86 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
87 u32 sec, min, hour, day, offset;
88 timeu64_t time;
90 time = rtc_tm_to_time64(tm);
92 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
93 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
94 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
95 day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
97 offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
99 writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
100 writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
102 return 0;
105 static const struct rtc_class_ops ftrtc010_rtc_ops = {
106 .read_time = ftrtc010_rtc_read_time,
107 .set_time = ftrtc010_rtc_set_time,
110 static int ftrtc010_rtc_probe(struct platform_device *pdev)
112 u32 days, hour, min, sec;
113 struct ftrtc010_rtc *rtc;
114 struct device *dev = &pdev->dev;
115 struct resource *res;
116 int ret;
118 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
119 if (unlikely(!rtc))
120 return -ENOMEM;
121 platform_set_drvdata(pdev, rtc);
123 rtc->pclk = devm_clk_get(dev, "PCLK");
124 if (IS_ERR(rtc->pclk)) {
125 dev_err(dev, "could not get PCLK\n");
126 } else {
127 ret = clk_prepare_enable(rtc->pclk);
128 if (ret) {
129 dev_err(dev, "failed to enable PCLK\n");
130 return ret;
133 rtc->extclk = devm_clk_get(dev, "EXTCLK");
134 if (IS_ERR(rtc->extclk)) {
135 dev_err(dev, "could not get EXTCLK\n");
136 } else {
137 ret = clk_prepare_enable(rtc->extclk);
138 if (ret) {
139 dev_err(dev, "failed to enable EXTCLK\n");
140 goto err_disable_pclk;
144 rtc->rtc_irq = platform_get_irq(pdev, 0);
145 if (rtc->rtc_irq < 0) {
146 ret = rtc->rtc_irq;
147 goto err_disable_extclk;
150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 if (!res) {
152 ret = -ENODEV;
153 goto err_disable_extclk;
156 rtc->rtc_base = devm_ioremap(dev, res->start,
157 resource_size(res));
158 if (!rtc->rtc_base) {
159 ret = -ENOMEM;
160 goto err_disable_extclk;
163 rtc->rtc_dev = devm_rtc_allocate_device(dev);
164 if (IS_ERR(rtc->rtc_dev)) {
165 ret = PTR_ERR(rtc->rtc_dev);
166 goto err_disable_extclk;
169 rtc->rtc_dev->ops = &ftrtc010_rtc_ops;
171 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
172 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
173 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
174 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
176 rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
177 min * 60 + sec;
178 rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;
180 ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
181 IRQF_SHARED, pdev->name, dev);
182 if (unlikely(ret))
183 goto err_disable_extclk;
185 return devm_rtc_register_device(rtc->rtc_dev);
187 err_disable_extclk:
188 clk_disable_unprepare(rtc->extclk);
189 err_disable_pclk:
190 clk_disable_unprepare(rtc->pclk);
191 return ret;
194 static void ftrtc010_rtc_remove(struct platform_device *pdev)
196 struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
198 if (!IS_ERR(rtc->extclk))
199 clk_disable_unprepare(rtc->extclk);
200 if (!IS_ERR(rtc->pclk))
201 clk_disable_unprepare(rtc->pclk);
204 static const struct of_device_id ftrtc010_rtc_dt_match[] = {
205 { .compatible = "cortina,gemini-rtc" },
206 { .compatible = "faraday,ftrtc010" },
209 MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
211 static struct platform_driver ftrtc010_rtc_driver = {
212 .driver = {
213 .name = DRV_NAME,
214 .of_match_table = ftrtc010_rtc_dt_match,
216 .probe = ftrtc010_rtc_probe,
217 .remove = ftrtc010_rtc_remove,
220 module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);