blob: 46609ae56ffdc4f3d09d6620e04742d8d6381084 [file] [log] [blame]
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +08001/*
2 * An RTC driver for Allwinner A31/A23
3 *
4 * Copyright (c) 2014, Chen-Yu Tsai <wens@csie.org>
5 *
6 * based on rtc-sunxi.c
7 *
8 * An RTC driver for Allwinner A10/A20
9 *
10 * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 */
22
Maxime Ripard3855c2c2017-01-23 11:41:49 +010023#include <linux/clk.h>
24#include <linux/clk-provider.h>
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +080025#include <linux/delay.h>
26#include <linux/err.h>
27#include <linux/fs.h>
28#include <linux/init.h>
29#include <linux/interrupt.h>
30#include <linux/io.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/of.h>
34#include <linux/of_address.h>
35#include <linux/of_device.h>
36#include <linux/platform_device.h>
37#include <linux/rtc.h>
Maxime Ripard3855c2c2017-01-23 11:41:49 +010038#include <linux/slab.h>
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +080039#include <linux/types.h>
40
41/* Control register */
42#define SUN6I_LOSC_CTRL 0x0000
Maxime Ripardfb61bb82017-01-23 11:41:48 +010043#define SUN6I_LOSC_CTRL_KEY (0x16aa << 16)
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +080044#define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9)
45#define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8)
46#define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7)
Maxime Ripardfb61bb82017-01-23 11:41:48 +010047#define SUN6I_LOSC_CTRL_EXT_OSC BIT(0)
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +080048#define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7)
49
Maxime Ripard3855c2c2017-01-23 11:41:49 +010050#define SUN6I_LOSC_CLK_PRESCAL 0x0008
51
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +080052/* RTC */
53#define SUN6I_RTC_YMD 0x0010
54#define SUN6I_RTC_HMS 0x0014
55
56/* Alarm 0 (counter) */
57#define SUN6I_ALRM_COUNTER 0x0020
58#define SUN6I_ALRM_CUR_VAL 0x0024
59#define SUN6I_ALRM_EN 0x0028
60#define SUN6I_ALRM_EN_CNT_EN BIT(0)
61#define SUN6I_ALRM_IRQ_EN 0x002c
62#define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
63#define SUN6I_ALRM_IRQ_STA 0x0030
64#define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
65
66/* Alarm 1 (wall clock) */
67#define SUN6I_ALRM1_EN 0x0044
68#define SUN6I_ALRM1_IRQ_EN 0x0048
69#define SUN6I_ALRM1_IRQ_STA 0x004c
70#define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND BIT(0)
71
72/* Alarm config */
73#define SUN6I_ALARM_CONFIG 0x0050
74#define SUN6I_ALARM_CONFIG_WAKEUP BIT(0)
75
Maxime Ripard17ecd242017-08-25 09:42:02 +020076#define SUN6I_LOSC_OUT_GATING 0x0060
Michael Trimarchi09018d42018-05-30 23:57:44 +053077#define SUN6I_LOSC_OUT_GATING_EN_OFFSET 0
Maxime Ripard17ecd242017-08-25 09:42:02 +020078
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +080079/*
80 * Get date values
81 */
82#define SUN6I_DATE_GET_DAY_VALUE(x) ((x) & 0x0000001f)
83#define SUN6I_DATE_GET_MON_VALUE(x) (((x) & 0x00000f00) >> 8)
84#define SUN6I_DATE_GET_YEAR_VALUE(x) (((x) & 0x003f0000) >> 16)
85#define SUN6I_LEAP_GET_VALUE(x) (((x) & 0x00400000) >> 22)
86
87/*
88 * Get time values
89 */
90#define SUN6I_TIME_GET_SEC_VALUE(x) ((x) & 0x0000003f)
91#define SUN6I_TIME_GET_MIN_VALUE(x) (((x) & 0x00003f00) >> 8)
92#define SUN6I_TIME_GET_HOUR_VALUE(x) (((x) & 0x001f0000) >> 16)
93
94/*
95 * Set date values
96 */
97#define SUN6I_DATE_SET_DAY_VALUE(x) ((x) & 0x0000001f)
98#define SUN6I_DATE_SET_MON_VALUE(x) ((x) << 8 & 0x00000f00)
99#define SUN6I_DATE_SET_YEAR_VALUE(x) ((x) << 16 & 0x003f0000)
100#define SUN6I_LEAP_SET_VALUE(x) ((x) << 22 & 0x00400000)
101
102/*
103 * Set time values
104 */
105#define SUN6I_TIME_SET_SEC_VALUE(x) ((x) & 0x0000003f)
106#define SUN6I_TIME_SET_MIN_VALUE(x) ((x) << 8 & 0x00003f00)
107#define SUN6I_TIME_SET_HOUR_VALUE(x) ((x) << 16 & 0x001f0000)
108
109/*
110 * The year parameter passed to the driver is usually an offset relative to
111 * the year 1900. This macro is used to convert this offset to another one
112 * relative to the minimum year allowed by the hardware.
113 *
114 * The year range is 1970 - 2033. This range is selected to match Allwinner's
115 * driver, even though it is somewhat limited.
116 */
117#define SUN6I_YEAR_MIN 1970
118#define SUN6I_YEAR_MAX 2033
119#define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900)
120
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800121/*
122 * There are other differences between models, including:
123 *
124 * - number of GPIO pins that can be configured to hold a certain level
125 * - crypto-key related registers (H5, H6)
126 * - boot process related (super standby, secondary processor entry address)
127 * registers (R40, H6)
128 * - SYS power domain controls (R40)
129 * - DCXO controls (H6)
130 * - RC oscillator calibration (H6)
131 *
132 * These functions are not covered by this driver.
133 */
134struct sun6i_rtc_clk_data {
135 unsigned long rc_osc_rate;
136 unsigned int fixed_prescaler : 16;
137 unsigned int has_prescaler : 1;
138 unsigned int has_out_clk : 1;
139};
140
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800141struct sun6i_rtc_dev {
142 struct rtc_device *rtc;
143 struct device *dev;
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800144 const struct sun6i_rtc_clk_data *data;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800145 void __iomem *base;
146 int irq;
147 unsigned long alarm;
Maxime Riparda9422a12017-01-23 11:41:47 +0100148
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100149 struct clk_hw hw;
150 struct clk_hw *int_osc;
151 struct clk *losc;
Maxime Ripard17ecd242017-08-25 09:42:02 +0200152 struct clk *ext_losc;
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100153
Maxime Riparda9422a12017-01-23 11:41:47 +0100154 spinlock_t lock;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800155};
156
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100157static struct sun6i_rtc_dev *sun6i_rtc;
158
159static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw,
160 unsigned long parent_rate)
161{
162 struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800163 u32 val = 0;
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100164
165 val = readl(rtc->base + SUN6I_LOSC_CTRL);
166 if (val & SUN6I_LOSC_CTRL_EXT_OSC)
167 return parent_rate;
168
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800169 if (rtc->data->fixed_prescaler)
170 parent_rate /= rtc->data->fixed_prescaler;
171
172 if (rtc->data->has_prescaler) {
173 val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL);
174 val &= GENMASK(4, 0);
175 }
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100176
177 return parent_rate / (val + 1);
178}
179
180static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw)
181{
182 struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
183
184 return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC;
185}
186
187static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index)
188{
189 struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
190 unsigned long flags;
191 u32 val;
192
193 if (index > 1)
194 return -EINVAL;
195
196 spin_lock_irqsave(&rtc->lock, flags);
197 val = readl(rtc->base + SUN6I_LOSC_CTRL);
198 val &= ~SUN6I_LOSC_CTRL_EXT_OSC;
199 val |= SUN6I_LOSC_CTRL_KEY;
200 val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0;
201 writel(val, rtc->base + SUN6I_LOSC_CTRL);
202 spin_unlock_irqrestore(&rtc->lock, flags);
203
204 return 0;
205}
206
207static const struct clk_ops sun6i_rtc_osc_ops = {
208 .recalc_rate = sun6i_rtc_osc_recalc_rate,
209
210 .get_parent = sun6i_rtc_osc_get_parent,
211 .set_parent = sun6i_rtc_osc_set_parent,
212};
213
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800214static void __init sun6i_rtc_clk_init(struct device_node *node,
215 const struct sun6i_rtc_clk_data *data)
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100216{
217 struct clk_hw_onecell_data *clk_data;
218 struct sun6i_rtc_dev *rtc;
219 struct clk_init_data init = {
220 .ops = &sun6i_rtc_osc_ops,
Chen-Yu Tsai459b6ea2018-12-03 22:58:16 +0800221 .name = "losc",
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100222 };
Maxime Ripard17ecd242017-08-25 09:42:02 +0200223 const char *clkout_name = "osc32k-out";
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100224 const char *parents[2];
225
226 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
227 if (!rtc)
228 return;
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100229
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800230 rtc->data = data;
Gustavo A. R. Silva725e0e12018-08-23 13:51:40 -0500231 clk_data = kzalloc(struct_size(clk_data, hws, 2), GFP_KERNEL);
Colin Ian Kinge9982022017-11-22 17:16:18 +0000232 if (!clk_data) {
233 kfree(rtc);
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100234 return;
Colin Ian Kinge9982022017-11-22 17:16:18 +0000235 }
Alexey Klimov319ff832017-07-12 11:59:48 +0100236
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100237 spin_lock_init(&rtc->lock);
238
239 rtc->base = of_io_request_and_map(node, 0, of_node_full_name(node));
Wei Yongjunaaa65a92017-02-09 00:16:13 +0000240 if (IS_ERR(rtc->base)) {
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100241 pr_crit("Can't map RTC registers");
Colin Ian King1a37c342017-07-19 17:57:02 +0100242 goto err;
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100243 }
244
245 /* Switch to the external, more precise, oscillator */
246 writel(SUN6I_LOSC_CTRL_KEY | SUN6I_LOSC_CTRL_EXT_OSC,
247 rtc->base + SUN6I_LOSC_CTRL);
248
Chen-Yu Tsai15829cf2017-01-29 18:13:43 +0800249 /* Yes, I know, this is ugly. */
250 sun6i_rtc = rtc;
251
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100252 /* Deal with old DTs */
253 if (!of_get_property(node, "clocks", NULL))
Colin Ian King1a37c342017-07-19 17:57:02 +0100254 goto err;
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100255
256 rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL,
257 "rtc-int-osc",
258 NULL, 0,
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800259 rtc->data->rc_osc_rate,
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100260 300000000);
261 if (IS_ERR(rtc->int_osc)) {
262 pr_crit("Couldn't register the internal oscillator\n");
263 return;
264 }
265
266 parents[0] = clk_hw_get_name(rtc->int_osc);
267 parents[1] = of_clk_get_parent_name(node, 0);
268
269 rtc->hw.init = &init;
270
271 init.parent_names = parents;
272 init.num_parents = of_clk_get_parent_count(node) + 1;
Maxime Ripard17ecd242017-08-25 09:42:02 +0200273 of_property_read_string_index(node, "clock-output-names", 0,
274 &init.name);
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100275
276 rtc->losc = clk_register(NULL, &rtc->hw);
277 if (IS_ERR(rtc->losc)) {
278 pr_crit("Couldn't register the LOSC clock\n");
279 return;
280 }
281
Maxime Ripard17ecd242017-08-25 09:42:02 +0200282 of_property_read_string_index(node, "clock-output-names", 1,
283 &clkout_name);
284 rtc->ext_losc = clk_register_gate(NULL, clkout_name, rtc->hw.init->name,
285 0, rtc->base + SUN6I_LOSC_OUT_GATING,
Michael Trimarchi09018d42018-05-30 23:57:44 +0530286 SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0,
Maxime Ripard17ecd242017-08-25 09:42:02 +0200287 &rtc->lock);
288 if (IS_ERR(rtc->ext_losc)) {
289 pr_crit("Couldn't register the LOSC external gate\n");
290 return;
291 }
292
293 clk_data->num = 2;
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100294 clk_data->hws[0] = &rtc->hw;
Maxime Ripard17ecd242017-08-25 09:42:02 +0200295 clk_data->hws[1] = __clk_get_hw(rtc->ext_losc);
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100296 of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
Colin Ian King1a37c342017-07-19 17:57:02 +0100297 return;
298
299err:
300 kfree(clk_data);
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100301}
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800302
303static const struct sun6i_rtc_clk_data sun6i_a31_rtc_data = {
304 .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
305 .has_prescaler = 1,
306};
307
308static void __init sun6i_a31_rtc_clk_init(struct device_node *node)
309{
310 sun6i_rtc_clk_init(node, &sun6i_a31_rtc_data);
311}
312CLK_OF_DECLARE_DRIVER(sun6i_a31_rtc_clk, "allwinner,sun6i-a31-rtc",
313 sun6i_a31_rtc_clk_init);
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100314
Chen-Yu Tsai7cd1aca2018-12-03 22:58:18 +0800315static const struct sun6i_rtc_clk_data sun8i_a23_rtc_data = {
316 .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
317 .has_prescaler = 1,
318 .has_out_clk = 1,
319};
320
321static void __init sun8i_a23_rtc_clk_init(struct device_node *node)
322{
323 sun6i_rtc_clk_init(node, &sun8i_a23_rtc_data);
324}
325CLK_OF_DECLARE_DRIVER(sun8i_a23_rtc_clk, "allwinner,sun8i-a23-rtc",
326 sun8i_a23_rtc_clk_init);
327
328static const struct sun6i_rtc_clk_data sun8i_h3_rtc_data = {
329 .rc_osc_rate = 16000000,
330 .fixed_prescaler = 32,
331 .has_prescaler = 1,
332 .has_out_clk = 1,
333};
334
335static void __init sun8i_h3_rtc_clk_init(struct device_node *node)
336{
337 sun6i_rtc_clk_init(node, &sun8i_h3_rtc_data);
338}
339CLK_OF_DECLARE_DRIVER(sun8i_h3_rtc_clk, "allwinner,sun8i-h3-rtc",
340 sun8i_h3_rtc_clk_init);
341/* As far as we are concerned, clocks for H5 are the same as H3 */
342CLK_OF_DECLARE_DRIVER(sun50i_h5_rtc_clk, "allwinner,sun50i-h5-rtc",
343 sun8i_h3_rtc_clk_init);
344
345static const struct sun6i_rtc_clk_data sun8i_v3_rtc_data = {
346 .rc_osc_rate = 32000,
347 .has_out_clk = 1,
348};
349
350static void __init sun8i_v3_rtc_clk_init(struct device_node *node)
351{
352 sun6i_rtc_clk_init(node, &sun8i_v3_rtc_data);
353}
354CLK_OF_DECLARE_DRIVER(sun8i_v3_rtc_clk, "allwinner,sun8i-v3-rtc",
355 sun8i_v3_rtc_clk_init);
356
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800357static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
358{
359 struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
Maxime Riparda9422a12017-01-23 11:41:47 +0100360 irqreturn_t ret = IRQ_NONE;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800361 u32 val;
362
Maxime Riparda9422a12017-01-23 11:41:47 +0100363 spin_lock(&chip->lock);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800364 val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
365
366 if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
367 val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
368 writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
369
370 rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
371
Maxime Riparda9422a12017-01-23 11:41:47 +0100372 ret = IRQ_HANDLED;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800373 }
Maxime Riparda9422a12017-01-23 11:41:47 +0100374 spin_unlock(&chip->lock);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800375
Maxime Riparda9422a12017-01-23 11:41:47 +0100376 return ret;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800377}
378
379static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
380{
381 u32 alrm_val = 0;
382 u32 alrm_irq_val = 0;
383 u32 alrm_wake_val = 0;
Maxime Riparda9422a12017-01-23 11:41:47 +0100384 unsigned long flags;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800385
386 if (to) {
387 alrm_val = SUN6I_ALRM_EN_CNT_EN;
388 alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
389 alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
390 } else {
391 writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
392 chip->base + SUN6I_ALRM_IRQ_STA);
393 }
394
Maxime Riparda9422a12017-01-23 11:41:47 +0100395 spin_lock_irqsave(&chip->lock, flags);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800396 writel(alrm_val, chip->base + SUN6I_ALRM_EN);
397 writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
398 writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
Maxime Riparda9422a12017-01-23 11:41:47 +0100399 spin_unlock_irqrestore(&chip->lock, flags);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800400}
401
402static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
403{
404 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
405 u32 date, time;
406
407 /*
408 * read again in case it changes
409 */
410 do {
411 date = readl(chip->base + SUN6I_RTC_YMD);
412 time = readl(chip->base + SUN6I_RTC_HMS);
413 } while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
414 (time != readl(chip->base + SUN6I_RTC_HMS)));
415
416 rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time);
417 rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time);
418 rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
419
420 rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
421 rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date);
422 rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
423
424 rtc_tm->tm_mon -= 1;
425
426 /*
427 * switch from (data_year->min)-relative offset to
428 * a (1900)-relative one
429 */
430 rtc_tm->tm_year += SUN6I_YEAR_OFF;
431
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100432 return 0;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800433}
434
435static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
436{
437 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
Maxime Riparda9422a12017-01-23 11:41:47 +0100438 unsigned long flags;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800439 u32 alrm_st;
440 u32 alrm_en;
441
Maxime Riparda9422a12017-01-23 11:41:47 +0100442 spin_lock_irqsave(&chip->lock, flags);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800443 alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
444 alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
Maxime Riparda9422a12017-01-23 11:41:47 +0100445 spin_unlock_irqrestore(&chip->lock, flags);
446
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800447 wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
448 wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
449 rtc_time_to_tm(chip->alarm, &wkalrm->time);
450
451 return 0;
452}
453
454static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
455{
456 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
457 struct rtc_time *alrm_tm = &wkalrm->time;
458 struct rtc_time tm_now;
459 unsigned long time_now = 0;
460 unsigned long time_set = 0;
461 unsigned long time_gap = 0;
462 int ret = 0;
463
464 ret = sun6i_rtc_gettime(dev, &tm_now);
465 if (ret < 0) {
466 dev_err(dev, "Error in getting time\n");
467 return -EINVAL;
468 }
469
470 rtc_tm_to_time(alrm_tm, &time_set);
471 rtc_tm_to_time(&tm_now, &time_now);
472 if (time_set <= time_now) {
473 dev_err(dev, "Date to set in the past\n");
474 return -EINVAL;
475 }
476
477 time_gap = time_set - time_now;
478
479 if (time_gap > U32_MAX) {
480 dev_err(dev, "Date too far in the future\n");
481 return -EINVAL;
482 }
483
484 sun6i_rtc_setaie(0, chip);
485 writel(0, chip->base + SUN6I_ALRM_COUNTER);
486 usleep_range(100, 300);
487
488 writel(time_gap, chip->base + SUN6I_ALRM_COUNTER);
489 chip->alarm = time_set;
490
491 sun6i_rtc_setaie(wkalrm->enabled, chip);
492
493 return 0;
494}
495
496static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
497 unsigned int mask, unsigned int ms_timeout)
498{
499 const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
500 u32 reg;
501
502 do {
503 reg = readl(chip->base + offset);
504 reg &= mask;
505
506 if (!reg)
507 return 0;
508
509 } while (time_before(jiffies, timeout));
510
511 return -ETIMEDOUT;
512}
513
514static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
515{
516 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
517 u32 date = 0;
518 u32 time = 0;
519 int year;
520
521 year = rtc_tm->tm_year + 1900;
522 if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) {
523 dev_err(dev, "rtc only supports year in range %d - %d\n",
524 SUN6I_YEAR_MIN, SUN6I_YEAR_MAX);
525 return -EINVAL;
526 }
527
528 rtc_tm->tm_year -= SUN6I_YEAR_OFF;
529 rtc_tm->tm_mon += 1;
530
531 date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
532 SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
533 SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
534
535 if (is_leap_year(year))
536 date |= SUN6I_LEAP_SET_VALUE(1);
537
538 time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
539 SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
540 SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
541
542 /* Check whether registers are writable */
543 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
544 SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
545 dev_err(dev, "rtc is still busy.\n");
546 return -EBUSY;
547 }
548
549 writel(time, chip->base + SUN6I_RTC_HMS);
550
551 /*
552 * After writing the RTC HH-MM-SS register, the
553 * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
554 * be cleared until the real writing operation is finished
555 */
556
557 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
558 SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
559 dev_err(dev, "Failed to set rtc time.\n");
560 return -ETIMEDOUT;
561 }
562
563 writel(date, chip->base + SUN6I_RTC_YMD);
564
565 /*
566 * After writing the RTC YY-MM-DD register, the
567 * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
568 * be cleared until the real writing operation is finished
569 */
570
571 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
572 SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
573 dev_err(dev, "Failed to set rtc time.\n");
574 return -ETIMEDOUT;
575 }
576
577 return 0;
578}
579
580static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
581{
582 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
583
584 if (!enabled)
585 sun6i_rtc_setaie(enabled, chip);
586
587 return 0;
588}
589
590static const struct rtc_class_ops sun6i_rtc_ops = {
591 .read_time = sun6i_rtc_gettime,
592 .set_time = sun6i_rtc_settime,
593 .read_alarm = sun6i_rtc_getalarm,
594 .set_alarm = sun6i_rtc_setalarm,
595 .alarm_irq_enable = sun6i_rtc_alarm_irq_enable
596};
597
598static int sun6i_rtc_probe(struct platform_device *pdev)
599{
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100600 struct sun6i_rtc_dev *chip = sun6i_rtc;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800601 int ret;
602
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800603 if (!chip)
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100604 return -ENODEV;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800605
606 platform_set_drvdata(pdev, chip);
607 chip->dev = &pdev->dev;
608
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800609 chip->irq = platform_get_irq(pdev, 0);
610 if (chip->irq < 0) {
611 dev_err(&pdev->dev, "No IRQ resource\n");
612 return chip->irq;
613 }
614
615 ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
616 0, dev_name(&pdev->dev), chip);
617 if (ret) {
618 dev_err(&pdev->dev, "Could not request IRQ\n");
619 return ret;
620 }
621
622 /* clear the alarm counter value */
623 writel(0, chip->base + SUN6I_ALRM_COUNTER);
624
625 /* disable counter alarm */
626 writel(0, chip->base + SUN6I_ALRM_EN);
627
628 /* disable counter alarm interrupt */
629 writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
630
631 /* disable week alarm */
632 writel(0, chip->base + SUN6I_ALRM1_EN);
633
634 /* disable week alarm interrupt */
635 writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
636
637 /* clear counter alarm pending interrupts */
638 writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
639 chip->base + SUN6I_ALRM_IRQ_STA);
640
641 /* clear week alarm pending interrupts */
642 writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
643 chip->base + SUN6I_ALRM1_IRQ_STA);
644
645 /* disable alarm wakeup */
646 writel(0, chip->base + SUN6I_ALARM_CONFIG);
647
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100648 clk_prepare_enable(chip->losc);
Maxime Ripardfb61bb82017-01-23 11:41:48 +0100649
Maxime Ripard5dff3a32017-01-23 11:41:50 +0100650 chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-sun6i",
651 &sun6i_rtc_ops, THIS_MODULE);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800652 if (IS_ERR(chip->rtc)) {
653 dev_err(&pdev->dev, "unable to register device\n");
654 return PTR_ERR(chip->rtc);
655 }
656
657 dev_info(&pdev->dev, "RTC enabled\n");
658
659 return 0;
660}
661
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800662/*
663 * As far as RTC functionality goes, all models are the same. The
664 * datasheets claim that different models have different number of
665 * registers available for non-volatile storage, but experiments show
666 * that all SoCs have 16 registers available for this purpose.
667 */
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800668static const struct of_device_id sun6i_rtc_dt_ids[] = {
669 { .compatible = "allwinner,sun6i-a31-rtc" },
Chen-Yu Tsai7cd1aca2018-12-03 22:58:18 +0800670 { .compatible = "allwinner,sun8i-a23-rtc" },
671 { .compatible = "allwinner,sun8i-h3-rtc" },
672 { .compatible = "allwinner,sun8i-v3-rtc" },
673 { .compatible = "allwinner,sun50i-h5-rtc" },
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800674 { /* sentinel */ },
675};
676MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
677
678static struct platform_driver sun6i_rtc_driver = {
679 .probe = sun6i_rtc_probe,
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800680 .driver = {
681 .name = "sun6i-rtc",
682 .of_match_table = sun6i_rtc_dt_ids,
683 },
684};
Maxime Ripard37539412017-01-23 11:41:46 +0100685builtin_platform_driver(sun6i_rtc_driver);