blob: d1866d90b9ef23901959428bab17db51c5fd7d3d [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 Tsai9765d2d2014-08-26 11:54:55 +0800315static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
316{
317 struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
Maxime Riparda9422a12017-01-23 11:41:47 +0100318 irqreturn_t ret = IRQ_NONE;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800319 u32 val;
320
Maxime Riparda9422a12017-01-23 11:41:47 +0100321 spin_lock(&chip->lock);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800322 val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
323
324 if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
325 val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
326 writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
327
328 rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
329
Maxime Riparda9422a12017-01-23 11:41:47 +0100330 ret = IRQ_HANDLED;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800331 }
Maxime Riparda9422a12017-01-23 11:41:47 +0100332 spin_unlock(&chip->lock);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800333
Maxime Riparda9422a12017-01-23 11:41:47 +0100334 return ret;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800335}
336
337static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
338{
339 u32 alrm_val = 0;
340 u32 alrm_irq_val = 0;
341 u32 alrm_wake_val = 0;
Maxime Riparda9422a12017-01-23 11:41:47 +0100342 unsigned long flags;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800343
344 if (to) {
345 alrm_val = SUN6I_ALRM_EN_CNT_EN;
346 alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
347 alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
348 } else {
349 writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
350 chip->base + SUN6I_ALRM_IRQ_STA);
351 }
352
Maxime Riparda9422a12017-01-23 11:41:47 +0100353 spin_lock_irqsave(&chip->lock, flags);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800354 writel(alrm_val, chip->base + SUN6I_ALRM_EN);
355 writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
356 writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
Maxime Riparda9422a12017-01-23 11:41:47 +0100357 spin_unlock_irqrestore(&chip->lock, flags);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800358}
359
360static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
361{
362 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
363 u32 date, time;
364
365 /*
366 * read again in case it changes
367 */
368 do {
369 date = readl(chip->base + SUN6I_RTC_YMD);
370 time = readl(chip->base + SUN6I_RTC_HMS);
371 } while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
372 (time != readl(chip->base + SUN6I_RTC_HMS)));
373
374 rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time);
375 rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time);
376 rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
377
378 rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
379 rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date);
380 rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
381
382 rtc_tm->tm_mon -= 1;
383
384 /*
385 * switch from (data_year->min)-relative offset to
386 * a (1900)-relative one
387 */
388 rtc_tm->tm_year += SUN6I_YEAR_OFF;
389
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100390 return 0;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800391}
392
393static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
394{
395 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
Maxime Riparda9422a12017-01-23 11:41:47 +0100396 unsigned long flags;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800397 u32 alrm_st;
398 u32 alrm_en;
399
Maxime Riparda9422a12017-01-23 11:41:47 +0100400 spin_lock_irqsave(&chip->lock, flags);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800401 alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
402 alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
Maxime Riparda9422a12017-01-23 11:41:47 +0100403 spin_unlock_irqrestore(&chip->lock, flags);
404
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800405 wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
406 wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
407 rtc_time_to_tm(chip->alarm, &wkalrm->time);
408
409 return 0;
410}
411
412static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
413{
414 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
415 struct rtc_time *alrm_tm = &wkalrm->time;
416 struct rtc_time tm_now;
417 unsigned long time_now = 0;
418 unsigned long time_set = 0;
419 unsigned long time_gap = 0;
420 int ret = 0;
421
422 ret = sun6i_rtc_gettime(dev, &tm_now);
423 if (ret < 0) {
424 dev_err(dev, "Error in getting time\n");
425 return -EINVAL;
426 }
427
428 rtc_tm_to_time(alrm_tm, &time_set);
429 rtc_tm_to_time(&tm_now, &time_now);
430 if (time_set <= time_now) {
431 dev_err(dev, "Date to set in the past\n");
432 return -EINVAL;
433 }
434
435 time_gap = time_set - time_now;
436
437 if (time_gap > U32_MAX) {
438 dev_err(dev, "Date too far in the future\n");
439 return -EINVAL;
440 }
441
442 sun6i_rtc_setaie(0, chip);
443 writel(0, chip->base + SUN6I_ALRM_COUNTER);
444 usleep_range(100, 300);
445
446 writel(time_gap, chip->base + SUN6I_ALRM_COUNTER);
447 chip->alarm = time_set;
448
449 sun6i_rtc_setaie(wkalrm->enabled, chip);
450
451 return 0;
452}
453
454static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
455 unsigned int mask, unsigned int ms_timeout)
456{
457 const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
458 u32 reg;
459
460 do {
461 reg = readl(chip->base + offset);
462 reg &= mask;
463
464 if (!reg)
465 return 0;
466
467 } while (time_before(jiffies, timeout));
468
469 return -ETIMEDOUT;
470}
471
472static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
473{
474 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
475 u32 date = 0;
476 u32 time = 0;
477 int year;
478
479 year = rtc_tm->tm_year + 1900;
480 if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) {
481 dev_err(dev, "rtc only supports year in range %d - %d\n",
482 SUN6I_YEAR_MIN, SUN6I_YEAR_MAX);
483 return -EINVAL;
484 }
485
486 rtc_tm->tm_year -= SUN6I_YEAR_OFF;
487 rtc_tm->tm_mon += 1;
488
489 date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
490 SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
491 SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
492
493 if (is_leap_year(year))
494 date |= SUN6I_LEAP_SET_VALUE(1);
495
496 time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
497 SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
498 SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
499
500 /* Check whether registers are writable */
501 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
502 SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
503 dev_err(dev, "rtc is still busy.\n");
504 return -EBUSY;
505 }
506
507 writel(time, chip->base + SUN6I_RTC_HMS);
508
509 /*
510 * After writing the RTC HH-MM-SS register, the
511 * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
512 * be cleared until the real writing operation is finished
513 */
514
515 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
516 SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
517 dev_err(dev, "Failed to set rtc time.\n");
518 return -ETIMEDOUT;
519 }
520
521 writel(date, chip->base + SUN6I_RTC_YMD);
522
523 /*
524 * After writing the RTC YY-MM-DD register, the
525 * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
526 * be cleared until the real writing operation is finished
527 */
528
529 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
530 SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
531 dev_err(dev, "Failed to set rtc time.\n");
532 return -ETIMEDOUT;
533 }
534
535 return 0;
536}
537
538static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
539{
540 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
541
542 if (!enabled)
543 sun6i_rtc_setaie(enabled, chip);
544
545 return 0;
546}
547
548static const struct rtc_class_ops sun6i_rtc_ops = {
549 .read_time = sun6i_rtc_gettime,
550 .set_time = sun6i_rtc_settime,
551 .read_alarm = sun6i_rtc_getalarm,
552 .set_alarm = sun6i_rtc_setalarm,
553 .alarm_irq_enable = sun6i_rtc_alarm_irq_enable
554};
555
556static int sun6i_rtc_probe(struct platform_device *pdev)
557{
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100558 struct sun6i_rtc_dev *chip = sun6i_rtc;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800559 int ret;
560
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800561 if (!chip)
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100562 return -ENODEV;
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800563
564 platform_set_drvdata(pdev, chip);
565 chip->dev = &pdev->dev;
566
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800567 chip->irq = platform_get_irq(pdev, 0);
568 if (chip->irq < 0) {
569 dev_err(&pdev->dev, "No IRQ resource\n");
570 return chip->irq;
571 }
572
573 ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
574 0, dev_name(&pdev->dev), chip);
575 if (ret) {
576 dev_err(&pdev->dev, "Could not request IRQ\n");
577 return ret;
578 }
579
580 /* clear the alarm counter value */
581 writel(0, chip->base + SUN6I_ALRM_COUNTER);
582
583 /* disable counter alarm */
584 writel(0, chip->base + SUN6I_ALRM_EN);
585
586 /* disable counter alarm interrupt */
587 writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
588
589 /* disable week alarm */
590 writel(0, chip->base + SUN6I_ALRM1_EN);
591
592 /* disable week alarm interrupt */
593 writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
594
595 /* clear counter alarm pending interrupts */
596 writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
597 chip->base + SUN6I_ALRM_IRQ_STA);
598
599 /* clear week alarm pending interrupts */
600 writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
601 chip->base + SUN6I_ALRM1_IRQ_STA);
602
603 /* disable alarm wakeup */
604 writel(0, chip->base + SUN6I_ALARM_CONFIG);
605
Maxime Ripard3855c2c2017-01-23 11:41:49 +0100606 clk_prepare_enable(chip->losc);
Maxime Ripardfb61bb82017-01-23 11:41:48 +0100607
Maxime Ripard5dff3a32017-01-23 11:41:50 +0100608 chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-sun6i",
609 &sun6i_rtc_ops, THIS_MODULE);
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800610 if (IS_ERR(chip->rtc)) {
611 dev_err(&pdev->dev, "unable to register device\n");
612 return PTR_ERR(chip->rtc);
613 }
614
615 dev_info(&pdev->dev, "RTC enabled\n");
616
617 return 0;
618}
619
Chen-Yu Tsai403a3c32018-12-03 22:58:17 +0800620/*
621 * As far as RTC functionality goes, all models are the same. The
622 * datasheets claim that different models have different number of
623 * registers available for non-volatile storage, but experiments show
624 * that all SoCs have 16 registers available for this purpose.
625 */
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800626static const struct of_device_id sun6i_rtc_dt_ids[] = {
627 { .compatible = "allwinner,sun6i-a31-rtc" },
628 { /* sentinel */ },
629};
630MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
631
632static struct platform_driver sun6i_rtc_driver = {
633 .probe = sun6i_rtc_probe,
Chen-Yu Tsai9765d2d2014-08-26 11:54:55 +0800634 .driver = {
635 .name = "sun6i-rtc",
636 .of_match_table = sun6i_rtc_dt_ids,
637 },
638};
Maxime Ripard37539412017-01-23 11:41:46 +0100639builtin_platform_driver(sun6i_rtc_driver);