blob: 61f798c6101fec6a716a50722c47faed9b36fecb [file] [log] [blame]
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +02001/*
Linus Walleij1d61d252017-05-30 09:53:32 +02002 * Faraday Technology FTRTC010 driver
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +02003 *
4 * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * Original code for older kernel 2.6.15 are from Stormlinksemi
17 * first update from Janos Laube for > 2.6.29 kernels
18 *
19 * checkpatch fixes and usage of rtc-lib code
20 * Hans Ulli Kroll <ulli.kroll@googlemail.com>
21 */
22
23#include <linux/rtc.h>
24#include <linux/io.h>
25#include <linux/slab.h>
26#include <linux/platform_device.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
Linus Walleijac05fba2017-05-30 09:53:30 +020029#include <linux/clk.h>
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020030
Linus Walleij1d61d252017-05-30 09:53:32 +020031#define DRV_NAME "rtc-ftrtc010"
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020032
33MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
34MODULE_DESCRIPTION("RTC driver for Gemini SoC");
35MODULE_LICENSE("GPL");
36MODULE_ALIAS("platform:" DRV_NAME);
37
Linus Walleij1d61d252017-05-30 09:53:32 +020038struct ftrtc010_rtc {
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020039 struct rtc_device *rtc_dev;
40 void __iomem *rtc_base;
41 int rtc_irq;
Linus Walleijac05fba2017-05-30 09:53:30 +020042 struct clk *pclk;
43 struct clk *extclk;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020044};
45
Linus Walleij1d61d252017-05-30 09:53:32 +020046enum ftrtc010_rtc_offsets {
47 FTRTC010_RTC_SECOND = 0x00,
48 FTRTC010_RTC_MINUTE = 0x04,
49 FTRTC010_RTC_HOUR = 0x08,
50 FTRTC010_RTC_DAYS = 0x0C,
51 FTRTC010_RTC_ALARM_SECOND = 0x10,
52 FTRTC010_RTC_ALARM_MINUTE = 0x14,
53 FTRTC010_RTC_ALARM_HOUR = 0x18,
54 FTRTC010_RTC_RECORD = 0x1C,
55 FTRTC010_RTC_CR = 0x20,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020056};
57
Linus Walleij1d61d252017-05-30 09:53:32 +020058static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020059{
60 return IRQ_HANDLED;
61}
62
63/*
64 * Looks like the RTC in the Gemini SoC is (totaly) broken
65 * We can't read/write directly the time from RTC registers.
66 * We must do some "offset" calculation to get the real time
67 *
68 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
69 * the same thing, without the rtc-lib.c calls.
70 */
71
Linus Walleij1d61d252017-05-30 09:53:32 +020072static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020073{
Linus Walleij1d61d252017-05-30 09:53:32 +020074 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020075
Alexandre Belloni73318f82018-06-04 16:15:27 +020076 u32 days, hour, min, sec, offset;
77 timeu64_t time;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020078
Linus Walleij1d61d252017-05-30 09:53:32 +020079 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
80 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
81 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
82 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
83 offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020084
85 time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
86
Alexandre Belloni73318f82018-06-04 16:15:27 +020087 rtc_time64_to_tm(time, tm);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020088
89 return 0;
90}
91
Linus Walleij1d61d252017-05-30 09:53:32 +020092static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020093{
Linus Walleij1d61d252017-05-30 09:53:32 +020094 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
Alexandre Belloni73318f82018-06-04 16:15:27 +020095 u32 sec, min, hour, day, offset;
96 timeu64_t time;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020097
Alexandre Belloni73318f82018-06-04 16:15:27 +020098 time = rtc_tm_to_time64(tm);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020099
Linus Walleij1d61d252017-05-30 09:53:32 +0200100 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
101 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
102 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
103 day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200104
105 offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
106
Linus Walleij1d61d252017-05-30 09:53:32 +0200107 writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
108 writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200109
110 return 0;
111}
112
Linus Walleij1d61d252017-05-30 09:53:32 +0200113static const struct rtc_class_ops ftrtc010_rtc_ops = {
114 .read_time = ftrtc010_rtc_read_time,
115 .set_time = ftrtc010_rtc_set_time,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200116};
117
Linus Walleij1d61d252017-05-30 09:53:32 +0200118static int ftrtc010_rtc_probe(struct platform_device *pdev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200119{
Alexandre Bellonib8e62b52018-06-04 16:15:28 +0200120 u32 days, hour, min, sec;
Linus Walleij1d61d252017-05-30 09:53:32 +0200121 struct ftrtc010_rtc *rtc;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200122 struct device *dev = &pdev->dev;
123 struct resource *res;
124 int ret;
125
126 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
127 if (unlikely(!rtc))
128 return -ENOMEM;
129 platform_set_drvdata(pdev, rtc);
130
Linus Walleijac05fba2017-05-30 09:53:30 +0200131 rtc->pclk = devm_clk_get(dev, "PCLK");
132 if (IS_ERR(rtc->pclk)) {
133 dev_err(dev, "could not get PCLK\n");
134 } else {
135 ret = clk_prepare_enable(rtc->pclk);
136 if (ret) {
137 dev_err(dev, "failed to enable PCLK\n");
138 return ret;
139 }
140 }
141 rtc->extclk = devm_clk_get(dev, "EXTCLK");
142 if (IS_ERR(rtc->extclk)) {
143 dev_err(dev, "could not get EXTCLK\n");
144 } else {
145 ret = clk_prepare_enable(rtc->extclk);
146 if (ret) {
147 dev_err(dev, "failed to enable EXTCLK\n");
148 return ret;
149 }
150 }
151
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200152 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
153 if (!res)
154 return -ENODEV;
155
156 rtc->rtc_irq = res->start;
157
158 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
159 if (!res)
160 return -ENODEV;
161
162 rtc->rtc_base = devm_ioremap(dev, res->start,
kbuild test robot45b4c852015-06-13 21:16:56 +0800163 resource_size(res));
Pan Bian332e0d12017-04-23 20:48:07 +0800164 if (!rtc->rtc_base)
165 return -ENOMEM;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200166
Alexandre Bellonie38d1612018-06-04 16:15:26 +0200167 rtc->rtc_dev = devm_rtc_allocate_device(dev);
168 if (IS_ERR(rtc->rtc_dev))
169 return PTR_ERR(rtc->rtc_dev);
170
171 rtc->rtc_dev->ops = &ftrtc010_rtc_ops;
172
Alexandre Bellonib8e62b52018-06-04 16:15:28 +0200173 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
174 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
175 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
176 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
177
178 rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
179 min * 60 + sec;
180 rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;
181
Linus Walleij1d61d252017-05-30 09:53:32 +0200182 ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200183 IRQF_SHARED, pdev->name, dev);
184 if (unlikely(ret))
185 return ret;
186
Alexandre Bellonie38d1612018-06-04 16:15:26 +0200187 return rtc_register_device(rtc->rtc_dev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200188}
189
Linus Walleij1d61d252017-05-30 09:53:32 +0200190static int ftrtc010_rtc_remove(struct platform_device *pdev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200191{
Linus Walleij1d61d252017-05-30 09:53:32 +0200192 struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200193
Linus Walleijac05fba2017-05-30 09:53:30 +0200194 if (!IS_ERR(rtc->extclk))
195 clk_disable_unprepare(rtc->extclk);
196 if (!IS_ERR(rtc->pclk))
197 clk_disable_unprepare(rtc->pclk);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200198
199 return 0;
200}
201
Linus Walleij1d61d252017-05-30 09:53:32 +0200202static const struct of_device_id ftrtc010_rtc_dt_match[] = {
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100203 { .compatible = "cortina,gemini-rtc" },
Linus Walleij1d61d252017-05-30 09:53:32 +0200204 { .compatible = "faraday,ftrtc010" },
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100205 { }
206};
Linus Walleij1d61d252017-05-30 09:53:32 +0200207MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100208
Linus Walleij1d61d252017-05-30 09:53:32 +0200209static struct platform_driver ftrtc010_rtc_driver = {
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200210 .driver = {
211 .name = DRV_NAME,
Linus Walleij1d61d252017-05-30 09:53:32 +0200212 .of_match_table = ftrtc010_rtc_dt_match,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200213 },
Linus Walleij1d61d252017-05-30 09:53:32 +0200214 .probe = ftrtc010_rtc_probe,
215 .remove = ftrtc010_rtc_remove,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200216};
217
Linus Walleij1d61d252017-05-30 09:53:32 +0200218module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);