blob: 8f1dd88fa8278b4672a6740acf167c9698cd2f87 [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>
Randy Dunlapac316722018-06-19 22:47:28 -070029#include <linux/mod_devicetable.h>
Linus Walleijac05fba2017-05-30 09:53:30 +020030#include <linux/clk.h>
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020031
Linus Walleij1d61d252017-05-30 09:53:32 +020032#define DRV_NAME "rtc-ftrtc010"
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020033
34MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
35MODULE_DESCRIPTION("RTC driver for Gemini SoC");
36MODULE_LICENSE("GPL");
37MODULE_ALIAS("platform:" DRV_NAME);
38
Linus Walleij1d61d252017-05-30 09:53:32 +020039struct ftrtc010_rtc {
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020040 struct rtc_device *rtc_dev;
41 void __iomem *rtc_base;
42 int rtc_irq;
Linus Walleijac05fba2017-05-30 09:53:30 +020043 struct clk *pclk;
44 struct clk *extclk;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020045};
46
Linus Walleij1d61d252017-05-30 09:53:32 +020047enum ftrtc010_rtc_offsets {
48 FTRTC010_RTC_SECOND = 0x00,
49 FTRTC010_RTC_MINUTE = 0x04,
50 FTRTC010_RTC_HOUR = 0x08,
51 FTRTC010_RTC_DAYS = 0x0C,
52 FTRTC010_RTC_ALARM_SECOND = 0x10,
53 FTRTC010_RTC_ALARM_MINUTE = 0x14,
54 FTRTC010_RTC_ALARM_HOUR = 0x18,
55 FTRTC010_RTC_RECORD = 0x1C,
56 FTRTC010_RTC_CR = 0x20,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020057};
58
Linus Walleij1d61d252017-05-30 09:53:32 +020059static irqreturn_t ftrtc010_rtc_interrupt(int irq, void *dev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020060{
61 return IRQ_HANDLED;
62}
63
64/*
65 * Looks like the RTC in the Gemini SoC is (totaly) broken
66 * We can't read/write directly the time from RTC registers.
67 * We must do some "offset" calculation to get the real time
68 *
69 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
70 * the same thing, without the rtc-lib.c calls.
71 */
72
Linus Walleij1d61d252017-05-30 09:53:32 +020073static int ftrtc010_rtc_read_time(struct device *dev, struct rtc_time *tm)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020074{
Linus Walleij1d61d252017-05-30 09:53:32 +020075 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020076
Alexandre Belloni73318f82018-06-04 16:15:27 +020077 u32 days, hour, min, sec, offset;
78 timeu64_t time;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020079
Linus Walleij1d61d252017-05-30 09:53:32 +020080 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
81 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
82 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
83 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
84 offset = readl(rtc->rtc_base + FTRTC010_RTC_RECORD);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020085
86 time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
87
Alexandre Belloni73318f82018-06-04 16:15:27 +020088 rtc_time64_to_tm(time, tm);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020089
90 return 0;
91}
92
Linus Walleij1d61d252017-05-30 09:53:32 +020093static int ftrtc010_rtc_set_time(struct device *dev, struct rtc_time *tm)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020094{
Linus Walleij1d61d252017-05-30 09:53:32 +020095 struct ftrtc010_rtc *rtc = dev_get_drvdata(dev);
Alexandre Belloni73318f82018-06-04 16:15:27 +020096 u32 sec, min, hour, day, offset;
97 timeu64_t time;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +020098
Alexandre Belloni73318f82018-06-04 16:15:27 +020099 time = rtc_tm_to_time64(tm);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200100
Linus Walleij1d61d252017-05-30 09:53:32 +0200101 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
102 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
103 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
104 day = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200105
106 offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
107
Linus Walleij1d61d252017-05-30 09:53:32 +0200108 writel(offset, rtc->rtc_base + FTRTC010_RTC_RECORD);
109 writel(0x01, rtc->rtc_base + FTRTC010_RTC_CR);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200110
111 return 0;
112}
113
Linus Walleij1d61d252017-05-30 09:53:32 +0200114static const struct rtc_class_ops ftrtc010_rtc_ops = {
115 .read_time = ftrtc010_rtc_read_time,
116 .set_time = ftrtc010_rtc_set_time,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200117};
118
Linus Walleij1d61d252017-05-30 09:53:32 +0200119static int ftrtc010_rtc_probe(struct platform_device *pdev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200120{
Alexandre Bellonib8e62b52018-06-04 16:15:28 +0200121 u32 days, hour, min, sec;
Linus Walleij1d61d252017-05-30 09:53:32 +0200122 struct ftrtc010_rtc *rtc;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200123 struct device *dev = &pdev->dev;
124 struct resource *res;
125 int ret;
126
127 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
128 if (unlikely(!rtc))
129 return -ENOMEM;
130 platform_set_drvdata(pdev, rtc);
131
Linus Walleijac05fba2017-05-30 09:53:30 +0200132 rtc->pclk = devm_clk_get(dev, "PCLK");
133 if (IS_ERR(rtc->pclk)) {
134 dev_err(dev, "could not get PCLK\n");
135 } else {
136 ret = clk_prepare_enable(rtc->pclk);
137 if (ret) {
138 dev_err(dev, "failed to enable PCLK\n");
139 return ret;
140 }
141 }
142 rtc->extclk = devm_clk_get(dev, "EXTCLK");
143 if (IS_ERR(rtc->extclk)) {
144 dev_err(dev, "could not get EXTCLK\n");
145 } else {
146 ret = clk_prepare_enable(rtc->extclk);
147 if (ret) {
148 dev_err(dev, "failed to enable EXTCLK\n");
149 return ret;
150 }
151 }
152
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200153 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
154 if (!res)
155 return -ENODEV;
156
157 rtc->rtc_irq = res->start;
158
159 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160 if (!res)
161 return -ENODEV;
162
163 rtc->rtc_base = devm_ioremap(dev, res->start,
kbuild test robot45b4c852015-06-13 21:16:56 +0800164 resource_size(res));
Pan Bian332e0d12017-04-23 20:48:07 +0800165 if (!rtc->rtc_base)
166 return -ENOMEM;
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200167
Alexandre Bellonie38d1612018-06-04 16:15:26 +0200168 rtc->rtc_dev = devm_rtc_allocate_device(dev);
169 if (IS_ERR(rtc->rtc_dev))
170 return PTR_ERR(rtc->rtc_dev);
171
172 rtc->rtc_dev->ops = &ftrtc010_rtc_ops;
173
Alexandre Bellonib8e62b52018-06-04 16:15:28 +0200174 sec = readl(rtc->rtc_base + FTRTC010_RTC_SECOND);
175 min = readl(rtc->rtc_base + FTRTC010_RTC_MINUTE);
176 hour = readl(rtc->rtc_base + FTRTC010_RTC_HOUR);
177 days = readl(rtc->rtc_base + FTRTC010_RTC_DAYS);
178
179 rtc->rtc_dev->range_min = (u64)days * 86400 + hour * 3600 +
180 min * 60 + sec;
181 rtc->rtc_dev->range_max = U32_MAX + rtc->rtc_dev->range_min;
182
Linus Walleij1d61d252017-05-30 09:53:32 +0200183 ret = devm_request_irq(dev, rtc->rtc_irq, ftrtc010_rtc_interrupt,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200184 IRQF_SHARED, pdev->name, dev);
185 if (unlikely(ret))
186 return ret;
187
Alexandre Bellonie38d1612018-06-04 16:15:26 +0200188 return rtc_register_device(rtc->rtc_dev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200189}
190
Linus Walleij1d61d252017-05-30 09:53:32 +0200191static int ftrtc010_rtc_remove(struct platform_device *pdev)
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200192{
Linus Walleij1d61d252017-05-30 09:53:32 +0200193 struct ftrtc010_rtc *rtc = platform_get_drvdata(pdev);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200194
Linus Walleijac05fba2017-05-30 09:53:30 +0200195 if (!IS_ERR(rtc->extclk))
196 clk_disable_unprepare(rtc->extclk);
197 if (!IS_ERR(rtc->pclk))
198 clk_disable_unprepare(rtc->pclk);
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200199
200 return 0;
201}
202
Linus Walleij1d61d252017-05-30 09:53:32 +0200203static const struct of_device_id ftrtc010_rtc_dt_match[] = {
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100204 { .compatible = "cortina,gemini-rtc" },
Linus Walleij1d61d252017-05-30 09:53:32 +0200205 { .compatible = "faraday,ftrtc010" },
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100206 { }
207};
Linus Walleij1d61d252017-05-30 09:53:32 +0200208MODULE_DEVICE_TABLE(of, ftrtc010_rtc_dt_match);
Linus Walleijbc7d8eb2017-01-22 13:19:50 +0100209
Linus Walleij1d61d252017-05-30 09:53:32 +0200210static struct platform_driver ftrtc010_rtc_driver = {
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200211 .driver = {
212 .name = DRV_NAME,
Linus Walleij1d61d252017-05-30 09:53:32 +0200213 .of_match_table = ftrtc010_rtc_dt_match,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200214 },
Linus Walleij1d61d252017-05-30 09:53:32 +0200215 .probe = ftrtc010_rtc_probe,
216 .remove = ftrtc010_rtc_remove,
Hans Ulli Kroll98a9bb52015-05-20 17:49:31 +0200217};
218
Linus Walleij1d61d252017-05-30 09:53:32 +0200219module_platform_driver_probe(ftrtc010_rtc_driver, ftrtc010_rtc_probe);