blob: da59917c9ee8453cb30e6ca6da2aa650c62b239c [file] [log] [blame]
Alexandre Belloni7d6246212019-04-07 23:10:27 +02001// SPDX-License-Identifier: GPL-2.0
Linus Walleijaa958f52009-09-22 16:46:24 -07002/*
3 * Copyright (C) 2007-2009 ST-Ericsson AB
Linus Walleijaa958f52009-09-22 16:46:24 -07004 * Real Time Clock interface for ST-Ericsson AB COH 901 331 RTC.
5 * Author: Linus Walleij <linus.walleij@stericsson.com>
6 * Based on rtc-pl031.c by Deepak Saxena <dsaxena@plexity.net>
7 * Copyright 2006 (c) MontaVista Software, Inc.
8 */
9#include <linux/init.h>
10#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070011#include <linux/mod_devicetable.h>
Linus Walleijaa958f52009-09-22 16:46:24 -070012#include <linux/rtc.h>
13#include <linux/clk.h>
14#include <linux/interrupt.h>
15#include <linux/pm.h>
16#include <linux/platform_device.h>
17#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Walleijaa958f52009-09-22 16:46:24 -070019
20/*
21 * Registers in the COH 901 331
22 */
23/* Alarm value 32bit (R/W) */
24#define COH901331_ALARM 0x00U
25/* Used to set current time 32bit (R/W) */
26#define COH901331_SET_TIME 0x04U
27/* Indication if current time is valid 32bit (R/-) */
28#define COH901331_VALID 0x08U
29/* Read the current time 32bit (R/-) */
30#define COH901331_CUR_TIME 0x0cU
31/* Event register for the "alarm" interrupt */
32#define COH901331_IRQ_EVENT 0x10U
33/* Mask register for the "alarm" interrupt */
34#define COH901331_IRQ_MASK 0x14U
35/* Force register for the "alarm" interrupt */
36#define COH901331_IRQ_FORCE 0x18U
37
38/*
39 * Reference to RTC block clock
40 * Notice that the frequent clk_enable()/clk_disable() on this
41 * clock is mainly to be able to turn on/off other clocks in the
42 * hierarchy as needed, the RTC clock is always on anyway.
43 */
44struct coh901331_port {
45 struct rtc_device *rtc;
46 struct clk *clk;
Linus Walleijaa958f52009-09-22 16:46:24 -070047 void __iomem *virtbase;
48 int irq;
Jingoo Han62068e22013-04-29 16:21:00 -070049#ifdef CONFIG_PM_SLEEP
Linus Walleijaa958f52009-09-22 16:46:24 -070050 u32 irqmaskstore;
51#endif
52};
53
54static irqreturn_t coh901331_interrupt(int irq, void *data)
55{
56 struct coh901331_port *rtap = data;
57
58 clk_enable(rtap->clk);
59 /* Ack IRQ */
60 writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
Linus Walleij378ce742009-11-14 01:03:24 +010061 /*
62 * Disable the interrupt. This is necessary because
63 * the RTC lives on a lower-clocked line and will
64 * not release the IRQ line until after a few (slower)
65 * clock cycles. The interrupt will be re-enabled when
66 * a new alarm is set anyway.
67 */
68 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
Linus Walleijaa958f52009-09-22 16:46:24 -070069 clk_disable(rtap->clk);
Linus Walleij378ce742009-11-14 01:03:24 +010070
Linus Walleijaa958f52009-09-22 16:46:24 -070071 /* Set alarm flag */
72 rtc_update_irq(rtap->rtc, 1, RTC_AF);
73
74 return IRQ_HANDLED;
75}
76
77static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
78{
79 struct coh901331_port *rtap = dev_get_drvdata(dev);
80
81 clk_enable(rtap->clk);
82 /* Check if the time is valid */
Alexandre Belloni9cf2f9b2019-04-07 23:10:25 +020083 if (!readl(rtap->virtbase + COH901331_VALID)) {
Linus Walleijaa958f52009-09-22 16:46:24 -070084 clk_disable(rtap->clk);
Alexandre Belloni9cf2f9b2019-04-07 23:10:25 +020085 return -EINVAL;
Linus Walleijaa958f52009-09-22 16:46:24 -070086 }
Alexandre Belloni9cf2f9b2019-04-07 23:10:25 +020087
88 rtc_time64_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
Linus Walleijaa958f52009-09-22 16:46:24 -070089 clk_disable(rtap->clk);
Alexandre Belloni9cf2f9b2019-04-07 23:10:25 +020090 return 0;
Linus Walleijaa958f52009-09-22 16:46:24 -070091}
92
Alexandre Bellonifebad792019-04-07 23:10:26 +020093static int coh901331_set_time(struct device *dev, struct rtc_time *tm)
Linus Walleijaa958f52009-09-22 16:46:24 -070094{
95 struct coh901331_port *rtap = dev_get_drvdata(dev);
96
97 clk_enable(rtap->clk);
Alexandre Bellonifebad792019-04-07 23:10:26 +020098 writel(rtc_tm_to_time64(tm), rtap->virtbase + COH901331_SET_TIME);
Linus Walleijaa958f52009-09-22 16:46:24 -070099 clk_disable(rtap->clk);
100
101 return 0;
102}
103
104static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
105{
106 struct coh901331_port *rtap = dev_get_drvdata(dev);
107
108 clk_enable(rtap->clk);
Alexandre Belloni9cf2f9b2019-04-07 23:10:25 +0200109 rtc_time64_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
Linus Walleijaa958f52009-09-22 16:46:24 -0700110 alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
111 alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
112 clk_disable(rtap->clk);
113
114 return 0;
115}
116
117static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
118{
119 struct coh901331_port *rtap = dev_get_drvdata(dev);
Alexandre Belloni9cf2f9b2019-04-07 23:10:25 +0200120 unsigned long time = rtc_tm_to_time64(&alarm->time);
Linus Walleijaa958f52009-09-22 16:46:24 -0700121
Linus Walleijaa958f52009-09-22 16:46:24 -0700122 clk_enable(rtap->clk);
123 writel(time, rtap->virtbase + COH901331_ALARM);
124 writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
125 clk_disable(rtap->clk);
126
127 return 0;
128}
129
130static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
131{
132 struct coh901331_port *rtap = dev_get_drvdata(dev);
133
134 clk_enable(rtap->clk);
135 if (enabled)
136 writel(1, rtap->virtbase + COH901331_IRQ_MASK);
137 else
138 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
139 clk_disable(rtap->clk);
Linus Walleij378ce742009-11-14 01:03:24 +0100140
141 return 0;
Linus Walleijaa958f52009-09-22 16:46:24 -0700142}
143
Julia Lawall34c7b3a2016-08-31 10:05:25 +0200144static const struct rtc_class_ops coh901331_ops = {
Linus Walleijaa958f52009-09-22 16:46:24 -0700145 .read_time = coh901331_read_time,
Alexandre Bellonifebad792019-04-07 23:10:26 +0200146 .set_time = coh901331_set_time,
Linus Walleijaa958f52009-09-22 16:46:24 -0700147 .read_alarm = coh901331_read_alarm,
148 .set_alarm = coh901331_set_alarm,
149 .alarm_irq_enable = coh901331_alarm_irq_enable,
150};
151
152static int __exit coh901331_remove(struct platform_device *pdev)
153{
Jingoo Han3a028932013-07-03 15:07:12 -0700154 struct coh901331_port *rtap = platform_get_drvdata(pdev);
Linus Walleijaa958f52009-09-22 16:46:24 -0700155
Jingoo Han7fcbf5f2013-07-03 15:06:16 -0700156 if (rtap)
Linus Walleij8384dfe2012-07-30 14:41:31 -0700157 clk_unprepare(rtap->clk);
Linus Walleijaa958f52009-09-22 16:46:24 -0700158
159 return 0;
160}
161
162
163static int __init coh901331_probe(struct platform_device *pdev)
164{
165 int ret;
166 struct coh901331_port *rtap;
Linus Walleijaa958f52009-09-22 16:46:24 -0700167
Linus Walleij36ac1d242012-07-30 14:41:32 -0700168 rtap = devm_kzalloc(&pdev->dev,
169 sizeof(struct coh901331_port), GFP_KERNEL);
Linus Walleijaa958f52009-09-22 16:46:24 -0700170 if (!rtap)
171 return -ENOMEM;
172
YueHaibing09ef18b2019-10-06 18:29:20 +0800173 rtap->virtbase = devm_platform_ioremap_resource(pdev, 0);
Jingoo Handaaf90f2014-04-03 14:49:47 -0700174 if (IS_ERR(rtap->virtbase))
175 return PTR_ERR(rtap->virtbase);
Linus Walleijaa958f52009-09-22 16:46:24 -0700176
177 rtap->irq = platform_get_irq(pdev, 0);
Linus Walleij36ac1d242012-07-30 14:41:32 -0700178 if (devm_request_irq(&pdev->dev, rtap->irq, coh901331_interrupt, 0,
179 "RTC COH 901 331 Alarm", rtap))
180 return -EIO;
Linus Walleijaa958f52009-09-22 16:46:24 -0700181
Jingoo Han3b759d72013-02-21 16:45:38 -0800182 rtap->clk = devm_clk_get(&pdev->dev, NULL);
Linus Walleijaa958f52009-09-22 16:46:24 -0700183 if (IS_ERR(rtap->clk)) {
184 ret = PTR_ERR(rtap->clk);
185 dev_err(&pdev->dev, "could not get clock\n");
Linus Walleij36ac1d242012-07-30 14:41:32 -0700186 return ret;
Linus Walleijaa958f52009-09-22 16:46:24 -0700187 }
188
Alexandre Belloni06cfd662019-04-07 23:10:24 +0200189 rtap->rtc = devm_rtc_allocate_device(&pdev->dev);
190 if (IS_ERR(rtap->rtc))
191 return PTR_ERR(rtap->rtc);
192
193 rtap->rtc->ops = &coh901331_ops;
194 rtap->rtc->range_max = U32_MAX;
195
Linus Walleijaa958f52009-09-22 16:46:24 -0700196 /* We enable/disable the clock only to assure it works */
Linus Walleij8384dfe2012-07-30 14:41:31 -0700197 ret = clk_prepare_enable(rtap->clk);
Linus Walleijaa958f52009-09-22 16:46:24 -0700198 if (ret) {
199 dev_err(&pdev->dev, "could not enable clock\n");
Jingoo Han3b759d72013-02-21 16:45:38 -0800200 return ret;
Linus Walleijaa958f52009-09-22 16:46:24 -0700201 }
202 clk_disable(rtap->clk);
203
Linus Walleij9cf3b5f2011-04-17 20:32:19 +0200204 platform_set_drvdata(pdev, rtap);
Alexandre Belloni06cfd662019-04-07 23:10:24 +0200205
206 ret = rtc_register_device(rtap->rtc);
207 if (ret)
Linus Walleijaa958f52009-09-22 16:46:24 -0700208 goto out_no_rtc;
Linus Walleijaa958f52009-09-22 16:46:24 -0700209
Linus Walleijaa958f52009-09-22 16:46:24 -0700210 return 0;
211
212 out_no_rtc:
Linus Walleij8384dfe2012-07-30 14:41:31 -0700213 clk_unprepare(rtap->clk);
Linus Walleijaa958f52009-09-22 16:46:24 -0700214 return ret;
215}
216
Jingoo Han62068e22013-04-29 16:21:00 -0700217#ifdef CONFIG_PM_SLEEP
218static int coh901331_suspend(struct device *dev)
Linus Walleijaa958f52009-09-22 16:46:24 -0700219{
Jingoo Han62068e22013-04-29 16:21:00 -0700220 struct coh901331_port *rtap = dev_get_drvdata(dev);
Linus Walleijaa958f52009-09-22 16:46:24 -0700221
222 /*
223 * If this RTC alarm will be used for waking the system up,
224 * don't disable it of course. Else we just disable the alarm
225 * and await suspension.
226 */
Jingoo Han62068e22013-04-29 16:21:00 -0700227 if (device_may_wakeup(dev)) {
Linus Walleijaa958f52009-09-22 16:46:24 -0700228 enable_irq_wake(rtap->irq);
229 } else {
230 clk_enable(rtap->clk);
231 rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
232 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
233 clk_disable(rtap->clk);
234 }
Linus Walleij8384dfe2012-07-30 14:41:31 -0700235 clk_unprepare(rtap->clk);
Linus Walleijaa958f52009-09-22 16:46:24 -0700236 return 0;
237}
238
Jingoo Han62068e22013-04-29 16:21:00 -0700239static int coh901331_resume(struct device *dev)
Linus Walleijaa958f52009-09-22 16:46:24 -0700240{
Kangjie Lu5910fa02018-12-25 20:43:33 -0600241 int ret;
Jingoo Han62068e22013-04-29 16:21:00 -0700242 struct coh901331_port *rtap = dev_get_drvdata(dev);
Linus Walleijaa958f52009-09-22 16:46:24 -0700243
Kangjie Lu5910fa02018-12-25 20:43:33 -0600244 ret = clk_prepare(rtap->clk);
245 if (ret)
246 return ret;
247
Jingoo Han62068e22013-04-29 16:21:00 -0700248 if (device_may_wakeup(dev)) {
Linus Walleijaa958f52009-09-22 16:46:24 -0700249 disable_irq_wake(rtap->irq);
James Hogan5a98c042010-03-05 13:44:31 -0800250 } else {
Linus Walleijaa958f52009-09-22 16:46:24 -0700251 clk_enable(rtap->clk);
252 writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
253 clk_disable(rtap->clk);
James Hogan5a98c042010-03-05 13:44:31 -0800254 }
Linus Walleijaa958f52009-09-22 16:46:24 -0700255 return 0;
256}
Linus Walleijaa958f52009-09-22 16:46:24 -0700257#endif
258
Jingoo Han62068e22013-04-29 16:21:00 -0700259static SIMPLE_DEV_PM_OPS(coh901331_pm_ops, coh901331_suspend, coh901331_resume);
260
Linus Walleijaa958f52009-09-22 16:46:24 -0700261static void coh901331_shutdown(struct platform_device *pdev)
262{
Jingoo Han3a028932013-07-03 15:07:12 -0700263 struct coh901331_port *rtap = platform_get_drvdata(pdev);
Linus Walleijaa958f52009-09-22 16:46:24 -0700264
265 clk_enable(rtap->clk);
266 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
Julia Lawall828296d2012-10-04 17:14:07 -0700267 clk_disable_unprepare(rtap->clk);
Linus Walleijaa958f52009-09-22 16:46:24 -0700268}
269
Linus Walleija16b6c62013-04-19 13:03:13 +0200270static const struct of_device_id coh901331_dt_match[] = {
271 { .compatible = "stericsson,coh901331" },
272 {},
273};
Javier Martinez Canillas73798d52015-08-27 13:52:02 +0200274MODULE_DEVICE_TABLE(of, coh901331_dt_match);
Linus Walleija16b6c62013-04-19 13:03:13 +0200275
Linus Walleijaa958f52009-09-22 16:46:24 -0700276static struct platform_driver coh901331_driver = {
277 .driver = {
278 .name = "rtc-coh901331",
Jingoo Han62068e22013-04-29 16:21:00 -0700279 .pm = &coh901331_pm_ops,
Linus Walleija16b6c62013-04-19 13:03:13 +0200280 .of_match_table = coh901331_dt_match,
Linus Walleijaa958f52009-09-22 16:46:24 -0700281 },
282 .remove = __exit_p(coh901331_remove),
Linus Walleijaa958f52009-09-22 16:46:24 -0700283 .shutdown = coh901331_shutdown,
284};
285
Jingoo Han4fdf7a92013-04-29 16:18:37 -0700286module_platform_driver_probe(coh901331_driver, coh901331_probe);
Linus Walleijaa958f52009-09-22 16:46:24 -0700287
288MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
289MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
290MODULE_LICENSE("GPL");