blob: 53bb08fe1cd462628d8dbedabdbed7f6fe7a15d3 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +02002/*
Linus Walleij1d61d252017-05-30 09:53:32 +02003 * Faraday Technology FTRTC010 driver
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +02004 *
5 * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
6 *
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +02007 * Original code for older kernel 2.6.15 are from Stormlinksemi
8 * first update from Janos Laube for > 2.6.29 kernels
9 *
10 * checkpatch fixes and usage of rtc-lib code
11 * Hans Ulli Kroll <ulli.kroll@googlemail.com>
12 */
13
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>
Randy Dunlapac316722018-06-19 22:47:28 -070020#include <linux/mod_devicetable.h>
Linus Walleijac05fba2017-05-30 09:53:30 +020021#include <linux/clk.h>
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020022
Linus Walleij1d61d252017-05-30 09:53:32 +020023#define DRV_NAME "rtc-ftrtc010"
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020024
25MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
26MODULE_DESCRIPTION("RTC driver for Gemini SoC");
27MODULE_LICENSE("GPL");
28MODULE_ALIAS("platform:" DRV_NAME);
29
Linus Walleij1d61d252017-05-30 09:53:32 +020030struct ftrtc010_rtc {
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020031 struct rtc_device *rtc_dev;
32 void __iomem *rtc_base;
33 int rtc_irq;
Linus Walleijac05fba2017-05-30 09:53:30 +020034 struct clk *pclk;
35 struct clk *extclk;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020036};
37
Linus Walleij1d61d252017-05-30 09:53:32 +020038enum 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,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020048};
49
Linus Walleij1d61d252017-05-30 09:53:32 +020050static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020051{
52 return IRQ_HANDLED;
53}
54
55/*
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
59 *
60 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
61 * the same thing, without the rtc-lib.c calls.
62 */
63
Linus Walleij1d61d252017-05-30 09:53:32 +020064static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020065{
Linus Walleij1d61d252017-05-30 09:53:32 +020066 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020067
Alexandre Belloni73318f82018-06-04 16:15:27 +020068 u32 days, hour, min, sec, offset;
69 timeu64_t time;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020070
Linus Walleij1d61d252017-05-30 09:53:32 +020071 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);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020076
77 time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
78
Alexandre Belloni73318f82018-06-04 16:15:27 +020079 rtc_time64_to_tm(time, tm);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020080
81 return 0;
82}
83
Linus Walleij1d61d252017-05-30 09:53:32 +020084static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020085{
Linus Walleij1d61d252017-05-30 09:53:32 +020086 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
Alexandre Belloni73318f82018-06-04 16:15:27 +020087 u32 sec, min, hour, day, offset;
88 timeu64_t time;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020089
Alexandre Belloni73318f82018-06-04 16:15:27 +020090 time = rtc_tm_to_time64(tm);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020091
Linus Walleij1d61d252017-05-30 09:53:32 +020092 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);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020096
97 offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
98
Linus Walleij1d61d252017-05-30 09:53:32 +020099 writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
100 writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200101
102 return 0;
103}
104
Linus Walleij1d61d252017-05-30 09:53:32 +0200105static const struct rtc_class_ops ftrtc010_rtc_ops = {
106 .read_time = ftrtc010_rtc_read_time,
107 .set_time = ftrtc010_rtc_set_time,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200108};
109
Linus Walleij1d61d252017-05-30 09:53:32 +0200110static int ftrtc010_rtc_probe(struct platform_device *pdev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200111{
Alexandre Bellonib8e62b52018-06-04 16:15:28 +0200112 u32 days, hour, min, sec;
Linus Walleij1d61d252017-05-30 09:53:32 +0200113 struct ftrtc010_rtc *rtc;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200114 struct device *dev = &pdev->dev;
115 struct resource *res;
116 int ret;
117
118 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
119 if (unlikely(!rtc))
120 return -ENOMEM;
121 platform_set_drvdata(pdev, rtc);
122
Linus Walleijac05fba2017-05-30 09:53:30 +0200123 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;
131 }
132 }
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 return ret;
141 }
142 }
143
Lad Prabhakar05020a72021-12-20 01:15:24 +0000144 rtc->rtc_irq = platform_get_irq(pdev, 0);
145 if (rtc->rtc_irq < 0)
146 return rtc->rtc_irq;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200147
148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 if (!res)
150 return -ENODEV;
151
152 rtc->rtc_base = devm_ioremap(dev, res->start,
kbuild test robot45b4c852015-06-13 21:16:56 +0800153 resource_size(res));
Pan Bian332e0d12017-04-23 20:48:07 +0800154 if (!rtc->rtc_base)
155 return -ENOMEM;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200156
Alexandre Bellonie38d1612018-06-04 16:15:26 +0200157 rtc->rtc_dev = devm_rtc_allocate_device(dev);
158 if (IS_ERR(rtc->rtc_dev))
159 return PTR_ERR(rtc->rtc_dev);
160
161 rtc->rtc_dev->ops = &ftrtc010_rtc_ops;
162
Alexandre Bellonib8e62b52018-06-04 16:15:28 +0200163 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
164 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
165 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
166 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
167
168 rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
169 min * 60 + sec;
170 rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;
171
Linus Walleij1d61d252017-05-30 09:53:32 +0200172 ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200173 IRQF_SHARED, pdev->name, dev);
174 if (unlikely(ret))
175 return ret;
176
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100177 return devm_rtc_register_device(rtc->rtc_dev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200178}
179
Linus Walleij1d61d252017-05-30 09:53:32 +0200180static int ftrtc010_rtc_remove(struct platform_device *pdev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200181{
Linus Walleij1d61d252017-05-30 09:53:32 +0200182 struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200183
Linus Walleijac05fba2017-05-30 09:53:30 +0200184 if (!IS_ERR(rtc->extclk))
185 clk_disable_unprepare(rtc->extclk);
186 if (!IS_ERR(rtc->pclk))
187 clk_disable_unprepare(rtc->pclk);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200188
189 return 0;
190}
191
Linus Walleij1d61d252017-05-30 09:53:32 +0200192static const struct of_device_id ftrtc010_rtc_dt_match[] = {
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100193 { .compatible = "cortina,gemini-rtc" },
Linus Walleij1d61d252017-05-30 09:53:32 +0200194 { .compatible = "faraday,ftrtc010" },
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100195 { }
196};
Linus Walleij1d61d252017-05-30 09:53:32 +0200197MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100198
Linus Walleij1d61d252017-05-30 09:53:32 +0200199static struct platform_driver ftrtc010_rtc_driver = {
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200200 .driver = {
201 .name = DRV_NAME,
Linus Walleij1d61d252017-05-30 09:53:32 +0200202 .of_match_table = ftrtc010_rtc_dt_match,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200203 },
Linus Walleij1d61d252017-05-30 09:53:32 +0200204 .probe = ftrtc010_rtc_probe,
205 .remove = ftrtc010_rtc_remove,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200206};
207
Linus Walleij1d61d252017-05-30 09:53:32 +0200208module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);