xref: /openbmc/linux/drivers/rtc/rtc-sun6i.c (revision 403a3c3dd0ec93c2504b94667d16485729fc0393)
19765d2d9SChen-Yu Tsai /*
29765d2d9SChen-Yu Tsai  * An RTC driver for Allwinner A31/A23
39765d2d9SChen-Yu Tsai  *
49765d2d9SChen-Yu Tsai  * Copyright (c) 2014, Chen-Yu Tsai <wens@csie.org>
59765d2d9SChen-Yu Tsai  *
69765d2d9SChen-Yu Tsai  * based on rtc-sunxi.c
79765d2d9SChen-Yu Tsai  *
89765d2d9SChen-Yu Tsai  * An RTC driver for Allwinner A10/A20
99765d2d9SChen-Yu Tsai  *
109765d2d9SChen-Yu Tsai  * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
119765d2d9SChen-Yu Tsai  *
129765d2d9SChen-Yu Tsai  * This program is free software; you can redistribute it and/or modify
139765d2d9SChen-Yu Tsai  * it under the terms of the GNU General Public License as published by
149765d2d9SChen-Yu Tsai  * the Free Software Foundation; either version 2 of the License, or
159765d2d9SChen-Yu Tsai  * (at your option) any later version.
169765d2d9SChen-Yu Tsai  *
179765d2d9SChen-Yu Tsai  * This program is distributed in the hope that it will be useful, but WITHOUT
189765d2d9SChen-Yu Tsai  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
199765d2d9SChen-Yu Tsai  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
209765d2d9SChen-Yu Tsai  * more details.
219765d2d9SChen-Yu Tsai  */
229765d2d9SChen-Yu Tsai 
233855c2c3SMaxime Ripard #include <linux/clk.h>
243855c2c3SMaxime Ripard #include <linux/clk-provider.h>
259765d2d9SChen-Yu Tsai #include <linux/delay.h>
269765d2d9SChen-Yu Tsai #include <linux/err.h>
279765d2d9SChen-Yu Tsai #include <linux/fs.h>
289765d2d9SChen-Yu Tsai #include <linux/init.h>
299765d2d9SChen-Yu Tsai #include <linux/interrupt.h>
309765d2d9SChen-Yu Tsai #include <linux/io.h>
319765d2d9SChen-Yu Tsai #include <linux/kernel.h>
329765d2d9SChen-Yu Tsai #include <linux/module.h>
339765d2d9SChen-Yu Tsai #include <linux/of.h>
349765d2d9SChen-Yu Tsai #include <linux/of_address.h>
359765d2d9SChen-Yu Tsai #include <linux/of_device.h>
369765d2d9SChen-Yu Tsai #include <linux/platform_device.h>
379765d2d9SChen-Yu Tsai #include <linux/rtc.h>
383855c2c3SMaxime Ripard #include <linux/slab.h>
399765d2d9SChen-Yu Tsai #include <linux/types.h>
409765d2d9SChen-Yu Tsai 
419765d2d9SChen-Yu Tsai /* Control register */
429765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL				0x0000
43fb61bb82SMaxime Ripard #define SUN6I_LOSC_CTRL_KEY			(0x16aa << 16)
449765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC		BIT(9)
459765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_RTC_HMS_ACC		BIT(8)
469765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_RTC_YMD_ACC		BIT(7)
47fb61bb82SMaxime Ripard #define SUN6I_LOSC_CTRL_EXT_OSC			BIT(0)
489765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_ACC_MASK		GENMASK(9, 7)
499765d2d9SChen-Yu Tsai 
503855c2c3SMaxime Ripard #define SUN6I_LOSC_CLK_PRESCAL			0x0008
513855c2c3SMaxime Ripard 
529765d2d9SChen-Yu Tsai /* RTC */
539765d2d9SChen-Yu Tsai #define SUN6I_RTC_YMD				0x0010
549765d2d9SChen-Yu Tsai #define SUN6I_RTC_HMS				0x0014
559765d2d9SChen-Yu Tsai 
569765d2d9SChen-Yu Tsai /* Alarm 0 (counter) */
579765d2d9SChen-Yu Tsai #define SUN6I_ALRM_COUNTER			0x0020
589765d2d9SChen-Yu Tsai #define SUN6I_ALRM_CUR_VAL			0x0024
599765d2d9SChen-Yu Tsai #define SUN6I_ALRM_EN				0x0028
609765d2d9SChen-Yu Tsai #define SUN6I_ALRM_EN_CNT_EN			BIT(0)
619765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_EN			0x002c
629765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN		BIT(0)
639765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_STA			0x0030
649765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND		BIT(0)
659765d2d9SChen-Yu Tsai 
669765d2d9SChen-Yu Tsai /* Alarm 1 (wall clock) */
679765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_EN				0x0044
689765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_EN			0x0048
699765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_STA			0x004c
709765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND	BIT(0)
719765d2d9SChen-Yu Tsai 
729765d2d9SChen-Yu Tsai /* Alarm config */
739765d2d9SChen-Yu Tsai #define SUN6I_ALARM_CONFIG			0x0050
749765d2d9SChen-Yu Tsai #define SUN6I_ALARM_CONFIG_WAKEUP		BIT(0)
759765d2d9SChen-Yu Tsai 
7617ecd246SMaxime Ripard #define SUN6I_LOSC_OUT_GATING			0x0060
7709018d4bSMichael Trimarchi #define SUN6I_LOSC_OUT_GATING_EN_OFFSET		0
7817ecd246SMaxime Ripard 
799765d2d9SChen-Yu Tsai /*
809765d2d9SChen-Yu Tsai  * Get date values
819765d2d9SChen-Yu Tsai  */
829765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_DAY_VALUE(x)		((x)  & 0x0000001f)
839765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_MON_VALUE(x)		(((x) & 0x00000f00) >> 8)
849765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_YEAR_VALUE(x)		(((x) & 0x003f0000) >> 16)
859765d2d9SChen-Yu Tsai #define SUN6I_LEAP_GET_VALUE(x)			(((x) & 0x00400000) >> 22)
869765d2d9SChen-Yu Tsai 
879765d2d9SChen-Yu Tsai /*
889765d2d9SChen-Yu Tsai  * Get time values
899765d2d9SChen-Yu Tsai  */
909765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_SEC_VALUE(x)		((x)  & 0x0000003f)
919765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_MIN_VALUE(x)		(((x) & 0x00003f00) >> 8)
929765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_HOUR_VALUE(x)		(((x) & 0x001f0000) >> 16)
939765d2d9SChen-Yu Tsai 
949765d2d9SChen-Yu Tsai /*
959765d2d9SChen-Yu Tsai  * Set date values
969765d2d9SChen-Yu Tsai  */
979765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_DAY_VALUE(x)		((x)       & 0x0000001f)
989765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_MON_VALUE(x)		((x) <<  8 & 0x00000f00)
999765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_YEAR_VALUE(x)		((x) << 16 & 0x003f0000)
1009765d2d9SChen-Yu Tsai #define SUN6I_LEAP_SET_VALUE(x)			((x) << 22 & 0x00400000)
1019765d2d9SChen-Yu Tsai 
1029765d2d9SChen-Yu Tsai /*
1039765d2d9SChen-Yu Tsai  * Set time values
1049765d2d9SChen-Yu Tsai  */
1059765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_SEC_VALUE(x)		((x)       & 0x0000003f)
1069765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_MIN_VALUE(x)		((x) <<  8 & 0x00003f00)
1079765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_HOUR_VALUE(x)		((x) << 16 & 0x001f0000)
1089765d2d9SChen-Yu Tsai 
1099765d2d9SChen-Yu Tsai /*
1109765d2d9SChen-Yu Tsai  * The year parameter passed to the driver is usually an offset relative to
1119765d2d9SChen-Yu Tsai  * the year 1900. This macro is used to convert this offset to another one
1129765d2d9SChen-Yu Tsai  * relative to the minimum year allowed by the hardware.
1139765d2d9SChen-Yu Tsai  *
1149765d2d9SChen-Yu Tsai  * The year range is 1970 - 2033. This range is selected to match Allwinner's
1159765d2d9SChen-Yu Tsai  * driver, even though it is somewhat limited.
1169765d2d9SChen-Yu Tsai  */
1179765d2d9SChen-Yu Tsai #define SUN6I_YEAR_MIN				1970
1189765d2d9SChen-Yu Tsai #define SUN6I_YEAR_MAX				2033
1199765d2d9SChen-Yu Tsai #define SUN6I_YEAR_OFF				(SUN6I_YEAR_MIN - 1900)
1209765d2d9SChen-Yu Tsai 
121*403a3c3dSChen-Yu Tsai /*
122*403a3c3dSChen-Yu Tsai  * There are other differences between models, including:
123*403a3c3dSChen-Yu Tsai  *
124*403a3c3dSChen-Yu Tsai  *   - number of GPIO pins that can be configured to hold a certain level
125*403a3c3dSChen-Yu Tsai  *   - crypto-key related registers (H5, H6)
126*403a3c3dSChen-Yu Tsai  *   - boot process related (super standby, secondary processor entry address)
127*403a3c3dSChen-Yu Tsai  *     registers (R40, H6)
128*403a3c3dSChen-Yu Tsai  *   - SYS power domain controls (R40)
129*403a3c3dSChen-Yu Tsai  *   - DCXO controls (H6)
130*403a3c3dSChen-Yu Tsai  *   - RC oscillator calibration (H6)
131*403a3c3dSChen-Yu Tsai  *
132*403a3c3dSChen-Yu Tsai  * These functions are not covered by this driver.
133*403a3c3dSChen-Yu Tsai  */
134*403a3c3dSChen-Yu Tsai struct sun6i_rtc_clk_data {
135*403a3c3dSChen-Yu Tsai 	unsigned long rc_osc_rate;
136*403a3c3dSChen-Yu Tsai 	unsigned int fixed_prescaler : 16;
137*403a3c3dSChen-Yu Tsai 	unsigned int has_prescaler : 1;
138*403a3c3dSChen-Yu Tsai 	unsigned int has_out_clk : 1;
139*403a3c3dSChen-Yu Tsai };
140*403a3c3dSChen-Yu Tsai 
1419765d2d9SChen-Yu Tsai struct sun6i_rtc_dev {
1429765d2d9SChen-Yu Tsai 	struct rtc_device *rtc;
1439765d2d9SChen-Yu Tsai 	struct device *dev;
144*403a3c3dSChen-Yu Tsai 	const struct sun6i_rtc_clk_data *data;
1459765d2d9SChen-Yu Tsai 	void __iomem *base;
1469765d2d9SChen-Yu Tsai 	int irq;
1479765d2d9SChen-Yu Tsai 	unsigned long alarm;
148a9422a19SMaxime Ripard 
1493855c2c3SMaxime Ripard 	struct clk_hw hw;
1503855c2c3SMaxime Ripard 	struct clk_hw *int_osc;
1513855c2c3SMaxime Ripard 	struct clk *losc;
15217ecd246SMaxime Ripard 	struct clk *ext_losc;
1533855c2c3SMaxime Ripard 
154a9422a19SMaxime Ripard 	spinlock_t lock;
1559765d2d9SChen-Yu Tsai };
1569765d2d9SChen-Yu Tsai 
1573855c2c3SMaxime Ripard static struct sun6i_rtc_dev *sun6i_rtc;
1583855c2c3SMaxime Ripard 
1593855c2c3SMaxime Ripard static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw,
1603855c2c3SMaxime Ripard 					       unsigned long parent_rate)
1613855c2c3SMaxime Ripard {
1623855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
163*403a3c3dSChen-Yu Tsai 	u32 val = 0;
1643855c2c3SMaxime Ripard 
1653855c2c3SMaxime Ripard 	val = readl(rtc->base + SUN6I_LOSC_CTRL);
1663855c2c3SMaxime Ripard 	if (val & SUN6I_LOSC_CTRL_EXT_OSC)
1673855c2c3SMaxime Ripard 		return parent_rate;
1683855c2c3SMaxime Ripard 
169*403a3c3dSChen-Yu Tsai 	if (rtc->data->fixed_prescaler)
170*403a3c3dSChen-Yu Tsai 		parent_rate /= rtc->data->fixed_prescaler;
171*403a3c3dSChen-Yu Tsai 
172*403a3c3dSChen-Yu Tsai 	if (rtc->data->has_prescaler) {
1733855c2c3SMaxime Ripard 		val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL);
1743855c2c3SMaxime Ripard 		val &= GENMASK(4, 0);
175*403a3c3dSChen-Yu Tsai 	}
1763855c2c3SMaxime Ripard 
1773855c2c3SMaxime Ripard 	return parent_rate / (val + 1);
1783855c2c3SMaxime Ripard }
1793855c2c3SMaxime Ripard 
1803855c2c3SMaxime Ripard static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw)
1813855c2c3SMaxime Ripard {
1823855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
1833855c2c3SMaxime Ripard 
1843855c2c3SMaxime Ripard 	return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC;
1853855c2c3SMaxime Ripard }
1863855c2c3SMaxime Ripard 
1873855c2c3SMaxime Ripard static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index)
1883855c2c3SMaxime Ripard {
1893855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
1903855c2c3SMaxime Ripard 	unsigned long flags;
1913855c2c3SMaxime Ripard 	u32 val;
1923855c2c3SMaxime Ripard 
1933855c2c3SMaxime Ripard 	if (index > 1)
1943855c2c3SMaxime Ripard 		return -EINVAL;
1953855c2c3SMaxime Ripard 
1963855c2c3SMaxime Ripard 	spin_lock_irqsave(&rtc->lock, flags);
1973855c2c3SMaxime Ripard 	val = readl(rtc->base + SUN6I_LOSC_CTRL);
1983855c2c3SMaxime Ripard 	val &= ~SUN6I_LOSC_CTRL_EXT_OSC;
1993855c2c3SMaxime Ripard 	val |= SUN6I_LOSC_CTRL_KEY;
2003855c2c3SMaxime Ripard 	val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0;
2013855c2c3SMaxime Ripard 	writel(val, rtc->base + SUN6I_LOSC_CTRL);
2023855c2c3SMaxime Ripard 	spin_unlock_irqrestore(&rtc->lock, flags);
2033855c2c3SMaxime Ripard 
2043855c2c3SMaxime Ripard 	return 0;
2053855c2c3SMaxime Ripard }
2063855c2c3SMaxime Ripard 
2073855c2c3SMaxime Ripard static const struct clk_ops sun6i_rtc_osc_ops = {
2083855c2c3SMaxime Ripard 	.recalc_rate	= sun6i_rtc_osc_recalc_rate,
2093855c2c3SMaxime Ripard 
2103855c2c3SMaxime Ripard 	.get_parent	= sun6i_rtc_osc_get_parent,
2113855c2c3SMaxime Ripard 	.set_parent	= sun6i_rtc_osc_set_parent,
2123855c2c3SMaxime Ripard };
2133855c2c3SMaxime Ripard 
214*403a3c3dSChen-Yu Tsai static void __init sun6i_rtc_clk_init(struct device_node *node,
215*403a3c3dSChen-Yu Tsai 				      const struct sun6i_rtc_clk_data *data)
2163855c2c3SMaxime Ripard {
2173855c2c3SMaxime Ripard 	struct clk_hw_onecell_data *clk_data;
2183855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc;
2193855c2c3SMaxime Ripard 	struct clk_init_data init = {
2203855c2c3SMaxime Ripard 		.ops		= &sun6i_rtc_osc_ops,
221459b6ea0SChen-Yu Tsai 		.name		= "losc",
2223855c2c3SMaxime Ripard 	};
22317ecd246SMaxime Ripard 	const char *clkout_name = "osc32k-out";
2243855c2c3SMaxime Ripard 	const char *parents[2];
2253855c2c3SMaxime Ripard 
2263855c2c3SMaxime Ripard 	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
2273855c2c3SMaxime Ripard 	if (!rtc)
2283855c2c3SMaxime Ripard 		return;
2293855c2c3SMaxime Ripard 
230*403a3c3dSChen-Yu Tsai 	rtc->data = data;
231725e0e15SGustavo A. R. Silva 	clk_data = kzalloc(struct_size(clk_data, hws, 2), GFP_KERNEL);
232e9982024SColin Ian King 	if (!clk_data) {
233e9982024SColin Ian King 		kfree(rtc);
2343855c2c3SMaxime Ripard 		return;
235e9982024SColin Ian King 	}
236319ff835SAlexey Klimov 
2373855c2c3SMaxime Ripard 	spin_lock_init(&rtc->lock);
2383855c2c3SMaxime Ripard 
2393855c2c3SMaxime Ripard 	rtc->base = of_io_request_and_map(node, 0, of_node_full_name(node));
240aaa65a9cSWei Yongjun 	if (IS_ERR(rtc->base)) {
2413855c2c3SMaxime Ripard 		pr_crit("Can't map RTC registers");
2421a37c348SColin Ian King 		goto err;
2433855c2c3SMaxime Ripard 	}
2443855c2c3SMaxime Ripard 
2453855c2c3SMaxime Ripard 	/* Switch to the external, more precise, oscillator */
2463855c2c3SMaxime Ripard 	writel(SUN6I_LOSC_CTRL_KEY | SUN6I_LOSC_CTRL_EXT_OSC,
2473855c2c3SMaxime Ripard 	       rtc->base + SUN6I_LOSC_CTRL);
2483855c2c3SMaxime Ripard 
24915829cf4SChen-Yu Tsai 	/* Yes, I know, this is ugly. */
25015829cf4SChen-Yu Tsai 	sun6i_rtc = rtc;
25115829cf4SChen-Yu Tsai 
2523855c2c3SMaxime Ripard 	/* Deal with old DTs */
2533855c2c3SMaxime Ripard 	if (!of_get_property(node, "clocks", NULL))
2541a37c348SColin Ian King 		goto err;
2553855c2c3SMaxime Ripard 
2563855c2c3SMaxime Ripard 	rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL,
2573855c2c3SMaxime Ripard 								"rtc-int-osc",
2583855c2c3SMaxime Ripard 								NULL, 0,
259*403a3c3dSChen-Yu Tsai 								rtc->data->rc_osc_rate,
2603855c2c3SMaxime Ripard 								300000000);
2613855c2c3SMaxime Ripard 	if (IS_ERR(rtc->int_osc)) {
2623855c2c3SMaxime Ripard 		pr_crit("Couldn't register the internal oscillator\n");
2633855c2c3SMaxime Ripard 		return;
2643855c2c3SMaxime Ripard 	}
2653855c2c3SMaxime Ripard 
2663855c2c3SMaxime Ripard 	parents[0] = clk_hw_get_name(rtc->int_osc);
2673855c2c3SMaxime Ripard 	parents[1] = of_clk_get_parent_name(node, 0);
2683855c2c3SMaxime Ripard 
2693855c2c3SMaxime Ripard 	rtc->hw.init = &init;
2703855c2c3SMaxime Ripard 
2713855c2c3SMaxime Ripard 	init.parent_names = parents;
2723855c2c3SMaxime Ripard 	init.num_parents = of_clk_get_parent_count(node) + 1;
27317ecd246SMaxime Ripard 	of_property_read_string_index(node, "clock-output-names", 0,
27417ecd246SMaxime Ripard 				      &init.name);
2753855c2c3SMaxime Ripard 
2763855c2c3SMaxime Ripard 	rtc->losc = clk_register(NULL, &rtc->hw);
2773855c2c3SMaxime Ripard 	if (IS_ERR(rtc->losc)) {
2783855c2c3SMaxime Ripard 		pr_crit("Couldn't register the LOSC clock\n");
2793855c2c3SMaxime Ripard 		return;
2803855c2c3SMaxime Ripard 	}
2813855c2c3SMaxime Ripard 
28217ecd246SMaxime Ripard 	of_property_read_string_index(node, "clock-output-names", 1,
28317ecd246SMaxime Ripard 				      &clkout_name);
28417ecd246SMaxime Ripard 	rtc->ext_losc = clk_register_gate(NULL, clkout_name, rtc->hw.init->name,
28517ecd246SMaxime Ripard 					  0, rtc->base + SUN6I_LOSC_OUT_GATING,
28609018d4bSMichael Trimarchi 					  SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0,
28717ecd246SMaxime Ripard 					  &rtc->lock);
28817ecd246SMaxime Ripard 	if (IS_ERR(rtc->ext_losc)) {
28917ecd246SMaxime Ripard 		pr_crit("Couldn't register the LOSC external gate\n");
29017ecd246SMaxime Ripard 		return;
29117ecd246SMaxime Ripard 	}
29217ecd246SMaxime Ripard 
29317ecd246SMaxime Ripard 	clk_data->num = 2;
2943855c2c3SMaxime Ripard 	clk_data->hws[0] = &rtc->hw;
29517ecd246SMaxime Ripard 	clk_data->hws[1] = __clk_get_hw(rtc->ext_losc);
2963855c2c3SMaxime Ripard 	of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
2971a37c348SColin Ian King 	return;
2981a37c348SColin Ian King 
2991a37c348SColin Ian King err:
3001a37c348SColin Ian King 	kfree(clk_data);
3013855c2c3SMaxime Ripard }
302*403a3c3dSChen-Yu Tsai 
303*403a3c3dSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun6i_a31_rtc_data = {
304*403a3c3dSChen-Yu Tsai 	.rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
305*403a3c3dSChen-Yu Tsai 	.has_prescaler = 1,
306*403a3c3dSChen-Yu Tsai };
307*403a3c3dSChen-Yu Tsai 
308*403a3c3dSChen-Yu Tsai static void __init sun6i_a31_rtc_clk_init(struct device_node *node)
309*403a3c3dSChen-Yu Tsai {
310*403a3c3dSChen-Yu Tsai 	sun6i_rtc_clk_init(node, &sun6i_a31_rtc_data);
311*403a3c3dSChen-Yu Tsai }
312*403a3c3dSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun6i_a31_rtc_clk, "allwinner,sun6i-a31-rtc",
313*403a3c3dSChen-Yu Tsai 		      sun6i_a31_rtc_clk_init);
3143855c2c3SMaxime Ripard 
3159765d2d9SChen-Yu Tsai static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
3169765d2d9SChen-Yu Tsai {
3179765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
318a9422a19SMaxime Ripard 	irqreturn_t ret = IRQ_NONE;
3199765d2d9SChen-Yu Tsai 	u32 val;
3209765d2d9SChen-Yu Tsai 
321a9422a19SMaxime Ripard 	spin_lock(&chip->lock);
3229765d2d9SChen-Yu Tsai 	val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
3239765d2d9SChen-Yu Tsai 
3249765d2d9SChen-Yu Tsai 	if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
3259765d2d9SChen-Yu Tsai 		val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
3269765d2d9SChen-Yu Tsai 		writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
3279765d2d9SChen-Yu Tsai 
3289765d2d9SChen-Yu Tsai 		rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
3299765d2d9SChen-Yu Tsai 
330a9422a19SMaxime Ripard 		ret = IRQ_HANDLED;
3319765d2d9SChen-Yu Tsai 	}
332a9422a19SMaxime Ripard 	spin_unlock(&chip->lock);
3339765d2d9SChen-Yu Tsai 
334a9422a19SMaxime Ripard 	return ret;
3359765d2d9SChen-Yu Tsai }
3369765d2d9SChen-Yu Tsai 
3379765d2d9SChen-Yu Tsai static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
3389765d2d9SChen-Yu Tsai {
3399765d2d9SChen-Yu Tsai 	u32 alrm_val = 0;
3409765d2d9SChen-Yu Tsai 	u32 alrm_irq_val = 0;
3419765d2d9SChen-Yu Tsai 	u32 alrm_wake_val = 0;
342a9422a19SMaxime Ripard 	unsigned long flags;
3439765d2d9SChen-Yu Tsai 
3449765d2d9SChen-Yu Tsai 	if (to) {
3459765d2d9SChen-Yu Tsai 		alrm_val = SUN6I_ALRM_EN_CNT_EN;
3469765d2d9SChen-Yu Tsai 		alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
3479765d2d9SChen-Yu Tsai 		alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
3489765d2d9SChen-Yu Tsai 	} else {
3499765d2d9SChen-Yu Tsai 		writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
3509765d2d9SChen-Yu Tsai 		       chip->base + SUN6I_ALRM_IRQ_STA);
3519765d2d9SChen-Yu Tsai 	}
3529765d2d9SChen-Yu Tsai 
353a9422a19SMaxime Ripard 	spin_lock_irqsave(&chip->lock, flags);
3549765d2d9SChen-Yu Tsai 	writel(alrm_val, chip->base + SUN6I_ALRM_EN);
3559765d2d9SChen-Yu Tsai 	writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
3569765d2d9SChen-Yu Tsai 	writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
357a9422a19SMaxime Ripard 	spin_unlock_irqrestore(&chip->lock, flags);
3589765d2d9SChen-Yu Tsai }
3599765d2d9SChen-Yu Tsai 
3609765d2d9SChen-Yu Tsai static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
3619765d2d9SChen-Yu Tsai {
3629765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
3639765d2d9SChen-Yu Tsai 	u32 date, time;
3649765d2d9SChen-Yu Tsai 
3659765d2d9SChen-Yu Tsai 	/*
3669765d2d9SChen-Yu Tsai 	 * read again in case it changes
3679765d2d9SChen-Yu Tsai 	 */
3689765d2d9SChen-Yu Tsai 	do {
3699765d2d9SChen-Yu Tsai 		date = readl(chip->base + SUN6I_RTC_YMD);
3709765d2d9SChen-Yu Tsai 		time = readl(chip->base + SUN6I_RTC_HMS);
3719765d2d9SChen-Yu Tsai 	} while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
3729765d2d9SChen-Yu Tsai 		 (time != readl(chip->base + SUN6I_RTC_HMS)));
3739765d2d9SChen-Yu Tsai 
3749765d2d9SChen-Yu Tsai 	rtc_tm->tm_sec  = SUN6I_TIME_GET_SEC_VALUE(time);
3759765d2d9SChen-Yu Tsai 	rtc_tm->tm_min  = SUN6I_TIME_GET_MIN_VALUE(time);
3769765d2d9SChen-Yu Tsai 	rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
3779765d2d9SChen-Yu Tsai 
3789765d2d9SChen-Yu Tsai 	rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
3799765d2d9SChen-Yu Tsai 	rtc_tm->tm_mon  = SUN6I_DATE_GET_MON_VALUE(date);
3809765d2d9SChen-Yu Tsai 	rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
3819765d2d9SChen-Yu Tsai 
3829765d2d9SChen-Yu Tsai 	rtc_tm->tm_mon  -= 1;
3839765d2d9SChen-Yu Tsai 
3849765d2d9SChen-Yu Tsai 	/*
3859765d2d9SChen-Yu Tsai 	 * switch from (data_year->min)-relative offset to
3869765d2d9SChen-Yu Tsai 	 * a (1900)-relative one
3879765d2d9SChen-Yu Tsai 	 */
3889765d2d9SChen-Yu Tsai 	rtc_tm->tm_year += SUN6I_YEAR_OFF;
3899765d2d9SChen-Yu Tsai 
39022652ba7SAlexandre Belloni 	return 0;
3919765d2d9SChen-Yu Tsai }
3929765d2d9SChen-Yu Tsai 
3939765d2d9SChen-Yu Tsai static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
3949765d2d9SChen-Yu Tsai {
3959765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
396a9422a19SMaxime Ripard 	unsigned long flags;
3979765d2d9SChen-Yu Tsai 	u32 alrm_st;
3989765d2d9SChen-Yu Tsai 	u32 alrm_en;
3999765d2d9SChen-Yu Tsai 
400a9422a19SMaxime Ripard 	spin_lock_irqsave(&chip->lock, flags);
4019765d2d9SChen-Yu Tsai 	alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
4029765d2d9SChen-Yu Tsai 	alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
403a9422a19SMaxime Ripard 	spin_unlock_irqrestore(&chip->lock, flags);
404a9422a19SMaxime Ripard 
4059765d2d9SChen-Yu Tsai 	wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
4069765d2d9SChen-Yu Tsai 	wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
4079765d2d9SChen-Yu Tsai 	rtc_time_to_tm(chip->alarm, &wkalrm->time);
4089765d2d9SChen-Yu Tsai 
4099765d2d9SChen-Yu Tsai 	return 0;
4109765d2d9SChen-Yu Tsai }
4119765d2d9SChen-Yu Tsai 
4129765d2d9SChen-Yu Tsai static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
4139765d2d9SChen-Yu Tsai {
4149765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
4159765d2d9SChen-Yu Tsai 	struct rtc_time *alrm_tm = &wkalrm->time;
4169765d2d9SChen-Yu Tsai 	struct rtc_time tm_now;
4179765d2d9SChen-Yu Tsai 	unsigned long time_now = 0;
4189765d2d9SChen-Yu Tsai 	unsigned long time_set = 0;
4199765d2d9SChen-Yu Tsai 	unsigned long time_gap = 0;
4209765d2d9SChen-Yu Tsai 	int ret = 0;
4219765d2d9SChen-Yu Tsai 
4229765d2d9SChen-Yu Tsai 	ret = sun6i_rtc_gettime(dev, &tm_now);
4239765d2d9SChen-Yu Tsai 	if (ret < 0) {
4249765d2d9SChen-Yu Tsai 		dev_err(dev, "Error in getting time\n");
4259765d2d9SChen-Yu Tsai 		return -EINVAL;
4269765d2d9SChen-Yu Tsai 	}
4279765d2d9SChen-Yu Tsai 
4289765d2d9SChen-Yu Tsai 	rtc_tm_to_time(alrm_tm, &time_set);
4299765d2d9SChen-Yu Tsai 	rtc_tm_to_time(&tm_now, &time_now);
4309765d2d9SChen-Yu Tsai 	if (time_set <= time_now) {
4319765d2d9SChen-Yu Tsai 		dev_err(dev, "Date to set in the past\n");
4329765d2d9SChen-Yu Tsai 		return -EINVAL;
4339765d2d9SChen-Yu Tsai 	}
4349765d2d9SChen-Yu Tsai 
4359765d2d9SChen-Yu Tsai 	time_gap = time_set - time_now;
4369765d2d9SChen-Yu Tsai 
4379765d2d9SChen-Yu Tsai 	if (time_gap > U32_MAX) {
4389765d2d9SChen-Yu Tsai 		dev_err(dev, "Date too far in the future\n");
4399765d2d9SChen-Yu Tsai 		return -EINVAL;
4409765d2d9SChen-Yu Tsai 	}
4419765d2d9SChen-Yu Tsai 
4429765d2d9SChen-Yu Tsai 	sun6i_rtc_setaie(0, chip);
4439765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_COUNTER);
4449765d2d9SChen-Yu Tsai 	usleep_range(100, 300);
4459765d2d9SChen-Yu Tsai 
4469765d2d9SChen-Yu Tsai 	writel(time_gap, chip->base + SUN6I_ALRM_COUNTER);
4479765d2d9SChen-Yu Tsai 	chip->alarm = time_set;
4489765d2d9SChen-Yu Tsai 
4499765d2d9SChen-Yu Tsai 	sun6i_rtc_setaie(wkalrm->enabled, chip);
4509765d2d9SChen-Yu Tsai 
4519765d2d9SChen-Yu Tsai 	return 0;
4529765d2d9SChen-Yu Tsai }
4539765d2d9SChen-Yu Tsai 
4549765d2d9SChen-Yu Tsai static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
4559765d2d9SChen-Yu Tsai 			  unsigned int mask, unsigned int ms_timeout)
4569765d2d9SChen-Yu Tsai {
4579765d2d9SChen-Yu Tsai 	const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
4589765d2d9SChen-Yu Tsai 	u32 reg;
4599765d2d9SChen-Yu Tsai 
4609765d2d9SChen-Yu Tsai 	do {
4619765d2d9SChen-Yu Tsai 		reg = readl(chip->base + offset);
4629765d2d9SChen-Yu Tsai 		reg &= mask;
4639765d2d9SChen-Yu Tsai 
4649765d2d9SChen-Yu Tsai 		if (!reg)
4659765d2d9SChen-Yu Tsai 			return 0;
4669765d2d9SChen-Yu Tsai 
4679765d2d9SChen-Yu Tsai 	} while (time_before(jiffies, timeout));
4689765d2d9SChen-Yu Tsai 
4699765d2d9SChen-Yu Tsai 	return -ETIMEDOUT;
4709765d2d9SChen-Yu Tsai }
4719765d2d9SChen-Yu Tsai 
4729765d2d9SChen-Yu Tsai static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
4739765d2d9SChen-Yu Tsai {
4749765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
4759765d2d9SChen-Yu Tsai 	u32 date = 0;
4769765d2d9SChen-Yu Tsai 	u32 time = 0;
4779765d2d9SChen-Yu Tsai 	int year;
4789765d2d9SChen-Yu Tsai 
4799765d2d9SChen-Yu Tsai 	year = rtc_tm->tm_year + 1900;
4809765d2d9SChen-Yu Tsai 	if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) {
4819765d2d9SChen-Yu Tsai 		dev_err(dev, "rtc only supports year in range %d - %d\n",
4829765d2d9SChen-Yu Tsai 			SUN6I_YEAR_MIN, SUN6I_YEAR_MAX);
4839765d2d9SChen-Yu Tsai 		return -EINVAL;
4849765d2d9SChen-Yu Tsai 	}
4859765d2d9SChen-Yu Tsai 
4869765d2d9SChen-Yu Tsai 	rtc_tm->tm_year -= SUN6I_YEAR_OFF;
4879765d2d9SChen-Yu Tsai 	rtc_tm->tm_mon += 1;
4889765d2d9SChen-Yu Tsai 
4899765d2d9SChen-Yu Tsai 	date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
4909765d2d9SChen-Yu Tsai 		SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon)  |
4919765d2d9SChen-Yu Tsai 		SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
4929765d2d9SChen-Yu Tsai 
4939765d2d9SChen-Yu Tsai 	if (is_leap_year(year))
4949765d2d9SChen-Yu Tsai 		date |= SUN6I_LEAP_SET_VALUE(1);
4959765d2d9SChen-Yu Tsai 
4969765d2d9SChen-Yu Tsai 	time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec)  |
4979765d2d9SChen-Yu Tsai 		SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min)  |
4989765d2d9SChen-Yu Tsai 		SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
4999765d2d9SChen-Yu Tsai 
5009765d2d9SChen-Yu Tsai 	/* Check whether registers are writable */
5019765d2d9SChen-Yu Tsai 	if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
5029765d2d9SChen-Yu Tsai 			   SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
5039765d2d9SChen-Yu Tsai 		dev_err(dev, "rtc is still busy.\n");
5049765d2d9SChen-Yu Tsai 		return -EBUSY;
5059765d2d9SChen-Yu Tsai 	}
5069765d2d9SChen-Yu Tsai 
5079765d2d9SChen-Yu Tsai 	writel(time, chip->base + SUN6I_RTC_HMS);
5089765d2d9SChen-Yu Tsai 
5099765d2d9SChen-Yu Tsai 	/*
5109765d2d9SChen-Yu Tsai 	 * After writing the RTC HH-MM-SS register, the
5119765d2d9SChen-Yu Tsai 	 * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
5129765d2d9SChen-Yu Tsai 	 * be cleared until the real writing operation is finished
5139765d2d9SChen-Yu Tsai 	 */
5149765d2d9SChen-Yu Tsai 
5159765d2d9SChen-Yu Tsai 	if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
5169765d2d9SChen-Yu Tsai 			   SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
5179765d2d9SChen-Yu Tsai 		dev_err(dev, "Failed to set rtc time.\n");
5189765d2d9SChen-Yu Tsai 		return -ETIMEDOUT;
5199765d2d9SChen-Yu Tsai 	}
5209765d2d9SChen-Yu Tsai 
5219765d2d9SChen-Yu Tsai 	writel(date, chip->base + SUN6I_RTC_YMD);
5229765d2d9SChen-Yu Tsai 
5239765d2d9SChen-Yu Tsai 	/*
5249765d2d9SChen-Yu Tsai 	 * After writing the RTC YY-MM-DD register, the
5259765d2d9SChen-Yu Tsai 	 * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
5269765d2d9SChen-Yu Tsai 	 * be cleared until the real writing operation is finished
5279765d2d9SChen-Yu Tsai 	 */
5289765d2d9SChen-Yu Tsai 
5299765d2d9SChen-Yu Tsai 	if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
5309765d2d9SChen-Yu Tsai 			   SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
5319765d2d9SChen-Yu Tsai 		dev_err(dev, "Failed to set rtc time.\n");
5329765d2d9SChen-Yu Tsai 		return -ETIMEDOUT;
5339765d2d9SChen-Yu Tsai 	}
5349765d2d9SChen-Yu Tsai 
5359765d2d9SChen-Yu Tsai 	return 0;
5369765d2d9SChen-Yu Tsai }
5379765d2d9SChen-Yu Tsai 
5389765d2d9SChen-Yu Tsai static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
5399765d2d9SChen-Yu Tsai {
5409765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
5419765d2d9SChen-Yu Tsai 
5429765d2d9SChen-Yu Tsai 	if (!enabled)
5439765d2d9SChen-Yu Tsai 		sun6i_rtc_setaie(enabled, chip);
5449765d2d9SChen-Yu Tsai 
5459765d2d9SChen-Yu Tsai 	return 0;
5469765d2d9SChen-Yu Tsai }
5479765d2d9SChen-Yu Tsai 
5489765d2d9SChen-Yu Tsai static const struct rtc_class_ops sun6i_rtc_ops = {
5499765d2d9SChen-Yu Tsai 	.read_time		= sun6i_rtc_gettime,
5509765d2d9SChen-Yu Tsai 	.set_time		= sun6i_rtc_settime,
5519765d2d9SChen-Yu Tsai 	.read_alarm		= sun6i_rtc_getalarm,
5529765d2d9SChen-Yu Tsai 	.set_alarm		= sun6i_rtc_setalarm,
5539765d2d9SChen-Yu Tsai 	.alarm_irq_enable	= sun6i_rtc_alarm_irq_enable
5549765d2d9SChen-Yu Tsai };
5559765d2d9SChen-Yu Tsai 
5569765d2d9SChen-Yu Tsai static int sun6i_rtc_probe(struct platform_device *pdev)
5579765d2d9SChen-Yu Tsai {
5583855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *chip = sun6i_rtc;
5599765d2d9SChen-Yu Tsai 	int ret;
5609765d2d9SChen-Yu Tsai 
5619765d2d9SChen-Yu Tsai 	if (!chip)
5623855c2c3SMaxime Ripard 		return -ENODEV;
5639765d2d9SChen-Yu Tsai 
5649765d2d9SChen-Yu Tsai 	platform_set_drvdata(pdev, chip);
5659765d2d9SChen-Yu Tsai 	chip->dev = &pdev->dev;
5669765d2d9SChen-Yu Tsai 
5679765d2d9SChen-Yu Tsai 	chip->irq = platform_get_irq(pdev, 0);
5689765d2d9SChen-Yu Tsai 	if (chip->irq < 0) {
5699765d2d9SChen-Yu Tsai 		dev_err(&pdev->dev, "No IRQ resource\n");
5709765d2d9SChen-Yu Tsai 		return chip->irq;
5719765d2d9SChen-Yu Tsai 	}
5729765d2d9SChen-Yu Tsai 
5739765d2d9SChen-Yu Tsai 	ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
5749765d2d9SChen-Yu Tsai 			       0, dev_name(&pdev->dev), chip);
5759765d2d9SChen-Yu Tsai 	if (ret) {
5769765d2d9SChen-Yu Tsai 		dev_err(&pdev->dev, "Could not request IRQ\n");
5779765d2d9SChen-Yu Tsai 		return ret;
5789765d2d9SChen-Yu Tsai 	}
5799765d2d9SChen-Yu Tsai 
5809765d2d9SChen-Yu Tsai 	/* clear the alarm counter value */
5819765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_COUNTER);
5829765d2d9SChen-Yu Tsai 
5839765d2d9SChen-Yu Tsai 	/* disable counter alarm */
5849765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_EN);
5859765d2d9SChen-Yu Tsai 
5869765d2d9SChen-Yu Tsai 	/* disable counter alarm interrupt */
5879765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
5889765d2d9SChen-Yu Tsai 
5899765d2d9SChen-Yu Tsai 	/* disable week alarm */
5909765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM1_EN);
5919765d2d9SChen-Yu Tsai 
5929765d2d9SChen-Yu Tsai 	/* disable week alarm interrupt */
5939765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
5949765d2d9SChen-Yu Tsai 
5959765d2d9SChen-Yu Tsai 	/* clear counter alarm pending interrupts */
5969765d2d9SChen-Yu Tsai 	writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
5979765d2d9SChen-Yu Tsai 	       chip->base + SUN6I_ALRM_IRQ_STA);
5989765d2d9SChen-Yu Tsai 
5999765d2d9SChen-Yu Tsai 	/* clear week alarm pending interrupts */
6009765d2d9SChen-Yu Tsai 	writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
6019765d2d9SChen-Yu Tsai 	       chip->base + SUN6I_ALRM1_IRQ_STA);
6029765d2d9SChen-Yu Tsai 
6039765d2d9SChen-Yu Tsai 	/* disable alarm wakeup */
6049765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALARM_CONFIG);
6059765d2d9SChen-Yu Tsai 
6063855c2c3SMaxime Ripard 	clk_prepare_enable(chip->losc);
607fb61bb82SMaxime Ripard 
6085dff3a31SMaxime Ripard 	chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-sun6i",
6099765d2d9SChen-Yu Tsai 					     &sun6i_rtc_ops, THIS_MODULE);
6109765d2d9SChen-Yu Tsai 	if (IS_ERR(chip->rtc)) {
6119765d2d9SChen-Yu Tsai 		dev_err(&pdev->dev, "unable to register device\n");
6129765d2d9SChen-Yu Tsai 		return PTR_ERR(chip->rtc);
6139765d2d9SChen-Yu Tsai 	}
6149765d2d9SChen-Yu Tsai 
6159765d2d9SChen-Yu Tsai 	dev_info(&pdev->dev, "RTC enabled\n");
6169765d2d9SChen-Yu Tsai 
6179765d2d9SChen-Yu Tsai 	return 0;
6189765d2d9SChen-Yu Tsai }
6199765d2d9SChen-Yu Tsai 
620*403a3c3dSChen-Yu Tsai /*
621*403a3c3dSChen-Yu Tsai  * As far as RTC functionality goes, all models are the same. The
622*403a3c3dSChen-Yu Tsai  * datasheets claim that different models have different number of
623*403a3c3dSChen-Yu Tsai  * registers available for non-volatile storage, but experiments show
624*403a3c3dSChen-Yu Tsai  * that all SoCs have 16 registers available for this purpose.
625*403a3c3dSChen-Yu Tsai  */
6269765d2d9SChen-Yu Tsai static const struct of_device_id sun6i_rtc_dt_ids[] = {
6279765d2d9SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-rtc" },
6289765d2d9SChen-Yu Tsai 	{ /* sentinel */ },
6299765d2d9SChen-Yu Tsai };
6309765d2d9SChen-Yu Tsai MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
6319765d2d9SChen-Yu Tsai 
6329765d2d9SChen-Yu Tsai static struct platform_driver sun6i_rtc_driver = {
6339765d2d9SChen-Yu Tsai 	.probe		= sun6i_rtc_probe,
6349765d2d9SChen-Yu Tsai 	.driver		= {
6359765d2d9SChen-Yu Tsai 		.name		= "sun6i-rtc",
6369765d2d9SChen-Yu Tsai 		.of_match_table = sun6i_rtc_dt_ids,
6379765d2d9SChen-Yu Tsai 	},
6389765d2d9SChen-Yu Tsai };
63937539414SMaxime Ripard builtin_platform_driver(sun6i_rtc_driver);
640