xref: /openbmc/linux/drivers/rtc/rtc-sun6i.c (revision d91612d7f01aca454469976d25db761c5085ae4d)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29765d2d9SChen-Yu Tsai /*
39765d2d9SChen-Yu Tsai  * An RTC driver for Allwinner A31/A23
49765d2d9SChen-Yu Tsai  *
59765d2d9SChen-Yu Tsai  * Copyright (c) 2014, Chen-Yu Tsai <wens@csie.org>
69765d2d9SChen-Yu Tsai  *
79765d2d9SChen-Yu Tsai  * based on rtc-sunxi.c
89765d2d9SChen-Yu Tsai  *
99765d2d9SChen-Yu Tsai  * An RTC driver for Allwinner A10/A20
109765d2d9SChen-Yu Tsai  *
119765d2d9SChen-Yu Tsai  * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
129765d2d9SChen-Yu Tsai  */
139765d2d9SChen-Yu Tsai 
143855c2c3SMaxime Ripard #include <linux/clk.h>
153855c2c3SMaxime Ripard #include <linux/clk-provider.h>
16*d91612d7SSamuel Holland #include <linux/clk/sunxi-ng.h>
179765d2d9SChen-Yu Tsai #include <linux/delay.h>
189765d2d9SChen-Yu Tsai #include <linux/err.h>
199765d2d9SChen-Yu Tsai #include <linux/fs.h>
209765d2d9SChen-Yu Tsai #include <linux/init.h>
219765d2d9SChen-Yu Tsai #include <linux/interrupt.h>
229765d2d9SChen-Yu Tsai #include <linux/io.h>
239765d2d9SChen-Yu Tsai #include <linux/kernel.h>
249765d2d9SChen-Yu Tsai #include <linux/module.h>
259765d2d9SChen-Yu Tsai #include <linux/of.h>
269765d2d9SChen-Yu Tsai #include <linux/of_address.h>
279765d2d9SChen-Yu Tsai #include <linux/of_device.h>
289765d2d9SChen-Yu Tsai #include <linux/platform_device.h>
299765d2d9SChen-Yu Tsai #include <linux/rtc.h>
303855c2c3SMaxime Ripard #include <linux/slab.h>
319765d2d9SChen-Yu Tsai #include <linux/types.h>
329765d2d9SChen-Yu Tsai 
339765d2d9SChen-Yu Tsai /* Control register */
349765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL				0x0000
35fb61bb82SMaxime Ripard #define SUN6I_LOSC_CTRL_KEY			(0x16aa << 16)
36b60ff2cfSOndrej Jirman #define SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS		BIT(15)
379765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC		BIT(9)
389765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_RTC_HMS_ACC		BIT(8)
399765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_RTC_YMD_ACC		BIT(7)
40b60ff2cfSOndrej Jirman #define SUN6I_LOSC_CTRL_EXT_LOSC_EN		BIT(4)
41fb61bb82SMaxime Ripard #define SUN6I_LOSC_CTRL_EXT_OSC			BIT(0)
429765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_ACC_MASK		GENMASK(9, 7)
439765d2d9SChen-Yu Tsai 
443855c2c3SMaxime Ripard #define SUN6I_LOSC_CLK_PRESCAL			0x0008
453855c2c3SMaxime Ripard 
469765d2d9SChen-Yu Tsai /* RTC */
479765d2d9SChen-Yu Tsai #define SUN6I_RTC_YMD				0x0010
489765d2d9SChen-Yu Tsai #define SUN6I_RTC_HMS				0x0014
499765d2d9SChen-Yu Tsai 
509765d2d9SChen-Yu Tsai /* Alarm 0 (counter) */
519765d2d9SChen-Yu Tsai #define SUN6I_ALRM_COUNTER			0x0020
527878fec4SAndre Przywara /* This holds the remaining alarm seconds on older SoCs (current value) */
537878fec4SAndre Przywara #define SUN6I_ALRM_COUNTER_HMS			0x0024
549765d2d9SChen-Yu Tsai #define SUN6I_ALRM_EN				0x0028
559765d2d9SChen-Yu Tsai #define SUN6I_ALRM_EN_CNT_EN			BIT(0)
569765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_EN			0x002c
579765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN		BIT(0)
589765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_STA			0x0030
599765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND		BIT(0)
609765d2d9SChen-Yu Tsai 
619765d2d9SChen-Yu Tsai /* Alarm 1 (wall clock) */
629765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_EN				0x0044
639765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_EN			0x0048
649765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_STA			0x004c
659765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND	BIT(0)
669765d2d9SChen-Yu Tsai 
679765d2d9SChen-Yu Tsai /* Alarm config */
689765d2d9SChen-Yu Tsai #define SUN6I_ALARM_CONFIG			0x0050
699765d2d9SChen-Yu Tsai #define SUN6I_ALARM_CONFIG_WAKEUP		BIT(0)
709765d2d9SChen-Yu Tsai 
7117ecd246SMaxime Ripard #define SUN6I_LOSC_OUT_GATING			0x0060
7209018d4bSMichael Trimarchi #define SUN6I_LOSC_OUT_GATING_EN_OFFSET		0
7317ecd246SMaxime Ripard 
749765d2d9SChen-Yu Tsai /*
759765d2d9SChen-Yu Tsai  * Get date values
769765d2d9SChen-Yu Tsai  */
779765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_DAY_VALUE(x)		((x)  & 0x0000001f)
789765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_MON_VALUE(x)		(((x) & 0x00000f00) >> 8)
799765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_YEAR_VALUE(x)		(((x) & 0x003f0000) >> 16)
809765d2d9SChen-Yu Tsai #define SUN6I_LEAP_GET_VALUE(x)			(((x) & 0x00400000) >> 22)
819765d2d9SChen-Yu Tsai 
829765d2d9SChen-Yu Tsai /*
839765d2d9SChen-Yu Tsai  * Get time values
849765d2d9SChen-Yu Tsai  */
859765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_SEC_VALUE(x)		((x)  & 0x0000003f)
869765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_MIN_VALUE(x)		(((x) & 0x00003f00) >> 8)
879765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_HOUR_VALUE(x)		(((x) & 0x001f0000) >> 16)
889765d2d9SChen-Yu Tsai 
899765d2d9SChen-Yu Tsai /*
909765d2d9SChen-Yu Tsai  * Set date values
919765d2d9SChen-Yu Tsai  */
929765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_DAY_VALUE(x)		((x)       & 0x0000001f)
939765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_MON_VALUE(x)		((x) <<  8 & 0x00000f00)
949765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_YEAR_VALUE(x)		((x) << 16 & 0x003f0000)
959765d2d9SChen-Yu Tsai #define SUN6I_LEAP_SET_VALUE(x)			((x) << 22 & 0x00400000)
969765d2d9SChen-Yu Tsai 
979765d2d9SChen-Yu Tsai /*
989765d2d9SChen-Yu Tsai  * Set time values
999765d2d9SChen-Yu Tsai  */
1009765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_SEC_VALUE(x)		((x)       & 0x0000003f)
1019765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_MIN_VALUE(x)		((x) <<  8 & 0x00003f00)
1029765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_HOUR_VALUE(x)		((x) << 16 & 0x001f0000)
1039765d2d9SChen-Yu Tsai 
1049765d2d9SChen-Yu Tsai /*
1059765d2d9SChen-Yu Tsai  * The year parameter passed to the driver is usually an offset relative to
1069765d2d9SChen-Yu Tsai  * the year 1900. This macro is used to convert this offset to another one
1079765d2d9SChen-Yu Tsai  * relative to the minimum year allowed by the hardware.
1089765d2d9SChen-Yu Tsai  *
1099765d2d9SChen-Yu Tsai  * The year range is 1970 - 2033. This range is selected to match Allwinner's
1109765d2d9SChen-Yu Tsai  * driver, even though it is somewhat limited.
1119765d2d9SChen-Yu Tsai  */
1129765d2d9SChen-Yu Tsai #define SUN6I_YEAR_MIN				1970
1139765d2d9SChen-Yu Tsai #define SUN6I_YEAR_OFF				(SUN6I_YEAR_MIN - 1900)
1149765d2d9SChen-Yu Tsai 
115648c151aSAndre Przywara #define SECS_PER_DAY				(24 * 3600ULL)
116648c151aSAndre Przywara 
117403a3c3dSChen-Yu Tsai /*
118403a3c3dSChen-Yu Tsai  * There are other differences between models, including:
119403a3c3dSChen-Yu Tsai  *
120403a3c3dSChen-Yu Tsai  *   - number of GPIO pins that can be configured to hold a certain level
121403a3c3dSChen-Yu Tsai  *   - crypto-key related registers (H5, H6)
122403a3c3dSChen-Yu Tsai  *   - boot process related (super standby, secondary processor entry address)
123403a3c3dSChen-Yu Tsai  *     registers (R40, H6)
124403a3c3dSChen-Yu Tsai  *   - SYS power domain controls (R40)
125403a3c3dSChen-Yu Tsai  *   - DCXO controls (H6)
126403a3c3dSChen-Yu Tsai  *   - RC oscillator calibration (H6)
127403a3c3dSChen-Yu Tsai  *
128403a3c3dSChen-Yu Tsai  * These functions are not covered by this driver.
129403a3c3dSChen-Yu Tsai  */
130403a3c3dSChen-Yu Tsai struct sun6i_rtc_clk_data {
131403a3c3dSChen-Yu Tsai 	unsigned long rc_osc_rate;
132403a3c3dSChen-Yu Tsai 	unsigned int fixed_prescaler : 16;
133403a3c3dSChen-Yu Tsai 	unsigned int has_prescaler : 1;
134403a3c3dSChen-Yu Tsai 	unsigned int has_out_clk : 1;
135c56afc18SChen-Yu Tsai 	unsigned int export_iosc : 1;
136b60ff2cfSOndrej Jirman 	unsigned int has_losc_en : 1;
137b60ff2cfSOndrej Jirman 	unsigned int has_auto_swt : 1;
138403a3c3dSChen-Yu Tsai };
139403a3c3dSChen-Yu Tsai 
140648c151aSAndre Przywara #define RTC_LINEAR_DAY	BIT(0)
141648c151aSAndre Przywara 
1429765d2d9SChen-Yu Tsai struct sun6i_rtc_dev {
1439765d2d9SChen-Yu Tsai 	struct rtc_device *rtc;
144403a3c3dSChen-Yu Tsai 	const struct sun6i_rtc_clk_data *data;
1459765d2d9SChen-Yu Tsai 	void __iomem *base;
1469765d2d9SChen-Yu Tsai 	int irq;
1479f6cd82eSAndre Przywara 	time64_t alarm;
148648c151aSAndre Przywara 	unsigned long flags;
149a9422a19SMaxime Ripard 
1503855c2c3SMaxime Ripard 	struct clk_hw hw;
1513855c2c3SMaxime Ripard 	struct clk_hw *int_osc;
1523855c2c3SMaxime Ripard 	struct clk *losc;
15317ecd246SMaxime Ripard 	struct clk *ext_losc;
1543855c2c3SMaxime Ripard 
155a9422a19SMaxime Ripard 	spinlock_t lock;
1569765d2d9SChen-Yu Tsai };
1579765d2d9SChen-Yu Tsai 
1583855c2c3SMaxime Ripard static struct sun6i_rtc_dev *sun6i_rtc;
1593855c2c3SMaxime Ripard 
1603855c2c3SMaxime Ripard static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw,
1613855c2c3SMaxime Ripard 					       unsigned long parent_rate)
1623855c2c3SMaxime Ripard {
1633855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
164403a3c3dSChen-Yu Tsai 	u32 val = 0;
1653855c2c3SMaxime Ripard 
1663855c2c3SMaxime Ripard 	val = readl(rtc->base + SUN6I_LOSC_CTRL);
1673855c2c3SMaxime Ripard 	if (val & SUN6I_LOSC_CTRL_EXT_OSC)
1683855c2c3SMaxime Ripard 		return parent_rate;
1693855c2c3SMaxime Ripard 
170403a3c3dSChen-Yu Tsai 	if (rtc->data->fixed_prescaler)
171403a3c3dSChen-Yu Tsai 		parent_rate /= rtc->data->fixed_prescaler;
172403a3c3dSChen-Yu Tsai 
173403a3c3dSChen-Yu Tsai 	if (rtc->data->has_prescaler) {
1743855c2c3SMaxime Ripard 		val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL);
1753855c2c3SMaxime Ripard 		val &= GENMASK(4, 0);
176403a3c3dSChen-Yu Tsai 	}
1773855c2c3SMaxime Ripard 
1783855c2c3SMaxime Ripard 	return parent_rate / (val + 1);
1793855c2c3SMaxime Ripard }
1803855c2c3SMaxime Ripard 
1813855c2c3SMaxime Ripard static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw)
1823855c2c3SMaxime Ripard {
1833855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
1843855c2c3SMaxime Ripard 
1853855c2c3SMaxime Ripard 	return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC;
1863855c2c3SMaxime Ripard }
1873855c2c3SMaxime Ripard 
1883855c2c3SMaxime Ripard static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index)
1893855c2c3SMaxime Ripard {
1903855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
1913855c2c3SMaxime Ripard 	unsigned long flags;
1923855c2c3SMaxime Ripard 	u32 val;
1933855c2c3SMaxime Ripard 
1943855c2c3SMaxime Ripard 	if (index > 1)
1953855c2c3SMaxime Ripard 		return -EINVAL;
1963855c2c3SMaxime Ripard 
1973855c2c3SMaxime Ripard 	spin_lock_irqsave(&rtc->lock, flags);
1983855c2c3SMaxime Ripard 	val = readl(rtc->base + SUN6I_LOSC_CTRL);
1993855c2c3SMaxime Ripard 	val &= ~SUN6I_LOSC_CTRL_EXT_OSC;
2003855c2c3SMaxime Ripard 	val |= SUN6I_LOSC_CTRL_KEY;
2013855c2c3SMaxime Ripard 	val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0;
202b60ff2cfSOndrej Jirman 	if (rtc->data->has_losc_en) {
203b60ff2cfSOndrej Jirman 		val &= ~SUN6I_LOSC_CTRL_EXT_LOSC_EN;
204b60ff2cfSOndrej Jirman 		val |= index ? SUN6I_LOSC_CTRL_EXT_LOSC_EN : 0;
205b60ff2cfSOndrej Jirman 	}
2063855c2c3SMaxime Ripard 	writel(val, rtc->base + SUN6I_LOSC_CTRL);
2073855c2c3SMaxime Ripard 	spin_unlock_irqrestore(&rtc->lock, flags);
2083855c2c3SMaxime Ripard 
2093855c2c3SMaxime Ripard 	return 0;
2103855c2c3SMaxime Ripard }
2113855c2c3SMaxime Ripard 
2123855c2c3SMaxime Ripard static const struct clk_ops sun6i_rtc_osc_ops = {
2133855c2c3SMaxime Ripard 	.recalc_rate	= sun6i_rtc_osc_recalc_rate,
2143855c2c3SMaxime Ripard 
2153855c2c3SMaxime Ripard 	.get_parent	= sun6i_rtc_osc_get_parent,
2163855c2c3SMaxime Ripard 	.set_parent	= sun6i_rtc_osc_set_parent,
2173855c2c3SMaxime Ripard };
2183855c2c3SMaxime Ripard 
219403a3c3dSChen-Yu Tsai static void __init sun6i_rtc_clk_init(struct device_node *node,
220403a3c3dSChen-Yu Tsai 				      const struct sun6i_rtc_clk_data *data)
2213855c2c3SMaxime Ripard {
2223855c2c3SMaxime Ripard 	struct clk_hw_onecell_data *clk_data;
2233855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *rtc;
2243855c2c3SMaxime Ripard 	struct clk_init_data init = {
2253855c2c3SMaxime Ripard 		.ops		= &sun6i_rtc_osc_ops,
226459b6ea0SChen-Yu Tsai 		.name		= "losc",
2273855c2c3SMaxime Ripard 	};
228c56afc18SChen-Yu Tsai 	const char *iosc_name = "rtc-int-osc";
22917ecd246SMaxime Ripard 	const char *clkout_name = "osc32k-out";
2303855c2c3SMaxime Ripard 	const char *parents[2];
231b60ff2cfSOndrej Jirman 	u32 reg;
2323855c2c3SMaxime Ripard 
2333855c2c3SMaxime Ripard 	rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
2343855c2c3SMaxime Ripard 	if (!rtc)
2353855c2c3SMaxime Ripard 		return;
2363855c2c3SMaxime Ripard 
237403a3c3dSChen-Yu Tsai 	rtc->data = data;
238c56afc18SChen-Yu Tsai 	clk_data = kzalloc(struct_size(clk_data, hws, 3), GFP_KERNEL);
239e9982024SColin Ian King 	if (!clk_data) {
240e9982024SColin Ian King 		kfree(rtc);
2413855c2c3SMaxime Ripard 		return;
242e9982024SColin Ian King 	}
243319ff835SAlexey Klimov 
2443855c2c3SMaxime Ripard 	spin_lock_init(&rtc->lock);
2453855c2c3SMaxime Ripard 
2463855c2c3SMaxime Ripard 	rtc->base = of_io_request_and_map(node, 0, of_node_full_name(node));
247aaa65a9cSWei Yongjun 	if (IS_ERR(rtc->base)) {
2483855c2c3SMaxime Ripard 		pr_crit("Can't map RTC registers");
2491a37c348SColin Ian King 		goto err;
2503855c2c3SMaxime Ripard 	}
2513855c2c3SMaxime Ripard 
252b60ff2cfSOndrej Jirman 	reg = SUN6I_LOSC_CTRL_KEY;
253b60ff2cfSOndrej Jirman 	if (rtc->data->has_auto_swt) {
254b60ff2cfSOndrej Jirman 		/* Bypass auto-switch to int osc, on ext losc failure */
255b60ff2cfSOndrej Jirman 		reg |= SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS;
256b60ff2cfSOndrej Jirman 		writel(reg, rtc->base + SUN6I_LOSC_CTRL);
257b60ff2cfSOndrej Jirman 	}
258b60ff2cfSOndrej Jirman 
259ec98a875SJernej Skrabec 	/* Switch to the external, more precise, oscillator, if present */
260ec98a875SJernej Skrabec 	if (of_get_property(node, "clocks", NULL)) {
261b60ff2cfSOndrej Jirman 		reg |= SUN6I_LOSC_CTRL_EXT_OSC;
262b60ff2cfSOndrej Jirman 		if (rtc->data->has_losc_en)
263b60ff2cfSOndrej Jirman 			reg |= SUN6I_LOSC_CTRL_EXT_LOSC_EN;
264ec98a875SJernej Skrabec 	}
265b60ff2cfSOndrej Jirman 	writel(reg, rtc->base + SUN6I_LOSC_CTRL);
2663855c2c3SMaxime Ripard 
26715829cf4SChen-Yu Tsai 	/* Yes, I know, this is ugly. */
26815829cf4SChen-Yu Tsai 	sun6i_rtc = rtc;
26915829cf4SChen-Yu Tsai 
270c56afc18SChen-Yu Tsai 	/* Only read IOSC name from device tree if it is exported */
271c56afc18SChen-Yu Tsai 	if (rtc->data->export_iosc)
272c56afc18SChen-Yu Tsai 		of_property_read_string_index(node, "clock-output-names", 2,
273c56afc18SChen-Yu Tsai 					      &iosc_name);
274c56afc18SChen-Yu Tsai 
2753855c2c3SMaxime Ripard 	rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL,
276c56afc18SChen-Yu Tsai 								iosc_name,
2773855c2c3SMaxime Ripard 								NULL, 0,
278403a3c3dSChen-Yu Tsai 								rtc->data->rc_osc_rate,
2793855c2c3SMaxime Ripard 								300000000);
2803855c2c3SMaxime Ripard 	if (IS_ERR(rtc->int_osc)) {
2813855c2c3SMaxime Ripard 		pr_crit("Couldn't register the internal oscillator\n");
28228d21191SDinghao Liu 		goto err;
2833855c2c3SMaxime Ripard 	}
2843855c2c3SMaxime Ripard 
2853855c2c3SMaxime Ripard 	parents[0] = clk_hw_get_name(rtc->int_osc);
286ec98a875SJernej Skrabec 	/* If there is no external oscillator, this will be NULL and ... */
2873855c2c3SMaxime Ripard 	parents[1] = of_clk_get_parent_name(node, 0);
2883855c2c3SMaxime Ripard 
2893855c2c3SMaxime Ripard 	rtc->hw.init = &init;
2903855c2c3SMaxime Ripard 
2913855c2c3SMaxime Ripard 	init.parent_names = parents;
292ec98a875SJernej Skrabec 	/* ... number of clock parents will be 1. */
2933855c2c3SMaxime Ripard 	init.num_parents = of_clk_get_parent_count(node) + 1;
29417ecd246SMaxime Ripard 	of_property_read_string_index(node, "clock-output-names", 0,
29517ecd246SMaxime Ripard 				      &init.name);
2963855c2c3SMaxime Ripard 
2973855c2c3SMaxime Ripard 	rtc->losc = clk_register(NULL, &rtc->hw);
2983855c2c3SMaxime Ripard 	if (IS_ERR(rtc->losc)) {
2993855c2c3SMaxime Ripard 		pr_crit("Couldn't register the LOSC clock\n");
30028d21191SDinghao Liu 		goto err_register;
3013855c2c3SMaxime Ripard 	}
3023855c2c3SMaxime Ripard 
30317ecd246SMaxime Ripard 	of_property_read_string_index(node, "clock-output-names", 1,
30417ecd246SMaxime Ripard 				      &clkout_name);
30521ef77deSStephen Boyd 	rtc->ext_losc = clk_register_gate(NULL, clkout_name, init.name,
30617ecd246SMaxime Ripard 					  0, rtc->base + SUN6I_LOSC_OUT_GATING,
30709018d4bSMichael Trimarchi 					  SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0,
30817ecd246SMaxime Ripard 					  &rtc->lock);
30917ecd246SMaxime Ripard 	if (IS_ERR(rtc->ext_losc)) {
31017ecd246SMaxime Ripard 		pr_crit("Couldn't register the LOSC external gate\n");
31128d21191SDinghao Liu 		goto err_register;
31217ecd246SMaxime Ripard 	}
31317ecd246SMaxime Ripard 
31417ecd246SMaxime Ripard 	clk_data->num = 2;
3153855c2c3SMaxime Ripard 	clk_data->hws[0] = &rtc->hw;
31617ecd246SMaxime Ripard 	clk_data->hws[1] = __clk_get_hw(rtc->ext_losc);
317c56afc18SChen-Yu Tsai 	if (rtc->data->export_iosc) {
318c56afc18SChen-Yu Tsai 		clk_data->hws[2] = rtc->int_osc;
319c56afc18SChen-Yu Tsai 		clk_data->num = 3;
320c56afc18SChen-Yu Tsai 	}
3213855c2c3SMaxime Ripard 	of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
3221a37c348SColin Ian King 	return;
3231a37c348SColin Ian King 
32428d21191SDinghao Liu err_register:
32528d21191SDinghao Liu 	clk_hw_unregister_fixed_rate(rtc->int_osc);
3261a37c348SColin Ian King err:
3271a37c348SColin Ian King 	kfree(clk_data);
3283855c2c3SMaxime Ripard }
329403a3c3dSChen-Yu Tsai 
330403a3c3dSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun6i_a31_rtc_data = {
331403a3c3dSChen-Yu Tsai 	.rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
332403a3c3dSChen-Yu Tsai 	.has_prescaler = 1,
333403a3c3dSChen-Yu Tsai };
334403a3c3dSChen-Yu Tsai 
335403a3c3dSChen-Yu Tsai static void __init sun6i_a31_rtc_clk_init(struct device_node *node)
336403a3c3dSChen-Yu Tsai {
337403a3c3dSChen-Yu Tsai 	sun6i_rtc_clk_init(node, &sun6i_a31_rtc_data);
338403a3c3dSChen-Yu Tsai }
339403a3c3dSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun6i_a31_rtc_clk, "allwinner,sun6i-a31-rtc",
340403a3c3dSChen-Yu Tsai 		      sun6i_a31_rtc_clk_init);
3413855c2c3SMaxime Ripard 
3427cd1acaeSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_a23_rtc_data = {
3437cd1acaeSChen-Yu Tsai 	.rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
3447cd1acaeSChen-Yu Tsai 	.has_prescaler = 1,
3457cd1acaeSChen-Yu Tsai 	.has_out_clk = 1,
3467cd1acaeSChen-Yu Tsai };
3477cd1acaeSChen-Yu Tsai 
3487cd1acaeSChen-Yu Tsai static void __init sun8i_a23_rtc_clk_init(struct device_node *node)
3497cd1acaeSChen-Yu Tsai {
3507cd1acaeSChen-Yu Tsai 	sun6i_rtc_clk_init(node, &sun8i_a23_rtc_data);
3517cd1acaeSChen-Yu Tsai }
3527cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_a23_rtc_clk, "allwinner,sun8i-a23-rtc",
3537cd1acaeSChen-Yu Tsai 		      sun8i_a23_rtc_clk_init);
3547cd1acaeSChen-Yu Tsai 
3557cd1acaeSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_h3_rtc_data = {
3567cd1acaeSChen-Yu Tsai 	.rc_osc_rate = 16000000,
3577cd1acaeSChen-Yu Tsai 	.fixed_prescaler = 32,
3587cd1acaeSChen-Yu Tsai 	.has_prescaler = 1,
3597cd1acaeSChen-Yu Tsai 	.has_out_clk = 1,
360c56afc18SChen-Yu Tsai 	.export_iosc = 1,
3617cd1acaeSChen-Yu Tsai };
3627cd1acaeSChen-Yu Tsai 
3637cd1acaeSChen-Yu Tsai static void __init sun8i_h3_rtc_clk_init(struct device_node *node)
3647cd1acaeSChen-Yu Tsai {
3657cd1acaeSChen-Yu Tsai 	sun6i_rtc_clk_init(node, &sun8i_h3_rtc_data);
3667cd1acaeSChen-Yu Tsai }
3677cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_h3_rtc_clk, "allwinner,sun8i-h3-rtc",
3687cd1acaeSChen-Yu Tsai 		      sun8i_h3_rtc_clk_init);
3697cd1acaeSChen-Yu Tsai /* As far as we are concerned, clocks for H5 are the same as H3 */
3707cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun50i_h5_rtc_clk, "allwinner,sun50i-h5-rtc",
3717cd1acaeSChen-Yu Tsai 		      sun8i_h3_rtc_clk_init);
3727cd1acaeSChen-Yu Tsai 
373b60ff2cfSOndrej Jirman static const struct sun6i_rtc_clk_data sun50i_h6_rtc_data = {
374b60ff2cfSOndrej Jirman 	.rc_osc_rate = 16000000,
375b60ff2cfSOndrej Jirman 	.fixed_prescaler = 32,
376b60ff2cfSOndrej Jirman 	.has_prescaler = 1,
377b60ff2cfSOndrej Jirman 	.has_out_clk = 1,
378b60ff2cfSOndrej Jirman 	.export_iosc = 1,
379b60ff2cfSOndrej Jirman 	.has_losc_en = 1,
380b60ff2cfSOndrej Jirman 	.has_auto_swt = 1,
381b60ff2cfSOndrej Jirman };
382b60ff2cfSOndrej Jirman 
383b60ff2cfSOndrej Jirman static void __init sun50i_h6_rtc_clk_init(struct device_node *node)
384b60ff2cfSOndrej Jirman {
385b60ff2cfSOndrej Jirman 	sun6i_rtc_clk_init(node, &sun50i_h6_rtc_data);
386b60ff2cfSOndrej Jirman }
387b60ff2cfSOndrej Jirman CLK_OF_DECLARE_DRIVER(sun50i_h6_rtc_clk, "allwinner,sun50i-h6-rtc",
388b60ff2cfSOndrej Jirman 		      sun50i_h6_rtc_clk_init);
389b60ff2cfSOndrej Jirman 
390111bf02bSChen-Yu Tsai /*
391111bf02bSChen-Yu Tsai  * The R40 user manual is self-conflicting on whether the prescaler is
392111bf02bSChen-Yu Tsai  * fixed or configurable. The clock diagram shows it as fixed, but there
393111bf02bSChen-Yu Tsai  * is also a configurable divider in the RTC block.
394111bf02bSChen-Yu Tsai  */
395111bf02bSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_r40_rtc_data = {
396111bf02bSChen-Yu Tsai 	.rc_osc_rate = 16000000,
397111bf02bSChen-Yu Tsai 	.fixed_prescaler = 512,
398111bf02bSChen-Yu Tsai };
399111bf02bSChen-Yu Tsai static void __init sun8i_r40_rtc_clk_init(struct device_node *node)
400111bf02bSChen-Yu Tsai {
401111bf02bSChen-Yu Tsai 	sun6i_rtc_clk_init(node, &sun8i_r40_rtc_data);
402111bf02bSChen-Yu Tsai }
403111bf02bSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_r40_rtc_clk, "allwinner,sun8i-r40-rtc",
404111bf02bSChen-Yu Tsai 		      sun8i_r40_rtc_clk_init);
405111bf02bSChen-Yu Tsai 
4067cd1acaeSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_v3_rtc_data = {
4077cd1acaeSChen-Yu Tsai 	.rc_osc_rate = 32000,
4087cd1acaeSChen-Yu Tsai 	.has_out_clk = 1,
4097cd1acaeSChen-Yu Tsai };
4107cd1acaeSChen-Yu Tsai 
4117cd1acaeSChen-Yu Tsai static void __init sun8i_v3_rtc_clk_init(struct device_node *node)
4127cd1acaeSChen-Yu Tsai {
4137cd1acaeSChen-Yu Tsai 	sun6i_rtc_clk_init(node, &sun8i_v3_rtc_data);
4147cd1acaeSChen-Yu Tsai }
4157cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_v3_rtc_clk, "allwinner,sun8i-v3-rtc",
4167cd1acaeSChen-Yu Tsai 		      sun8i_v3_rtc_clk_init);
4177cd1acaeSChen-Yu Tsai 
4189765d2d9SChen-Yu Tsai static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
4199765d2d9SChen-Yu Tsai {
4209765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
421a9422a19SMaxime Ripard 	irqreturn_t ret = IRQ_NONE;
4229765d2d9SChen-Yu Tsai 	u32 val;
4239765d2d9SChen-Yu Tsai 
424a9422a19SMaxime Ripard 	spin_lock(&chip->lock);
4259765d2d9SChen-Yu Tsai 	val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
4269765d2d9SChen-Yu Tsai 
4279765d2d9SChen-Yu Tsai 	if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
4289765d2d9SChen-Yu Tsai 		val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
4299765d2d9SChen-Yu Tsai 		writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
4309765d2d9SChen-Yu Tsai 
4319765d2d9SChen-Yu Tsai 		rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
4329765d2d9SChen-Yu Tsai 
433a9422a19SMaxime Ripard 		ret = IRQ_HANDLED;
4349765d2d9SChen-Yu Tsai 	}
435a9422a19SMaxime Ripard 	spin_unlock(&chip->lock);
4369765d2d9SChen-Yu Tsai 
437a9422a19SMaxime Ripard 	return ret;
4389765d2d9SChen-Yu Tsai }
4399765d2d9SChen-Yu Tsai 
4409765d2d9SChen-Yu Tsai static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
4419765d2d9SChen-Yu Tsai {
4429765d2d9SChen-Yu Tsai 	u32 alrm_val = 0;
4439765d2d9SChen-Yu Tsai 	u32 alrm_irq_val = 0;
4449765d2d9SChen-Yu Tsai 	u32 alrm_wake_val = 0;
445a9422a19SMaxime Ripard 	unsigned long flags;
4469765d2d9SChen-Yu Tsai 
4479765d2d9SChen-Yu Tsai 	if (to) {
4489765d2d9SChen-Yu Tsai 		alrm_val = SUN6I_ALRM_EN_CNT_EN;
4499765d2d9SChen-Yu Tsai 		alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
4509765d2d9SChen-Yu Tsai 		alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
4519765d2d9SChen-Yu Tsai 	} else {
4529765d2d9SChen-Yu Tsai 		writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
4539765d2d9SChen-Yu Tsai 		       chip->base + SUN6I_ALRM_IRQ_STA);
4549765d2d9SChen-Yu Tsai 	}
4559765d2d9SChen-Yu Tsai 
456a9422a19SMaxime Ripard 	spin_lock_irqsave(&chip->lock, flags);
4579765d2d9SChen-Yu Tsai 	writel(alrm_val, chip->base + SUN6I_ALRM_EN);
4589765d2d9SChen-Yu Tsai 	writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
4599765d2d9SChen-Yu Tsai 	writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
460a9422a19SMaxime Ripard 	spin_unlock_irqrestore(&chip->lock, flags);
4619765d2d9SChen-Yu Tsai }
4629765d2d9SChen-Yu Tsai 
4639765d2d9SChen-Yu Tsai static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
4649765d2d9SChen-Yu Tsai {
4659765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
4669765d2d9SChen-Yu Tsai 	u32 date, time;
4679765d2d9SChen-Yu Tsai 
4689765d2d9SChen-Yu Tsai 	/*
4699765d2d9SChen-Yu Tsai 	 * read again in case it changes
4709765d2d9SChen-Yu Tsai 	 */
4719765d2d9SChen-Yu Tsai 	do {
4729765d2d9SChen-Yu Tsai 		date = readl(chip->base + SUN6I_RTC_YMD);
4739765d2d9SChen-Yu Tsai 		time = readl(chip->base + SUN6I_RTC_HMS);
4749765d2d9SChen-Yu Tsai 	} while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
4759765d2d9SChen-Yu Tsai 		 (time != readl(chip->base + SUN6I_RTC_HMS)));
4769765d2d9SChen-Yu Tsai 
477648c151aSAndre Przywara 	if (chip->flags & RTC_LINEAR_DAY) {
478648c151aSAndre Przywara 		/*
479648c151aSAndre Przywara 		 * Newer chips store a linear day number, the manual
480648c151aSAndre Przywara 		 * does not mandate any epoch base. The BSP driver uses
481648c151aSAndre Przywara 		 * the UNIX epoch, let's just copy that, as it's the
482648c151aSAndre Przywara 		 * easiest anyway.
483648c151aSAndre Przywara 		 */
484648c151aSAndre Przywara 		rtc_time64_to_tm((date & 0xffff) * SECS_PER_DAY, rtc_tm);
485648c151aSAndre Przywara 	} else {
4869765d2d9SChen-Yu Tsai 		rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
487648c151aSAndre Przywara 		rtc_tm->tm_mon  = SUN6I_DATE_GET_MON_VALUE(date) - 1;
4889765d2d9SChen-Yu Tsai 		rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
4899765d2d9SChen-Yu Tsai 
4909765d2d9SChen-Yu Tsai 		/*
4919765d2d9SChen-Yu Tsai 		 * switch from (data_year->min)-relative offset to
4929765d2d9SChen-Yu Tsai 		 * a (1900)-relative one
4939765d2d9SChen-Yu Tsai 		 */
4949765d2d9SChen-Yu Tsai 		rtc_tm->tm_year += SUN6I_YEAR_OFF;
495648c151aSAndre Przywara 	}
496648c151aSAndre Przywara 
497648c151aSAndre Przywara 	rtc_tm->tm_sec  = SUN6I_TIME_GET_SEC_VALUE(time);
498648c151aSAndre Przywara 	rtc_tm->tm_min  = SUN6I_TIME_GET_MIN_VALUE(time);
499648c151aSAndre Przywara 	rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
5009765d2d9SChen-Yu Tsai 
50122652ba7SAlexandre Belloni 	return 0;
5029765d2d9SChen-Yu Tsai }
5039765d2d9SChen-Yu Tsai 
5049765d2d9SChen-Yu Tsai static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
5059765d2d9SChen-Yu Tsai {
5069765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
507a9422a19SMaxime Ripard 	unsigned long flags;
5089765d2d9SChen-Yu Tsai 	u32 alrm_st;
5099765d2d9SChen-Yu Tsai 	u32 alrm_en;
5109765d2d9SChen-Yu Tsai 
511a9422a19SMaxime Ripard 	spin_lock_irqsave(&chip->lock, flags);
5129765d2d9SChen-Yu Tsai 	alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
5139765d2d9SChen-Yu Tsai 	alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
514a9422a19SMaxime Ripard 	spin_unlock_irqrestore(&chip->lock, flags);
515a9422a19SMaxime Ripard 
5169765d2d9SChen-Yu Tsai 	wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
5179765d2d9SChen-Yu Tsai 	wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
51899b7ac9cSAlexandre Belloni 	rtc_time64_to_tm(chip->alarm, &wkalrm->time);
5199765d2d9SChen-Yu Tsai 
5209765d2d9SChen-Yu Tsai 	return 0;
5219765d2d9SChen-Yu Tsai }
5229765d2d9SChen-Yu Tsai 
5239765d2d9SChen-Yu Tsai static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
5249765d2d9SChen-Yu Tsai {
5259765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
5269765d2d9SChen-Yu Tsai 	struct rtc_time *alrm_tm = &wkalrm->time;
5279765d2d9SChen-Yu Tsai 	struct rtc_time tm_now;
5287878fec4SAndre Przywara 	time64_t time_set;
5297878fec4SAndre Przywara 	u32 counter_val, counter_val_hms;
5309f6cd82eSAndre Przywara 	int ret;
5319765d2d9SChen-Yu Tsai 
5327878fec4SAndre Przywara 	time_set = rtc_tm_to_time64(alrm_tm);
5337878fec4SAndre Przywara 
5347878fec4SAndre Przywara 	if (chip->flags & RTC_LINEAR_DAY) {
5357878fec4SAndre Przywara 		/*
5367878fec4SAndre Przywara 		 * The alarm registers hold the actual alarm time, encoded
5377878fec4SAndre Przywara 		 * in the same way (linear day + HMS) as the current time.
5387878fec4SAndre Przywara 		 */
5397878fec4SAndre Przywara 		counter_val_hms = SUN6I_TIME_SET_SEC_VALUE(alrm_tm->tm_sec)  |
5407878fec4SAndre Przywara 				  SUN6I_TIME_SET_MIN_VALUE(alrm_tm->tm_min)  |
5417878fec4SAndre Przywara 				  SUN6I_TIME_SET_HOUR_VALUE(alrm_tm->tm_hour);
5427878fec4SAndre Przywara 		/* The division will cut off the H:M:S part of alrm_tm. */
5437878fec4SAndre Przywara 		counter_val = div_u64(rtc_tm_to_time64(alrm_tm), SECS_PER_DAY);
5447878fec4SAndre Przywara 	} else {
5457878fec4SAndre Przywara 		/* The alarm register holds the number of seconds left. */
5467878fec4SAndre Przywara 		time64_t time_now;
5477878fec4SAndre Przywara 
5489765d2d9SChen-Yu Tsai 		ret = sun6i_rtc_gettime(dev, &tm_now);
5499765d2d9SChen-Yu Tsai 		if (ret < 0) {
5509765d2d9SChen-Yu Tsai 			dev_err(dev, "Error in getting time\n");
5519765d2d9SChen-Yu Tsai 			return -EINVAL;
5529765d2d9SChen-Yu Tsai 		}
5539765d2d9SChen-Yu Tsai 
55499b7ac9cSAlexandre Belloni 		time_now = rtc_tm_to_time64(&tm_now);
5559765d2d9SChen-Yu Tsai 		if (time_set <= time_now) {
5569765d2d9SChen-Yu Tsai 			dev_err(dev, "Date to set in the past\n");
5579765d2d9SChen-Yu Tsai 			return -EINVAL;
5589765d2d9SChen-Yu Tsai 		}
5599f6cd82eSAndre Przywara 		if ((time_set - time_now) > U32_MAX) {
5609765d2d9SChen-Yu Tsai 			dev_err(dev, "Date too far in the future\n");
5619765d2d9SChen-Yu Tsai 			return -EINVAL;
5629765d2d9SChen-Yu Tsai 		}
5639765d2d9SChen-Yu Tsai 
5647878fec4SAndre Przywara 		counter_val = time_set - time_now;
5657878fec4SAndre Przywara 	}
5667878fec4SAndre Przywara 
5679765d2d9SChen-Yu Tsai 	sun6i_rtc_setaie(0, chip);
5689765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_COUNTER);
5697878fec4SAndre Przywara 	if (chip->flags & RTC_LINEAR_DAY)
5707878fec4SAndre Przywara 		writel(0, chip->base + SUN6I_ALRM_COUNTER_HMS);
5719765d2d9SChen-Yu Tsai 	usleep_range(100, 300);
5729765d2d9SChen-Yu Tsai 
5737878fec4SAndre Przywara 	writel(counter_val, chip->base + SUN6I_ALRM_COUNTER);
5747878fec4SAndre Przywara 	if (chip->flags & RTC_LINEAR_DAY)
5757878fec4SAndre Przywara 		writel(counter_val_hms, chip->base + SUN6I_ALRM_COUNTER_HMS);
5769765d2d9SChen-Yu Tsai 	chip->alarm = time_set;
5779765d2d9SChen-Yu Tsai 
5789765d2d9SChen-Yu Tsai 	sun6i_rtc_setaie(wkalrm->enabled, chip);
5799765d2d9SChen-Yu Tsai 
5809765d2d9SChen-Yu Tsai 	return 0;
5819765d2d9SChen-Yu Tsai }
5829765d2d9SChen-Yu Tsai 
5839765d2d9SChen-Yu Tsai static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
5849765d2d9SChen-Yu Tsai 			  unsigned int mask, unsigned int ms_timeout)
5859765d2d9SChen-Yu Tsai {
5869765d2d9SChen-Yu Tsai 	const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
5879765d2d9SChen-Yu Tsai 	u32 reg;
5889765d2d9SChen-Yu Tsai 
5899765d2d9SChen-Yu Tsai 	do {
5909765d2d9SChen-Yu Tsai 		reg = readl(chip->base + offset);
5919765d2d9SChen-Yu Tsai 		reg &= mask;
5929765d2d9SChen-Yu Tsai 
5939765d2d9SChen-Yu Tsai 		if (!reg)
5949765d2d9SChen-Yu Tsai 			return 0;
5959765d2d9SChen-Yu Tsai 
5969765d2d9SChen-Yu Tsai 	} while (time_before(jiffies, timeout));
5979765d2d9SChen-Yu Tsai 
5989765d2d9SChen-Yu Tsai 	return -ETIMEDOUT;
5999765d2d9SChen-Yu Tsai }
6009765d2d9SChen-Yu Tsai 
6019765d2d9SChen-Yu Tsai static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
6029765d2d9SChen-Yu Tsai {
6039765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
6049765d2d9SChen-Yu Tsai 	u32 date = 0;
6059765d2d9SChen-Yu Tsai 	u32 time = 0;
6069765d2d9SChen-Yu Tsai 
607648c151aSAndre Przywara 	time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec)  |
608648c151aSAndre Przywara 		SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min)  |
609648c151aSAndre Przywara 		SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
610648c151aSAndre Przywara 
611648c151aSAndre Przywara 	if (chip->flags & RTC_LINEAR_DAY) {
612648c151aSAndre Przywara 		/* The division will cut off the H:M:S part of rtc_tm. */
613648c151aSAndre Przywara 		date = div_u64(rtc_tm_to_time64(rtc_tm), SECS_PER_DAY);
614648c151aSAndre Przywara 	} else {
6159765d2d9SChen-Yu Tsai 		rtc_tm->tm_year -= SUN6I_YEAR_OFF;
6169765d2d9SChen-Yu Tsai 		rtc_tm->tm_mon += 1;
6179765d2d9SChen-Yu Tsai 
6189765d2d9SChen-Yu Tsai 		date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
6199765d2d9SChen-Yu Tsai 			SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon)  |
6209765d2d9SChen-Yu Tsai 			SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
6219765d2d9SChen-Yu Tsai 
6228ae79be7SAlexandre Belloni 		if (is_leap_year(rtc_tm->tm_year + SUN6I_YEAR_MIN))
6239765d2d9SChen-Yu Tsai 			date |= SUN6I_LEAP_SET_VALUE(1);
624648c151aSAndre Przywara 	}
6259765d2d9SChen-Yu Tsai 
6269765d2d9SChen-Yu Tsai 	/* Check whether registers are writable */
6279765d2d9SChen-Yu Tsai 	if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
6289765d2d9SChen-Yu Tsai 			   SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
6299765d2d9SChen-Yu Tsai 		dev_err(dev, "rtc is still busy.\n");
6309765d2d9SChen-Yu Tsai 		return -EBUSY;
6319765d2d9SChen-Yu Tsai 	}
6329765d2d9SChen-Yu Tsai 
6339765d2d9SChen-Yu Tsai 	writel(time, chip->base + SUN6I_RTC_HMS);
6349765d2d9SChen-Yu Tsai 
6359765d2d9SChen-Yu Tsai 	/*
6369765d2d9SChen-Yu Tsai 	 * After writing the RTC HH-MM-SS register, the
6379765d2d9SChen-Yu Tsai 	 * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
6389765d2d9SChen-Yu Tsai 	 * be cleared until the real writing operation is finished
6399765d2d9SChen-Yu Tsai 	 */
6409765d2d9SChen-Yu Tsai 
6419765d2d9SChen-Yu Tsai 	if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
6429765d2d9SChen-Yu Tsai 			   SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
6439765d2d9SChen-Yu Tsai 		dev_err(dev, "Failed to set rtc time.\n");
6449765d2d9SChen-Yu Tsai 		return -ETIMEDOUT;
6459765d2d9SChen-Yu Tsai 	}
6469765d2d9SChen-Yu Tsai 
6479765d2d9SChen-Yu Tsai 	writel(date, chip->base + SUN6I_RTC_YMD);
6489765d2d9SChen-Yu Tsai 
6499765d2d9SChen-Yu Tsai 	/*
6509765d2d9SChen-Yu Tsai 	 * After writing the RTC YY-MM-DD register, the
6519765d2d9SChen-Yu Tsai 	 * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
6529765d2d9SChen-Yu Tsai 	 * be cleared until the real writing operation is finished
6539765d2d9SChen-Yu Tsai 	 */
6549765d2d9SChen-Yu Tsai 
6559765d2d9SChen-Yu Tsai 	if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
6569765d2d9SChen-Yu Tsai 			   SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
6579765d2d9SChen-Yu Tsai 		dev_err(dev, "Failed to set rtc time.\n");
6589765d2d9SChen-Yu Tsai 		return -ETIMEDOUT;
6599765d2d9SChen-Yu Tsai 	}
6609765d2d9SChen-Yu Tsai 
6619765d2d9SChen-Yu Tsai 	return 0;
6629765d2d9SChen-Yu Tsai }
6639765d2d9SChen-Yu Tsai 
6649765d2d9SChen-Yu Tsai static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
6659765d2d9SChen-Yu Tsai {
6669765d2d9SChen-Yu Tsai 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
6679765d2d9SChen-Yu Tsai 
6689765d2d9SChen-Yu Tsai 	if (!enabled)
6699765d2d9SChen-Yu Tsai 		sun6i_rtc_setaie(enabled, chip);
6709765d2d9SChen-Yu Tsai 
6719765d2d9SChen-Yu Tsai 	return 0;
6729765d2d9SChen-Yu Tsai }
6739765d2d9SChen-Yu Tsai 
6749765d2d9SChen-Yu Tsai static const struct rtc_class_ops sun6i_rtc_ops = {
6759765d2d9SChen-Yu Tsai 	.read_time		= sun6i_rtc_gettime,
6769765d2d9SChen-Yu Tsai 	.set_time		= sun6i_rtc_settime,
6779765d2d9SChen-Yu Tsai 	.read_alarm		= sun6i_rtc_getalarm,
6789765d2d9SChen-Yu Tsai 	.set_alarm		= sun6i_rtc_setalarm,
6799765d2d9SChen-Yu Tsai 	.alarm_irq_enable	= sun6i_rtc_alarm_irq_enable
6809765d2d9SChen-Yu Tsai };
6819765d2d9SChen-Yu Tsai 
682d76a81d0SAlejandro González #ifdef CONFIG_PM_SLEEP
683d76a81d0SAlejandro González /* Enable IRQ wake on suspend, to wake up from RTC. */
684d76a81d0SAlejandro González static int sun6i_rtc_suspend(struct device *dev)
685d76a81d0SAlejandro González {
686d76a81d0SAlejandro González 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
687d76a81d0SAlejandro González 
688d76a81d0SAlejandro González 	if (device_may_wakeup(dev))
689d76a81d0SAlejandro González 		enable_irq_wake(chip->irq);
690d76a81d0SAlejandro González 
691d76a81d0SAlejandro González 	return 0;
692d76a81d0SAlejandro González }
693d76a81d0SAlejandro González 
694d76a81d0SAlejandro González /* Disable IRQ wake on resume. */
695d76a81d0SAlejandro González static int sun6i_rtc_resume(struct device *dev)
696d76a81d0SAlejandro González {
697d76a81d0SAlejandro González 	struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
698d76a81d0SAlejandro González 
699d76a81d0SAlejandro González 	if (device_may_wakeup(dev))
700d76a81d0SAlejandro González 		disable_irq_wake(chip->irq);
701d76a81d0SAlejandro González 
702d76a81d0SAlejandro González 	return 0;
703d76a81d0SAlejandro González }
704d76a81d0SAlejandro González #endif
705d76a81d0SAlejandro González 
706d76a81d0SAlejandro González static SIMPLE_DEV_PM_OPS(sun6i_rtc_pm_ops,
707d76a81d0SAlejandro González 	sun6i_rtc_suspend, sun6i_rtc_resume);
708d76a81d0SAlejandro González 
7092ca03e29SSamuel Holland static void sun6i_rtc_bus_clk_cleanup(void *data)
7102ca03e29SSamuel Holland {
7112ca03e29SSamuel Holland 	struct clk *bus_clk = data;
7122ca03e29SSamuel Holland 
7132ca03e29SSamuel Holland 	clk_disable_unprepare(bus_clk);
7142ca03e29SSamuel Holland }
7152ca03e29SSamuel Holland 
7169765d2d9SChen-Yu Tsai static int sun6i_rtc_probe(struct platform_device *pdev)
7179765d2d9SChen-Yu Tsai {
7183855c2c3SMaxime Ripard 	struct sun6i_rtc_dev *chip = sun6i_rtc;
7192ca03e29SSamuel Holland 	struct device *dev = &pdev->dev;
7202ca03e29SSamuel Holland 	struct clk *bus_clk;
7219765d2d9SChen-Yu Tsai 	int ret;
7229765d2d9SChen-Yu Tsai 
7232ca03e29SSamuel Holland 	bus_clk = devm_clk_get_optional(dev, "bus");
7242ca03e29SSamuel Holland 	if (IS_ERR(bus_clk))
7252ca03e29SSamuel Holland 		return PTR_ERR(bus_clk);
7262ca03e29SSamuel Holland 
7272ca03e29SSamuel Holland 	if (bus_clk) {
7282ca03e29SSamuel Holland 		ret = clk_prepare_enable(bus_clk);
7292ca03e29SSamuel Holland 		if (ret)
7302ca03e29SSamuel Holland 			return ret;
7312ca03e29SSamuel Holland 
7322ca03e29SSamuel Holland 		ret = devm_add_action_or_reset(dev, sun6i_rtc_bus_clk_cleanup,
7332ca03e29SSamuel Holland 					       bus_clk);
7342ca03e29SSamuel Holland 		if (ret)
7352ca03e29SSamuel Holland 			return ret;
7362ca03e29SSamuel Holland 	}
7372ca03e29SSamuel Holland 
738814691c7SSamuel Holland 	if (!chip) {
739814691c7SSamuel Holland 		chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
7409765d2d9SChen-Yu Tsai 		if (!chip)
741814691c7SSamuel Holland 			return -ENOMEM;
742814691c7SSamuel Holland 
743814691c7SSamuel Holland 		spin_lock_init(&chip->lock);
744814691c7SSamuel Holland 
745814691c7SSamuel Holland 		chip->base = devm_platform_ioremap_resource(pdev, 0);
746814691c7SSamuel Holland 		if (IS_ERR(chip->base))
747814691c7SSamuel Holland 			return PTR_ERR(chip->base);
748*d91612d7SSamuel Holland 
749*d91612d7SSamuel Holland 		if (IS_REACHABLE(CONFIG_SUN6I_RTC_CCU)) {
750*d91612d7SSamuel Holland 			ret = sun6i_rtc_ccu_probe(dev, chip->base);
751*d91612d7SSamuel Holland 			if (ret)
752*d91612d7SSamuel Holland 				return ret;
753*d91612d7SSamuel Holland 		}
754814691c7SSamuel Holland 	}
7559765d2d9SChen-Yu Tsai 
7569765d2d9SChen-Yu Tsai 	platform_set_drvdata(pdev, chip);
7579765d2d9SChen-Yu Tsai 
758648c151aSAndre Przywara 	chip->flags = (unsigned long)of_device_get_match_data(&pdev->dev);
759648c151aSAndre Przywara 
7609765d2d9SChen-Yu Tsai 	chip->irq = platform_get_irq(pdev, 0);
761faac9102SStephen Boyd 	if (chip->irq < 0)
7629765d2d9SChen-Yu Tsai 		return chip->irq;
7639765d2d9SChen-Yu Tsai 
7649765d2d9SChen-Yu Tsai 	ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
7659765d2d9SChen-Yu Tsai 			       0, dev_name(&pdev->dev), chip);
7669765d2d9SChen-Yu Tsai 	if (ret) {
7679765d2d9SChen-Yu Tsai 		dev_err(&pdev->dev, "Could not request IRQ\n");
7689765d2d9SChen-Yu Tsai 		return ret;
7699765d2d9SChen-Yu Tsai 	}
7709765d2d9SChen-Yu Tsai 
7719765d2d9SChen-Yu Tsai 	/* clear the alarm counter value */
7729765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_COUNTER);
7739765d2d9SChen-Yu Tsai 
7749765d2d9SChen-Yu Tsai 	/* disable counter alarm */
7759765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_EN);
7769765d2d9SChen-Yu Tsai 
7779765d2d9SChen-Yu Tsai 	/* disable counter alarm interrupt */
7789765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
7799765d2d9SChen-Yu Tsai 
7809765d2d9SChen-Yu Tsai 	/* disable week alarm */
7819765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM1_EN);
7829765d2d9SChen-Yu Tsai 
7839765d2d9SChen-Yu Tsai 	/* disable week alarm interrupt */
7849765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
7859765d2d9SChen-Yu Tsai 
7869765d2d9SChen-Yu Tsai 	/* clear counter alarm pending interrupts */
7879765d2d9SChen-Yu Tsai 	writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
7889765d2d9SChen-Yu Tsai 	       chip->base + SUN6I_ALRM_IRQ_STA);
7899765d2d9SChen-Yu Tsai 
7909765d2d9SChen-Yu Tsai 	/* clear week alarm pending interrupts */
7919765d2d9SChen-Yu Tsai 	writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
7929765d2d9SChen-Yu Tsai 	       chip->base + SUN6I_ALRM1_IRQ_STA);
7939765d2d9SChen-Yu Tsai 
7949765d2d9SChen-Yu Tsai 	/* disable alarm wakeup */
7959765d2d9SChen-Yu Tsai 	writel(0, chip->base + SUN6I_ALARM_CONFIG);
7969765d2d9SChen-Yu Tsai 
7973855c2c3SMaxime Ripard 	clk_prepare_enable(chip->losc);
798fb61bb82SMaxime Ripard 
799d76a81d0SAlejandro González 	device_init_wakeup(&pdev->dev, 1);
800d76a81d0SAlejandro González 
8018ae79be7SAlexandre Belloni 	chip->rtc = devm_rtc_allocate_device(&pdev->dev);
8028ae79be7SAlexandre Belloni 	if (IS_ERR(chip->rtc))
8039765d2d9SChen-Yu Tsai 		return PTR_ERR(chip->rtc);
8048ae79be7SAlexandre Belloni 
8058ae79be7SAlexandre Belloni 	chip->rtc->ops = &sun6i_rtc_ops;
806648c151aSAndre Przywara 	if (chip->flags & RTC_LINEAR_DAY)
807648c151aSAndre Przywara 		chip->rtc->range_max = (65536 * SECS_PER_DAY) - 1;
808648c151aSAndre Przywara 	else
8098ae79be7SAlexandre Belloni 		chip->rtc->range_max = 2019686399LL; /* 2033-12-31 23:59:59 */
8108ae79be7SAlexandre Belloni 
811fdcfd854SBartosz Golaszewski 	ret = devm_rtc_register_device(chip->rtc);
8128ae79be7SAlexandre Belloni 	if (ret)
8138ae79be7SAlexandre Belloni 		return ret;
8149765d2d9SChen-Yu Tsai 
8159765d2d9SChen-Yu Tsai 	dev_info(&pdev->dev, "RTC enabled\n");
8169765d2d9SChen-Yu Tsai 
8179765d2d9SChen-Yu Tsai 	return 0;
8189765d2d9SChen-Yu Tsai }
8199765d2d9SChen-Yu Tsai 
820403a3c3dSChen-Yu Tsai /*
821403a3c3dSChen-Yu Tsai  * As far as RTC functionality goes, all models are the same. The
822403a3c3dSChen-Yu Tsai  * datasheets claim that different models have different number of
823403a3c3dSChen-Yu Tsai  * registers available for non-volatile storage, but experiments show
824403a3c3dSChen-Yu Tsai  * that all SoCs have 16 registers available for this purpose.
825403a3c3dSChen-Yu Tsai  */
8269765d2d9SChen-Yu Tsai static const struct of_device_id sun6i_rtc_dt_ids[] = {
8279765d2d9SChen-Yu Tsai 	{ .compatible = "allwinner,sun6i-a31-rtc" },
8287cd1acaeSChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a23-rtc" },
8297cd1acaeSChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-h3-rtc" },
830d6624cc7SMaxime Ripard 	{ .compatible = "allwinner,sun8i-r40-rtc" },
8317cd1acaeSChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-v3-rtc" },
8327cd1acaeSChen-Yu Tsai 	{ .compatible = "allwinner,sun50i-h5-rtc" },
833b60ff2cfSOndrej Jirman 	{ .compatible = "allwinner,sun50i-h6-rtc" },
8348a937203SAndre Przywara 	{ .compatible = "allwinner,sun50i-h616-rtc",
8358a937203SAndre Przywara 		.data = (void *)RTC_LINEAR_DAY },
8369765d2d9SChen-Yu Tsai 	{ /* sentinel */ },
8379765d2d9SChen-Yu Tsai };
8389765d2d9SChen-Yu Tsai MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
8399765d2d9SChen-Yu Tsai 
8409765d2d9SChen-Yu Tsai static struct platform_driver sun6i_rtc_driver = {
8419765d2d9SChen-Yu Tsai 	.probe		= sun6i_rtc_probe,
8429765d2d9SChen-Yu Tsai 	.driver		= {
8439765d2d9SChen-Yu Tsai 		.name		= "sun6i-rtc",
8449765d2d9SChen-Yu Tsai 		.of_match_table = sun6i_rtc_dt_ids,
845d76a81d0SAlejandro González 		.pm = &sun6i_rtc_pm_ops,
8469765d2d9SChen-Yu Tsai 	},
8479765d2d9SChen-Yu Tsai };
84837539414SMaxime Ripard builtin_platform_driver(sun6i_rtc_driver);
849