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> 169765d2d9SChen-Yu Tsai #include <linux/delay.h> 179765d2d9SChen-Yu Tsai #include <linux/err.h> 189765d2d9SChen-Yu Tsai #include <linux/fs.h> 199765d2d9SChen-Yu Tsai #include <linux/init.h> 209765d2d9SChen-Yu Tsai #include <linux/interrupt.h> 219765d2d9SChen-Yu Tsai #include <linux/io.h> 229765d2d9SChen-Yu Tsai #include <linux/kernel.h> 239765d2d9SChen-Yu Tsai #include <linux/module.h> 249765d2d9SChen-Yu Tsai #include <linux/of.h> 259765d2d9SChen-Yu Tsai #include <linux/of_address.h> 269765d2d9SChen-Yu Tsai #include <linux/of_device.h> 279765d2d9SChen-Yu Tsai #include <linux/platform_device.h> 289765d2d9SChen-Yu Tsai #include <linux/rtc.h> 293855c2c3SMaxime Ripard #include <linux/slab.h> 309765d2d9SChen-Yu Tsai #include <linux/types.h> 319765d2d9SChen-Yu Tsai 329765d2d9SChen-Yu Tsai /* Control register */ 339765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL 0x0000 34fb61bb82SMaxime Ripard #define SUN6I_LOSC_CTRL_KEY (0x16aa << 16) 35b60ff2cfSOndrej Jirman #define SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS BIT(15) 369765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9) 379765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8) 389765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7) 39b60ff2cfSOndrej Jirman #define SUN6I_LOSC_CTRL_EXT_LOSC_EN BIT(4) 40fb61bb82SMaxime Ripard #define SUN6I_LOSC_CTRL_EXT_OSC BIT(0) 419765d2d9SChen-Yu Tsai #define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7) 429765d2d9SChen-Yu Tsai 433855c2c3SMaxime Ripard #define SUN6I_LOSC_CLK_PRESCAL 0x0008 443855c2c3SMaxime Ripard 459765d2d9SChen-Yu Tsai /* RTC */ 469765d2d9SChen-Yu Tsai #define SUN6I_RTC_YMD 0x0010 479765d2d9SChen-Yu Tsai #define SUN6I_RTC_HMS 0x0014 489765d2d9SChen-Yu Tsai 499765d2d9SChen-Yu Tsai /* Alarm 0 (counter) */ 509765d2d9SChen-Yu Tsai #define SUN6I_ALRM_COUNTER 0x0020 519765d2d9SChen-Yu Tsai #define SUN6I_ALRM_CUR_VAL 0x0024 529765d2d9SChen-Yu Tsai #define SUN6I_ALRM_EN 0x0028 539765d2d9SChen-Yu Tsai #define SUN6I_ALRM_EN_CNT_EN BIT(0) 549765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_EN 0x002c 559765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0) 569765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_STA 0x0030 579765d2d9SChen-Yu Tsai #define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0) 589765d2d9SChen-Yu Tsai 599765d2d9SChen-Yu Tsai /* Alarm 1 (wall clock) */ 609765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_EN 0x0044 619765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_EN 0x0048 629765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_STA 0x004c 639765d2d9SChen-Yu Tsai #define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND BIT(0) 649765d2d9SChen-Yu Tsai 659765d2d9SChen-Yu Tsai /* Alarm config */ 669765d2d9SChen-Yu Tsai #define SUN6I_ALARM_CONFIG 0x0050 679765d2d9SChen-Yu Tsai #define SUN6I_ALARM_CONFIG_WAKEUP BIT(0) 689765d2d9SChen-Yu Tsai 6917ecd246SMaxime Ripard #define SUN6I_LOSC_OUT_GATING 0x0060 7009018d4bSMichael Trimarchi #define SUN6I_LOSC_OUT_GATING_EN_OFFSET 0 7117ecd246SMaxime Ripard 729765d2d9SChen-Yu Tsai /* 739765d2d9SChen-Yu Tsai * Get date values 749765d2d9SChen-Yu Tsai */ 759765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_DAY_VALUE(x) ((x) & 0x0000001f) 769765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_MON_VALUE(x) (((x) & 0x00000f00) >> 8) 779765d2d9SChen-Yu Tsai #define SUN6I_DATE_GET_YEAR_VALUE(x) (((x) & 0x003f0000) >> 16) 789765d2d9SChen-Yu Tsai #define SUN6I_LEAP_GET_VALUE(x) (((x) & 0x00400000) >> 22) 799765d2d9SChen-Yu Tsai 809765d2d9SChen-Yu Tsai /* 819765d2d9SChen-Yu Tsai * Get time values 829765d2d9SChen-Yu Tsai */ 839765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_SEC_VALUE(x) ((x) & 0x0000003f) 849765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_MIN_VALUE(x) (((x) & 0x00003f00) >> 8) 859765d2d9SChen-Yu Tsai #define SUN6I_TIME_GET_HOUR_VALUE(x) (((x) & 0x001f0000) >> 16) 869765d2d9SChen-Yu Tsai 879765d2d9SChen-Yu Tsai /* 889765d2d9SChen-Yu Tsai * Set date values 899765d2d9SChen-Yu Tsai */ 909765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_DAY_VALUE(x) ((x) & 0x0000001f) 919765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_MON_VALUE(x) ((x) << 8 & 0x00000f00) 929765d2d9SChen-Yu Tsai #define SUN6I_DATE_SET_YEAR_VALUE(x) ((x) << 16 & 0x003f0000) 939765d2d9SChen-Yu Tsai #define SUN6I_LEAP_SET_VALUE(x) ((x) << 22 & 0x00400000) 949765d2d9SChen-Yu Tsai 959765d2d9SChen-Yu Tsai /* 969765d2d9SChen-Yu Tsai * Set time values 979765d2d9SChen-Yu Tsai */ 989765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_SEC_VALUE(x) ((x) & 0x0000003f) 999765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_MIN_VALUE(x) ((x) << 8 & 0x00003f00) 1009765d2d9SChen-Yu Tsai #define SUN6I_TIME_SET_HOUR_VALUE(x) ((x) << 16 & 0x001f0000) 1019765d2d9SChen-Yu Tsai 1029765d2d9SChen-Yu Tsai /* 1039765d2d9SChen-Yu Tsai * The year parameter passed to the driver is usually an offset relative to 1049765d2d9SChen-Yu Tsai * the year 1900. This macro is used to convert this offset to another one 1059765d2d9SChen-Yu Tsai * relative to the minimum year allowed by the hardware. 1069765d2d9SChen-Yu Tsai * 1079765d2d9SChen-Yu Tsai * The year range is 1970 - 2033. This range is selected to match Allwinner's 1089765d2d9SChen-Yu Tsai * driver, even though it is somewhat limited. 1099765d2d9SChen-Yu Tsai */ 1109765d2d9SChen-Yu Tsai #define SUN6I_YEAR_MIN 1970 1119765d2d9SChen-Yu Tsai #define SUN6I_YEAR_MAX 2033 1129765d2d9SChen-Yu Tsai #define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900) 1139765d2d9SChen-Yu Tsai 114403a3c3dSChen-Yu Tsai /* 115403a3c3dSChen-Yu Tsai * There are other differences between models, including: 116403a3c3dSChen-Yu Tsai * 117403a3c3dSChen-Yu Tsai * - number of GPIO pins that can be configured to hold a certain level 118403a3c3dSChen-Yu Tsai * - crypto-key related registers (H5, H6) 119403a3c3dSChen-Yu Tsai * - boot process related (super standby, secondary processor entry address) 120403a3c3dSChen-Yu Tsai * registers (R40, H6) 121403a3c3dSChen-Yu Tsai * - SYS power domain controls (R40) 122403a3c3dSChen-Yu Tsai * - DCXO controls (H6) 123403a3c3dSChen-Yu Tsai * - RC oscillator calibration (H6) 124403a3c3dSChen-Yu Tsai * 125403a3c3dSChen-Yu Tsai * These functions are not covered by this driver. 126403a3c3dSChen-Yu Tsai */ 127403a3c3dSChen-Yu Tsai struct sun6i_rtc_clk_data { 128403a3c3dSChen-Yu Tsai unsigned long rc_osc_rate; 129403a3c3dSChen-Yu Tsai unsigned int fixed_prescaler : 16; 130403a3c3dSChen-Yu Tsai unsigned int has_prescaler : 1; 131403a3c3dSChen-Yu Tsai unsigned int has_out_clk : 1; 132c56afc18SChen-Yu Tsai unsigned int export_iosc : 1; 133b60ff2cfSOndrej Jirman unsigned int has_losc_en : 1; 134b60ff2cfSOndrej Jirman unsigned int has_auto_swt : 1; 135403a3c3dSChen-Yu Tsai }; 136403a3c3dSChen-Yu Tsai 1379765d2d9SChen-Yu Tsai struct sun6i_rtc_dev { 1389765d2d9SChen-Yu Tsai struct rtc_device *rtc; 139403a3c3dSChen-Yu Tsai const struct sun6i_rtc_clk_data *data; 1409765d2d9SChen-Yu Tsai void __iomem *base; 1419765d2d9SChen-Yu Tsai int irq; 1429765d2d9SChen-Yu Tsai unsigned long alarm; 143a9422a19SMaxime Ripard 1443855c2c3SMaxime Ripard struct clk_hw hw; 1453855c2c3SMaxime Ripard struct clk_hw *int_osc; 1463855c2c3SMaxime Ripard struct clk *losc; 14717ecd246SMaxime Ripard struct clk *ext_losc; 1483855c2c3SMaxime Ripard 149a9422a19SMaxime Ripard spinlock_t lock; 1509765d2d9SChen-Yu Tsai }; 1519765d2d9SChen-Yu Tsai 1523855c2c3SMaxime Ripard static struct sun6i_rtc_dev *sun6i_rtc; 1533855c2c3SMaxime Ripard 1543855c2c3SMaxime Ripard static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw, 1553855c2c3SMaxime Ripard unsigned long parent_rate) 1563855c2c3SMaxime Ripard { 1573855c2c3SMaxime Ripard struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw); 158403a3c3dSChen-Yu Tsai u32 val = 0; 1593855c2c3SMaxime Ripard 1603855c2c3SMaxime Ripard val = readl(rtc->base + SUN6I_LOSC_CTRL); 1613855c2c3SMaxime Ripard if (val & SUN6I_LOSC_CTRL_EXT_OSC) 1623855c2c3SMaxime Ripard return parent_rate; 1633855c2c3SMaxime Ripard 164403a3c3dSChen-Yu Tsai if (rtc->data->fixed_prescaler) 165403a3c3dSChen-Yu Tsai parent_rate /= rtc->data->fixed_prescaler; 166403a3c3dSChen-Yu Tsai 167403a3c3dSChen-Yu Tsai if (rtc->data->has_prescaler) { 1683855c2c3SMaxime Ripard val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL); 1693855c2c3SMaxime Ripard val &= GENMASK(4, 0); 170403a3c3dSChen-Yu Tsai } 1713855c2c3SMaxime Ripard 1723855c2c3SMaxime Ripard return parent_rate / (val + 1); 1733855c2c3SMaxime Ripard } 1743855c2c3SMaxime Ripard 1753855c2c3SMaxime Ripard static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw) 1763855c2c3SMaxime Ripard { 1773855c2c3SMaxime Ripard struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw); 1783855c2c3SMaxime Ripard 1793855c2c3SMaxime Ripard return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC; 1803855c2c3SMaxime Ripard } 1813855c2c3SMaxime Ripard 1823855c2c3SMaxime Ripard static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index) 1833855c2c3SMaxime Ripard { 1843855c2c3SMaxime Ripard struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw); 1853855c2c3SMaxime Ripard unsigned long flags; 1863855c2c3SMaxime Ripard u32 val; 1873855c2c3SMaxime Ripard 1883855c2c3SMaxime Ripard if (index > 1) 1893855c2c3SMaxime Ripard return -EINVAL; 1903855c2c3SMaxime Ripard 1913855c2c3SMaxime Ripard spin_lock_irqsave(&rtc->lock, flags); 1923855c2c3SMaxime Ripard val = readl(rtc->base + SUN6I_LOSC_CTRL); 1933855c2c3SMaxime Ripard val &= ~SUN6I_LOSC_CTRL_EXT_OSC; 1943855c2c3SMaxime Ripard val |= SUN6I_LOSC_CTRL_KEY; 1953855c2c3SMaxime Ripard val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0; 196b60ff2cfSOndrej Jirman if (rtc->data->has_losc_en) { 197b60ff2cfSOndrej Jirman val &= ~SUN6I_LOSC_CTRL_EXT_LOSC_EN; 198b60ff2cfSOndrej Jirman val |= index ? SUN6I_LOSC_CTRL_EXT_LOSC_EN : 0; 199b60ff2cfSOndrej Jirman } 2003855c2c3SMaxime Ripard writel(val, rtc->base + SUN6I_LOSC_CTRL); 2013855c2c3SMaxime Ripard spin_unlock_irqrestore(&rtc->lock, flags); 2023855c2c3SMaxime Ripard 2033855c2c3SMaxime Ripard return 0; 2043855c2c3SMaxime Ripard } 2053855c2c3SMaxime Ripard 2063855c2c3SMaxime Ripard static const struct clk_ops sun6i_rtc_osc_ops = { 2073855c2c3SMaxime Ripard .recalc_rate = sun6i_rtc_osc_recalc_rate, 2083855c2c3SMaxime Ripard 2093855c2c3SMaxime Ripard .get_parent = sun6i_rtc_osc_get_parent, 2103855c2c3SMaxime Ripard .set_parent = sun6i_rtc_osc_set_parent, 2113855c2c3SMaxime Ripard }; 2123855c2c3SMaxime Ripard 213403a3c3dSChen-Yu Tsai static void __init sun6i_rtc_clk_init(struct device_node *node, 214403a3c3dSChen-Yu Tsai const struct sun6i_rtc_clk_data *data) 2153855c2c3SMaxime Ripard { 2163855c2c3SMaxime Ripard struct clk_hw_onecell_data *clk_data; 2173855c2c3SMaxime Ripard struct sun6i_rtc_dev *rtc; 2183855c2c3SMaxime Ripard struct clk_init_data init = { 2193855c2c3SMaxime Ripard .ops = &sun6i_rtc_osc_ops, 220459b6ea0SChen-Yu Tsai .name = "losc", 2213855c2c3SMaxime Ripard }; 222c56afc18SChen-Yu Tsai const char *iosc_name = "rtc-int-osc"; 22317ecd246SMaxime Ripard const char *clkout_name = "osc32k-out"; 2243855c2c3SMaxime Ripard const char *parents[2]; 225b60ff2cfSOndrej Jirman u32 reg; 2263855c2c3SMaxime Ripard 2273855c2c3SMaxime Ripard rtc = kzalloc(sizeof(*rtc), GFP_KERNEL); 2283855c2c3SMaxime Ripard if (!rtc) 2293855c2c3SMaxime Ripard return; 2303855c2c3SMaxime Ripard 231403a3c3dSChen-Yu Tsai rtc->data = data; 232c56afc18SChen-Yu Tsai clk_data = kzalloc(struct_size(clk_data, hws, 3), GFP_KERNEL); 233e9982024SColin Ian King if (!clk_data) { 234e9982024SColin Ian King kfree(rtc); 2353855c2c3SMaxime Ripard return; 236e9982024SColin Ian King } 237319ff835SAlexey Klimov 2383855c2c3SMaxime Ripard spin_lock_init(&rtc->lock); 2393855c2c3SMaxime Ripard 2403855c2c3SMaxime Ripard rtc->base = of_io_request_and_map(node, 0, of_node_full_name(node)); 241aaa65a9cSWei Yongjun if (IS_ERR(rtc->base)) { 2423855c2c3SMaxime Ripard pr_crit("Can't map RTC registers"); 2431a37c348SColin Ian King goto err; 2443855c2c3SMaxime Ripard } 2453855c2c3SMaxime Ripard 246b60ff2cfSOndrej Jirman reg = SUN6I_LOSC_CTRL_KEY; 247b60ff2cfSOndrej Jirman if (rtc->data->has_auto_swt) { 248b60ff2cfSOndrej Jirman /* Bypass auto-switch to int osc, on ext losc failure */ 249b60ff2cfSOndrej Jirman reg |= SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS; 250b60ff2cfSOndrej Jirman writel(reg, rtc->base + SUN6I_LOSC_CTRL); 251b60ff2cfSOndrej Jirman } 252b60ff2cfSOndrej Jirman 253*ec98a875SJernej Skrabec /* Switch to the external, more precise, oscillator, if present */ 254*ec98a875SJernej Skrabec if (of_get_property(node, "clocks", NULL)) { 255b60ff2cfSOndrej Jirman reg |= SUN6I_LOSC_CTRL_EXT_OSC; 256b60ff2cfSOndrej Jirman if (rtc->data->has_losc_en) 257b60ff2cfSOndrej Jirman reg |= SUN6I_LOSC_CTRL_EXT_LOSC_EN; 258*ec98a875SJernej Skrabec } 259b60ff2cfSOndrej Jirman writel(reg, rtc->base + SUN6I_LOSC_CTRL); 2603855c2c3SMaxime Ripard 26115829cf4SChen-Yu Tsai /* Yes, I know, this is ugly. */ 26215829cf4SChen-Yu Tsai sun6i_rtc = rtc; 26315829cf4SChen-Yu Tsai 264c56afc18SChen-Yu Tsai /* Only read IOSC name from device tree if it is exported */ 265c56afc18SChen-Yu Tsai if (rtc->data->export_iosc) 266c56afc18SChen-Yu Tsai of_property_read_string_index(node, "clock-output-names", 2, 267c56afc18SChen-Yu Tsai &iosc_name); 268c56afc18SChen-Yu Tsai 2693855c2c3SMaxime Ripard rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL, 270c56afc18SChen-Yu Tsai iosc_name, 2713855c2c3SMaxime Ripard NULL, 0, 272403a3c3dSChen-Yu Tsai rtc->data->rc_osc_rate, 2733855c2c3SMaxime Ripard 300000000); 2743855c2c3SMaxime Ripard if (IS_ERR(rtc->int_osc)) { 2753855c2c3SMaxime Ripard pr_crit("Couldn't register the internal oscillator\n"); 2763855c2c3SMaxime Ripard return; 2773855c2c3SMaxime Ripard } 2783855c2c3SMaxime Ripard 2793855c2c3SMaxime Ripard parents[0] = clk_hw_get_name(rtc->int_osc); 280*ec98a875SJernej Skrabec /* If there is no external oscillator, this will be NULL and ... */ 2813855c2c3SMaxime Ripard parents[1] = of_clk_get_parent_name(node, 0); 2823855c2c3SMaxime Ripard 2833855c2c3SMaxime Ripard rtc->hw.init = &init; 2843855c2c3SMaxime Ripard 2853855c2c3SMaxime Ripard init.parent_names = parents; 286*ec98a875SJernej Skrabec /* ... number of clock parents will be 1. */ 2873855c2c3SMaxime Ripard init.num_parents = of_clk_get_parent_count(node) + 1; 28817ecd246SMaxime Ripard of_property_read_string_index(node, "clock-output-names", 0, 28917ecd246SMaxime Ripard &init.name); 2903855c2c3SMaxime Ripard 2913855c2c3SMaxime Ripard rtc->losc = clk_register(NULL, &rtc->hw); 2923855c2c3SMaxime Ripard if (IS_ERR(rtc->losc)) { 2933855c2c3SMaxime Ripard pr_crit("Couldn't register the LOSC clock\n"); 2943855c2c3SMaxime Ripard return; 2953855c2c3SMaxime Ripard } 2963855c2c3SMaxime Ripard 29717ecd246SMaxime Ripard of_property_read_string_index(node, "clock-output-names", 1, 29817ecd246SMaxime Ripard &clkout_name); 29921ef77deSStephen Boyd rtc->ext_losc = clk_register_gate(NULL, clkout_name, init.name, 30017ecd246SMaxime Ripard 0, rtc->base + SUN6I_LOSC_OUT_GATING, 30109018d4bSMichael Trimarchi SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0, 30217ecd246SMaxime Ripard &rtc->lock); 30317ecd246SMaxime Ripard if (IS_ERR(rtc->ext_losc)) { 30417ecd246SMaxime Ripard pr_crit("Couldn't register the LOSC external gate\n"); 30517ecd246SMaxime Ripard return; 30617ecd246SMaxime Ripard } 30717ecd246SMaxime Ripard 30817ecd246SMaxime Ripard clk_data->num = 2; 3093855c2c3SMaxime Ripard clk_data->hws[0] = &rtc->hw; 31017ecd246SMaxime Ripard clk_data->hws[1] = __clk_get_hw(rtc->ext_losc); 311c56afc18SChen-Yu Tsai if (rtc->data->export_iosc) { 312c56afc18SChen-Yu Tsai clk_data->hws[2] = rtc->int_osc; 313c56afc18SChen-Yu Tsai clk_data->num = 3; 314c56afc18SChen-Yu Tsai } 3153855c2c3SMaxime Ripard of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data); 3161a37c348SColin Ian King return; 3171a37c348SColin Ian King 3181a37c348SColin Ian King err: 3191a37c348SColin Ian King kfree(clk_data); 3203855c2c3SMaxime Ripard } 321403a3c3dSChen-Yu Tsai 322403a3c3dSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun6i_a31_rtc_data = { 323403a3c3dSChen-Yu Tsai .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */ 324403a3c3dSChen-Yu Tsai .has_prescaler = 1, 325403a3c3dSChen-Yu Tsai }; 326403a3c3dSChen-Yu Tsai 327403a3c3dSChen-Yu Tsai static void __init sun6i_a31_rtc_clk_init(struct device_node *node) 328403a3c3dSChen-Yu Tsai { 329403a3c3dSChen-Yu Tsai sun6i_rtc_clk_init(node, &sun6i_a31_rtc_data); 330403a3c3dSChen-Yu Tsai } 331403a3c3dSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun6i_a31_rtc_clk, "allwinner,sun6i-a31-rtc", 332403a3c3dSChen-Yu Tsai sun6i_a31_rtc_clk_init); 3333855c2c3SMaxime Ripard 3347cd1acaeSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_a23_rtc_data = { 3357cd1acaeSChen-Yu Tsai .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */ 3367cd1acaeSChen-Yu Tsai .has_prescaler = 1, 3377cd1acaeSChen-Yu Tsai .has_out_clk = 1, 3387cd1acaeSChen-Yu Tsai }; 3397cd1acaeSChen-Yu Tsai 3407cd1acaeSChen-Yu Tsai static void __init sun8i_a23_rtc_clk_init(struct device_node *node) 3417cd1acaeSChen-Yu Tsai { 3427cd1acaeSChen-Yu Tsai sun6i_rtc_clk_init(node, &sun8i_a23_rtc_data); 3437cd1acaeSChen-Yu Tsai } 3447cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_a23_rtc_clk, "allwinner,sun8i-a23-rtc", 3457cd1acaeSChen-Yu Tsai sun8i_a23_rtc_clk_init); 3467cd1acaeSChen-Yu Tsai 3477cd1acaeSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_h3_rtc_data = { 3487cd1acaeSChen-Yu Tsai .rc_osc_rate = 16000000, 3497cd1acaeSChen-Yu Tsai .fixed_prescaler = 32, 3507cd1acaeSChen-Yu Tsai .has_prescaler = 1, 3517cd1acaeSChen-Yu Tsai .has_out_clk = 1, 352c56afc18SChen-Yu Tsai .export_iosc = 1, 3537cd1acaeSChen-Yu Tsai }; 3547cd1acaeSChen-Yu Tsai 3557cd1acaeSChen-Yu Tsai static void __init sun8i_h3_rtc_clk_init(struct device_node *node) 3567cd1acaeSChen-Yu Tsai { 3577cd1acaeSChen-Yu Tsai sun6i_rtc_clk_init(node, &sun8i_h3_rtc_data); 3587cd1acaeSChen-Yu Tsai } 3597cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_h3_rtc_clk, "allwinner,sun8i-h3-rtc", 3607cd1acaeSChen-Yu Tsai sun8i_h3_rtc_clk_init); 3617cd1acaeSChen-Yu Tsai /* As far as we are concerned, clocks for H5 are the same as H3 */ 3627cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun50i_h5_rtc_clk, "allwinner,sun50i-h5-rtc", 3637cd1acaeSChen-Yu Tsai sun8i_h3_rtc_clk_init); 3647cd1acaeSChen-Yu Tsai 365b60ff2cfSOndrej Jirman static const struct sun6i_rtc_clk_data sun50i_h6_rtc_data = { 366b60ff2cfSOndrej Jirman .rc_osc_rate = 16000000, 367b60ff2cfSOndrej Jirman .fixed_prescaler = 32, 368b60ff2cfSOndrej Jirman .has_prescaler = 1, 369b60ff2cfSOndrej Jirman .has_out_clk = 1, 370b60ff2cfSOndrej Jirman .export_iosc = 1, 371b60ff2cfSOndrej Jirman .has_losc_en = 1, 372b60ff2cfSOndrej Jirman .has_auto_swt = 1, 373b60ff2cfSOndrej Jirman }; 374b60ff2cfSOndrej Jirman 375b60ff2cfSOndrej Jirman static void __init sun50i_h6_rtc_clk_init(struct device_node *node) 376b60ff2cfSOndrej Jirman { 377b60ff2cfSOndrej Jirman sun6i_rtc_clk_init(node, &sun50i_h6_rtc_data); 378b60ff2cfSOndrej Jirman } 379b60ff2cfSOndrej Jirman CLK_OF_DECLARE_DRIVER(sun50i_h6_rtc_clk, "allwinner,sun50i-h6-rtc", 380b60ff2cfSOndrej Jirman sun50i_h6_rtc_clk_init); 381b60ff2cfSOndrej Jirman 382111bf02bSChen-Yu Tsai /* 383111bf02bSChen-Yu Tsai * The R40 user manual is self-conflicting on whether the prescaler is 384111bf02bSChen-Yu Tsai * fixed or configurable. The clock diagram shows it as fixed, but there 385111bf02bSChen-Yu Tsai * is also a configurable divider in the RTC block. 386111bf02bSChen-Yu Tsai */ 387111bf02bSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_r40_rtc_data = { 388111bf02bSChen-Yu Tsai .rc_osc_rate = 16000000, 389111bf02bSChen-Yu Tsai .fixed_prescaler = 512, 390111bf02bSChen-Yu Tsai }; 391111bf02bSChen-Yu Tsai static void __init sun8i_r40_rtc_clk_init(struct device_node *node) 392111bf02bSChen-Yu Tsai { 393111bf02bSChen-Yu Tsai sun6i_rtc_clk_init(node, &sun8i_r40_rtc_data); 394111bf02bSChen-Yu Tsai } 395111bf02bSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_r40_rtc_clk, "allwinner,sun8i-r40-rtc", 396111bf02bSChen-Yu Tsai sun8i_r40_rtc_clk_init); 397111bf02bSChen-Yu Tsai 3987cd1acaeSChen-Yu Tsai static const struct sun6i_rtc_clk_data sun8i_v3_rtc_data = { 3997cd1acaeSChen-Yu Tsai .rc_osc_rate = 32000, 4007cd1acaeSChen-Yu Tsai .has_out_clk = 1, 4017cd1acaeSChen-Yu Tsai }; 4027cd1acaeSChen-Yu Tsai 4037cd1acaeSChen-Yu Tsai static void __init sun8i_v3_rtc_clk_init(struct device_node *node) 4047cd1acaeSChen-Yu Tsai { 4057cd1acaeSChen-Yu Tsai sun6i_rtc_clk_init(node, &sun8i_v3_rtc_data); 4067cd1acaeSChen-Yu Tsai } 4077cd1acaeSChen-Yu Tsai CLK_OF_DECLARE_DRIVER(sun8i_v3_rtc_clk, "allwinner,sun8i-v3-rtc", 4087cd1acaeSChen-Yu Tsai sun8i_v3_rtc_clk_init); 4097cd1acaeSChen-Yu Tsai 4109765d2d9SChen-Yu Tsai static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id) 4119765d2d9SChen-Yu Tsai { 4129765d2d9SChen-Yu Tsai struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id; 413a9422a19SMaxime Ripard irqreturn_t ret = IRQ_NONE; 4149765d2d9SChen-Yu Tsai u32 val; 4159765d2d9SChen-Yu Tsai 416a9422a19SMaxime Ripard spin_lock(&chip->lock); 4179765d2d9SChen-Yu Tsai val = readl(chip->base + SUN6I_ALRM_IRQ_STA); 4189765d2d9SChen-Yu Tsai 4199765d2d9SChen-Yu Tsai if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) { 4209765d2d9SChen-Yu Tsai val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND; 4219765d2d9SChen-Yu Tsai writel(val, chip->base + SUN6I_ALRM_IRQ_STA); 4229765d2d9SChen-Yu Tsai 4239765d2d9SChen-Yu Tsai rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF); 4249765d2d9SChen-Yu Tsai 425a9422a19SMaxime Ripard ret = IRQ_HANDLED; 4269765d2d9SChen-Yu Tsai } 427a9422a19SMaxime Ripard spin_unlock(&chip->lock); 4289765d2d9SChen-Yu Tsai 429a9422a19SMaxime Ripard return ret; 4309765d2d9SChen-Yu Tsai } 4319765d2d9SChen-Yu Tsai 4329765d2d9SChen-Yu Tsai static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip) 4339765d2d9SChen-Yu Tsai { 4349765d2d9SChen-Yu Tsai u32 alrm_val = 0; 4359765d2d9SChen-Yu Tsai u32 alrm_irq_val = 0; 4369765d2d9SChen-Yu Tsai u32 alrm_wake_val = 0; 437a9422a19SMaxime Ripard unsigned long flags; 4389765d2d9SChen-Yu Tsai 4399765d2d9SChen-Yu Tsai if (to) { 4409765d2d9SChen-Yu Tsai alrm_val = SUN6I_ALRM_EN_CNT_EN; 4419765d2d9SChen-Yu Tsai alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN; 4429765d2d9SChen-Yu Tsai alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP; 4439765d2d9SChen-Yu Tsai } else { 4449765d2d9SChen-Yu Tsai writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND, 4459765d2d9SChen-Yu Tsai chip->base + SUN6I_ALRM_IRQ_STA); 4469765d2d9SChen-Yu Tsai } 4479765d2d9SChen-Yu Tsai 448a9422a19SMaxime Ripard spin_lock_irqsave(&chip->lock, flags); 4499765d2d9SChen-Yu Tsai writel(alrm_val, chip->base + SUN6I_ALRM_EN); 4509765d2d9SChen-Yu Tsai writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN); 4519765d2d9SChen-Yu Tsai writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG); 452a9422a19SMaxime Ripard spin_unlock_irqrestore(&chip->lock, flags); 4539765d2d9SChen-Yu Tsai } 4549765d2d9SChen-Yu Tsai 4559765d2d9SChen-Yu Tsai static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm) 4569765d2d9SChen-Yu Tsai { 4579765d2d9SChen-Yu Tsai struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 4589765d2d9SChen-Yu Tsai u32 date, time; 4599765d2d9SChen-Yu Tsai 4609765d2d9SChen-Yu Tsai /* 4619765d2d9SChen-Yu Tsai * read again in case it changes 4629765d2d9SChen-Yu Tsai */ 4639765d2d9SChen-Yu Tsai do { 4649765d2d9SChen-Yu Tsai date = readl(chip->base + SUN6I_RTC_YMD); 4659765d2d9SChen-Yu Tsai time = readl(chip->base + SUN6I_RTC_HMS); 4669765d2d9SChen-Yu Tsai } while ((date != readl(chip->base + SUN6I_RTC_YMD)) || 4679765d2d9SChen-Yu Tsai (time != readl(chip->base + SUN6I_RTC_HMS))); 4689765d2d9SChen-Yu Tsai 4699765d2d9SChen-Yu Tsai rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time); 4709765d2d9SChen-Yu Tsai rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time); 4719765d2d9SChen-Yu Tsai rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time); 4729765d2d9SChen-Yu Tsai 4739765d2d9SChen-Yu Tsai rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date); 4749765d2d9SChen-Yu Tsai rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date); 4759765d2d9SChen-Yu Tsai rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date); 4769765d2d9SChen-Yu Tsai 4779765d2d9SChen-Yu Tsai rtc_tm->tm_mon -= 1; 4789765d2d9SChen-Yu Tsai 4799765d2d9SChen-Yu Tsai /* 4809765d2d9SChen-Yu Tsai * switch from (data_year->min)-relative offset to 4819765d2d9SChen-Yu Tsai * a (1900)-relative one 4829765d2d9SChen-Yu Tsai */ 4839765d2d9SChen-Yu Tsai rtc_tm->tm_year += SUN6I_YEAR_OFF; 4849765d2d9SChen-Yu Tsai 48522652ba7SAlexandre Belloni return 0; 4869765d2d9SChen-Yu Tsai } 4879765d2d9SChen-Yu Tsai 4889765d2d9SChen-Yu Tsai static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm) 4899765d2d9SChen-Yu Tsai { 4909765d2d9SChen-Yu Tsai struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 491a9422a19SMaxime Ripard unsigned long flags; 4929765d2d9SChen-Yu Tsai u32 alrm_st; 4939765d2d9SChen-Yu Tsai u32 alrm_en; 4949765d2d9SChen-Yu Tsai 495a9422a19SMaxime Ripard spin_lock_irqsave(&chip->lock, flags); 4969765d2d9SChen-Yu Tsai alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN); 4979765d2d9SChen-Yu Tsai alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA); 498a9422a19SMaxime Ripard spin_unlock_irqrestore(&chip->lock, flags); 499a9422a19SMaxime Ripard 5009765d2d9SChen-Yu Tsai wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN); 5019765d2d9SChen-Yu Tsai wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN); 5029765d2d9SChen-Yu Tsai rtc_time_to_tm(chip->alarm, &wkalrm->time); 5039765d2d9SChen-Yu Tsai 5049765d2d9SChen-Yu Tsai return 0; 5059765d2d9SChen-Yu Tsai } 5069765d2d9SChen-Yu Tsai 5079765d2d9SChen-Yu Tsai static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm) 5089765d2d9SChen-Yu Tsai { 5099765d2d9SChen-Yu Tsai struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 5109765d2d9SChen-Yu Tsai struct rtc_time *alrm_tm = &wkalrm->time; 5119765d2d9SChen-Yu Tsai struct rtc_time tm_now; 5129765d2d9SChen-Yu Tsai unsigned long time_now = 0; 5139765d2d9SChen-Yu Tsai unsigned long time_set = 0; 5149765d2d9SChen-Yu Tsai unsigned long time_gap = 0; 5159765d2d9SChen-Yu Tsai int ret = 0; 5169765d2d9SChen-Yu Tsai 5179765d2d9SChen-Yu Tsai ret = sun6i_rtc_gettime(dev, &tm_now); 5189765d2d9SChen-Yu Tsai if (ret < 0) { 5199765d2d9SChen-Yu Tsai dev_err(dev, "Error in getting time\n"); 5209765d2d9SChen-Yu Tsai return -EINVAL; 5219765d2d9SChen-Yu Tsai } 5229765d2d9SChen-Yu Tsai 5239765d2d9SChen-Yu Tsai rtc_tm_to_time(alrm_tm, &time_set); 5249765d2d9SChen-Yu Tsai rtc_tm_to_time(&tm_now, &time_now); 5259765d2d9SChen-Yu Tsai if (time_set <= time_now) { 5269765d2d9SChen-Yu Tsai dev_err(dev, "Date to set in the past\n"); 5279765d2d9SChen-Yu Tsai return -EINVAL; 5289765d2d9SChen-Yu Tsai } 5299765d2d9SChen-Yu Tsai 5309765d2d9SChen-Yu Tsai time_gap = time_set - time_now; 5319765d2d9SChen-Yu Tsai 5329765d2d9SChen-Yu Tsai if (time_gap > U32_MAX) { 5339765d2d9SChen-Yu Tsai dev_err(dev, "Date too far in the future\n"); 5349765d2d9SChen-Yu Tsai return -EINVAL; 5359765d2d9SChen-Yu Tsai } 5369765d2d9SChen-Yu Tsai 5379765d2d9SChen-Yu Tsai sun6i_rtc_setaie(0, chip); 5389765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALRM_COUNTER); 5399765d2d9SChen-Yu Tsai usleep_range(100, 300); 5409765d2d9SChen-Yu Tsai 5419765d2d9SChen-Yu Tsai writel(time_gap, chip->base + SUN6I_ALRM_COUNTER); 5429765d2d9SChen-Yu Tsai chip->alarm = time_set; 5439765d2d9SChen-Yu Tsai 5449765d2d9SChen-Yu Tsai sun6i_rtc_setaie(wkalrm->enabled, chip); 5459765d2d9SChen-Yu Tsai 5469765d2d9SChen-Yu Tsai return 0; 5479765d2d9SChen-Yu Tsai } 5489765d2d9SChen-Yu Tsai 5499765d2d9SChen-Yu Tsai static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset, 5509765d2d9SChen-Yu Tsai unsigned int mask, unsigned int ms_timeout) 5519765d2d9SChen-Yu Tsai { 5529765d2d9SChen-Yu Tsai const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout); 5539765d2d9SChen-Yu Tsai u32 reg; 5549765d2d9SChen-Yu Tsai 5559765d2d9SChen-Yu Tsai do { 5569765d2d9SChen-Yu Tsai reg = readl(chip->base + offset); 5579765d2d9SChen-Yu Tsai reg &= mask; 5589765d2d9SChen-Yu Tsai 5599765d2d9SChen-Yu Tsai if (!reg) 5609765d2d9SChen-Yu Tsai return 0; 5619765d2d9SChen-Yu Tsai 5629765d2d9SChen-Yu Tsai } while (time_before(jiffies, timeout)); 5639765d2d9SChen-Yu Tsai 5649765d2d9SChen-Yu Tsai return -ETIMEDOUT; 5659765d2d9SChen-Yu Tsai } 5669765d2d9SChen-Yu Tsai 5679765d2d9SChen-Yu Tsai static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm) 5689765d2d9SChen-Yu Tsai { 5699765d2d9SChen-Yu Tsai struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 5709765d2d9SChen-Yu Tsai u32 date = 0; 5719765d2d9SChen-Yu Tsai u32 time = 0; 5729765d2d9SChen-Yu Tsai int year; 5739765d2d9SChen-Yu Tsai 5749765d2d9SChen-Yu Tsai year = rtc_tm->tm_year + 1900; 5759765d2d9SChen-Yu Tsai if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) { 5769765d2d9SChen-Yu Tsai dev_err(dev, "rtc only supports year in range %d - %d\n", 5779765d2d9SChen-Yu Tsai SUN6I_YEAR_MIN, SUN6I_YEAR_MAX); 5789765d2d9SChen-Yu Tsai return -EINVAL; 5799765d2d9SChen-Yu Tsai } 5809765d2d9SChen-Yu Tsai 5819765d2d9SChen-Yu Tsai rtc_tm->tm_year -= SUN6I_YEAR_OFF; 5829765d2d9SChen-Yu Tsai rtc_tm->tm_mon += 1; 5839765d2d9SChen-Yu Tsai 5849765d2d9SChen-Yu Tsai date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) | 5859765d2d9SChen-Yu Tsai SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) | 5869765d2d9SChen-Yu Tsai SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year); 5879765d2d9SChen-Yu Tsai 5889765d2d9SChen-Yu Tsai if (is_leap_year(year)) 5899765d2d9SChen-Yu Tsai date |= SUN6I_LEAP_SET_VALUE(1); 5909765d2d9SChen-Yu Tsai 5919765d2d9SChen-Yu Tsai time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) | 5929765d2d9SChen-Yu Tsai SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) | 5939765d2d9SChen-Yu Tsai SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour); 5949765d2d9SChen-Yu Tsai 5959765d2d9SChen-Yu Tsai /* Check whether registers are writable */ 5969765d2d9SChen-Yu Tsai if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL, 5979765d2d9SChen-Yu Tsai SUN6I_LOSC_CTRL_ACC_MASK, 50)) { 5989765d2d9SChen-Yu Tsai dev_err(dev, "rtc is still busy.\n"); 5999765d2d9SChen-Yu Tsai return -EBUSY; 6009765d2d9SChen-Yu Tsai } 6019765d2d9SChen-Yu Tsai 6029765d2d9SChen-Yu Tsai writel(time, chip->base + SUN6I_RTC_HMS); 6039765d2d9SChen-Yu Tsai 6049765d2d9SChen-Yu Tsai /* 6059765d2d9SChen-Yu Tsai * After writing the RTC HH-MM-SS register, the 6069765d2d9SChen-Yu Tsai * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not 6079765d2d9SChen-Yu Tsai * be cleared until the real writing operation is finished 6089765d2d9SChen-Yu Tsai */ 6099765d2d9SChen-Yu Tsai 6109765d2d9SChen-Yu Tsai if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL, 6119765d2d9SChen-Yu Tsai SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) { 6129765d2d9SChen-Yu Tsai dev_err(dev, "Failed to set rtc time.\n"); 6139765d2d9SChen-Yu Tsai return -ETIMEDOUT; 6149765d2d9SChen-Yu Tsai } 6159765d2d9SChen-Yu Tsai 6169765d2d9SChen-Yu Tsai writel(date, chip->base + SUN6I_RTC_YMD); 6179765d2d9SChen-Yu Tsai 6189765d2d9SChen-Yu Tsai /* 6199765d2d9SChen-Yu Tsai * After writing the RTC YY-MM-DD register, the 6209765d2d9SChen-Yu Tsai * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not 6219765d2d9SChen-Yu Tsai * be cleared until the real writing operation is finished 6229765d2d9SChen-Yu Tsai */ 6239765d2d9SChen-Yu Tsai 6249765d2d9SChen-Yu Tsai if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL, 6259765d2d9SChen-Yu Tsai SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) { 6269765d2d9SChen-Yu Tsai dev_err(dev, "Failed to set rtc time.\n"); 6279765d2d9SChen-Yu Tsai return -ETIMEDOUT; 6289765d2d9SChen-Yu Tsai } 6299765d2d9SChen-Yu Tsai 6309765d2d9SChen-Yu Tsai return 0; 6319765d2d9SChen-Yu Tsai } 6329765d2d9SChen-Yu Tsai 6339765d2d9SChen-Yu Tsai static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 6349765d2d9SChen-Yu Tsai { 6359765d2d9SChen-Yu Tsai struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 6369765d2d9SChen-Yu Tsai 6379765d2d9SChen-Yu Tsai if (!enabled) 6389765d2d9SChen-Yu Tsai sun6i_rtc_setaie(enabled, chip); 6399765d2d9SChen-Yu Tsai 6409765d2d9SChen-Yu Tsai return 0; 6419765d2d9SChen-Yu Tsai } 6429765d2d9SChen-Yu Tsai 6439765d2d9SChen-Yu Tsai static const struct rtc_class_ops sun6i_rtc_ops = { 6449765d2d9SChen-Yu Tsai .read_time = sun6i_rtc_gettime, 6459765d2d9SChen-Yu Tsai .set_time = sun6i_rtc_settime, 6469765d2d9SChen-Yu Tsai .read_alarm = sun6i_rtc_getalarm, 6479765d2d9SChen-Yu Tsai .set_alarm = sun6i_rtc_setalarm, 6489765d2d9SChen-Yu Tsai .alarm_irq_enable = sun6i_rtc_alarm_irq_enable 6499765d2d9SChen-Yu Tsai }; 6509765d2d9SChen-Yu Tsai 651d76a81d0SAlejandro González #ifdef CONFIG_PM_SLEEP 652d76a81d0SAlejandro González /* Enable IRQ wake on suspend, to wake up from RTC. */ 653d76a81d0SAlejandro González static int sun6i_rtc_suspend(struct device *dev) 654d76a81d0SAlejandro González { 655d76a81d0SAlejandro González struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 656d76a81d0SAlejandro González 657d76a81d0SAlejandro González if (device_may_wakeup(dev)) 658d76a81d0SAlejandro González enable_irq_wake(chip->irq); 659d76a81d0SAlejandro González 660d76a81d0SAlejandro González return 0; 661d76a81d0SAlejandro González } 662d76a81d0SAlejandro González 663d76a81d0SAlejandro González /* Disable IRQ wake on resume. */ 664d76a81d0SAlejandro González static int sun6i_rtc_resume(struct device *dev) 665d76a81d0SAlejandro González { 666d76a81d0SAlejandro González struct sun6i_rtc_dev *chip = dev_get_drvdata(dev); 667d76a81d0SAlejandro González 668d76a81d0SAlejandro González if (device_may_wakeup(dev)) 669d76a81d0SAlejandro González disable_irq_wake(chip->irq); 670d76a81d0SAlejandro González 671d76a81d0SAlejandro González return 0; 672d76a81d0SAlejandro González } 673d76a81d0SAlejandro González #endif 674d76a81d0SAlejandro González 675d76a81d0SAlejandro González static SIMPLE_DEV_PM_OPS(sun6i_rtc_pm_ops, 676d76a81d0SAlejandro González sun6i_rtc_suspend, sun6i_rtc_resume); 677d76a81d0SAlejandro González 6789765d2d9SChen-Yu Tsai static int sun6i_rtc_probe(struct platform_device *pdev) 6799765d2d9SChen-Yu Tsai { 6803855c2c3SMaxime Ripard struct sun6i_rtc_dev *chip = sun6i_rtc; 6819765d2d9SChen-Yu Tsai int ret; 6829765d2d9SChen-Yu Tsai 6839765d2d9SChen-Yu Tsai if (!chip) 6843855c2c3SMaxime Ripard return -ENODEV; 6859765d2d9SChen-Yu Tsai 6869765d2d9SChen-Yu Tsai platform_set_drvdata(pdev, chip); 6879765d2d9SChen-Yu Tsai 6889765d2d9SChen-Yu Tsai chip->irq = platform_get_irq(pdev, 0); 689faac9102SStephen Boyd if (chip->irq < 0) 6909765d2d9SChen-Yu Tsai return chip->irq; 6919765d2d9SChen-Yu Tsai 6929765d2d9SChen-Yu Tsai ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq, 6939765d2d9SChen-Yu Tsai 0, dev_name(&pdev->dev), chip); 6949765d2d9SChen-Yu Tsai if (ret) { 6959765d2d9SChen-Yu Tsai dev_err(&pdev->dev, "Could not request IRQ\n"); 6969765d2d9SChen-Yu Tsai return ret; 6979765d2d9SChen-Yu Tsai } 6989765d2d9SChen-Yu Tsai 6999765d2d9SChen-Yu Tsai /* clear the alarm counter value */ 7009765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALRM_COUNTER); 7019765d2d9SChen-Yu Tsai 7029765d2d9SChen-Yu Tsai /* disable counter alarm */ 7039765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALRM_EN); 7049765d2d9SChen-Yu Tsai 7059765d2d9SChen-Yu Tsai /* disable counter alarm interrupt */ 7069765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALRM_IRQ_EN); 7079765d2d9SChen-Yu Tsai 7089765d2d9SChen-Yu Tsai /* disable week alarm */ 7099765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALRM1_EN); 7109765d2d9SChen-Yu Tsai 7119765d2d9SChen-Yu Tsai /* disable week alarm interrupt */ 7129765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALRM1_IRQ_EN); 7139765d2d9SChen-Yu Tsai 7149765d2d9SChen-Yu Tsai /* clear counter alarm pending interrupts */ 7159765d2d9SChen-Yu Tsai writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND, 7169765d2d9SChen-Yu Tsai chip->base + SUN6I_ALRM_IRQ_STA); 7179765d2d9SChen-Yu Tsai 7189765d2d9SChen-Yu Tsai /* clear week alarm pending interrupts */ 7199765d2d9SChen-Yu Tsai writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND, 7209765d2d9SChen-Yu Tsai chip->base + SUN6I_ALRM1_IRQ_STA); 7219765d2d9SChen-Yu Tsai 7229765d2d9SChen-Yu Tsai /* disable alarm wakeup */ 7239765d2d9SChen-Yu Tsai writel(0, chip->base + SUN6I_ALARM_CONFIG); 7249765d2d9SChen-Yu Tsai 7253855c2c3SMaxime Ripard clk_prepare_enable(chip->losc); 726fb61bb82SMaxime Ripard 727d76a81d0SAlejandro González device_init_wakeup(&pdev->dev, 1); 728d76a81d0SAlejandro González 7295dff3a31SMaxime Ripard chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-sun6i", 7309765d2d9SChen-Yu Tsai &sun6i_rtc_ops, THIS_MODULE); 7319765d2d9SChen-Yu Tsai if (IS_ERR(chip->rtc)) { 7329765d2d9SChen-Yu Tsai dev_err(&pdev->dev, "unable to register device\n"); 7339765d2d9SChen-Yu Tsai return PTR_ERR(chip->rtc); 7349765d2d9SChen-Yu Tsai } 7359765d2d9SChen-Yu Tsai 7369765d2d9SChen-Yu Tsai dev_info(&pdev->dev, "RTC enabled\n"); 7379765d2d9SChen-Yu Tsai 7389765d2d9SChen-Yu Tsai return 0; 7399765d2d9SChen-Yu Tsai } 7409765d2d9SChen-Yu Tsai 741403a3c3dSChen-Yu Tsai /* 742403a3c3dSChen-Yu Tsai * As far as RTC functionality goes, all models are the same. The 743403a3c3dSChen-Yu Tsai * datasheets claim that different models have different number of 744403a3c3dSChen-Yu Tsai * registers available for non-volatile storage, but experiments show 745403a3c3dSChen-Yu Tsai * that all SoCs have 16 registers available for this purpose. 746403a3c3dSChen-Yu Tsai */ 7479765d2d9SChen-Yu Tsai static const struct of_device_id sun6i_rtc_dt_ids[] = { 7489765d2d9SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-rtc" }, 7497cd1acaeSChen-Yu Tsai { .compatible = "allwinner,sun8i-a23-rtc" }, 7507cd1acaeSChen-Yu Tsai { .compatible = "allwinner,sun8i-h3-rtc" }, 751d6624cc7SMaxime Ripard { .compatible = "allwinner,sun8i-r40-rtc" }, 7527cd1acaeSChen-Yu Tsai { .compatible = "allwinner,sun8i-v3-rtc" }, 7537cd1acaeSChen-Yu Tsai { .compatible = "allwinner,sun50i-h5-rtc" }, 754b60ff2cfSOndrej Jirman { .compatible = "allwinner,sun50i-h6-rtc" }, 7559765d2d9SChen-Yu Tsai { /* sentinel */ }, 7569765d2d9SChen-Yu Tsai }; 7579765d2d9SChen-Yu Tsai MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids); 7589765d2d9SChen-Yu Tsai 7599765d2d9SChen-Yu Tsai static struct platform_driver sun6i_rtc_driver = { 7609765d2d9SChen-Yu Tsai .probe = sun6i_rtc_probe, 7619765d2d9SChen-Yu Tsai .driver = { 7629765d2d9SChen-Yu Tsai .name = "sun6i-rtc", 7639765d2d9SChen-Yu Tsai .of_match_table = sun6i_rtc_dt_ids, 764d76a81d0SAlejandro González .pm = &sun6i_rtc_pm_ops, 7659765d2d9SChen-Yu Tsai }, 7669765d2d9SChen-Yu Tsai }; 76737539414SMaxime Ripard builtin_platform_driver(sun6i_rtc_driver); 768