1 From 9b6e3291426efc69d1e8bf257721997f3eeb3009 Mon Sep 17 00:00:00 2001
2 From: Carlo Caione <carlo.caione@gmail.com>
3 Date: Wed, 16 Oct 2013 20:30:27 +0200
4 Subject: [PATCH] ARM: sun4i/sun7i: RTC driver
6 This patch introduces the driver for the RTC in the Allwinner A10 and
9 Signed-off-by: Carlo Caione <carlo.caione@gmail.com>
11 drivers/rtc/Kconfig | 7 +
12 drivers/rtc/Makefile | 1 +
13 drivers/rtc/rtc-sunxi.c | 487 ++++++++++++++++++++++++++++++++++++++++++++++++
14 3 files changed, 495 insertions(+)
15 create mode 100644 drivers/rtc/rtc-sunxi.c
17 diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
18 index 9654aa3..ef45e0b 100644
19 --- a/drivers/rtc/Kconfig
20 +++ b/drivers/rtc/Kconfig
21 @@ -1076,6 +1076,13 @@ config RTC_DRV_SUN4V
22 If you say Y here you will get support for the Hypervisor
23 based RTC on SUN4V systems.
26 + tristate "Allwinner sun4i/sun7i RTC"
27 + depends on ARCH_SUNXI
29 + If you say Y here you will get support for the RTC found on
32 config RTC_DRV_STARFIRE
35 diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
36 index 2dff3d2..8b52b5a 100644
37 --- a/drivers/rtc/Makefile
38 +++ b/drivers/rtc/Makefile
39 @@ -115,6 +115,7 @@ obj-$(CONFIG_RTC_DRV_STARFIRE) += rtc-starfire.o
40 obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o
41 obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o
42 obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o
43 +obj-$(CONFIG_RTC_DRV_SUNXI) += rtc-sunxi.o
44 obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o
45 obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
46 obj-$(CONFIG_RTC_DRV_TILE) += rtc-tile.o
47 diff --git a/drivers/rtc/rtc-sunxi.c b/drivers/rtc/rtc-sunxi.c
49 index 0000000..ccd48ae
51 +++ b/drivers/rtc/rtc-sunxi.c
54 + * An RTC driver for Allwinner A10/A20
56 + * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
58 + * This program is free software; you can redistribute it and/or modify
59 + * it under the terms of the GNU General Public License as published by
60 + * the Free Software Foundation; either version 2 of the License, or
61 + * (at your option) any later version.
63 + * This program is distributed in the hope that it will be useful, but WITHOUT
64 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
68 + * You should have received a copy of the GNU General Public License along
69 + * with this program; if not, write to the Free Software Foundation, Inc.,
70 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
73 +#include <linux/delay.h>
74 +#include <linux/err.h>
75 +#include <linux/fs.h>
76 +#include <linux/init.h>
77 +#include <linux/interrupt.h>
78 +#include <linux/io.h>
79 +#include <linux/kernel.h>
80 +#include <linux/module.h>
81 +#include <linux/of.h>
82 +#include <linux/of_address.h>
83 +#include <linux/of_device.h>
84 +#include <linux/platform_device.h>
85 +#include <linux/rtc.h>
86 +#include <linux/types.h>
88 +#define SUNXI_LOSC_CTRL 0x0000
89 +#define SUNXI_LOSC_CTRL_RTC_HMS_ACC BIT(8)
90 +#define SUNXI_LOSC_CTRL_RTC_YMD_ACC BIT(7)
92 +#define SUNXI_RTC_YMD 0x0004
94 +#define SUNXI_RTC_HMS 0x0008
96 +#define SUNXI_ALRM_DHMS 0x000c
98 +#define SUNXI_ALRM_EN 0x0014
99 +#define SUNXI_ALRM_EN_CNT_EN BIT(8)
101 +#define SUNXI_ALRM_IRQ_EN 0x0018
102 +#define SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
104 +#define SUNXI_ALRM_IRQ_STA 0x001c
105 +#define SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
107 +#define SUNXI_LOSC_CTRL_RTC_ACC \
108 + (SUNXI_LOSC_CTRL_RTC_HMS_ACC | SUNXI_LOSC_CTRL_RTC_YMD_ACC)
110 +#define SUNXI_MASK_DH 0x0000001f
111 +#define SUNXI_MASK_SM 0x0000003f
112 +#define SUNXI_MASK_M 0x0000000f
113 +#define SUNXI_MASK_LY 0x00000001
114 +#define SUNXI_MASK_D 0x00000ffe
115 +#define SUNXI_MASK_M 0x0000000f
117 +#define SUNXI_GET(x, mask, shift) (((x) & ((mask) << (shift))) \
120 +#define SUNXI_SET(x, mask, shift) (((x) & (mask)) << (shift))
122 +/* Get date values */
123 +#define SUNXI_DATE_GET_DAY_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 0)
124 +#define SUNXI_DATE_GET_MON_VALUE(x) SUNXI_GET(x, SUNXI_MASK_M, 8)
125 +#define SUNXI_DATE_GET_YEAR_VALUE(x, mask) SUNXI_GET(x, mask, 16)
127 +/* Get time values */
128 +#define SUNXI_TIME_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
129 +#define SUNXI_TIME_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
130 +#define SUNXI_TIME_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
132 +/* Get alarm values */
133 +#define SUNXI_ALRM_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
134 +#define SUNXI_ALRM_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
135 +#define SUNXI_ALRM_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
137 +/* Set date values */
138 +#define SUNXI_DATE_SET_DAY_VALUE(x) SUNXI_DATE_GET_DAY_VALUE(x)
139 +#define SUNXI_DATE_SET_MON_VALUE(x) SUNXI_SET(x, SUNXI_MASK_M, 8)
140 +#define SUNXI_DATE_SET_YEAR_VALUE(x, mask) SUNXI_SET(x, mask, 16)
141 +#define SUNXI_LEAP_SET_VALUE(x, shift) SUNXI_SET(x, SUNXI_MASK_LY, shift)
143 +/* Set time values */
144 +#define SUNXI_TIME_SET_SEC_VALUE(x) SUNXI_TIME_GET_SEC_VALUE(x)
145 +#define SUNXI_TIME_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
146 +#define SUNXI_TIME_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
148 +/* set alarm values */
149 +#define SUNXI_ALRM_SET_SEC_VALUE(x) SUNXI_ALRM_GET_SEC_VALUE(x)
150 +#define SUNXI_ALRM_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
151 +#define SUNXI_ALRM_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
152 +#define SUNXI_ALRM_SET_DAY_VALUE(x) SUNXI_SET(x, SUNXI_MASK_D, 21)
154 +/* time unit conversions */
155 +#define SEC_IN_MIN 60
156 +#define SEC_IN_HOUR (60 * SEC_IN_MIN)
157 +#define SEC_IN_DAY (24 * SEC_IN_HOUR)
159 +struct sunxi_rtc_data_year {
160 + unsigned int min; /* min year allowed */
161 + unsigned int max; /* max year allowed */
162 + unsigned int off; /* data year offset */
164 + unsigned char leap_shift; /* bit shift to get the leap year */
167 +static struct sunxi_rtc_data_year data_year_param[] = {
172 + .mask = 0x000000ff,
179 + .mask = 0x0000003f,
184 +struct sunxi_rtc_dev {
185 + struct rtc_device *rtc;
186 + struct device *dev;
187 + struct sunxi_rtc_data_year *data_year;
188 + void __iomem *base;
192 +static irqreturn_t sunxi_rtc_alarmirq(int irq, void *id)
194 + struct sunxi_rtc_dev *chip = (struct sunxi_rtc_dev *) id;
197 + val = readl(chip->base + SUNXI_ALRM_IRQ_STA);
199 + if (val & SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND) {
200 + val |= SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND;
201 + writel(val, chip->base + SUNXI_ALRM_IRQ_STA);
203 + rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
205 + return IRQ_HANDLED;
211 +static void sunxi_rtc_setaie(int to, struct sunxi_rtc_dev *chip)
214 + u32 alarm_irq_val = 0;
217 + alarm_val = readl(chip->base + SUNXI_ALRM_EN);
218 + alarm_val |= SUNXI_ALRM_EN_CNT_EN;
220 + alarm_irq_val = readl(chip->base + SUNXI_ALRM_IRQ_EN);
221 + alarm_irq_val |= SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN;
223 + writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND,
224 + chip->base + SUNXI_ALRM_IRQ_STA);
227 + writel(alarm_val, chip->base + SUNXI_ALRM_EN);
228 + writel(alarm_irq_val, chip->base + SUNXI_ALRM_IRQ_EN);
231 +static int sunxi_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
233 + struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
234 + struct rtc_time *alrm_tm = &alrm->time;
239 + alarm = readl(chip->base + SUNXI_ALRM_DHMS);
240 + date = readl(chip->base + SUNXI_RTC_YMD);
242 + alrm_tm->tm_sec = SUNXI_ALRM_GET_SEC_VALUE(alarm);
243 + alrm_tm->tm_min = SUNXI_ALRM_GET_MIN_VALUE(alarm);
244 + alrm_tm->tm_hour = SUNXI_ALRM_GET_HOUR_VALUE(alarm);
246 + alrm_tm->tm_mday = SUNXI_DATE_GET_DAY_VALUE(date);
247 + alrm_tm->tm_mon = SUNXI_DATE_GET_MON_VALUE(date);
248 + alrm_tm->tm_year = SUNXI_DATE_GET_YEAR_VALUE(date,
249 + chip->data_year->mask);
251 + alrm_tm->tm_year += chip->data_year->off;
252 + alrm_tm->tm_mon -= 1;
254 + alarm_en = readl(chip->base + SUNXI_ALRM_IRQ_EN);
255 + if (alarm_en & SUNXI_ALRM_EN_CNT_EN)
261 +static int sunxi_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
263 + struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
267 + /* read again if the system was mid-updated
269 + for (t = 0; t < 2; t++) {
270 + date = readl(chip->base + SUNXI_RTC_YMD);
271 + time = readl(chip->base + SUNXI_RTC_HMS);
273 + rtc_tm->tm_sec = SUNXI_TIME_GET_SEC_VALUE(time);
274 + rtc_tm->tm_min = SUNXI_TIME_GET_MIN_VALUE(time);
275 + rtc_tm->tm_hour = SUNXI_TIME_GET_HOUR_VALUE(time);
277 + rtc_tm->tm_mday = SUNXI_DATE_GET_DAY_VALUE(date);
278 + rtc_tm->tm_mon = SUNXI_DATE_GET_MON_VALUE(date);
279 + rtc_tm->tm_year = SUNXI_DATE_GET_YEAR_VALUE(date,
280 + chip->data_year->mask);
282 + if (rtc_tm->tm_sec == 0)
288 + rtc_tm->tm_year += chip->data_year->off;
289 + rtc_tm->tm_mon -= 1;
291 + return rtc_valid_tm(rtc_tm);
294 +static int sunxi_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
296 + struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
297 + struct rtc_time *alrm_tm = &alrm->time;
298 + struct rtc_time tm_now;
300 + unsigned long time_now = 0;
301 + unsigned long time_set = 0;
302 + unsigned long time_gap = 0;
303 + unsigned long time_gap_day = 0;
304 + unsigned long time_gap_hour = 0;
305 + unsigned long time_gap_min = 0;
308 + ret = sunxi_rtc_gettime(dev, &tm_now);
310 + dev_err(dev, "Error in getting time\n");
314 + rtc_tm_to_time(alrm_tm, &time_set);
315 + rtc_tm_to_time(&tm_now, &time_now);
316 + if (time_set <= time_now) {
317 + dev_err(dev, "Date to set in the past\n");
321 + time_gap = time_set - time_now;
322 + time_gap_day = time_gap / SEC_IN_DAY;
323 + time_gap -= time_gap_day * SEC_IN_DAY;
324 + time_gap_hour = time_gap / SEC_IN_HOUR;
325 + time_gap -= time_gap_hour * SEC_IN_HOUR;
326 + time_gap_min = time_gap / SEC_IN_MIN;
327 + time_gap -= time_gap_min * SEC_IN_MIN;
329 + if (time_gap_day > 255) {
330 + dev_err(dev, "Day must be in the range 0 - 255\n");
334 + sunxi_rtc_setaie(0, chip);
335 + writel(0, chip->base + SUNXI_ALRM_DHMS);
336 + usleep_range(100, 300);
338 + alarm = SUNXI_ALRM_SET_SEC_VALUE(time_gap) |
339 + SUNXI_ALRM_SET_MIN_VALUE(time_gap_min) |
340 + SUNXI_ALRM_SET_HOUR_VALUE(time_gap_hour) |
341 + SUNXI_ALRM_SET_DAY_VALUE(time_gap_day);
342 + writel(alarm, chip->base + SUNXI_ALRM_DHMS);
344 + writel(0, chip->base + SUNXI_ALRM_IRQ_EN);
345 + writel(SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN, chip->base + SUNXI_ALRM_IRQ_EN);
347 + sunxi_rtc_setaie(alrm->enabled, chip);
352 +static int sunxi_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
354 + struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
360 + year = rtc_tm->tm_year + 1900;
361 + if (year < chip->data_year->min || year > chip->data_year->max) {
362 + dev_err(dev, "rtc only supports year in range %d - %d\n",
363 + chip->data_year->min, chip->data_year->max);
367 + rtc_tm->tm_year -= chip->data_year->off;
368 + rtc_tm->tm_mon += 1;
370 + date = SUNXI_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
371 + SUNXI_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
372 + SUNXI_DATE_SET_YEAR_VALUE(rtc_tm->tm_year,
373 + chip->data_year->mask);
375 + if (is_leap_year(year))
376 + date |= SUNXI_LEAP_SET_VALUE(1, chip->data_year->leap_shift);
378 + time = SUNXI_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
379 + SUNXI_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
380 + SUNXI_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
382 + writel(0, chip->base + SUNXI_RTC_HMS);
383 + writel(0, chip->base + SUNXI_RTC_YMD);
385 + writel(time, chip->base + SUNXI_RTC_HMS);
387 + /* After writing the RCT HH-MM-SS register, the
388 + * SUNXI_LOSC_CTRL_RTC_HMS_ACC bit is set and it will be cleared until
389 + * the real writing operation is finished
391 + for (t = 0; t < 3; t++) {
392 + if ((readl(chip->base + SUNXI_LOSC_CTRL) &
393 + SUNXI_LOSC_CTRL_RTC_HMS_ACC) && --t)
399 + dev_err(dev, "Failed to set rtc time.\n");
403 + writel(date, chip->base + SUNXI_RTC_YMD);
405 + /* After writing the RCT YY-MM-DD register, the
406 + * SUNXI_LOSC_CTRL_RTC_YMD_ACC bit is set and it will be cleared until
407 + * the real writing operation is finished
409 + for (t = 0; t < 3; t++) {
410 + if ((readl(chip->base + SUNXI_LOSC_CTRL) &
411 + SUNXI_LOSC_CTRL_RTC_YMD_ACC) && --t)
417 + dev_err(dev, "Failed to set rtc date.\n");
421 + /* wait about 70us to make sure the the time is really written into
423 + usleep_range(70, 100);
428 +static int sunxi_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
430 + struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
433 + sunxi_rtc_setaie(enabled, chip);
438 +static const struct rtc_class_ops sunxi_rtc_ops = {
439 + .read_time = sunxi_rtc_gettime,
440 + .set_time = sunxi_rtc_settime,
441 + .read_alarm = sunxi_rtc_getalarm,
442 + .set_alarm = sunxi_rtc_setalarm,
443 + .alarm_irq_enable = sunxi_rtc_alarm_irq_enable
446 +static const struct of_device_id sunxi_rtc_dt_ids[] = {
447 + { .compatible = "allwinner,sun4i-rtc", .data = &data_year_param[0] },
448 + { .compatible = "allwinner,sun7i-a20-rtc", .data = &data_year_param[1] },
449 + { /* sentinel */ },
451 +MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
454 +static int sunxi_rtc_probe(struct platform_device *pdev)
456 + struct sunxi_rtc_dev *chip;
457 + struct resource *res;
458 + const struct of_device_id *of_id;
461 + chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
465 + platform_set_drvdata(pdev, chip);
466 + chip->dev = &pdev->dev;
468 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
469 + chip->base = devm_ioremap_resource(&pdev->dev, res);
470 + if (IS_ERR(chip->base))
471 + return PTR_ERR(chip->base);
473 + chip->irq = platform_get_irq(pdev, 0);
474 + if (chip->irq < 0) {
475 + dev_err(&pdev->dev, "No IRQ resource\n");
478 + ret = devm_request_irq(&pdev->dev, chip->irq, sunxi_rtc_alarmirq,
479 + 0, dev_name(&pdev->dev), chip);
481 + dev_err(&pdev->dev, "Could not request IRQ\n");
485 + of_id = of_match_device(sunxi_rtc_dt_ids, &pdev->dev);
487 + dev_err(&pdev->dev, "Unable to setup RTC data\n");
490 + chip->data_year = (struct sunxi_rtc_data_year *) of_id->data;
492 + /* clear the alarm count value */
493 + writel(0, chip->base + SUNXI_ALRM_DHMS);
495 + /* disable alarm, not generate irq pending */
496 + writel(0, chip->base + SUNXI_ALRM_EN);
498 + /* disable alarm week/cnt irq, unset to cpu */
499 + writel(0, chip->base + SUNXI_ALRM_IRQ_EN);
501 + /* clear alarm week/cnt irq pending */
502 + writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND, chip->base + SUNXI_ALRM_IRQ_STA);
504 + chip->rtc = rtc_device_register("rtc-sunxi", &pdev->dev,
505 + &sunxi_rtc_ops, THIS_MODULE);
506 + if (IS_ERR(chip->rtc)) {
507 + dev_err(&pdev->dev, "unable to register device\n");
508 + return PTR_ERR(chip->rtc);
511 + dev_info(&pdev->dev, "RTC enabled\n");
516 +static int sunxi_rtc_remove(struct platform_device *pdev)
518 + struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
520 + rtc_device_unregister(chip->rtc);
525 +static struct platform_driver sunxi_rtc_driver = {
526 + .probe = sunxi_rtc_probe,
527 + .remove = sunxi_rtc_remove,
529 + .name = "sunxi-rtc",
530 + .owner = THIS_MODULE,
531 + .of_match_table = sunxi_rtc_dt_ids,
535 +module_platform_driver(sunxi_rtc_driver);
537 +MODULE_DESCRIPTION("sunxi RTC driver");
538 +MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
539 +MODULE_LICENSE("GPL");