summaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-01-23 21:49:36 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2014-01-23 21:49:36 -0500
commit0ba3307a8ec35252f7b1e222e32889a6f3d9ceb3 (patch)
tree26126ed7a2080a706f0488c215549fc9f5f76a59 /drivers/rtc
parent903a9f77d1d00c8621bc37afd959ac45a4b3ebec (diff)
parentcd2f43a1f7400a74a084094502f70df2e169c6e8 (diff)
Merge tag 'drivers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
Pull ARM driver updates from Olof Johansson: "Updates of SoC-near drivers and other driver updates that makes more sense to take through our tree. The largest part of this is a conversion of device registration for some renesas shmobile/sh devices over to use resources. This has required coordination with the corresponding arch/sh changes, and we've agreed to merge the arch/sh changes through our tree. Added in this branch is support for Trusted Foundations secure firmware, which is what is used on many of the commercial Nvidia Tegra products that are in the market, including the Nvidia Shield. The code is local to arch/arm at this time since it's uncertain whether it will be shared with arm64 longer-term, if needed we will refactor later. A couple of new RTC drivers used on ARM boards, merged through our tree on request by the RTC maintainer. ... plus a bunch of smaller updates across the board, gpio conversions for davinci, etc" * tag 'drivers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc: (45 commits) watchdog: davinci: rename platform driver to davinci-wdt tty: serial: Limit msm_serial_hs driver to platforms that use it mmc: msm_sdcc: Limit driver to platforms that use it usb: phy: msm: Move mach dependent code to platform data clk: versatile: fixup IM-PD1 clock implementation clk: versatile: pass a name to ICST clock provider ARM: integrator: pass parent IRQ to the SIC irqchip: versatile FPGA: support cascaded interrupts from DT gpio: davinci: don't create irq_domain in case of unbanked irqs gpio: davinci: use chained_irq_enter/chained_irq_exit API gpio: davinci: add OF support gpio: davinci: remove unused variable intc_irq_num gpio: davinci: convert to use irqdomain support. gpio: introduce GPIO_DAVINCI kconfig option gpio: davinci: get rid of DAVINCI_N_GPIO gpio: davinci: use {readl|writel}_relaxed() instead of __raw_* serial: sh-sci: Add OF support serial: sh-sci: Add device tree bindings documentation serial: sh-sci: Remove platform data mapbase and irqs fields serial: sh-sci: Remove platform data scbrr_algo_id field ...
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig18
-rw-r--r--drivers/rtc/Makefile2
-rw-r--r--drivers/rtc/rtc-isl12057.c310
-rw-r--r--drivers/rtc/rtc-sunxi.c523
4 files changed, 853 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 007730222116..b1328a45b095 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -304,6 +304,17 @@ config RTC_DRV_ISL12022
304 This driver can also be built as a module. If so, the module 304 This driver can also be built as a module. If so, the module
305 will be called rtc-isl12022. 305 will be called rtc-isl12022.
306 306
307config RTC_DRV_ISL12057
308 depends on I2C
309 select REGMAP_I2C
310 tristate "Intersil ISL12057"
311 help
312 If you say yes here you get support for the Intersil ISL12057
313 I2C RTC chip.
314
315 This driver can also be built as a module. If so, the module
316 will be called rtc-isl12057.
317
307config RTC_DRV_X1205 318config RTC_DRV_X1205
308 tristate "Xicor/Intersil X1205" 319 tristate "Xicor/Intersil X1205"
309 help 320 help
@@ -1104,6 +1115,13 @@ config RTC_DRV_SUN4V
1104 If you say Y here you will get support for the Hypervisor 1115 If you say Y here you will get support for the Hypervisor
1105 based RTC on SUN4V systems. 1116 based RTC on SUN4V systems.
1106 1117
1118config RTC_DRV_SUNXI
1119 tristate "Allwinner sun4i/sun7i RTC"
1120 depends on ARCH_SUNXI
1121 help
1122 If you say Y here you will get support for the RTC found on
1123 Allwinner A10/A20.
1124
1107config RTC_DRV_STARFIRE 1125config RTC_DRV_STARFIRE
1108 bool "Starfire RTC" 1126 bool "Starfire RTC"
1109 depends on SPARC64 1127 depends on SPARC64
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 27b4bd884066..c00741a0bf10 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -58,6 +58,7 @@ obj-$(CONFIG_RTC_DRV_HID_SENSOR_TIME) += rtc-hid-sensor-time.o
58obj-$(CONFIG_RTC_DRV_IMXDI) += rtc-imxdi.o 58obj-$(CONFIG_RTC_DRV_IMXDI) += rtc-imxdi.o
59obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o 59obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
60obj-$(CONFIG_RTC_DRV_ISL12022) += rtc-isl12022.o 60obj-$(CONFIG_RTC_DRV_ISL12022) += rtc-isl12022.o
61obj-$(CONFIG_RTC_DRV_ISL12057) += rtc-isl12057.o
61obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o 62obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o
62obj-$(CONFIG_RTC_DRV_LP8788) += rtc-lp8788.o 63obj-$(CONFIG_RTC_DRV_LP8788) += rtc-lp8788.o
63obj-$(CONFIG_RTC_DRV_LPC32XX) += rtc-lpc32xx.o 64obj-$(CONFIG_RTC_DRV_LPC32XX) += rtc-lpc32xx.o
@@ -117,6 +118,7 @@ obj-$(CONFIG_RTC_DRV_STARFIRE) += rtc-starfire.o
117obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o 118obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o
118obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o 119obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o
119obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o 120obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o
121obj-$(CONFIG_RTC_DRV_SUNXI) += rtc-sunxi.o
120obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o 122obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o
121obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o 123obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
122obj-$(CONFIG_RTC_DRV_TILE) += rtc-tile.o 124obj-$(CONFIG_RTC_DRV_TILE) += rtc-tile.o
diff --git a/drivers/rtc/rtc-isl12057.c b/drivers/rtc/rtc-isl12057.c
new file mode 100644
index 000000000000..7854a656628f
--- /dev/null
+++ b/drivers/rtc/rtc-isl12057.c
@@ -0,0 +1,310 @@
1/*
2 * rtc-isl12057 - Driver for Intersil ISL12057 I2C Real Time Clock
3 *
4 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
5 *
6 * This work is largely based on Intersil ISL1208 driver developed by
7 * Hebert Valerio Riedel <hvr@gnu.org>.
8 *
9 * Detailed datasheet on which this development is based is available here:
10 *
11 * http://natisbad.org/NAS2/refs/ISL12057.pdf
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/module.h>
25#include <linux/mutex.h>
26#include <linux/rtc.h>
27#include <linux/i2c.h>
28#include <linux/bcd.h>
29#include <linux/rtc.h>
30#include <linux/of.h>
31#include <linux/of_device.h>
32#include <linux/regmap.h>
33
34#define DRV_NAME "rtc-isl12057"
35
36/* RTC section */
37#define ISL12057_REG_RTC_SC 0x00 /* Seconds */
38#define ISL12057_REG_RTC_MN 0x01 /* Minutes */
39#define ISL12057_REG_RTC_HR 0x02 /* Hours */
40#define ISL12057_REG_RTC_HR_PM BIT(5) /* AM/PM bit in 12h format */
41#define ISL12057_REG_RTC_HR_MIL BIT(6) /* 24h/12h format */
42#define ISL12057_REG_RTC_DW 0x03 /* Day of the Week */
43#define ISL12057_REG_RTC_DT 0x04 /* Date */
44#define ISL12057_REG_RTC_MO 0x05 /* Month */
45#define ISL12057_REG_RTC_YR 0x06 /* Year */
46#define ISL12057_RTC_SEC_LEN 7
47
48/* Alarm 1 section */
49#define ISL12057_REG_A1_SC 0x07 /* Alarm 1 Seconds */
50#define ISL12057_REG_A1_MN 0x08 /* Alarm 1 Minutes */
51#define ISL12057_REG_A1_HR 0x09 /* Alarm 1 Hours */
52#define ISL12057_REG_A1_HR_PM BIT(5) /* AM/PM bit in 12h format */
53#define ISL12057_REG_A1_HR_MIL BIT(6) /* 24h/12h format */
54#define ISL12057_REG_A1_DWDT 0x0A /* Alarm 1 Date / Day of the week */
55#define ISL12057_REG_A1_DWDT_B BIT(6) /* DW / DT selection bit */
56#define ISL12057_A1_SEC_LEN 4
57
58/* Alarm 2 section */
59#define ISL12057_REG_A2_MN 0x0B /* Alarm 2 Minutes */
60#define ISL12057_REG_A2_HR 0x0C /* Alarm 2 Hours */
61#define ISL12057_REG_A2_DWDT 0x0D /* Alarm 2 Date / Day of the week */
62#define ISL12057_A2_SEC_LEN 3
63
64/* Control/Status registers */
65#define ISL12057_REG_INT 0x0E
66#define ISL12057_REG_INT_A1IE BIT(0) /* Alarm 1 interrupt enable bit */
67#define ISL12057_REG_INT_A2IE BIT(1) /* Alarm 2 interrupt enable bit */
68#define ISL12057_REG_INT_INTCN BIT(2) /* Interrupt control enable bit */
69#define ISL12057_REG_INT_RS1 BIT(3) /* Freq out control bit 1 */
70#define ISL12057_REG_INT_RS2 BIT(4) /* Freq out control bit 2 */
71#define ISL12057_REG_INT_EOSC BIT(7) /* Oscillator enable bit */
72
73#define ISL12057_REG_SR 0x0F
74#define ISL12057_REG_SR_A1F BIT(0) /* Alarm 1 interrupt bit */
75#define ISL12057_REG_SR_A2F BIT(1) /* Alarm 2 interrupt bit */
76#define ISL12057_REG_SR_OSF BIT(7) /* Oscillator failure bit */
77
78/* Register memory map length */
79#define ISL12057_MEM_MAP_LEN 0x10
80
81struct isl12057_rtc_data {
82 struct regmap *regmap;
83 struct mutex lock;
84};
85
86static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs)
87{
88 tm->tm_sec = bcd2bin(regs[ISL12057_REG_RTC_SC]);
89 tm->tm_min = bcd2bin(regs[ISL12057_REG_RTC_MN]);
90
91 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_MIL) { /* AM/PM */
92 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x0f);
93 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_PM)
94 tm->tm_hour += 12;
95 } else { /* 24 hour mode */
96 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x3f);
97 }
98
99 tm->tm_mday = bcd2bin(regs[ISL12057_REG_RTC_DT]);
100 tm->tm_wday = bcd2bin(regs[ISL12057_REG_RTC_DW]) - 1; /* starts at 1 */
101 tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO]) - 1; /* starts at 1 */
102 tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100;
103}
104
105static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm)
106{
107 /*
108 * The clock has an 8 bit wide bcd-coded register for the year.
109 * tm_year is an offset from 1900 and we are interested in the
110 * 2000-2099 range, so any value less than 100 is invalid.
111 */
112 if (tm->tm_year < 100)
113 return -EINVAL;
114
115 regs[ISL12057_REG_RTC_SC] = bin2bcd(tm->tm_sec);
116 regs[ISL12057_REG_RTC_MN] = bin2bcd(tm->tm_min);
117 regs[ISL12057_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */
118 regs[ISL12057_REG_RTC_DT] = bin2bcd(tm->tm_mday);
119 regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1);
120 regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year - 100);
121 regs[ISL12057_REG_RTC_DW] = bin2bcd(tm->tm_wday + 1);
122
123 return 0;
124}
125
126/*
127 * Try and match register bits w/ fixed null values to see whether we
128 * are dealing with an ISL12057. Note: this function is called early
129 * during init and hence does need mutex protection.
130 */
131static int isl12057_i2c_validate_chip(struct regmap *regmap)
132{
133 u8 regs[ISL12057_MEM_MAP_LEN];
134 static const u8 mask[ISL12057_MEM_MAP_LEN] = { 0x80, 0x80, 0x80, 0xf8,
135 0xc0, 0x60, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00,
137 0x00, 0x00, 0x60, 0x7c };
138 int ret, i;
139
140 ret = regmap_bulk_read(regmap, 0, regs, ISL12057_MEM_MAP_LEN);
141 if (ret)
142 return ret;
143
144 for (i = 0; i < ISL12057_MEM_MAP_LEN; ++i) {
145 if (regs[i] & mask[i]) /* check if bits are cleared */
146 return -ENODEV;
147 }
148
149 return 0;
150}
151
152static int isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
153{
154 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
155 u8 regs[ISL12057_RTC_SEC_LEN];
156 int ret;
157
158 mutex_lock(&data->lock);
159 ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs,
160 ISL12057_RTC_SEC_LEN);
161 mutex_unlock(&data->lock);
162
163 if (ret) {
164 dev_err(dev, "%s: RTC read failed\n", __func__);
165 return ret;
166 }
167
168 isl12057_rtc_regs_to_tm(tm, regs);
169
170 return rtc_valid_tm(tm);
171}
172
173static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm)
174{
175 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
176 u8 regs[ISL12057_RTC_SEC_LEN];
177 int ret;
178
179 ret = isl12057_rtc_tm_to_regs(regs, tm);
180 if (ret)
181 return ret;
182
183 mutex_lock(&data->lock);
184 ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs,
185 ISL12057_RTC_SEC_LEN);
186 mutex_unlock(&data->lock);
187
188 if (ret)
189 dev_err(dev, "%s: RTC write failed\n", __func__);
190
191 return ret;
192}
193
194/*
195 * Check current RTC status and enable/disable what needs to be. Return 0 if
196 * everything went ok and a negative value upon error. Note: this function
197 * is called early during init and hence does need mutex protection.
198 */
199static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
200{
201 int ret;
202
203 /* Enable oscillator if not already running */
204 ret = regmap_update_bits(regmap, ISL12057_REG_INT,
205 ISL12057_REG_INT_EOSC, 0);
206 if (ret < 0) {
207 dev_err(dev, "Unable to enable oscillator\n");
208 return ret;
209 }
210
211 /* Clear oscillator failure bit if needed */
212 ret = regmap_update_bits(regmap, ISL12057_REG_SR,
213 ISL12057_REG_SR_OSF, 0);
214 if (ret < 0) {
215 dev_err(dev, "Unable to clear oscillator failure bit\n");
216 return ret;
217 }
218
219 /* Clear alarm bit if needed */
220 ret = regmap_update_bits(regmap, ISL12057_REG_SR,
221 ISL12057_REG_SR_A1F, 0);
222 if (ret < 0) {
223 dev_err(dev, "Unable to clear alarm bit\n");
224 return ret;
225 }
226
227 return 0;
228}
229
230static const struct rtc_class_ops rtc_ops = {
231 .read_time = isl12057_rtc_read_time,
232 .set_time = isl12057_rtc_set_time,
233};
234
235static struct regmap_config isl12057_rtc_regmap_config = {
236 .reg_bits = 8,
237 .val_bits = 8,
238};
239
240static int isl12057_probe(struct i2c_client *client,
241 const struct i2c_device_id *id)
242{
243 struct device *dev = &client->dev;
244 struct isl12057_rtc_data *data;
245 struct rtc_device *rtc;
246 struct regmap *regmap;
247 int ret;
248
249 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
250 I2C_FUNC_SMBUS_BYTE_DATA |
251 I2C_FUNC_SMBUS_I2C_BLOCK))
252 return -ENODEV;
253
254 regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config);
255 if (IS_ERR(regmap)) {
256 ret = PTR_ERR(regmap);
257 dev_err(dev, "regmap allocation failed: %d\n", ret);
258 return ret;
259 }
260
261 ret = isl12057_i2c_validate_chip(regmap);
262 if (ret)
263 return ret;
264
265 ret = isl12057_check_rtc_status(dev, regmap);
266 if (ret)
267 return ret;
268
269 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
270 if (!data)
271 return -ENOMEM;
272
273 mutex_init(&data->lock);
274 data->regmap = regmap;
275 dev_set_drvdata(dev, data);
276
277 rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops, THIS_MODULE);
278 if (IS_ERR(rtc))
279 return PTR_ERR(rtc);
280
281 return 0;
282}
283
284#ifdef CONFIG_OF
285static struct of_device_id isl12057_dt_match[] = {
286 { .compatible = "isl,isl12057" },
287 { },
288};
289#endif
290
291static const struct i2c_device_id isl12057_id[] = {
292 { "isl12057", 0 },
293 { }
294};
295MODULE_DEVICE_TABLE(i2c, isl12057_id);
296
297static struct i2c_driver isl12057_driver = {
298 .driver = {
299 .name = DRV_NAME,
300 .owner = THIS_MODULE,
301 .of_match_table = of_match_ptr(isl12057_dt_match),
302 },
303 .probe = isl12057_probe,
304 .id_table = isl12057_id,
305};
306module_i2c_driver(isl12057_driver);
307
308MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
309MODULE_DESCRIPTION("Intersil ISL12057 RTC driver");
310MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-sunxi.c b/drivers/rtc/rtc-sunxi.c
new file mode 100644
index 000000000000..68a35284e5ad
--- /dev/null
+++ b/drivers/rtc/rtc-sunxi.c
@@ -0,0 +1,523 @@
1/*
2 * An RTC driver for Allwinner A10/A20
3 *
4 * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include <linux/delay.h>
22#include <linux/err.h>
23#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_device.h>
32#include <linux/platform_device.h>
33#include <linux/rtc.h>
34#include <linux/types.h>
35
36#define SUNXI_LOSC_CTRL 0x0000
37#define SUNXI_LOSC_CTRL_RTC_HMS_ACC BIT(8)
38#define SUNXI_LOSC_CTRL_RTC_YMD_ACC BIT(7)
39
40#define SUNXI_RTC_YMD 0x0004
41
42#define SUNXI_RTC_HMS 0x0008
43
44#define SUNXI_ALRM_DHMS 0x000c
45
46#define SUNXI_ALRM_EN 0x0014
47#define SUNXI_ALRM_EN_CNT_EN BIT(8)
48
49#define SUNXI_ALRM_IRQ_EN 0x0018
50#define SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
51
52#define SUNXI_ALRM_IRQ_STA 0x001c
53#define SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
54
55#define SUNXI_MASK_DH 0x0000001f
56#define SUNXI_MASK_SM 0x0000003f
57#define SUNXI_MASK_M 0x0000000f
58#define SUNXI_MASK_LY 0x00000001
59#define SUNXI_MASK_D 0x00000ffe
60#define SUNXI_MASK_M 0x0000000f
61
62#define SUNXI_GET(x, mask, shift) (((x) & ((mask) << (shift))) \
63 >> (shift))
64
65#define SUNXI_SET(x, mask, shift) (((x) & (mask)) << (shift))
66
67/*
68 * Get date values
69 */
70#define SUNXI_DATE_GET_DAY_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 0)
71#define SUNXI_DATE_GET_MON_VALUE(x) SUNXI_GET(x, SUNXI_MASK_M, 8)
72#define SUNXI_DATE_GET_YEAR_VALUE(x, mask) SUNXI_GET(x, mask, 16)
73
74/*
75 * Get time values
76 */
77#define SUNXI_TIME_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
78#define SUNXI_TIME_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
79#define SUNXI_TIME_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
80
81/*
82 * Get alarm values
83 */
84#define SUNXI_ALRM_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
85#define SUNXI_ALRM_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
86#define SUNXI_ALRM_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
87
88/*
89 * Set date values
90 */
91#define SUNXI_DATE_SET_DAY_VALUE(x) SUNXI_DATE_GET_DAY_VALUE(x)
92#define SUNXI_DATE_SET_MON_VALUE(x) SUNXI_SET(x, SUNXI_MASK_M, 8)
93#define SUNXI_DATE_SET_YEAR_VALUE(x, mask) SUNXI_SET(x, mask, 16)
94#define SUNXI_LEAP_SET_VALUE(x, shift) SUNXI_SET(x, SUNXI_MASK_LY, shift)
95
96/*
97 * Set time values
98 */
99#define SUNXI_TIME_SET_SEC_VALUE(x) SUNXI_TIME_GET_SEC_VALUE(x)
100#define SUNXI_TIME_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
101#define SUNXI_TIME_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
102
103/*
104 * Set alarm values
105 */
106#define SUNXI_ALRM_SET_SEC_VALUE(x) SUNXI_ALRM_GET_SEC_VALUE(x)
107#define SUNXI_ALRM_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
108#define SUNXI_ALRM_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
109#define SUNXI_ALRM_SET_DAY_VALUE(x) SUNXI_SET(x, SUNXI_MASK_D, 21)
110
111/*
112 * Time unit conversions
113 */
114#define SEC_IN_MIN 60
115#define SEC_IN_HOUR (60 * SEC_IN_MIN)
116#define SEC_IN_DAY (24 * SEC_IN_HOUR)
117
118/*
119 * The year parameter passed to the driver is usually an offset relative to
120 * the year 1900. This macro is used to convert this offset to another one
121 * relative to the minimum year allowed by the hardware.
122 */
123#define SUNXI_YEAR_OFF(x) ((x)->min - 1900)
124
125/*
126 * min and max year are arbitrary set considering the limited range of the
127 * hardware register field
128 */
129struct sunxi_rtc_data_year {
130 unsigned int min; /* min year allowed */
131 unsigned int max; /* max year allowed */
132 unsigned int mask; /* mask for the year field */
133 unsigned char leap_shift; /* bit shift to get the leap year */
134};
135
136static struct sunxi_rtc_data_year data_year_param[] = {
137 [0] = {
138 .min = 2010,
139 .max = 2073,
140 .mask = 0x3f,
141 .leap_shift = 22,
142 },
143 [1] = {
144 .min = 1970,
145 .max = 2225,
146 .mask = 0xff,
147 .leap_shift = 24,
148 },
149};
150
151struct sunxi_rtc_dev {
152 struct rtc_device *rtc;
153 struct device *dev;
154 struct sunxi_rtc_data_year *data_year;
155 void __iomem *base;
156 int irq;
157};
158
159static irqreturn_t sunxi_rtc_alarmirq(int irq, void *id)
160{
161 struct sunxi_rtc_dev *chip = (struct sunxi_rtc_dev *) id;
162 u32 val;
163
164 val = readl(chip->base + SUNXI_ALRM_IRQ_STA);
165
166 if (val & SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND) {
167 val |= SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND;
168 writel(val, chip->base + SUNXI_ALRM_IRQ_STA);
169
170 rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
171
172 return IRQ_HANDLED;
173 }
174
175 return IRQ_NONE;
176}
177
178static void sunxi_rtc_setaie(int to, struct sunxi_rtc_dev *chip)
179{
180 u32 alrm_val = 0;
181 u32 alrm_irq_val = 0;
182
183 if (to) {
184 alrm_val = readl(chip->base + SUNXI_ALRM_EN);
185 alrm_val |= SUNXI_ALRM_EN_CNT_EN;
186
187 alrm_irq_val = readl(chip->base + SUNXI_ALRM_IRQ_EN);
188 alrm_irq_val |= SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN;
189 } else {
190 writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND,
191 chip->base + SUNXI_ALRM_IRQ_STA);
192 }
193
194 writel(alrm_val, chip->base + SUNXI_ALRM_EN);
195 writel(alrm_irq_val, chip->base + SUNXI_ALRM_IRQ_EN);
196}
197
198static int sunxi_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
199{
200 struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
201 struct rtc_time *alrm_tm = &wkalrm->time;
202 u32 alrm;
203 u32 alrm_en;
204 u32 date;
205
206 alrm = readl(chip->base + SUNXI_ALRM_DHMS);
207 date = readl(chip->base + SUNXI_RTC_YMD);
208
209 alrm_tm->tm_sec = SUNXI_ALRM_GET_SEC_VALUE(alrm);
210 alrm_tm->tm_min = SUNXI_ALRM_GET_MIN_VALUE(alrm);
211 alrm_tm->tm_hour = SUNXI_ALRM_GET_HOUR_VALUE(alrm);
212
213 alrm_tm->tm_mday = SUNXI_DATE_GET_DAY_VALUE(date);
214 alrm_tm->tm_mon = SUNXI_DATE_GET_MON_VALUE(date);
215 alrm_tm->tm_year = SUNXI_DATE_GET_YEAR_VALUE(date,
216 chip->data_year->mask);
217
218 alrm_tm->tm_mon -= 1;
219
220 /*
221 * switch from (data_year->min)-relative offset to
222 * a (1900)-relative one
223 */
224 alrm_tm->tm_year += SUNXI_YEAR_OFF(chip->data_year);
225
226 alrm_en = readl(chip->base + SUNXI_ALRM_IRQ_EN);
227 if (alrm_en & SUNXI_ALRM_EN_CNT_EN)
228 wkalrm->enabled = 1;
229
230 return 0;
231}
232
233static int sunxi_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
234{
235 struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
236 u32 date, time;
237
238 /*
239 * read again in case it changes
240 */
241 do {
242 date = readl(chip->base + SUNXI_RTC_YMD);
243 time = readl(chip->base + SUNXI_RTC_HMS);
244 } while ((date != readl(chip->base + SUNXI_RTC_YMD)) ||
245 (time != readl(chip->base + SUNXI_RTC_HMS)));
246
247 rtc_tm->tm_sec = SUNXI_TIME_GET_SEC_VALUE(time);
248 rtc_tm->tm_min = SUNXI_TIME_GET_MIN_VALUE(time);
249 rtc_tm->tm_hour = SUNXI_TIME_GET_HOUR_VALUE(time);
250
251 rtc_tm->tm_mday = SUNXI_DATE_GET_DAY_VALUE(date);
252 rtc_tm->tm_mon = SUNXI_DATE_GET_MON_VALUE(date);
253 rtc_tm->tm_year = SUNXI_DATE_GET_YEAR_VALUE(date,
254 chip->data_year->mask);
255
256 rtc_tm->tm_mon -= 1;
257
258 /*
259 * switch from (data_year->min)-relative offset to
260 * a (1900)-relative one
261 */
262 rtc_tm->tm_year += SUNXI_YEAR_OFF(chip->data_year);
263
264 return rtc_valid_tm(rtc_tm);
265}
266
267static int sunxi_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
268{
269 struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
270 struct rtc_time *alrm_tm = &wkalrm->time;
271 struct rtc_time tm_now;
272 u32 alrm = 0;
273 unsigned long time_now = 0;
274 unsigned long time_set = 0;
275 unsigned long time_gap = 0;
276 unsigned long time_gap_day = 0;
277 unsigned long time_gap_hour = 0;
278 unsigned long time_gap_min = 0;
279 int ret = 0;
280
281 ret = sunxi_rtc_gettime(dev, &tm_now);
282 if (ret < 0) {
283 dev_err(dev, "Error in getting time\n");
284 return -EINVAL;
285 }
286
287 rtc_tm_to_time(alrm_tm, &time_set);
288 rtc_tm_to_time(&tm_now, &time_now);
289 if (time_set <= time_now) {
290 dev_err(dev, "Date to set in the past\n");
291 return -EINVAL;
292 }
293
294 time_gap = time_set - time_now;
295 time_gap_day = time_gap / SEC_IN_DAY;
296 time_gap -= time_gap_day * SEC_IN_DAY;
297 time_gap_hour = time_gap / SEC_IN_HOUR;
298 time_gap -= time_gap_hour * SEC_IN_HOUR;
299 time_gap_min = time_gap / SEC_IN_MIN;
300 time_gap -= time_gap_min * SEC_IN_MIN;
301
302 if (time_gap_day > 255) {
303 dev_err(dev, "Day must be in the range 0 - 255\n");
304 return -EINVAL;
305 }
306
307 sunxi_rtc_setaie(0, chip);
308 writel(0, chip->base + SUNXI_ALRM_DHMS);
309 usleep_range(100, 300);
310
311 alrm = SUNXI_ALRM_SET_SEC_VALUE(time_gap) |
312 SUNXI_ALRM_SET_MIN_VALUE(time_gap_min) |
313 SUNXI_ALRM_SET_HOUR_VALUE(time_gap_hour) |
314 SUNXI_ALRM_SET_DAY_VALUE(time_gap_day);
315 writel(alrm, chip->base + SUNXI_ALRM_DHMS);
316
317 writel(0, chip->base + SUNXI_ALRM_IRQ_EN);
318 writel(SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN, chip->base + SUNXI_ALRM_IRQ_EN);
319
320 sunxi_rtc_setaie(wkalrm->enabled, chip);
321
322 return 0;
323}
324
325static int sunxi_rtc_wait(struct sunxi_rtc_dev *chip, int offset,
326 unsigned int mask, unsigned int ms_timeout)
327{
328 const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
329 u32 reg;
330
331 do {
332 reg = readl(chip->base + offset);
333 reg &= mask;
334
335 if (reg == mask)
336 return 0;
337
338 } while (time_before(jiffies, timeout));
339
340 return -ETIMEDOUT;
341}
342
343static int sunxi_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
344{
345 struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
346 u32 date = 0;
347 u32 time = 0;
348 int year;
349
350 /*
351 * the input rtc_tm->tm_year is the offset relative to 1900. We use
352 * the SUNXI_YEAR_OFF macro to rebase it with respect to the min year
353 * allowed by the hardware
354 */
355
356 year = rtc_tm->tm_year + 1900;
357 if (year < chip->data_year->min || year > chip->data_year->max) {
358 dev_err(dev, "rtc only supports year in range %d - %d\n",
359 chip->data_year->min, chip->data_year->max);
360 return -EINVAL;
361 }
362
363 rtc_tm->tm_year -= SUNXI_YEAR_OFF(chip->data_year);
364 rtc_tm->tm_mon += 1;
365
366 date = SUNXI_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
367 SUNXI_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
368 SUNXI_DATE_SET_YEAR_VALUE(rtc_tm->tm_year,
369 chip->data_year->mask);
370
371 if (is_leap_year(year))
372 date |= SUNXI_LEAP_SET_VALUE(1, chip->data_year->leap_shift);
373
374 time = SUNXI_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
375 SUNXI_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
376 SUNXI_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
377
378 writel(0, chip->base + SUNXI_RTC_HMS);
379 writel(0, chip->base + SUNXI_RTC_YMD);
380
381 writel(time, chip->base + SUNXI_RTC_HMS);
382
383 /*
384 * After writing the RTC HH-MM-SS register, the
385 * SUNXI_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
386 * be cleared until the real writing operation is finished
387 */
388
389 if (sunxi_rtc_wait(chip, SUNXI_LOSC_CTRL,
390 SUNXI_LOSC_CTRL_RTC_HMS_ACC, 50)) {
391 dev_err(dev, "Failed to set rtc time.\n");
392 return -1;
393 }
394
395 writel(date, chip->base + SUNXI_RTC_YMD);
396
397 /*
398 * After writing the RTC YY-MM-DD register, the
399 * SUNXI_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
400 * be cleared until the real writing operation is finished
401 */
402
403 if (sunxi_rtc_wait(chip, SUNXI_LOSC_CTRL,
404 SUNXI_LOSC_CTRL_RTC_YMD_ACC, 50)) {
405 dev_err(dev, "Failed to set rtc time.\n");
406 return -1;
407 }
408
409 return 0;
410}
411
412static int sunxi_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
413{
414 struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
415
416 if (!enabled)
417 sunxi_rtc_setaie(enabled, chip);
418
419 return 0;
420}
421
422static const struct rtc_class_ops sunxi_rtc_ops = {
423 .read_time = sunxi_rtc_gettime,
424 .set_time = sunxi_rtc_settime,
425 .read_alarm = sunxi_rtc_getalarm,
426 .set_alarm = sunxi_rtc_setalarm,
427 .alarm_irq_enable = sunxi_rtc_alarm_irq_enable
428};
429
430static const struct of_device_id sunxi_rtc_dt_ids[] = {
431 { .compatible = "allwinner,sun4i-rtc", .data = &data_year_param[0] },
432 { .compatible = "allwinner,sun7i-a20-rtc", .data = &data_year_param[1] },
433 { /* sentinel */ },
434};
435MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
436
437static int sunxi_rtc_probe(struct platform_device *pdev)
438{
439 struct sunxi_rtc_dev *chip;
440 struct resource *res;
441 const struct of_device_id *of_id;
442 int ret;
443
444 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
445 if (!chip)
446 return -ENOMEM;
447
448 platform_set_drvdata(pdev, chip);
449 chip->dev = &pdev->dev;
450
451 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
452 chip->base = devm_ioremap_resource(&pdev->dev, res);
453 if (IS_ERR(chip->base))
454 return PTR_ERR(chip->base);
455
456 chip->irq = platform_get_irq(pdev, 0);
457 if (chip->irq < 0) {
458 dev_err(&pdev->dev, "No IRQ resource\n");
459 return chip->irq;
460 }
461 ret = devm_request_irq(&pdev->dev, chip->irq, sunxi_rtc_alarmirq,
462 0, dev_name(&pdev->dev), chip);
463 if (ret) {
464 dev_err(&pdev->dev, "Could not request IRQ\n");
465 return ret;
466 }
467
468 of_id = of_match_device(sunxi_rtc_dt_ids, &pdev->dev);
469 if (!of_id) {
470 dev_err(&pdev->dev, "Unable to setup RTC data\n");
471 return -ENODEV;
472 }
473 chip->data_year = (struct sunxi_rtc_data_year *) of_id->data;
474
475 /* clear the alarm count value */
476 writel(0, chip->base + SUNXI_ALRM_DHMS);
477
478 /* disable alarm, not generate irq pending */
479 writel(0, chip->base + SUNXI_ALRM_EN);
480
481 /* disable alarm week/cnt irq, unset to cpu */
482 writel(0, chip->base + SUNXI_ALRM_IRQ_EN);
483
484 /* clear alarm week/cnt irq pending */
485 writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND, chip->base +
486 SUNXI_ALRM_IRQ_STA);
487
488 chip->rtc = rtc_device_register("rtc-sunxi", &pdev->dev,
489 &sunxi_rtc_ops, THIS_MODULE);
490 if (IS_ERR(chip->rtc)) {
491 dev_err(&pdev->dev, "unable to register device\n");
492 return PTR_ERR(chip->rtc);
493 }
494
495 dev_info(&pdev->dev, "RTC enabled\n");
496
497 return 0;
498}
499
500static int sunxi_rtc_remove(struct platform_device *pdev)
501{
502 struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
503
504 rtc_device_unregister(chip->rtc);
505
506 return 0;
507}
508
509static struct platform_driver sunxi_rtc_driver = {
510 .probe = sunxi_rtc_probe,
511 .remove = sunxi_rtc_remove,
512 .driver = {
513 .name = "sunxi-rtc",
514 .owner = THIS_MODULE,
515 .of_match_table = sunxi_rtc_dt_ids,
516 },
517};
518
519module_platform_driver(sunxi_rtc_driver);
520
521MODULE_DESCRIPTION("sunxi RTC driver");
522MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
523MODULE_LICENSE("GPL");