aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig49
-rw-r--r--drivers/rtc/Makefile1
-rw-r--r--drivers/rtc/class.c30
-rw-r--r--drivers/rtc/interface.c21
-rw-r--r--drivers/rtc/rtc-ab8500.c2
-rw-r--r--drivers/rtc/rtc-at91sam9.c138
-rw-r--r--drivers/rtc/rtc-ds1307.c127
-rw-r--r--drivers/rtc/rtc-ds1374.c285
-rw-r--r--drivers/rtc/rtc-isl12057.c83
-rw-r--r--drivers/rtc/rtc-lib.c38
-rw-r--r--drivers/rtc/rtc-omap.c547
-rw-r--r--drivers/rtc/rtc-opal.c261
-rw-r--r--drivers/rtc/rtc-pcf8563.c55
-rw-r--r--drivers/rtc/rtc-sirfsoc.c66
-rw-r--r--drivers/rtc/rtc-snvs.c50
15 files changed, 1366 insertions, 387 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 6dd12ddbabc6..f15cddfeb897 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -192,6 +192,14 @@ config RTC_DRV_DS1374
192 This driver can also be built as a module. If so, the module 192 This driver can also be built as a module. If so, the module
193 will be called rtc-ds1374. 193 will be called rtc-ds1374.
194 194
195config RTC_DRV_DS1374_WDT
196 bool "Dallas/Maxim DS1374 watchdog timer"
197 depends on RTC_DRV_DS1374
198 help
199 If you say Y here you will get support for the
200 watchdog timer in the Dallas Semiconductor DS1374
201 real-time clock chips.
202
195config RTC_DRV_DS1672 203config RTC_DRV_DS1672
196 tristate "Dallas/Maxim DS1672" 204 tristate "Dallas/Maxim DS1672"
197 help 205 help
@@ -979,6 +987,17 @@ config RTC_DRV_NUC900
979 If you say yes here you get support for the RTC subsystem of the 987 If you say yes here you get support for the RTC subsystem of the
980 NUC910/NUC920 used in embedded systems. 988 NUC910/NUC920 used in embedded systems.
981 989
990config RTC_DRV_OPAL
991 tristate "IBM OPAL RTC driver"
992 depends on PPC_POWERNV
993 default y
994 help
995 If you say yes here you get support for the PowerNV platform RTC
996 driver based on OPAL interfaces.
997
998 This driver can also be built as a module. If so, the module
999 will be called rtc-opal.
1000
982comment "on-CPU RTC drivers" 1001comment "on-CPU RTC drivers"
983 1002
984config RTC_DRV_DAVINCI 1003config RTC_DRV_DAVINCI
@@ -1109,36 +1128,42 @@ config RTC_DRV_AT91RM9200
1109 this is powered by the backup power supply. 1128 this is powered by the backup power supply.
1110 1129
1111config RTC_DRV_AT91SAM9 1130config RTC_DRV_AT91SAM9
1112 tristate "AT91SAM9x/AT91CAP9 RTT as RTC" 1131 tristate "AT91SAM9 RTT as RTC"
1113 depends on ARCH_AT91 && !(ARCH_AT91RM9200 || ARCH_AT91X40) 1132 depends on ARCH_AT91
1133 select MFD_SYSCON
1114 help 1134 help
1115 RTC driver for the Atmel AT91SAM9x and AT91CAP9 internal RTT 1135 Some AT91SAM9 SoCs provide an RTT (Real Time Timer) block which
1116 (Real Time Timer). These timers are powered by the backup power 1136 can be used as an RTC thanks to the backup power supply (e.g. a
1117 supply (such as a small coin cell battery), but do not need to 1137 small coin cell battery) which keeps this block and the GPBR
1118 be used as RTCs. 1138 (General Purpose Backup Registers) block powered when the device
1119 1139 is shutdown.
1120 (On AT91SAM9rl and AT91SAM9G45 chips you probably want to use the 1140 Some AT91SAM9 SoCs provide a real RTC block, on those ones you'd
1121 dedicated RTC module and leave the RTT available for other uses.) 1141 probably want to use the real RTC block instead of the "RTT as an
1142 RTC" driver.
1122 1143
1123config RTC_DRV_AT91SAM9_RTT 1144config RTC_DRV_AT91SAM9_RTT
1124 int 1145 int
1125 range 0 1 1146 range 0 1
1126 default 0 1147 default 0
1127 prompt "RTT module Number" if ARCH_AT91SAM9263
1128 depends on RTC_DRV_AT91SAM9 1148 depends on RTC_DRV_AT91SAM9
1129 help 1149 help
1150 This option is only relevant for legacy board support and
1151 won't be used when booting a DT board.
1152
1130 More than one RTT module is available. You can choose which 1153 More than one RTT module is available. You can choose which
1131 one will be used as an RTC. The default of zero is normally 1154 one will be used as an RTC. The default of zero is normally
1132 OK to use, though some systems use that for non-RTC purposes. 1155 OK to use, though some systems use that for non-RTC purposes.
1133 1156
1134config RTC_DRV_AT91SAM9_GPBR 1157config RTC_DRV_AT91SAM9_GPBR
1135 int 1158 int
1136 range 0 3 if !ARCH_AT91SAM9263 1159 range 0 3
1137 range 0 15 if ARCH_AT91SAM9263
1138 default 0 1160 default 0
1139 prompt "Backup Register Number" 1161 prompt "Backup Register Number"
1140 depends on RTC_DRV_AT91SAM9 1162 depends on RTC_DRV_AT91SAM9
1141 help 1163 help
1164 This option is only relevant for legacy board support and
1165 won't be used when booting a DT board.
1166
1142 The RTC driver needs to use one of the General Purpose Backup 1167 The RTC driver needs to use one of the General Purpose Backup
1143 Registers (GPBRs) as well as the RTT. You can choose which one 1168 Registers (GPBRs) as well as the RTT. You can choose which one
1144 will be used. The default of zero is normally OK to use, but 1169 will be used. The default of zero is normally OK to use, but
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index b188323c096a..c8ef3e1e6ccd 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -92,6 +92,7 @@ obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o
92obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o 92obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o
93obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o 93obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o
94obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o 94obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o
95obj-$(CONFIG_RTC_DRV_OPAL) += rtc-opal.o
95obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o 96obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o
96obj-$(CONFIG_RTC_DRV_PALMAS) += rtc-palmas.o 97obj-$(CONFIG_RTC_DRV_PALMAS) += rtc-palmas.o
97obj-$(CONFIG_RTC_DRV_PCAP) += rtc-pcap.o 98obj-$(CONFIG_RTC_DRV_PCAP) += rtc-pcap.o
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 38e26be705be..472a5adc4642 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -45,14 +45,14 @@ int rtc_hctosys_ret = -ENODEV;
45 * system's wall clock; restore it on resume(). 45 * system's wall clock; restore it on resume().
46 */ 46 */
47 47
48static struct timespec old_rtc, old_system, old_delta; 48static struct timespec64 old_rtc, old_system, old_delta;
49 49
50 50
51static int rtc_suspend(struct device *dev) 51static int rtc_suspend(struct device *dev)
52{ 52{
53 struct rtc_device *rtc = to_rtc_device(dev); 53 struct rtc_device *rtc = to_rtc_device(dev);
54 struct rtc_time tm; 54 struct rtc_time tm;
55 struct timespec delta, delta_delta; 55 struct timespec64 delta, delta_delta;
56 int err; 56 int err;
57 57
58 if (has_persistent_clock()) 58 if (has_persistent_clock())
@@ -68,8 +68,8 @@ static int rtc_suspend(struct device *dev)
68 return 0; 68 return 0;
69 } 69 }
70 70
71 getnstimeofday(&old_system); 71 getnstimeofday64(&old_system);
72 rtc_tm_to_time(&tm, &old_rtc.tv_sec); 72 old_rtc.tv_sec = rtc_tm_to_time64(&tm);
73 73
74 74
75 /* 75 /*
@@ -78,8 +78,8 @@ static int rtc_suspend(struct device *dev)
78 * try to compensate so the difference in system time 78 * try to compensate so the difference in system time
79 * and rtc time stays close to constant. 79 * and rtc time stays close to constant.
80 */ 80 */
81 delta = timespec_sub(old_system, old_rtc); 81 delta = timespec64_sub(old_system, old_rtc);
82 delta_delta = timespec_sub(delta, old_delta); 82 delta_delta = timespec64_sub(delta, old_delta);
83 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) { 83 if (delta_delta.tv_sec < -2 || delta_delta.tv_sec >= 2) {
84 /* 84 /*
85 * if delta_delta is too large, assume time correction 85 * if delta_delta is too large, assume time correction
@@ -88,7 +88,7 @@ static int rtc_suspend(struct device *dev)
88 old_delta = delta; 88 old_delta = delta;
89 } else { 89 } else {
90 /* Otherwise try to adjust old_system to compensate */ 90 /* Otherwise try to adjust old_system to compensate */
91 old_system = timespec_sub(old_system, delta_delta); 91 old_system = timespec64_sub(old_system, delta_delta);
92 } 92 }
93 93
94 return 0; 94 return 0;
@@ -98,8 +98,8 @@ static int rtc_resume(struct device *dev)
98{ 98{
99 struct rtc_device *rtc = to_rtc_device(dev); 99 struct rtc_device *rtc = to_rtc_device(dev);
100 struct rtc_time tm; 100 struct rtc_time tm;
101 struct timespec new_system, new_rtc; 101 struct timespec64 new_system, new_rtc;
102 struct timespec sleep_time; 102 struct timespec64 sleep_time;
103 int err; 103 int err;
104 104
105 if (has_persistent_clock()) 105 if (has_persistent_clock())
@@ -110,7 +110,7 @@ static int rtc_resume(struct device *dev)
110 return 0; 110 return 0;
111 111
112 /* snapshot the current rtc and system time at resume */ 112 /* snapshot the current rtc and system time at resume */
113 getnstimeofday(&new_system); 113 getnstimeofday64(&new_system);
114 err = rtc_read_time(rtc, &tm); 114 err = rtc_read_time(rtc, &tm);
115 if (err < 0) { 115 if (err < 0) {
116 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev)); 116 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
@@ -121,7 +121,7 @@ static int rtc_resume(struct device *dev)
121 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev)); 121 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
122 return 0; 122 return 0;
123 } 123 }
124 rtc_tm_to_time(&tm, &new_rtc.tv_sec); 124 new_rtc.tv_sec = rtc_tm_to_time64(&tm);
125 new_rtc.tv_nsec = 0; 125 new_rtc.tv_nsec = 0;
126 126
127 if (new_rtc.tv_sec < old_rtc.tv_sec) { 127 if (new_rtc.tv_sec < old_rtc.tv_sec) {
@@ -130,7 +130,7 @@ static int rtc_resume(struct device *dev)
130 } 130 }
131 131
132 /* calculate the RTC time delta (sleep time)*/ 132 /* calculate the RTC time delta (sleep time)*/
133 sleep_time = timespec_sub(new_rtc, old_rtc); 133 sleep_time = timespec64_sub(new_rtc, old_rtc);
134 134
135 /* 135 /*
136 * Since these RTC suspend/resume handlers are not called 136 * Since these RTC suspend/resume handlers are not called
@@ -139,11 +139,11 @@ static int rtc_resume(struct device *dev)
139 * so subtract kernel run-time between rtc_suspend to rtc_resume 139 * so subtract kernel run-time between rtc_suspend to rtc_resume
140 * to keep things accurate. 140 * to keep things accurate.
141 */ 141 */
142 sleep_time = timespec_sub(sleep_time, 142 sleep_time = timespec64_sub(sleep_time,
143 timespec_sub(new_system, old_system)); 143 timespec64_sub(new_system, old_system));
144 144
145 if (sleep_time.tv_sec >= 0) 145 if (sleep_time.tv_sec >= 0)
146 timekeeping_inject_sleeptime(&sleep_time); 146 timekeeping_inject_sleeptime64(&sleep_time);
147 rtc_hctosys_ret = 0; 147 rtc_hctosys_ret = 0;
148 return 0; 148 return 0;
149} 149}
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 5b2717f5dafa..45bfc28ee3aa 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -30,6 +30,14 @@ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
30 else { 30 else {
31 memset(tm, 0, sizeof(struct rtc_time)); 31 memset(tm, 0, sizeof(struct rtc_time));
32 err = rtc->ops->read_time(rtc->dev.parent, tm); 32 err = rtc->ops->read_time(rtc->dev.parent, tm);
33 if (err < 0) {
34 dev_err(&rtc->dev, "read_time: fail to read\n");
35 return err;
36 }
37
38 err = rtc_valid_tm(tm);
39 if (err < 0)
40 dev_err(&rtc->dev, "read_time: rtc_time isn't valid\n");
33 } 41 }
34 return err; 42 return err;
35} 43}
@@ -891,11 +899,24 @@ again:
891 if (next) { 899 if (next) {
892 struct rtc_wkalrm alarm; 900 struct rtc_wkalrm alarm;
893 int err; 901 int err;
902 int retry = 3;
903
894 alarm.time = rtc_ktime_to_tm(next->expires); 904 alarm.time = rtc_ktime_to_tm(next->expires);
895 alarm.enabled = 1; 905 alarm.enabled = 1;
906reprogram:
896 err = __rtc_set_alarm(rtc, &alarm); 907 err = __rtc_set_alarm(rtc, &alarm);
897 if (err == -ETIME) 908 if (err == -ETIME)
898 goto again; 909 goto again;
910 else if (err) {
911 if (retry-- > 0)
912 goto reprogram;
913
914 timer = container_of(next, struct rtc_timer, node);
915 timerqueue_del(&rtc->timerqueue, &timer->node);
916 timer->enabled = 0;
917 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
918 goto again;
919 }
899 } else 920 } else
900 rtc_alarm_disable(rtc); 921 rtc_alarm_disable(rtc);
901 922
diff --git a/drivers/rtc/rtc-ab8500.c b/drivers/rtc/rtc-ab8500.c
index 43c09ad5112b..6856f0a3a3d5 100644
--- a/drivers/rtc/rtc-ab8500.c
+++ b/drivers/rtc/rtc-ab8500.c
@@ -504,6 +504,8 @@ static int ab8500_rtc_probe(struct platform_device *pdev)
504 return err; 504 return err;
505 } 505 }
506 506
507 rtc->uie_unsupported = 1;
508
507 return 0; 509 return 0;
508} 510}
509 511
diff --git a/drivers/rtc/rtc-at91sam9.c b/drivers/rtc/rtc-at91sam9.c
index dcf211b941a5..6b9aaf1afc72 100644
--- a/drivers/rtc/rtc-at91sam9.c
+++ b/drivers/rtc/rtc-at91sam9.c
@@ -21,10 +21,9 @@
21#include <linux/slab.h> 21#include <linux/slab.h>
22#include <linux/platform_data/atmel.h> 22#include <linux/platform_data/atmel.h>
23#include <linux/io.h> 23#include <linux/io.h>
24 24#include <linux/mfd/syscon.h>
25#include <mach/at91_rtt.h> 25#include <linux/regmap.h>
26#include <mach/cpu.h> 26#include <linux/clk.h>
27#include <mach/hardware.h>
28 27
29/* 28/*
30 * This driver uses two configurable hardware resources that live in the 29 * This driver uses two configurable hardware resources that live in the
@@ -47,6 +46,22 @@
47 * registers available, likewise usable for more than "RTC" support. 46 * registers available, likewise usable for more than "RTC" support.
48 */ 47 */
49 48
49#define AT91_RTT_MR 0x00 /* Real-time Mode Register */
50#define AT91_RTT_RTPRES (0xffff << 0) /* Real-time Timer Prescaler Value */
51#define AT91_RTT_ALMIEN (1 << 16) /* Alarm Interrupt Enable */
52#define AT91_RTT_RTTINCIEN (1 << 17) /* Real Time Timer Increment Interrupt Enable */
53#define AT91_RTT_RTTRST (1 << 18) /* Real Time Timer Restart */
54
55#define AT91_RTT_AR 0x04 /* Real-time Alarm Register */
56#define AT91_RTT_ALMV (0xffffffff) /* Alarm Value */
57
58#define AT91_RTT_VR 0x08 /* Real-time Value Register */
59#define AT91_RTT_CRTV (0xffffffff) /* Current Real-time Value */
60
61#define AT91_RTT_SR 0x0c /* Real-time Status Register */
62#define AT91_RTT_ALMS (1 << 0) /* Real-time Alarm Status */
63#define AT91_RTT_RTTINC (1 << 1) /* Real-time Timer Increment */
64
50/* 65/*
51 * We store ALARM_DISABLED in ALMV to record that no alarm is set. 66 * We store ALARM_DISABLED in ALMV to record that no alarm is set.
52 * It's also the reset value for that field. 67 * It's also the reset value for that field.
@@ -58,19 +73,30 @@ struct sam9_rtc {
58 void __iomem *rtt; 73 void __iomem *rtt;
59 struct rtc_device *rtcdev; 74 struct rtc_device *rtcdev;
60 u32 imr; 75 u32 imr;
61 void __iomem *gpbr; 76 struct regmap *gpbr;
77 unsigned int gpbr_offset;
62 int irq; 78 int irq;
79 struct clk *sclk;
63}; 80};
64 81
65#define rtt_readl(rtc, field) \ 82#define rtt_readl(rtc, field) \
66 __raw_readl((rtc)->rtt + AT91_RTT_ ## field) 83 readl((rtc)->rtt + AT91_RTT_ ## field)
67#define rtt_writel(rtc, field, val) \ 84#define rtt_writel(rtc, field, val) \
68 __raw_writel((val), (rtc)->rtt + AT91_RTT_ ## field) 85 writel((val), (rtc)->rtt + AT91_RTT_ ## field)
86
87static inline unsigned int gpbr_readl(struct sam9_rtc *rtc)
88{
89 unsigned int val;
90
91 regmap_read(rtc->gpbr, rtc->gpbr_offset, &val);
69 92
70#define gpbr_readl(rtc) \ 93 return val;
71 __raw_readl((rtc)->gpbr) 94}
72#define gpbr_writel(rtc, val) \ 95
73 __raw_writel((val), (rtc)->gpbr) 96static inline void gpbr_writel(struct sam9_rtc *rtc, unsigned int val)
97{
98 regmap_write(rtc->gpbr, rtc->gpbr_offset, val);
99}
74 100
75/* 101/*
76 * Read current time and date in RTC 102 * Read current time and date in RTC
@@ -287,22 +313,22 @@ static const struct rtc_class_ops at91_rtc_ops = {
287 .alarm_irq_enable = at91_rtc_alarm_irq_enable, 313 .alarm_irq_enable = at91_rtc_alarm_irq_enable,
288}; 314};
289 315
316static struct regmap_config gpbr_regmap_config = {
317 .reg_bits = 32,
318 .val_bits = 32,
319 .reg_stride = 4,
320};
321
290/* 322/*
291 * Initialize and install RTC driver 323 * Initialize and install RTC driver
292 */ 324 */
293static int at91_rtc_probe(struct platform_device *pdev) 325static int at91_rtc_probe(struct platform_device *pdev)
294{ 326{
295 struct resource *r, *r_gpbr; 327 struct resource *r;
296 struct sam9_rtc *rtc; 328 struct sam9_rtc *rtc;
297 int ret, irq; 329 int ret, irq;
298 u32 mr; 330 u32 mr;
299 331 unsigned int sclk_rate;
300 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
301 r_gpbr = platform_get_resource(pdev, IORESOURCE_MEM, 1);
302 if (!r || !r_gpbr) {
303 dev_err(&pdev->dev, "need 2 ressources\n");
304 return -ENODEV;
305 }
306 332
307 irq = platform_get_irq(pdev, 0); 333 irq = platform_get_irq(pdev, 0);
308 if (irq < 0) { 334 if (irq < 0) {
@@ -321,24 +347,66 @@ static int at91_rtc_probe(struct platform_device *pdev)
321 device_init_wakeup(&pdev->dev, 1); 347 device_init_wakeup(&pdev->dev, 1);
322 348
323 platform_set_drvdata(pdev, rtc); 349 platform_set_drvdata(pdev, rtc);
324 rtc->rtt = devm_ioremap(&pdev->dev, r->start, resource_size(r)); 350
325 if (!rtc->rtt) { 351 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
326 dev_err(&pdev->dev, "failed to map registers, aborting.\n"); 352 rtc->rtt = devm_ioremap_resource(&pdev->dev, r);
327 return -ENOMEM; 353 if (IS_ERR(rtc->rtt))
354 return PTR_ERR(rtc->rtt);
355
356 if (!pdev->dev.of_node) {
357 /*
358 * TODO: Remove this code chunk when removing non DT board
359 * support. Remember to remove the gpbr_regmap_config
360 * variable too.
361 */
362 void __iomem *gpbr;
363
364 r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
365 gpbr = devm_ioremap_resource(&pdev->dev, r);
366 if (IS_ERR(gpbr))
367 return PTR_ERR(gpbr);
368
369 rtc->gpbr = regmap_init_mmio(NULL, gpbr,
370 &gpbr_regmap_config);
371 } else {
372 struct of_phandle_args args;
373
374 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
375 "atmel,rtt-rtc-time-reg", 1, 0,
376 &args);
377 if (ret)
378 return ret;
379
380 rtc->gpbr = syscon_node_to_regmap(args.np);
381 rtc->gpbr_offset = args.args[0];
328 } 382 }
329 383
330 rtc->gpbr = devm_ioremap(&pdev->dev, r_gpbr->start, 384 if (IS_ERR(rtc->gpbr)) {
331 resource_size(r_gpbr)); 385 dev_err(&pdev->dev, "failed to retrieve gpbr regmap, aborting.\n");
332 if (!rtc->gpbr) {
333 dev_err(&pdev->dev, "failed to map gpbr registers, aborting.\n");
334 return -ENOMEM; 386 return -ENOMEM;
335 } 387 }
336 388
389 rtc->sclk = devm_clk_get(&pdev->dev, NULL);
390 if (IS_ERR(rtc->sclk))
391 return PTR_ERR(rtc->sclk);
392
393 sclk_rate = clk_get_rate(rtc->sclk);
394 if (!sclk_rate || sclk_rate > AT91_RTT_RTPRES) {
395 dev_err(&pdev->dev, "Invalid slow clock rate\n");
396 return -EINVAL;
397 }
398
399 ret = clk_prepare_enable(rtc->sclk);
400 if (ret) {
401 dev_err(&pdev->dev, "Could not enable slow clock\n");
402 return ret;
403 }
404
337 mr = rtt_readl(rtc, MR); 405 mr = rtt_readl(rtc, MR);
338 406
339 /* unless RTT is counting at 1 Hz, re-initialize it */ 407 /* unless RTT is counting at 1 Hz, re-initialize it */
340 if ((mr & AT91_RTT_RTPRES) != AT91_SLOW_CLOCK) { 408 if ((mr & AT91_RTT_RTPRES) != sclk_rate) {
341 mr = AT91_RTT_RTTRST | (AT91_SLOW_CLOCK & AT91_RTT_RTPRES); 409 mr = AT91_RTT_RTTRST | (sclk_rate & AT91_RTT_RTPRES);
342 gpbr_writel(rtc, 0); 410 gpbr_writel(rtc, 0);
343 } 411 }
344 412
@@ -383,6 +451,9 @@ static int at91_rtc_remove(struct platform_device *pdev)
383 /* disable all interrupts */ 451 /* disable all interrupts */
384 rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN)); 452 rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
385 453
454 if (!IS_ERR(rtc->sclk))
455 clk_disable_unprepare(rtc->sclk);
456
386 return 0; 457 return 0;
387} 458}
388 459
@@ -440,6 +511,14 @@ static int at91_rtc_resume(struct device *dev)
440 511
441static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume); 512static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
442 513
514#ifdef CONFIG_OF
515static const struct of_device_id at91_rtc_dt_ids[] = {
516 { .compatible = "atmel,at91sam9260-rtt" },
517 { /* sentinel */ }
518};
519MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
520#endif
521
443static struct platform_driver at91_rtc_driver = { 522static struct platform_driver at91_rtc_driver = {
444 .probe = at91_rtc_probe, 523 .probe = at91_rtc_probe,
445 .remove = at91_rtc_remove, 524 .remove = at91_rtc_remove,
@@ -447,6 +526,7 @@ static struct platform_driver at91_rtc_driver = {
447 .driver = { 526 .driver = {
448 .name = "rtc-at91sam9", 527 .name = "rtc-at91sam9",
449 .pm = &at91_rtc_pm_ops, 528 .pm = &at91_rtc_pm_ops,
529 .of_match_table = of_match_ptr(at91_rtc_dt_ids),
450 }, 530 },
451}; 531};
452 532
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index bb43cf703efc..4ffabb322a9a 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -35,7 +35,7 @@ enum ds_type {
35 ds_1388, 35 ds_1388,
36 ds_3231, 36 ds_3231,
37 m41t00, 37 m41t00,
38 mcp7941x, 38 mcp794xx,
39 rx_8025, 39 rx_8025,
40 last_ds_type /* always last */ 40 last_ds_type /* always last */
41 /* rs5c372 too? different address... */ 41 /* rs5c372 too? different address... */
@@ -46,7 +46,7 @@ enum ds_type {
46#define DS1307_REG_SECS 0x00 /* 00-59 */ 46#define DS1307_REG_SECS 0x00 /* 00-59 */
47# define DS1307_BIT_CH 0x80 47# define DS1307_BIT_CH 0x80
48# define DS1340_BIT_nEOSC 0x80 48# define DS1340_BIT_nEOSC 0x80
49# define MCP7941X_BIT_ST 0x80 49# define MCP794XX_BIT_ST 0x80
50#define DS1307_REG_MIN 0x01 /* 00-59 */ 50#define DS1307_REG_MIN 0x01 /* 00-59 */
51#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */ 51#define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
52# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */ 52# define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
@@ -54,7 +54,7 @@ enum ds_type {
54# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */ 54# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
55# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */ 55# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
56#define DS1307_REG_WDAY 0x03 /* 01-07 */ 56#define DS1307_REG_WDAY 0x03 /* 01-07 */
57# define MCP7941X_BIT_VBATEN 0x08 57# define MCP794XX_BIT_VBATEN 0x08
58#define DS1307_REG_MDAY 0x04 /* 01-31 */ 58#define DS1307_REG_MDAY 0x04 /* 01-31 */
59#define DS1307_REG_MONTH 0x05 /* 01-12 */ 59#define DS1307_REG_MONTH 0x05 /* 01-12 */
60# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */ 60# define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
@@ -159,7 +159,7 @@ static struct chip_desc chips[last_ds_type] = {
159 [ds_3231] = { 159 [ds_3231] = {
160 .alarm = 1, 160 .alarm = 1,
161 }, 161 },
162 [mcp7941x] = { 162 [mcp794xx] = {
163 .alarm = 1, 163 .alarm = 1,
164 /* this is battery backed SRAM */ 164 /* this is battery backed SRAM */
165 .nvram_offset = 0x20, 165 .nvram_offset = 0x20,
@@ -176,7 +176,8 @@ static const struct i2c_device_id ds1307_id[] = {
176 { "ds1340", ds_1340 }, 176 { "ds1340", ds_1340 },
177 { "ds3231", ds_3231 }, 177 { "ds3231", ds_3231 },
178 { "m41t00", m41t00 }, 178 { "m41t00", m41t00 },
179 { "mcp7941x", mcp7941x }, 179 { "mcp7940x", mcp794xx },
180 { "mcp7941x", mcp794xx },
180 { "pt7c4338", ds_1307 }, 181 { "pt7c4338", ds_1307 },
181 { "rx8025", rx_8025 }, 182 { "rx8025", rx_8025 },
182 { } 183 { }
@@ -439,14 +440,14 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
439 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN 440 buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
440 | DS1340_BIT_CENTURY; 441 | DS1340_BIT_CENTURY;
441 break; 442 break;
442 case mcp7941x: 443 case mcp794xx:
443 /* 444 /*
444 * these bits were cleared when preparing the date/time 445 * these bits were cleared when preparing the date/time
445 * values and need to be set again before writing the 446 * values and need to be set again before writing the
446 * buffer out to the device. 447 * buffer out to the device.
447 */ 448 */
448 buf[DS1307_REG_SECS] |= MCP7941X_BIT_ST; 449 buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
449 buf[DS1307_REG_WDAY] |= MCP7941X_BIT_VBATEN; 450 buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
450 break; 451 break;
451 default: 452 default:
452 break; 453 break;
@@ -614,26 +615,26 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
614/*----------------------------------------------------------------------*/ 615/*----------------------------------------------------------------------*/
615 616
616/* 617/*
617 * Alarm support for mcp7941x devices. 618 * Alarm support for mcp794xx devices.
618 */ 619 */
619 620
620#define MCP7941X_REG_CONTROL 0x07 621#define MCP794XX_REG_CONTROL 0x07
621# define MCP7941X_BIT_ALM0_EN 0x10 622# define MCP794XX_BIT_ALM0_EN 0x10
622# define MCP7941X_BIT_ALM1_EN 0x20 623# define MCP794XX_BIT_ALM1_EN 0x20
623#define MCP7941X_REG_ALARM0_BASE 0x0a 624#define MCP794XX_REG_ALARM0_BASE 0x0a
624#define MCP7941X_REG_ALARM0_CTRL 0x0d 625#define MCP794XX_REG_ALARM0_CTRL 0x0d
625#define MCP7941X_REG_ALARM1_BASE 0x11 626#define MCP794XX_REG_ALARM1_BASE 0x11
626#define MCP7941X_REG_ALARM1_CTRL 0x14 627#define MCP794XX_REG_ALARM1_CTRL 0x14
627# define MCP7941X_BIT_ALMX_IF (1 << 3) 628# define MCP794XX_BIT_ALMX_IF (1 << 3)
628# define MCP7941X_BIT_ALMX_C0 (1 << 4) 629# define MCP794XX_BIT_ALMX_C0 (1 << 4)
629# define MCP7941X_BIT_ALMX_C1 (1 << 5) 630# define MCP794XX_BIT_ALMX_C1 (1 << 5)
630# define MCP7941X_BIT_ALMX_C2 (1 << 6) 631# define MCP794XX_BIT_ALMX_C2 (1 << 6)
631# define MCP7941X_BIT_ALMX_POL (1 << 7) 632# define MCP794XX_BIT_ALMX_POL (1 << 7)
632# define MCP7941X_MSK_ALMX_MATCH (MCP7941X_BIT_ALMX_C0 | \ 633# define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
633 MCP7941X_BIT_ALMX_C1 | \ 634 MCP794XX_BIT_ALMX_C1 | \
634 MCP7941X_BIT_ALMX_C2) 635 MCP794XX_BIT_ALMX_C2)
635 636
636static void mcp7941x_work(struct work_struct *work) 637static void mcp794xx_work(struct work_struct *work)
637{ 638{
638 struct ds1307 *ds1307 = container_of(work, struct ds1307, work); 639 struct ds1307 *ds1307 = container_of(work, struct ds1307, work);
639 struct i2c_client *client = ds1307->client; 640 struct i2c_client *client = ds1307->client;
@@ -642,22 +643,22 @@ static void mcp7941x_work(struct work_struct *work)
642 mutex_lock(&ds1307->rtc->ops_lock); 643 mutex_lock(&ds1307->rtc->ops_lock);
643 644
644 /* Check and clear alarm 0 interrupt flag. */ 645 /* Check and clear alarm 0 interrupt flag. */
645 reg = i2c_smbus_read_byte_data(client, MCP7941X_REG_ALARM0_CTRL); 646 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
646 if (reg < 0) 647 if (reg < 0)
647 goto out; 648 goto out;
648 if (!(reg & MCP7941X_BIT_ALMX_IF)) 649 if (!(reg & MCP794XX_BIT_ALMX_IF))
649 goto out; 650 goto out;
650 reg &= ~MCP7941X_BIT_ALMX_IF; 651 reg &= ~MCP794XX_BIT_ALMX_IF;
651 ret = i2c_smbus_write_byte_data(client, MCP7941X_REG_ALARM0_CTRL, reg); 652 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
652 if (ret < 0) 653 if (ret < 0)
653 goto out; 654 goto out;
654 655
655 /* Disable alarm 0. */ 656 /* Disable alarm 0. */
656 reg = i2c_smbus_read_byte_data(client, MCP7941X_REG_CONTROL); 657 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
657 if (reg < 0) 658 if (reg < 0)
658 goto out; 659 goto out;
659 reg &= ~MCP7941X_BIT_ALM0_EN; 660 reg &= ~MCP794XX_BIT_ALM0_EN;
660 ret = i2c_smbus_write_byte_data(client, MCP7941X_REG_CONTROL, reg); 661 ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
661 if (ret < 0) 662 if (ret < 0)
662 goto out; 663 goto out;
663 664
@@ -669,7 +670,7 @@ out:
669 mutex_unlock(&ds1307->rtc->ops_lock); 670 mutex_unlock(&ds1307->rtc->ops_lock);
670} 671}
671 672
672static int mcp7941x_read_alarm(struct device *dev, struct rtc_wkalrm *t) 673static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
673{ 674{
674 struct i2c_client *client = to_i2c_client(dev); 675 struct i2c_client *client = to_i2c_client(dev);
675 struct ds1307 *ds1307 = i2c_get_clientdata(client); 676 struct ds1307 *ds1307 = i2c_get_clientdata(client);
@@ -680,11 +681,11 @@ static int mcp7941x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
680 return -EINVAL; 681 return -EINVAL;
681 682
682 /* Read control and alarm 0 registers. */ 683 /* Read control and alarm 0 registers. */
683 ret = ds1307->read_block_data(client, MCP7941X_REG_CONTROL, 10, regs); 684 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
684 if (ret < 0) 685 if (ret < 0)
685 return ret; 686 return ret;
686 687
687 t->enabled = !!(regs[0] & MCP7941X_BIT_ALM0_EN); 688 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
688 689
689 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */ 690 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
690 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f); 691 t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
@@ -701,14 +702,14 @@ static int mcp7941x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
701 "enabled=%d polarity=%d irq=%d match=%d\n", __func__, 702 "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
702 t->time.tm_sec, t->time.tm_min, t->time.tm_hour, 703 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
703 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled, 704 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
704 !!(ds1307->regs[6] & MCP7941X_BIT_ALMX_POL), 705 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
705 !!(ds1307->regs[6] & MCP7941X_BIT_ALMX_IF), 706 !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
706 (ds1307->regs[6] & MCP7941X_MSK_ALMX_MATCH) >> 4); 707 (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
707 708
708 return 0; 709 return 0;
709} 710}
710 711
711static int mcp7941x_set_alarm(struct device *dev, struct rtc_wkalrm *t) 712static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
712{ 713{
713 struct i2c_client *client = to_i2c_client(dev); 714 struct i2c_client *client = to_i2c_client(dev);
714 struct ds1307 *ds1307 = i2c_get_clientdata(client); 715 struct ds1307 *ds1307 = i2c_get_clientdata(client);
@@ -725,7 +726,7 @@ static int mcp7941x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
725 t->enabled, t->pending); 726 t->enabled, t->pending);
726 727
727 /* Read control and alarm 0 registers. */ 728 /* Read control and alarm 0 registers. */
728 ret = ds1307->read_block_data(client, MCP7941X_REG_CONTROL, 10, regs); 729 ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
729 if (ret < 0) 730 if (ret < 0)
730 return ret; 731 return ret;
731 732
@@ -738,23 +739,23 @@ static int mcp7941x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
738 regs[8] = bin2bcd(t->time.tm_mon) + 1; 739 regs[8] = bin2bcd(t->time.tm_mon) + 1;
739 740
740 /* Clear the alarm 0 interrupt flag. */ 741 /* Clear the alarm 0 interrupt flag. */
741 regs[6] &= ~MCP7941X_BIT_ALMX_IF; 742 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
742 /* Set alarm match: second, minute, hour, day, date, month. */ 743 /* Set alarm match: second, minute, hour, day, date, month. */
743 regs[6] |= MCP7941X_MSK_ALMX_MATCH; 744 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
744 745
745 if (t->enabled) 746 if (t->enabled)
746 regs[0] |= MCP7941X_BIT_ALM0_EN; 747 regs[0] |= MCP794XX_BIT_ALM0_EN;
747 else 748 else
748 regs[0] &= ~MCP7941X_BIT_ALM0_EN; 749 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
749 750
750 ret = ds1307->write_block_data(client, MCP7941X_REG_CONTROL, 10, regs); 751 ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
751 if (ret < 0) 752 if (ret < 0)
752 return ret; 753 return ret;
753 754
754 return 0; 755 return 0;
755} 756}
756 757
757static int mcp7941x_alarm_irq_enable(struct device *dev, unsigned int enabled) 758static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
758{ 759{
759 struct i2c_client *client = to_i2c_client(dev); 760 struct i2c_client *client = to_i2c_client(dev);
760 struct ds1307 *ds1307 = i2c_get_clientdata(client); 761 struct ds1307 *ds1307 = i2c_get_clientdata(client);
@@ -763,24 +764,24 @@ static int mcp7941x_alarm_irq_enable(struct device *dev, unsigned int enabled)
763 if (!test_bit(HAS_ALARM, &ds1307->flags)) 764 if (!test_bit(HAS_ALARM, &ds1307->flags))
764 return -EINVAL; 765 return -EINVAL;
765 766
766 reg = i2c_smbus_read_byte_data(client, MCP7941X_REG_CONTROL); 767 reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
767 if (reg < 0) 768 if (reg < 0)
768 return reg; 769 return reg;
769 770
770 if (enabled) 771 if (enabled)
771 reg |= MCP7941X_BIT_ALM0_EN; 772 reg |= MCP794XX_BIT_ALM0_EN;
772 else 773 else
773 reg &= ~MCP7941X_BIT_ALM0_EN; 774 reg &= ~MCP794XX_BIT_ALM0_EN;
774 775
775 return i2c_smbus_write_byte_data(client, MCP7941X_REG_CONTROL, reg); 776 return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
776} 777}
777 778
778static const struct rtc_class_ops mcp7941x_rtc_ops = { 779static const struct rtc_class_ops mcp794xx_rtc_ops = {
779 .read_time = ds1307_get_time, 780 .read_time = ds1307_get_time,
780 .set_time = ds1307_set_time, 781 .set_time = ds1307_set_time,
781 .read_alarm = mcp7941x_read_alarm, 782 .read_alarm = mcp794xx_read_alarm,
782 .set_alarm = mcp7941x_set_alarm, 783 .set_alarm = mcp794xx_set_alarm,
783 .alarm_irq_enable = mcp7941x_alarm_irq_enable, 784 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
784}; 785};
785 786
786/*----------------------------------------------------------------------*/ 787/*----------------------------------------------------------------------*/
@@ -1049,10 +1050,10 @@ static int ds1307_probe(struct i2c_client *client,
1049 case ds_1388: 1050 case ds_1388:
1050 ds1307->offset = 1; /* Seconds starts at 1 */ 1051 ds1307->offset = 1; /* Seconds starts at 1 */
1051 break; 1052 break;
1052 case mcp7941x: 1053 case mcp794xx:
1053 rtc_ops = &mcp7941x_rtc_ops; 1054 rtc_ops = &mcp794xx_rtc_ops;
1054 if (ds1307->client->irq > 0 && chip->alarm) { 1055 if (ds1307->client->irq > 0 && chip->alarm) {
1055 INIT_WORK(&ds1307->work, mcp7941x_work); 1056 INIT_WORK(&ds1307->work, mcp794xx_work);
1056 want_irq = true; 1057 want_irq = true;
1057 } 1058 }
1058 break; 1059 break;
@@ -1117,18 +1118,18 @@ read_rtc:
1117 dev_warn(&client->dev, "SET TIME!\n"); 1118 dev_warn(&client->dev, "SET TIME!\n");
1118 } 1119 }
1119 break; 1120 break;
1120 case mcp7941x: 1121 case mcp794xx:
1121 /* make sure that the backup battery is enabled */ 1122 /* make sure that the backup battery is enabled */
1122 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP7941X_BIT_VBATEN)) { 1123 if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
1123 i2c_smbus_write_byte_data(client, DS1307_REG_WDAY, 1124 i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
1124 ds1307->regs[DS1307_REG_WDAY] 1125 ds1307->regs[DS1307_REG_WDAY]
1125 | MCP7941X_BIT_VBATEN); 1126 | MCP794XX_BIT_VBATEN);
1126 } 1127 }
1127 1128
1128 /* clock halted? turn it on, so clock can tick. */ 1129 /* clock halted? turn it on, so clock can tick. */
1129 if (!(tmp & MCP7941X_BIT_ST)) { 1130 if (!(tmp & MCP794XX_BIT_ST)) {
1130 i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 1131 i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
1131 MCP7941X_BIT_ST); 1132 MCP794XX_BIT_ST);
1132 dev_warn(&client->dev, "SET TIME!\n"); 1133 dev_warn(&client->dev, "SET TIME!\n");
1133 goto read_rtc; 1134 goto read_rtc;
1134 } 1135 }
diff --git a/drivers/rtc/rtc-ds1374.c b/drivers/rtc/rtc-ds1374.c
index 9e6e14fb53d7..8605fde394b2 100644
--- a/drivers/rtc/rtc-ds1374.c
+++ b/drivers/rtc/rtc-ds1374.c
@@ -4,6 +4,7 @@
4 * Based on code by Randy Vinson <rvinson@mvista.com>, 4 * Based on code by Randy Vinson <rvinson@mvista.com>,
5 * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>. 5 * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
6 * 6 *
7 * Copyright (C) 2014 Rose Technology
7 * Copyright (C) 2006-2007 Freescale Semiconductor 8 * Copyright (C) 2006-2007 Freescale Semiconductor
8 * 9 *
9 * 2005 (c) MontaVista Software, Inc. This file is licensed under 10 * 2005 (c) MontaVista Software, Inc. This file is licensed under
@@ -26,6 +27,13 @@
26#include <linux/workqueue.h> 27#include <linux/workqueue.h>
27#include <linux/slab.h> 28#include <linux/slab.h>
28#include <linux/pm.h> 29#include <linux/pm.h>
30#ifdef CONFIG_RTC_DRV_DS1374_WDT
31#include <linux/fs.h>
32#include <linux/ioctl.h>
33#include <linux/miscdevice.h>
34#include <linux/reboot.h>
35#include <linux/watchdog.h>
36#endif
29 37
30#define DS1374_REG_TOD0 0x00 /* Time of Day */ 38#define DS1374_REG_TOD0 0x00 /* Time of Day */
31#define DS1374_REG_TOD1 0x01 39#define DS1374_REG_TOD1 0x01
@@ -49,6 +57,14 @@ static const struct i2c_device_id ds1374_id[] = {
49}; 57};
50MODULE_DEVICE_TABLE(i2c, ds1374_id); 58MODULE_DEVICE_TABLE(i2c, ds1374_id);
51 59
60#ifdef CONFIG_OF
61static const struct of_device_id ds1374_of_match[] = {
62 { .compatible = "dallas,ds1374" },
63 { }
64};
65MODULE_DEVICE_TABLE(of, ds1374_of_match);
66#endif
67
52struct ds1374 { 68struct ds1374 {
53 struct i2c_client *client; 69 struct i2c_client *client;
54 struct rtc_device *rtc; 70 struct rtc_device *rtc;
@@ -162,6 +178,7 @@ static int ds1374_set_time(struct device *dev, struct rtc_time *time)
162 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4); 178 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
163} 179}
164 180
181#ifndef CONFIG_RTC_DRV_DS1374_WDT
165/* The ds1374 has a decrementer for an alarm, rather than a comparator. 182/* The ds1374 has a decrementer for an alarm, rather than a comparator.
166 * If the time of day is changed, then the alarm will need to be 183 * If the time of day is changed, then the alarm will need to be
167 * reset. 184 * reset.
@@ -263,6 +280,7 @@ out:
263 mutex_unlock(&ds1374->mutex); 280 mutex_unlock(&ds1374->mutex);
264 return ret; 281 return ret;
265} 282}
283#endif
266 284
267static irqreturn_t ds1374_irq(int irq, void *dev_id) 285static irqreturn_t ds1374_irq(int irq, void *dev_id)
268{ 286{
@@ -307,6 +325,7 @@ unlock:
307 mutex_unlock(&ds1374->mutex); 325 mutex_unlock(&ds1374->mutex);
308} 326}
309 327
328#ifndef CONFIG_RTC_DRV_DS1374_WDT
310static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled) 329static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
311{ 330{
312 struct i2c_client *client = to_i2c_client(dev); 331 struct i2c_client *client = to_i2c_client(dev);
@@ -331,15 +350,260 @@ out:
331 mutex_unlock(&ds1374->mutex); 350 mutex_unlock(&ds1374->mutex);
332 return ret; 351 return ret;
333} 352}
353#endif
334 354
335static const struct rtc_class_ops ds1374_rtc_ops = { 355static const struct rtc_class_ops ds1374_rtc_ops = {
336 .read_time = ds1374_read_time, 356 .read_time = ds1374_read_time,
337 .set_time = ds1374_set_time, 357 .set_time = ds1374_set_time,
358#ifndef CONFIG_RTC_DRV_DS1374_WDT
338 .read_alarm = ds1374_read_alarm, 359 .read_alarm = ds1374_read_alarm,
339 .set_alarm = ds1374_set_alarm, 360 .set_alarm = ds1374_set_alarm,
340 .alarm_irq_enable = ds1374_alarm_irq_enable, 361 .alarm_irq_enable = ds1374_alarm_irq_enable,
362#endif
363};
364
365#ifdef CONFIG_RTC_DRV_DS1374_WDT
366/*
367 *****************************************************************************
368 *
369 * Watchdog Driver
370 *
371 *****************************************************************************
372 */
373static struct i2c_client *save_client;
374/* Default margin */
375#define WD_TIMO 131762
376
377#define DRV_NAME "DS1374 Watchdog"
378
379static int wdt_margin = WD_TIMO;
380static unsigned long wdt_is_open;
381module_param(wdt_margin, int, 0);
382MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
383
384static const struct watchdog_info ds1374_wdt_info = {
385 .identity = "DS1374 WTD",
386 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
387 WDIOF_MAGICCLOSE,
341}; 388};
342 389
390static int ds1374_wdt_settimeout(unsigned int timeout)
391{
392 int ret = -ENOIOCTLCMD;
393 int cr;
394
395 ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
396 if (ret < 0)
397 goto out;
398
399 /* Disable any existing watchdog/alarm before setting the new one */
400 cr &= ~DS1374_REG_CR_WACE;
401
402 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
403 if (ret < 0)
404 goto out;
405
406 /* Set new watchdog time */
407 ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3);
408 if (ret) {
409 pr_info("rtc-ds1374 - couldn't set new watchdog time\n");
410 goto out;
411 }
412
413 /* Enable watchdog timer */
414 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
415 cr &= ~DS1374_REG_CR_AIE;
416
417 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
418 if (ret < 0)
419 goto out;
420
421 return 0;
422out:
423 return ret;
424}
425
426
427/*
428 * Reload the watchdog timer. (ie, pat the watchdog)
429 */
430static void ds1374_wdt_ping(void)
431{
432 u32 val;
433 int ret = 0;
434
435 ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3);
436 if (ret)
437 pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret);
438}
439
440static void ds1374_wdt_disable(void)
441{
442 int ret = -ENOIOCTLCMD;
443 int cr;
444
445 cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
446 /* Disable watchdog timer */
447 cr &= ~DS1374_REG_CR_WACE;
448
449 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
450}
451
452/*
453 * Watchdog device is opened, and watchdog starts running.
454 */
455static int ds1374_wdt_open(struct inode *inode, struct file *file)
456{
457 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
458
459 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
460 mutex_lock(&ds1374->mutex);
461 if (test_and_set_bit(0, &wdt_is_open)) {
462 mutex_unlock(&ds1374->mutex);
463 return -EBUSY;
464 }
465 /*
466 * Activate
467 */
468 wdt_is_open = 1;
469 mutex_unlock(&ds1374->mutex);
470 return nonseekable_open(inode, file);
471 }
472 return -ENODEV;
473}
474
475/*
476 * Close the watchdog device.
477 */
478static int ds1374_wdt_release(struct inode *inode, struct file *file)
479{
480 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
481 clear_bit(0, &wdt_is_open);
482
483 return 0;
484}
485
486/*
487 * Pat the watchdog whenever device is written to.
488 */
489static ssize_t ds1374_wdt_write(struct file *file, const char __user *data,
490 size_t len, loff_t *ppos)
491{
492 if (len) {
493 ds1374_wdt_ping();
494 return 1;
495 }
496 return 0;
497}
498
499static ssize_t ds1374_wdt_read(struct file *file, char __user *data,
500 size_t len, loff_t *ppos)
501{
502 return 0;
503}
504
505/*
506 * Handle commands from user-space.
507 */
508static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd,
509 unsigned long arg)
510{
511 int new_margin, options;
512
513 switch (cmd) {
514 case WDIOC_GETSUPPORT:
515 return copy_to_user((struct watchdog_info __user *)arg,
516 &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0;
517
518 case WDIOC_GETSTATUS:
519 case WDIOC_GETBOOTSTATUS:
520 return put_user(0, (int __user *)arg);
521 case WDIOC_KEEPALIVE:
522 ds1374_wdt_ping();
523 return 0;
524 case WDIOC_SETTIMEOUT:
525 if (get_user(new_margin, (int __user *)arg))
526 return -EFAULT;
527
528 if (new_margin < 1 || new_margin > 16777216)
529 return -EINVAL;
530
531 wdt_margin = new_margin;
532 ds1374_wdt_settimeout(new_margin);
533 ds1374_wdt_ping();
534 /* fallthrough */
535 case WDIOC_GETTIMEOUT:
536 return put_user(wdt_margin, (int __user *)arg);
537 case WDIOC_SETOPTIONS:
538 if (copy_from_user(&options, (int __user *)arg, sizeof(int)))
539 return -EFAULT;
540
541 if (options & WDIOS_DISABLECARD) {
542 pr_info("rtc-ds1374: disable watchdog\n");
543 ds1374_wdt_disable();
544 }
545
546 if (options & WDIOS_ENABLECARD) {
547 pr_info("rtc-ds1374: enable watchdog\n");
548 ds1374_wdt_settimeout(wdt_margin);
549 ds1374_wdt_ping();
550 }
551
552 return -EINVAL;
553 }
554 return -ENOTTY;
555}
556
557static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
558 unsigned long arg)
559{
560 int ret;
561 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
562
563 mutex_lock(&ds1374->mutex);
564 ret = ds1374_wdt_ioctl(file, cmd, arg);
565 mutex_unlock(&ds1374->mutex);
566
567 return ret;
568}
569
570static int ds1374_wdt_notify_sys(struct notifier_block *this,
571 unsigned long code, void *unused)
572{
573 if (code == SYS_DOWN || code == SYS_HALT)
574 /* Disable Watchdog */
575 ds1374_wdt_disable();
576 return NOTIFY_DONE;
577}
578
579static const struct file_operations ds1374_wdt_fops = {
580 .owner = THIS_MODULE,
581 .read = ds1374_wdt_read,
582 .unlocked_ioctl = ds1374_wdt_unlocked_ioctl,
583 .write = ds1374_wdt_write,
584 .open = ds1374_wdt_open,
585 .release = ds1374_wdt_release,
586 .llseek = no_llseek,
587};
588
589static struct miscdevice ds1374_miscdev = {
590 .minor = WATCHDOG_MINOR,
591 .name = "watchdog",
592 .fops = &ds1374_wdt_fops,
593};
594
595static struct notifier_block ds1374_wdt_notifier = {
596 .notifier_call = ds1374_wdt_notify_sys,
597};
598
599#endif /*CONFIG_RTC_DRV_DS1374_WDT*/
600/*
601 *****************************************************************************
602 *
603 * Driver Interface
604 *
605 *****************************************************************************
606 */
343static int ds1374_probe(struct i2c_client *client, 607static int ds1374_probe(struct i2c_client *client,
344 const struct i2c_device_id *id) 608 const struct i2c_device_id *id)
345{ 609{
@@ -378,12 +642,33 @@ static int ds1374_probe(struct i2c_client *client,
378 return PTR_ERR(ds1374->rtc); 642 return PTR_ERR(ds1374->rtc);
379 } 643 }
380 644
645#ifdef CONFIG_RTC_DRV_DS1374_WDT
646 save_client = client;
647 ret = misc_register(&ds1374_miscdev);
648 if (ret)
649 return ret;
650 ret = register_reboot_notifier(&ds1374_wdt_notifier);
651 if (ret) {
652 misc_deregister(&ds1374_miscdev);
653 return ret;
654 }
655 ds1374_wdt_settimeout(131072);
656#endif
657
381 return 0; 658 return 0;
382} 659}
383 660
384static int ds1374_remove(struct i2c_client *client) 661static int ds1374_remove(struct i2c_client *client)
385{ 662{
386 struct ds1374 *ds1374 = i2c_get_clientdata(client); 663 struct ds1374 *ds1374 = i2c_get_clientdata(client);
664#ifdef CONFIG_RTC_DRV_DS1374_WDT
665 int res;
666
667 res = misc_deregister(&ds1374_miscdev);
668 if (!res)
669 ds1374_miscdev.parent = NULL;
670 unregister_reboot_notifier(&ds1374_wdt_notifier);
671#endif
387 672
388 if (client->irq > 0) { 673 if (client->irq > 0) {
389 mutex_lock(&ds1374->mutex); 674 mutex_lock(&ds1374->mutex);
diff --git a/drivers/rtc/rtc-isl12057.c b/drivers/rtc/rtc-isl12057.c
index 455b601d731d..6e1fcfb5d7e6 100644
--- a/drivers/rtc/rtc-isl12057.c
+++ b/drivers/rtc/rtc-isl12057.c
@@ -41,6 +41,7 @@
41#define ISL12057_REG_RTC_DW 0x03 /* Day of the Week */ 41#define ISL12057_REG_RTC_DW 0x03 /* Day of the Week */
42#define ISL12057_REG_RTC_DT 0x04 /* Date */ 42#define ISL12057_REG_RTC_DT 0x04 /* Date */
43#define ISL12057_REG_RTC_MO 0x05 /* Month */ 43#define ISL12057_REG_RTC_MO 0x05 /* Month */
44#define ISL12057_REG_RTC_MO_CEN BIT(7) /* Century bit */
44#define ISL12057_REG_RTC_YR 0x06 /* Year */ 45#define ISL12057_REG_RTC_YR 0x06 /* Year */
45#define ISL12057_RTC_SEC_LEN 7 46#define ISL12057_RTC_SEC_LEN 7
46 47
@@ -88,7 +89,7 @@ static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs)
88 tm->tm_min = bcd2bin(regs[ISL12057_REG_RTC_MN]); 89 tm->tm_min = bcd2bin(regs[ISL12057_REG_RTC_MN]);
89 90
90 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_MIL) { /* AM/PM */ 91 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_MIL) { /* AM/PM */
91 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x0f); 92 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x1f);
92 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_PM) 93 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_PM)
93 tm->tm_hour += 12; 94 tm->tm_hour += 12;
94 } else { /* 24 hour mode */ 95 } else { /* 24 hour mode */
@@ -97,26 +98,37 @@ static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs)
97 98
98 tm->tm_mday = bcd2bin(regs[ISL12057_REG_RTC_DT]); 99 tm->tm_mday = bcd2bin(regs[ISL12057_REG_RTC_DT]);
99 tm->tm_wday = bcd2bin(regs[ISL12057_REG_RTC_DW]) - 1; /* starts at 1 */ 100 tm->tm_wday = bcd2bin(regs[ISL12057_REG_RTC_DW]) - 1; /* starts at 1 */
100 tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO]) - 1; /* starts at 1 */ 101 tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO] & 0x1f) - 1; /* ditto */
101 tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100; 102 tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100;
103
104 /* Check if years register has overflown from 99 to 00 */
105 if (regs[ISL12057_REG_RTC_MO] & ISL12057_REG_RTC_MO_CEN)
106 tm->tm_year += 100;
102} 107}
103 108
104static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm) 109static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm)
105{ 110{
111 u8 century_bit;
112
106 /* 113 /*
107 * The clock has an 8 bit wide bcd-coded register for the year. 114 * The clock has an 8 bit wide bcd-coded register for the year.
115 * It also has a century bit encoded in MO flag which provides
116 * information about overflow of year register from 99 to 00.
108 * tm_year is an offset from 1900 and we are interested in the 117 * tm_year is an offset from 1900 and we are interested in the
109 * 2000-2099 range, so any value less than 100 is invalid. 118 * 2000-2199 range, so any value less than 100 or larger than
119 * 299 is invalid.
110 */ 120 */
111 if (tm->tm_year < 100) 121 if (tm->tm_year < 100 || tm->tm_year > 299)
112 return -EINVAL; 122 return -EINVAL;
113 123
124 century_bit = (tm->tm_year > 199) ? ISL12057_REG_RTC_MO_CEN : 0;
125
114 regs[ISL12057_REG_RTC_SC] = bin2bcd(tm->tm_sec); 126 regs[ISL12057_REG_RTC_SC] = bin2bcd(tm->tm_sec);
115 regs[ISL12057_REG_RTC_MN] = bin2bcd(tm->tm_min); 127 regs[ISL12057_REG_RTC_MN] = bin2bcd(tm->tm_min);
116 regs[ISL12057_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */ 128 regs[ISL12057_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */
117 regs[ISL12057_REG_RTC_DT] = bin2bcd(tm->tm_mday); 129 regs[ISL12057_REG_RTC_DT] = bin2bcd(tm->tm_mday);
118 regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1); 130 regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1) | century_bit;
119 regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year - 100); 131 regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year % 100);
120 regs[ISL12057_REG_RTC_DW] = bin2bcd(tm->tm_wday + 1); 132 regs[ISL12057_REG_RTC_DW] = bin2bcd(tm->tm_wday + 1);
121 133
122 return 0; 134 return 0;
@@ -152,17 +164,33 @@ static int isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
152{ 164{
153 struct isl12057_rtc_data *data = dev_get_drvdata(dev); 165 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
154 u8 regs[ISL12057_RTC_SEC_LEN]; 166 u8 regs[ISL12057_RTC_SEC_LEN];
167 unsigned int sr;
155 int ret; 168 int ret;
156 169
157 mutex_lock(&data->lock); 170 mutex_lock(&data->lock);
171 ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr);
172 if (ret) {
173 dev_err(dev, "%s: unable to read oscillator status flag (%d)\n",
174 __func__, ret);
175 goto out;
176 } else {
177 if (sr & ISL12057_REG_SR_OSF) {
178 ret = -ENODATA;
179 goto out;
180 }
181 }
182
158 ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs, 183 ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs,
159 ISL12057_RTC_SEC_LEN); 184 ISL12057_RTC_SEC_LEN);
185 if (ret)
186 dev_err(dev, "%s: unable to read RTC time section (%d)\n",
187 __func__, ret);
188
189out:
160 mutex_unlock(&data->lock); 190 mutex_unlock(&data->lock);
161 191
162 if (ret) { 192 if (ret)
163 dev_err(dev, "%s: RTC read failed\n", __func__);
164 return ret; 193 return ret;
165 }
166 194
167 isl12057_rtc_regs_to_tm(tm, regs); 195 isl12057_rtc_regs_to_tm(tm, regs);
168 196
@@ -182,10 +210,24 @@ static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm)
182 mutex_lock(&data->lock); 210 mutex_lock(&data->lock);
183 ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs, 211 ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs,
184 ISL12057_RTC_SEC_LEN); 212 ISL12057_RTC_SEC_LEN);
185 mutex_unlock(&data->lock); 213 if (ret) {
214 dev_err(dev, "%s: unable to write RTC time section (%d)\n",
215 __func__, ret);
216 goto out;
217 }
186 218
187 if (ret) 219 /*
188 dev_err(dev, "%s: RTC write failed\n", __func__); 220 * Now that RTC time has been updated, let's clear oscillator
221 * failure flag, if needed.
222 */
223 ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
224 ISL12057_REG_SR_OSF, 0);
225 if (ret < 0)
226 dev_err(dev, "%s: unable to clear osc. failure bit (%d)\n",
227 __func__, ret);
228
229out:
230 mutex_unlock(&data->lock);
189 231
190 return ret; 232 return ret;
191} 233}
@@ -203,15 +245,8 @@ static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
203 ret = regmap_update_bits(regmap, ISL12057_REG_INT, 245 ret = regmap_update_bits(regmap, ISL12057_REG_INT,
204 ISL12057_REG_INT_EOSC, 0); 246 ISL12057_REG_INT_EOSC, 0);
205 if (ret < 0) { 247 if (ret < 0) {
206 dev_err(dev, "Unable to enable oscillator\n"); 248 dev_err(dev, "%s: unable to enable oscillator (%d)\n",
207 return ret; 249 __func__, ret);
208 }
209
210 /* Clear oscillator failure bit if needed */
211 ret = regmap_update_bits(regmap, ISL12057_REG_SR,
212 ISL12057_REG_SR_OSF, 0);
213 if (ret < 0) {
214 dev_err(dev, "Unable to clear oscillator failure bit\n");
215 return ret; 250 return ret;
216 } 251 }
217 252
@@ -219,7 +254,8 @@ static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
219 ret = regmap_update_bits(regmap, ISL12057_REG_SR, 254 ret = regmap_update_bits(regmap, ISL12057_REG_SR,
220 ISL12057_REG_SR_A1F, 0); 255 ISL12057_REG_SR_A1F, 0);
221 if (ret < 0) { 256 if (ret < 0) {
222 dev_err(dev, "Unable to clear alarm bit\n"); 257 dev_err(dev, "%s: unable to clear alarm bit (%d)\n",
258 __func__, ret);
223 return ret; 259 return ret;
224 } 260 }
225 261
@@ -253,7 +289,8 @@ static int isl12057_probe(struct i2c_client *client,
253 regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config); 289 regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config);
254 if (IS_ERR(regmap)) { 290 if (IS_ERR(regmap)) {
255 ret = PTR_ERR(regmap); 291 ret = PTR_ERR(regmap);
256 dev_err(dev, "regmap allocation failed: %d\n", ret); 292 dev_err(dev, "%s: regmap allocation failed (%d)\n",
293 __func__, ret);
257 return ret; 294 return ret;
258 } 295 }
259 296
diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index c4cf05731118..e6bfb9c42a10 100644
--- a/drivers/rtc/rtc-lib.c
+++ b/drivers/rtc/rtc-lib.c
@@ -45,16 +45,20 @@ int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
45} 45}
46EXPORT_SYMBOL(rtc_year_days); 46EXPORT_SYMBOL(rtc_year_days);
47 47
48
48/* 49/*
50 * rtc_time_to_tm64 - Converts time64_t to rtc_time.
49 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date. 51 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
50 */ 52 */
51void rtc_time_to_tm(unsigned long time, struct rtc_time *tm) 53void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
52{ 54{
53 unsigned int month, year; 55 unsigned int month, year;
56 unsigned long secs;
54 int days; 57 int days;
55 58
56 days = time / 86400; 59 /* time must be positive */
57 time -= (unsigned int) days * 86400; 60 days = div_s64(time, 86400);
61 secs = time - (unsigned int) days * 86400;
58 62
59 /* day of the week, 1970-01-01 was a Thursday */ 63 /* day of the week, 1970-01-01 was a Thursday */
60 tm->tm_wday = (days + 4) % 7; 64 tm->tm_wday = (days + 4) % 7;
@@ -81,14 +85,14 @@ void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
81 tm->tm_mon = month; 85 tm->tm_mon = month;
82 tm->tm_mday = days + 1; 86 tm->tm_mday = days + 1;
83 87
84 tm->tm_hour = time / 3600; 88 tm->tm_hour = secs / 3600;
85 time -= tm->tm_hour * 3600; 89 secs -= tm->tm_hour * 3600;
86 tm->tm_min = time / 60; 90 tm->tm_min = secs / 60;
87 tm->tm_sec = time - tm->tm_min * 60; 91 tm->tm_sec = secs - tm->tm_min * 60;
88 92
89 tm->tm_isdst = 0; 93 tm->tm_isdst = 0;
90} 94}
91EXPORT_SYMBOL(rtc_time_to_tm); 95EXPORT_SYMBOL(rtc_time64_to_tm);
92 96
93/* 97/*
94 * Does the rtc_time represent a valid date/time? 98 * Does the rtc_time represent a valid date/time?
@@ -109,24 +113,22 @@ int rtc_valid_tm(struct rtc_time *tm)
109EXPORT_SYMBOL(rtc_valid_tm); 113EXPORT_SYMBOL(rtc_valid_tm);
110 114
111/* 115/*
116 * rtc_tm_to_time64 - Converts rtc_time to time64_t.
112 * Convert Gregorian date to seconds since 01-01-1970 00:00:00. 117 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
113 */ 118 */
114int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time) 119time64_t rtc_tm_to_time64(struct rtc_time *tm)
115{ 120{
116 *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 121 return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
117 tm->tm_hour, tm->tm_min, tm->tm_sec); 122 tm->tm_hour, tm->tm_min, tm->tm_sec);
118 return 0;
119} 123}
120EXPORT_SYMBOL(rtc_tm_to_time); 124EXPORT_SYMBOL(rtc_tm_to_time64);
121 125
122/* 126/*
123 * Convert rtc_time to ktime 127 * Convert rtc_time to ktime
124 */ 128 */
125ktime_t rtc_tm_to_ktime(struct rtc_time tm) 129ktime_t rtc_tm_to_ktime(struct rtc_time tm)
126{ 130{
127 time_t time; 131 return ktime_set(rtc_tm_to_time64(&tm), 0);
128 rtc_tm_to_time(&tm, &time);
129 return ktime_set(time, 0);
130} 132}
131EXPORT_SYMBOL_GPL(rtc_tm_to_ktime); 133EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
132 134
@@ -135,14 +137,14 @@ EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
135 */ 137 */
136struct rtc_time rtc_ktime_to_tm(ktime_t kt) 138struct rtc_time rtc_ktime_to_tm(ktime_t kt)
137{ 139{
138 struct timespec ts; 140 struct timespec64 ts;
139 struct rtc_time ret; 141 struct rtc_time ret;
140 142
141 ts = ktime_to_timespec(kt); 143 ts = ktime_to_timespec64(kt);
142 /* Round up any ns */ 144 /* Round up any ns */
143 if (ts.tv_nsec) 145 if (ts.tv_nsec)
144 ts.tv_sec++; 146 ts.tv_sec++;
145 rtc_time_to_tm(ts.tv_sec, &ret); 147 rtc_time64_to_tm(ts.tv_sec, &ret);
146 return ret; 148 return ret;
147} 149}
148EXPORT_SYMBOL_GPL(rtc_ktime_to_tm); 150EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
index ae444a8bd602..8e5851aa4369 100644
--- a/drivers/rtc/rtc-omap.c
+++ b/drivers/rtc/rtc-omap.c
@@ -1,10 +1,11 @@
1/* 1/*
2 * TI OMAP1 Real Time Clock interface for Linux 2 * TI OMAP Real Time Clock interface for Linux
3 * 3 *
4 * Copyright (C) 2003 MontaVista Software, Inc. 4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com> 5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6 * 6 *
7 * Copyright (C) 2006 David Brownell (new RTC framework) 7 * Copyright (C) 2006 David Brownell (new RTC framework)
8 * Copyright (C) 2014 Johan Hovold <johan@kernel.org>
8 * 9 *
9 * This program is free software; you can redistribute it and/or 10 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License 11 * modify it under the terms of the GNU General Public License
@@ -25,7 +26,8 @@
25#include <linux/pm_runtime.h> 26#include <linux/pm_runtime.h>
26#include <linux/io.h> 27#include <linux/io.h>
27 28
28/* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock 29/*
30 * The OMAP RTC is a year/month/day/hours/minutes/seconds BCD clock
29 * with century-range alarm matching, driven by the 32kHz clock. 31 * with century-range alarm matching, driven by the 32kHz clock.
30 * 32 *
31 * The main user-visible ways it differs from PC RTCs are by omitting 33 * The main user-visible ways it differs from PC RTCs are by omitting
@@ -39,10 +41,6 @@
39 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment. 41 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
40 */ 42 */
41 43
42#define DRIVER_NAME "omap_rtc"
43
44#define OMAP_RTC_BASE 0xfffb4800
45
46/* RTC registers */ 44/* RTC registers */
47#define OMAP_RTC_SECONDS_REG 0x00 45#define OMAP_RTC_SECONDS_REG 0x00
48#define OMAP_RTC_MINUTES_REG 0x04 46#define OMAP_RTC_MINUTES_REG 0x04
@@ -72,6 +70,15 @@
72 70
73#define OMAP_RTC_IRQWAKEEN 0x7c 71#define OMAP_RTC_IRQWAKEEN 0x7c
74 72
73#define OMAP_RTC_ALARM2_SECONDS_REG 0x80
74#define OMAP_RTC_ALARM2_MINUTES_REG 0x84
75#define OMAP_RTC_ALARM2_HOURS_REG 0x88
76#define OMAP_RTC_ALARM2_DAYS_REG 0x8c
77#define OMAP_RTC_ALARM2_MONTHS_REG 0x90
78#define OMAP_RTC_ALARM2_YEARS_REG 0x94
79
80#define OMAP_RTC_PMIC_REG 0x98
81
75/* OMAP_RTC_CTRL_REG bit fields: */ 82/* OMAP_RTC_CTRL_REG bit fields: */
76#define OMAP_RTC_CTRL_SPLIT BIT(7) 83#define OMAP_RTC_CTRL_SPLIT BIT(7)
77#define OMAP_RTC_CTRL_DISABLE BIT(6) 84#define OMAP_RTC_CTRL_DISABLE BIT(6)
@@ -84,6 +91,7 @@
84 91
85/* OMAP_RTC_STATUS_REG bit fields: */ 92/* OMAP_RTC_STATUS_REG bit fields: */
86#define OMAP_RTC_STATUS_POWER_UP BIT(7) 93#define OMAP_RTC_STATUS_POWER_UP BIT(7)
94#define OMAP_RTC_STATUS_ALARM2 BIT(7)
87#define OMAP_RTC_STATUS_ALARM BIT(6) 95#define OMAP_RTC_STATUS_ALARM BIT(6)
88#define OMAP_RTC_STATUS_1D_EVENT BIT(5) 96#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
89#define OMAP_RTC_STATUS_1H_EVENT BIT(4) 97#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
@@ -93,6 +101,7 @@
93#define OMAP_RTC_STATUS_BUSY BIT(0) 101#define OMAP_RTC_STATUS_BUSY BIT(0)
94 102
95/* OMAP_RTC_INTERRUPTS_REG bit fields: */ 103/* OMAP_RTC_INTERRUPTS_REG bit fields: */
104#define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
96#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3) 105#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
97#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2) 106#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
98 107
@@ -102,61 +111,82 @@
102/* OMAP_RTC_IRQWAKEEN bit fields: */ 111/* OMAP_RTC_IRQWAKEEN bit fields: */
103#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1) 112#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
104 113
114/* OMAP_RTC_PMIC bit fields: */
115#define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
116
105/* OMAP_RTC_KICKER values */ 117/* OMAP_RTC_KICKER values */
106#define KICK0_VALUE 0x83e70b13 118#define KICK0_VALUE 0x83e70b13
107#define KICK1_VALUE 0x95a4f1e0 119#define KICK1_VALUE 0x95a4f1e0
108 120
109#define OMAP_RTC_HAS_KICKER BIT(0) 121struct omap_rtc_device_type {
110 122 bool has_32kclk_en;
111/* 123 bool has_kicker;
112 * Few RTC IP revisions has special WAKE-EN Register to enable Wakeup 124 bool has_irqwakeen;
113 * generation for event Alarm. 125 bool has_pmic_mode;
114 */ 126 bool has_power_up_reset;
115#define OMAP_RTC_HAS_IRQWAKEEN BIT(1) 127};
116 128
117/* 129struct omap_rtc {
118 * Some RTC IP revisions (like those in AM335x and DRA7x) need 130 struct rtc_device *rtc;
119 * the 32KHz clock to be explicitly enabled. 131 void __iomem *base;
120 */ 132 int irq_alarm;
121#define OMAP_RTC_HAS_32KCLK_EN BIT(2) 133 int irq_timer;
134 u8 interrupts_reg;
135 bool is_pmic_controller;
136 const struct omap_rtc_device_type *type;
137};
122 138
123static void __iomem *rtc_base; 139static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
140{
141 return readb(rtc->base + reg);
142}
124 143
125#define rtc_read(addr) readb(rtc_base + (addr)) 144static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
126#define rtc_write(val, addr) writeb(val, rtc_base + (addr)) 145{
146 return readl(rtc->base + reg);
147}
127 148
128#define rtc_writel(val, addr) writel(val, rtc_base + (addr)) 149static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
150{
151 writeb(val, rtc->base + reg);
152}
129 153
154static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
155{
156 writel(val, rtc->base + reg);
157}
130 158
131/* we rely on the rtc framework to handle locking (rtc->ops_lock), 159/*
160 * We rely on the rtc framework to handle locking (rtc->ops_lock),
132 * so the only other requirement is that register accesses which 161 * so the only other requirement is that register accesses which
133 * require BUSY to be clear are made with IRQs locally disabled 162 * require BUSY to be clear are made with IRQs locally disabled
134 */ 163 */
135static void rtc_wait_not_busy(void) 164static void rtc_wait_not_busy(struct omap_rtc *rtc)
136{ 165{
137 int count = 0; 166 int count;
138 u8 status; 167 u8 status;
139 168
140 /* BUSY may stay active for 1/32768 second (~30 usec) */ 169 /* BUSY may stay active for 1/32768 second (~30 usec) */
141 for (count = 0; count < 50; count++) { 170 for (count = 0; count < 50; count++) {
142 status = rtc_read(OMAP_RTC_STATUS_REG); 171 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
143 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0) 172 if (!(status & OMAP_RTC_STATUS_BUSY))
144 break; 173 break;
145 udelay(1); 174 udelay(1);
146 } 175 }
147 /* now we have ~15 usec to read/write various registers */ 176 /* now we have ~15 usec to read/write various registers */
148} 177}
149 178
150static irqreturn_t rtc_irq(int irq, void *rtc) 179static irqreturn_t rtc_irq(int irq, void *dev_id)
151{ 180{
152 unsigned long events = 0; 181 struct omap_rtc *rtc = dev_id;
153 u8 irq_data; 182 unsigned long events = 0;
183 u8 irq_data;
154 184
155 irq_data = rtc_read(OMAP_RTC_STATUS_REG); 185 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
156 186
157 /* alarm irq? */ 187 /* alarm irq? */
158 if (irq_data & OMAP_RTC_STATUS_ALARM) { 188 if (irq_data & OMAP_RTC_STATUS_ALARM) {
159 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); 189 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
160 events |= RTC_IRQF | RTC_AF; 190 events |= RTC_IRQF | RTC_AF;
161 } 191 }
162 192
@@ -164,23 +194,21 @@ static irqreturn_t rtc_irq(int irq, void *rtc)
164 if (irq_data & OMAP_RTC_STATUS_1S_EVENT) 194 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
165 events |= RTC_IRQF | RTC_UF; 195 events |= RTC_IRQF | RTC_UF;
166 196
167 rtc_update_irq(rtc, 1, events); 197 rtc_update_irq(rtc->rtc, 1, events);
168 198
169 return IRQ_HANDLED; 199 return IRQ_HANDLED;
170} 200}
171 201
172static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 202static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
173{ 203{
204 struct omap_rtc *rtc = dev_get_drvdata(dev);
174 u8 reg, irqwake_reg = 0; 205 u8 reg, irqwake_reg = 0;
175 struct platform_device *pdev = to_platform_device(dev);
176 const struct platform_device_id *id_entry =
177 platform_get_device_id(pdev);
178 206
179 local_irq_disable(); 207 local_irq_disable();
180 rtc_wait_not_busy(); 208 rtc_wait_not_busy(rtc);
181 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); 209 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
182 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) 210 if (rtc->type->has_irqwakeen)
183 irqwake_reg = rtc_read(OMAP_RTC_IRQWAKEEN); 211 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
184 212
185 if (enabled) { 213 if (enabled) {
186 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; 214 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
@@ -189,10 +217,10 @@ static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
189 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; 217 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
190 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN; 218 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
191 } 219 }
192 rtc_wait_not_busy(); 220 rtc_wait_not_busy(rtc);
193 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); 221 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
194 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) 222 if (rtc->type->has_irqwakeen)
195 rtc_write(irqwake_reg, OMAP_RTC_IRQWAKEEN); 223 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
196 local_irq_enable(); 224 local_irq_enable();
197 225
198 return 0; 226 return 0;
@@ -230,39 +258,47 @@ static void bcd2tm(struct rtc_time *tm)
230 tm->tm_year = bcd2bin(tm->tm_year) + 100; 258 tm->tm_year = bcd2bin(tm->tm_year) + 100;
231} 259}
232 260
261static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
262{
263 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
264 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
265 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
266 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
267 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
268 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
269}
233 270
234static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm) 271static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
235{ 272{
273 struct omap_rtc *rtc = dev_get_drvdata(dev);
274
236 /* we don't report wday/yday/isdst ... */ 275 /* we don't report wday/yday/isdst ... */
237 local_irq_disable(); 276 local_irq_disable();
238 rtc_wait_not_busy(); 277 rtc_wait_not_busy(rtc);
239 278 omap_rtc_read_time_raw(rtc, tm);
240 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
241 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
242 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
243 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
244 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
245 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
246
247 local_irq_enable(); 279 local_irq_enable();
248 280
249 bcd2tm(tm); 281 bcd2tm(tm);
282
250 return 0; 283 return 0;
251} 284}
252 285
253static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm) 286static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
254{ 287{
288 struct omap_rtc *rtc = dev_get_drvdata(dev);
289
255 if (tm2bcd(tm) < 0) 290 if (tm2bcd(tm) < 0)
256 return -EINVAL; 291 return -EINVAL;
292
257 local_irq_disable(); 293 local_irq_disable();
258 rtc_wait_not_busy(); 294 rtc_wait_not_busy(rtc);
259 295
260 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG); 296 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
261 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG); 297 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
262 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG); 298 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
263 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG); 299 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
264 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG); 300 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
265 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG); 301 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
266 302
267 local_irq_enable(); 303 local_irq_enable();
268 304
@@ -271,48 +307,50 @@ static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
271 307
272static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) 308static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
273{ 309{
310 struct omap_rtc *rtc = dev_get_drvdata(dev);
311 u8 interrupts;
312
274 local_irq_disable(); 313 local_irq_disable();
275 rtc_wait_not_busy(); 314 rtc_wait_not_busy(rtc);
276 315
277 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG); 316 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
278 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG); 317 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
279 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG); 318 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
280 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG); 319 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
281 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG); 320 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
282 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG); 321 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
283 322
284 local_irq_enable(); 323 local_irq_enable();
285 324
286 bcd2tm(&alm->time); 325 bcd2tm(&alm->time);
287 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG) 326
288 & OMAP_RTC_INTERRUPTS_IT_ALARM); 327 interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
328 alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
289 329
290 return 0; 330 return 0;
291} 331}
292 332
293static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) 333static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
294{ 334{
335 struct omap_rtc *rtc = dev_get_drvdata(dev);
295 u8 reg, irqwake_reg = 0; 336 u8 reg, irqwake_reg = 0;
296 struct platform_device *pdev = to_platform_device(dev);
297 const struct platform_device_id *id_entry =
298 platform_get_device_id(pdev);
299 337
300 if (tm2bcd(&alm->time) < 0) 338 if (tm2bcd(&alm->time) < 0)
301 return -EINVAL; 339 return -EINVAL;
302 340
303 local_irq_disable(); 341 local_irq_disable();
304 rtc_wait_not_busy(); 342 rtc_wait_not_busy(rtc);
305 343
306 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG); 344 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
307 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG); 345 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
308 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG); 346 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
309 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG); 347 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
310 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG); 348 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
311 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG); 349 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
312 350
313 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); 351 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
314 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) 352 if (rtc->type->has_irqwakeen)
315 irqwake_reg = rtc_read(OMAP_RTC_IRQWAKEEN); 353 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
316 354
317 if (alm->enabled) { 355 if (alm->enabled) {
318 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; 356 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
@@ -321,15 +359,79 @@ static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
321 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; 359 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
322 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN; 360 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
323 } 361 }
324 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); 362 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
325 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) 363 if (rtc->type->has_irqwakeen)
326 rtc_write(irqwake_reg, OMAP_RTC_IRQWAKEEN); 364 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
327 365
328 local_irq_enable(); 366 local_irq_enable();
329 367
330 return 0; 368 return 0;
331} 369}
332 370
371static struct omap_rtc *omap_rtc_power_off_rtc;
372
373/*
374 * omap_rtc_poweroff: RTC-controlled power off
375 *
376 * The RTC can be used to control an external PMIC via the pmic_power_en pin,
377 * which can be configured to transition to OFF on ALARM2 events.
378 *
379 * Notes:
380 * The two-second alarm offset is the shortest offset possible as the alarm
381 * registers must be set before the next timer update and the offset
382 * calculation is too heavy for everything to be done within a single access
383 * period (~15 us).
384 *
385 * Called with local interrupts disabled.
386 */
387static void omap_rtc_power_off(void)
388{
389 struct omap_rtc *rtc = omap_rtc_power_off_rtc;
390 struct rtc_time tm;
391 unsigned long now;
392 u32 val;
393
394 /* enable pmic_power_en control */
395 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
396 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
397
398 /* set alarm two seconds from now */
399 omap_rtc_read_time_raw(rtc, &tm);
400 bcd2tm(&tm);
401 rtc_tm_to_time(&tm, &now);
402 rtc_time_to_tm(now + 2, &tm);
403
404 if (tm2bcd(&tm) < 0) {
405 dev_err(&rtc->rtc->dev, "power off failed\n");
406 return;
407 }
408
409 rtc_wait_not_busy(rtc);
410
411 rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
412 rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
413 rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
414 rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
415 rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
416 rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
417
418 /*
419 * enable ALARM2 interrupt
420 *
421 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
422 */
423 val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
424 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
425 val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
426
427 /*
428 * Wait for alarm to trigger (within two seconds) and external PMIC to
429 * power off the system. Add a 500 ms margin for external latencies
430 * (e.g. debounce circuits).
431 */
432 mdelay(2500);
433}
434
333static struct rtc_class_ops omap_rtc_ops = { 435static struct rtc_class_ops omap_rtc_ops = {
334 .read_time = omap_rtc_read_time, 436 .read_time = omap_rtc_read_time,
335 .set_time = omap_rtc_set_time, 437 .set_time = omap_rtc_set_time,
@@ -338,137 +440,140 @@ static struct rtc_class_ops omap_rtc_ops = {
338 .alarm_irq_enable = omap_rtc_alarm_irq_enable, 440 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
339}; 441};
340 442
341static int omap_rtc_alarm; 443static const struct omap_rtc_device_type omap_rtc_default_type = {
342static int omap_rtc_timer; 444 .has_power_up_reset = true,
445};
343 446
344#define OMAP_RTC_DATA_AM3352_IDX 1 447static const struct omap_rtc_device_type omap_rtc_am3352_type = {
345#define OMAP_RTC_DATA_DA830_IDX 2 448 .has_32kclk_en = true,
449 .has_kicker = true,
450 .has_irqwakeen = true,
451 .has_pmic_mode = true,
452};
346 453
347static struct platform_device_id omap_rtc_devtype[] = { 454static const struct omap_rtc_device_type omap_rtc_da830_type = {
455 .has_kicker = true,
456};
457
458static const struct platform_device_id omap_rtc_id_table[] = {
348 { 459 {
349 .name = DRIVER_NAME, 460 .name = "omap_rtc",
350 }, 461 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
351 [OMAP_RTC_DATA_AM3352_IDX] = { 462 }, {
352 .name = "am3352-rtc", 463 .name = "am3352-rtc",
353 .driver_data = OMAP_RTC_HAS_KICKER | OMAP_RTC_HAS_IRQWAKEEN | 464 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
354 OMAP_RTC_HAS_32KCLK_EN, 465 }, {
355 },
356 [OMAP_RTC_DATA_DA830_IDX] = {
357 .name = "da830-rtc", 466 .name = "da830-rtc",
358 .driver_data = OMAP_RTC_HAS_KICKER, 467 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
359 }, 468 }, {
360 {}, 469 /* sentinel */
470 }
361}; 471};
362MODULE_DEVICE_TABLE(platform, omap_rtc_devtype); 472MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
363 473
364static const struct of_device_id omap_rtc_of_match[] = { 474static const struct of_device_id omap_rtc_of_match[] = {
365 { .compatible = "ti,da830-rtc", 475 {
366 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX], 476 .compatible = "ti,am3352-rtc",
367 }, 477 .data = &omap_rtc_am3352_type,
368 { .compatible = "ti,am3352-rtc", 478 }, {
369 .data = &omap_rtc_devtype[OMAP_RTC_DATA_AM3352_IDX], 479 .compatible = "ti,da830-rtc",
370 }, 480 .data = &omap_rtc_da830_type,
371 {}, 481 }, {
482 /* sentinel */
483 }
372}; 484};
373MODULE_DEVICE_TABLE(of, omap_rtc_of_match); 485MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
374 486
375static int __init omap_rtc_probe(struct platform_device *pdev) 487static int __init omap_rtc_probe(struct platform_device *pdev)
376{ 488{
377 struct resource *res; 489 struct omap_rtc *rtc;
378 struct rtc_device *rtc; 490 struct resource *res;
379 u8 reg, new_ctrl; 491 u8 reg, mask, new_ctrl;
380 const struct platform_device_id *id_entry; 492 const struct platform_device_id *id_entry;
381 const struct of_device_id *of_id; 493 const struct of_device_id *of_id;
494 int ret;
382 495
383 of_id = of_match_device(omap_rtc_of_match, &pdev->dev); 496 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
384 if (of_id) 497 if (!rtc)
385 pdev->id_entry = of_id->data; 498 return -ENOMEM;
386 499
387 id_entry = platform_get_device_id(pdev); 500 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
388 if (!id_entry) { 501 if (of_id) {
389 dev_err(&pdev->dev, "no matching device entry\n"); 502 rtc->type = of_id->data;
390 return -ENODEV; 503 rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
504 of_property_read_bool(pdev->dev.of_node,
505 "system-power-controller");
506 } else {
507 id_entry = platform_get_device_id(pdev);
508 rtc->type = (void *)id_entry->driver_data;
391 } 509 }
392 510
393 omap_rtc_timer = platform_get_irq(pdev, 0); 511 rtc->irq_timer = platform_get_irq(pdev, 0);
394 if (omap_rtc_timer <= 0) { 512 if (rtc->irq_timer <= 0)
395 pr_debug("%s: no update irq?\n", pdev->name);
396 return -ENOENT; 513 return -ENOENT;
397 }
398 514
399 omap_rtc_alarm = platform_get_irq(pdev, 1); 515 rtc->irq_alarm = platform_get_irq(pdev, 1);
400 if (omap_rtc_alarm <= 0) { 516 if (rtc->irq_alarm <= 0)
401 pr_debug("%s: no alarm irq?\n", pdev->name);
402 return -ENOENT; 517 return -ENOENT;
403 }
404 518
405 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 519 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
406 rtc_base = devm_ioremap_resource(&pdev->dev, res); 520 rtc->base = devm_ioremap_resource(&pdev->dev, res);
407 if (IS_ERR(rtc_base)) 521 if (IS_ERR(rtc->base))
408 return PTR_ERR(rtc_base); 522 return PTR_ERR(rtc->base);
523
524 platform_set_drvdata(pdev, rtc);
409 525
410 /* Enable the clock/module so that we can access the registers */ 526 /* Enable the clock/module so that we can access the registers */
411 pm_runtime_enable(&pdev->dev); 527 pm_runtime_enable(&pdev->dev);
412 pm_runtime_get_sync(&pdev->dev); 528 pm_runtime_get_sync(&pdev->dev);
413 529
414 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER) { 530 if (rtc->type->has_kicker) {
415 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG); 531 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
416 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG); 532 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
417 }
418
419 rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
420 &omap_rtc_ops, THIS_MODULE);
421 if (IS_ERR(rtc)) {
422 pr_debug("%s: can't register RTC device, err %ld\n",
423 pdev->name, PTR_ERR(rtc));
424 goto fail0;
425 } 533 }
426 platform_set_drvdata(pdev, rtc);
427 534
428 /* clear pending irqs, and set 1/second periodic, 535 /*
429 * which we'll use instead of update irqs 536 * disable interrupts
537 *
538 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
430 */ 539 */
431 rtc_write(0, OMAP_RTC_INTERRUPTS_REG); 540 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
432 541
433 /* enable RTC functional clock */ 542 /* enable RTC functional clock */
434 if (id_entry->driver_data & OMAP_RTC_HAS_32KCLK_EN) 543 if (rtc->type->has_32kclk_en) {
435 rtc_writel(OMAP_RTC_OSC_32KCLK_EN, OMAP_RTC_OSC_REG); 544 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
545 rtc_writel(rtc, OMAP_RTC_OSC_REG,
546 reg | OMAP_RTC_OSC_32KCLK_EN);
547 }
436 548
437 /* clear old status */ 549 /* clear old status */
438 reg = rtc_read(OMAP_RTC_STATUS_REG); 550 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
439 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
440 pr_info("%s: RTC power up reset detected\n",
441 pdev->name);
442 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
443 }
444 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
445 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
446 551
447 /* handle periodic and alarm irqs */ 552 mask = OMAP_RTC_STATUS_ALARM;
448 if (devm_request_irq(&pdev->dev, omap_rtc_timer, rtc_irq, 0, 553
449 dev_name(&rtc->dev), rtc)) { 554 if (rtc->type->has_pmic_mode)
450 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n", 555 mask |= OMAP_RTC_STATUS_ALARM2;
451 pdev->name, omap_rtc_timer); 556
452 goto fail0; 557 if (rtc->type->has_power_up_reset) {
453 } 558 mask |= OMAP_RTC_STATUS_POWER_UP;
454 if ((omap_rtc_timer != omap_rtc_alarm) && 559 if (reg & OMAP_RTC_STATUS_POWER_UP)
455 (devm_request_irq(&pdev->dev, omap_rtc_alarm, rtc_irq, 0, 560 dev_info(&pdev->dev, "RTC power up reset detected\n");
456 dev_name(&rtc->dev), rtc))) {
457 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
458 pdev->name, omap_rtc_alarm);
459 goto fail0;
460 } 561 }
461 562
563 if (reg & mask)
564 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
565
462 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */ 566 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
463 reg = rtc_read(OMAP_RTC_CTRL_REG); 567 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
464 if (reg & (u8) OMAP_RTC_CTRL_STOP) 568 if (reg & OMAP_RTC_CTRL_STOP)
465 pr_info("%s: already running\n", pdev->name); 569 dev_info(&pdev->dev, "already running\n");
466 570
467 /* force to 24 hour mode */ 571 /* force to 24 hour mode */
468 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP); 572 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
469 new_ctrl |= OMAP_RTC_CTRL_STOP; 573 new_ctrl |= OMAP_RTC_CTRL_STOP;
470 574
471 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE: 575 /*
576 * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
472 * 577 *
473 * - Device wake-up capability setting should come through chip 578 * - Device wake-up capability setting should come through chip
474 * init logic. OMAP1 boards should initialize the "wakeup capable" 579 * init logic. OMAP1 boards should initialize the "wakeup capable"
@@ -482,36 +587,70 @@ static int __init omap_rtc_probe(struct platform_device *pdev)
482 * is write-only, and always reads as zero...) 587 * is write-only, and always reads as zero...)
483 */ 588 */
484 589
590 if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
591 dev_info(&pdev->dev, "split power mode\n");
592
593 if (reg != new_ctrl)
594 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
595
485 device_init_wakeup(&pdev->dev, true); 596 device_init_wakeup(&pdev->dev, true);
486 597
487 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT) 598 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
488 pr_info("%s: split power mode\n", pdev->name); 599 &omap_rtc_ops, THIS_MODULE);
600 if (IS_ERR(rtc->rtc)) {
601 ret = PTR_ERR(rtc->rtc);
602 goto err;
603 }
489 604
490 if (reg != new_ctrl) 605 /* handle periodic and alarm irqs */
491 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG); 606 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
607 dev_name(&rtc->rtc->dev), rtc);
608 if (ret)
609 goto err;
610
611 if (rtc->irq_timer != rtc->irq_alarm) {
612 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
613 dev_name(&rtc->rtc->dev), rtc);
614 if (ret)
615 goto err;
616 }
617
618 if (rtc->is_pmic_controller) {
619 if (!pm_power_off) {
620 omap_rtc_power_off_rtc = rtc;
621 pm_power_off = omap_rtc_power_off;
622 }
623 }
492 624
493 return 0; 625 return 0;
494 626
495fail0: 627err:
496 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER) 628 device_init_wakeup(&pdev->dev, false);
497 rtc_writel(0, OMAP_RTC_KICK0_REG); 629 if (rtc->type->has_kicker)
630 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
498 pm_runtime_put_sync(&pdev->dev); 631 pm_runtime_put_sync(&pdev->dev);
499 pm_runtime_disable(&pdev->dev); 632 pm_runtime_disable(&pdev->dev);
500 return -EIO; 633
634 return ret;
501} 635}
502 636
503static int __exit omap_rtc_remove(struct platform_device *pdev) 637static int __exit omap_rtc_remove(struct platform_device *pdev)
504{ 638{
505 const struct platform_device_id *id_entry = 639 struct omap_rtc *rtc = platform_get_drvdata(pdev);
506 platform_get_device_id(pdev); 640
641 if (pm_power_off == omap_rtc_power_off &&
642 omap_rtc_power_off_rtc == rtc) {
643 pm_power_off = NULL;
644 omap_rtc_power_off_rtc = NULL;
645 }
507 646
508 device_init_wakeup(&pdev->dev, 0); 647 device_init_wakeup(&pdev->dev, 0);
509 648
510 /* leave rtc running, but disable irqs */ 649 /* leave rtc running, but disable irqs */
511 rtc_write(0, OMAP_RTC_INTERRUPTS_REG); 650 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
512 651
513 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER) 652 if (rtc->type->has_kicker)
514 rtc_writel(0, OMAP_RTC_KICK0_REG); 653 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
515 654
516 /* Disable the clock/module */ 655 /* Disable the clock/module */
517 pm_runtime_put_sync(&pdev->dev); 656 pm_runtime_put_sync(&pdev->dev);
@@ -521,20 +660,21 @@ static int __exit omap_rtc_remove(struct platform_device *pdev)
521} 660}
522 661
523#ifdef CONFIG_PM_SLEEP 662#ifdef CONFIG_PM_SLEEP
524static u8 irqstat;
525
526static int omap_rtc_suspend(struct device *dev) 663static int omap_rtc_suspend(struct device *dev)
527{ 664{
528 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG); 665 struct omap_rtc *rtc = dev_get_drvdata(dev);
529 666
530 /* FIXME the RTC alarm is not currently acting as a wakeup event 667 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
668
669 /*
670 * FIXME: the RTC alarm is not currently acting as a wakeup event
531 * source on some platforms, and in fact this enable() call is just 671 * source on some platforms, and in fact this enable() call is just
532 * saving a flag that's never used... 672 * saving a flag that's never used...
533 */ 673 */
534 if (device_may_wakeup(dev)) 674 if (device_may_wakeup(dev))
535 enable_irq_wake(omap_rtc_alarm); 675 enable_irq_wake(rtc->irq_alarm);
536 else 676 else
537 rtc_write(0, OMAP_RTC_INTERRUPTS_REG); 677 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
538 678
539 /* Disable the clock/module */ 679 /* Disable the clock/module */
540 pm_runtime_put_sync(dev); 680 pm_runtime_put_sync(dev);
@@ -544,13 +684,15 @@ static int omap_rtc_suspend(struct device *dev)
544 684
545static int omap_rtc_resume(struct device *dev) 685static int omap_rtc_resume(struct device *dev)
546{ 686{
687 struct omap_rtc *rtc = dev_get_drvdata(dev);
688
547 /* Enable the clock/module so that we can access the registers */ 689 /* Enable the clock/module so that we can access the registers */
548 pm_runtime_get_sync(dev); 690 pm_runtime_get_sync(dev);
549 691
550 if (device_may_wakeup(dev)) 692 if (device_may_wakeup(dev))
551 disable_irq_wake(omap_rtc_alarm); 693 disable_irq_wake(rtc->irq_alarm);
552 else 694 else
553 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG); 695 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
554 696
555 return 0; 697 return 0;
556} 698}
@@ -560,22 +702,31 @@ static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
560 702
561static void omap_rtc_shutdown(struct platform_device *pdev) 703static void omap_rtc_shutdown(struct platform_device *pdev)
562{ 704{
563 rtc_write(0, OMAP_RTC_INTERRUPTS_REG); 705 struct omap_rtc *rtc = platform_get_drvdata(pdev);
706 u8 mask;
707
708 /*
709 * Keep the ALARM interrupt enabled to allow the system to power up on
710 * alarm events.
711 */
712 mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
713 mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
714 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
564} 715}
565 716
566MODULE_ALIAS("platform:omap_rtc");
567static struct platform_driver omap_rtc_driver = { 717static struct platform_driver omap_rtc_driver = {
568 .remove = __exit_p(omap_rtc_remove), 718 .remove = __exit_p(omap_rtc_remove),
569 .shutdown = omap_rtc_shutdown, 719 .shutdown = omap_rtc_shutdown,
570 .driver = { 720 .driver = {
571 .name = DRIVER_NAME, 721 .name = "omap_rtc",
572 .pm = &omap_rtc_pm_ops, 722 .pm = &omap_rtc_pm_ops,
573 .of_match_table = omap_rtc_of_match, 723 .of_match_table = omap_rtc_of_match,
574 }, 724 },
575 .id_table = omap_rtc_devtype, 725 .id_table = omap_rtc_id_table,
576}; 726};
577 727
578module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe); 728module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
579 729
730MODULE_ALIAS("platform:omap_rtc");
580MODULE_AUTHOR("George G. Davis (and others)"); 731MODULE_AUTHOR("George G. Davis (and others)");
581MODULE_LICENSE("GPL"); 732MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-opal.c b/drivers/rtc/rtc-opal.c
new file mode 100644
index 000000000000..95f652165fe9
--- /dev/null
+++ b/drivers/rtc/rtc-opal.c
@@ -0,0 +1,261 @@
1/*
2 * IBM OPAL RTC driver
3 * Copyright (C) 2014 IBM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 */
18
19#define DRVNAME "rtc-opal"
20#define pr_fmt(fmt) DRVNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/err.h>
24#include <linux/rtc.h>
25#include <linux/delay.h>
26#include <linux/bcd.h>
27#include <linux/platform_device.h>
28#include <linux/of.h>
29#include <asm/opal.h>
30#include <asm/firmware.h>
31
32static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
33{
34 tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
35 bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
36 tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;
37 tm->tm_mday = bcd2bin(y_m_d & 0xff);
38 tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
39 tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);
40 tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);
41
42 GregorianDay(tm);
43}
44
45static void tm_to_opal(struct rtc_time *tm, u32 *y_m_d, u64 *h_m_s_ms)
46{
47 *y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
48 *y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
49 *y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
50 *y_m_d |= ((u32)bin2bcd(tm->tm_mday));
51
52 *h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
53 *h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
54 *h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
55}
56
57static int opal_get_rtc_time(struct device *dev, struct rtc_time *tm)
58{
59 long rc = OPAL_BUSY;
60 u32 y_m_d;
61 u64 h_m_s_ms;
62 __be32 __y_m_d;
63 __be64 __h_m_s_ms;
64
65 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
66 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
67 if (rc == OPAL_BUSY_EVENT)
68 opal_poll_events(NULL);
69 else
70 msleep(10);
71 }
72
73 if (rc != OPAL_SUCCESS)
74 return -EIO;
75
76 y_m_d = be32_to_cpu(__y_m_d);
77 h_m_s_ms = be64_to_cpu(__h_m_s_ms);
78 opal_to_tm(y_m_d, h_m_s_ms, tm);
79
80 return 0;
81}
82
83static int opal_set_rtc_time(struct device *dev, struct rtc_time *tm)
84{
85 long rc = OPAL_BUSY;
86 u32 y_m_d = 0;
87 u64 h_m_s_ms = 0;
88
89 tm_to_opal(tm, &y_m_d, &h_m_s_ms);
90 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
91 rc = opal_rtc_write(y_m_d, h_m_s_ms);
92 if (rc == OPAL_BUSY_EVENT)
93 opal_poll_events(NULL);
94 else
95 msleep(10);
96 }
97
98 return rc == OPAL_SUCCESS ? 0 : -EIO;
99}
100
101/*
102 * TPO Timed Power-On
103 *
104 * TPO get/set OPAL calls care about the hour and min and to make it consistent
105 * with the rtc utility time conversion functions, we use the 'u64' to store
106 * its value and perform bit shift by 32 before use..
107 */
108static int opal_get_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
109{
110 __be32 __y_m_d, __h_m;
111 struct opal_msg msg;
112 int rc, token;
113 u64 h_m_s_ms;
114 u32 y_m_d;
115
116 token = opal_async_get_token_interruptible();
117 if (token < 0) {
118 if (token != -ERESTARTSYS)
119 pr_err("Failed to get the async token\n");
120
121 return token;
122 }
123
124 rc = opal_tpo_read(token, &__y_m_d, &__h_m);
125 if (rc != OPAL_ASYNC_COMPLETION) {
126 rc = -EIO;
127 goto exit;
128 }
129
130 rc = opal_async_wait_response(token, &msg);
131 if (rc) {
132 rc = -EIO;
133 goto exit;
134 }
135
136 rc = be64_to_cpu(msg.params[1]);
137 if (rc != OPAL_SUCCESS) {
138 rc = -EIO;
139 goto exit;
140 }
141
142 y_m_d = be32_to_cpu(__y_m_d);
143 h_m_s_ms = ((u64)be32_to_cpu(__h_m) << 32);
144 opal_to_tm(y_m_d, h_m_s_ms, &alarm->time);
145
146exit:
147 opal_async_release_token(token);
148 return rc;
149}
150
151/* Set Timed Power-On */
152static int opal_set_tpo_time(struct device *dev, struct rtc_wkalrm *alarm)
153{
154 u64 h_m_s_ms = 0, token;
155 struct opal_msg msg;
156 u32 y_m_d = 0;
157 int rc;
158
159 tm_to_opal(&alarm->time, &y_m_d, &h_m_s_ms);
160
161 token = opal_async_get_token_interruptible();
162 if (token < 0) {
163 if (token != -ERESTARTSYS)
164 pr_err("Failed to get the async token\n");
165
166 return token;
167 }
168
169 /* TPO, we care about hour and minute */
170 rc = opal_tpo_write(token, y_m_d,
171 (u32)((h_m_s_ms >> 32) & 0xffff0000));
172 if (rc != OPAL_ASYNC_COMPLETION) {
173 rc = -EIO;
174 goto exit;
175 }
176
177 rc = opal_async_wait_response(token, &msg);
178 if (rc) {
179 rc = -EIO;
180 goto exit;
181 }
182
183 rc = be64_to_cpu(msg.params[1]);
184 if (rc != OPAL_SUCCESS)
185 rc = -EIO;
186
187exit:
188 opal_async_release_token(token);
189 return rc;
190}
191
192static const struct rtc_class_ops opal_rtc_ops = {
193 .read_time = opal_get_rtc_time,
194 .set_time = opal_set_rtc_time,
195 .read_alarm = opal_get_tpo_time,
196 .set_alarm = opal_set_tpo_time,
197};
198
199static int opal_rtc_probe(struct platform_device *pdev)
200{
201 struct rtc_device *rtc;
202
203 if (pdev->dev.of_node && of_get_property(pdev->dev.of_node, "has-tpo",
204 NULL))
205 device_set_wakeup_capable(&pdev->dev, true);
206
207 rtc = devm_rtc_device_register(&pdev->dev, DRVNAME, &opal_rtc_ops,
208 THIS_MODULE);
209 if (IS_ERR(rtc))
210 return PTR_ERR(rtc);
211
212 rtc->uie_unsupported = 1;
213
214 return 0;
215}
216
217static const struct of_device_id opal_rtc_match[] = {
218 {
219 .compatible = "ibm,opal-rtc",
220 },
221 { }
222};
223MODULE_DEVICE_TABLE(of, opal_rtc_match);
224
225static const struct platform_device_id opal_rtc_driver_ids[] = {
226 {
227 .name = "opal-rtc",
228 },
229 { }
230};
231MODULE_DEVICE_TABLE(platform, opal_rtc_driver_ids);
232
233static struct platform_driver opal_rtc_driver = {
234 .probe = opal_rtc_probe,
235 .id_table = opal_rtc_driver_ids,
236 .driver = {
237 .name = DRVNAME,
238 .owner = THIS_MODULE,
239 .of_match_table = opal_rtc_match,
240 },
241};
242
243static int __init opal_rtc_init(void)
244{
245 if (!firmware_has_feature(FW_FEATURE_OPAL))
246 return -ENODEV;
247
248 return platform_driver_register(&opal_rtc_driver);
249}
250
251static void __exit opal_rtc_exit(void)
252{
253 platform_driver_unregister(&opal_rtc_driver);
254}
255
256MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
257MODULE_DESCRIPTION("IBM OPAL RTC driver");
258MODULE_LICENSE("GPL");
259
260module_init(opal_rtc_init);
261module_exit(opal_rtc_exit);
diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c
index c2ef0a22ee94..96fb32e7d6f8 100644
--- a/drivers/rtc/rtc-pcf8563.c
+++ b/drivers/rtc/rtc-pcf8563.c
@@ -28,6 +28,7 @@
28#define PCF8563_REG_ST2 0x01 28#define PCF8563_REG_ST2 0x01
29#define PCF8563_BIT_AIE (1 << 1) 29#define PCF8563_BIT_AIE (1 << 1)
30#define PCF8563_BIT_AF (1 << 3) 30#define PCF8563_BIT_AF (1 << 3)
31#define PCF8563_BITS_ST2_N (7 << 5)
31 32
32#define PCF8563_REG_SC 0x02 /* datetime */ 33#define PCF8563_REG_SC 0x02 /* datetime */
33#define PCF8563_REG_MN 0x03 34#define PCF8563_REG_MN 0x03
@@ -41,6 +42,13 @@
41 42
42#define PCF8563_REG_CLKO 0x0D /* clock out */ 43#define PCF8563_REG_CLKO 0x0D /* clock out */
43#define PCF8563_REG_TMRC 0x0E /* timer control */ 44#define PCF8563_REG_TMRC 0x0E /* timer control */
45#define PCF8563_TMRC_ENABLE BIT(7)
46#define PCF8563_TMRC_4096 0
47#define PCF8563_TMRC_64 1
48#define PCF8563_TMRC_1 2
49#define PCF8563_TMRC_1_60 3
50#define PCF8563_TMRC_MASK 3
51
44#define PCF8563_REG_TMR 0x0F /* timer */ 52#define PCF8563_REG_TMR 0x0F /* timer */
45 53
46#define PCF8563_SC_LV 0x80 /* low voltage */ 54#define PCF8563_SC_LV 0x80 /* low voltage */
@@ -118,22 +126,21 @@ static int pcf8563_write_block_data(struct i2c_client *client,
118 126
119static int pcf8563_set_alarm_mode(struct i2c_client *client, bool on) 127static int pcf8563_set_alarm_mode(struct i2c_client *client, bool on)
120{ 128{
121 unsigned char buf[2]; 129 unsigned char buf;
122 int err; 130 int err;
123 131
124 err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, buf + 1); 132 err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, &buf);
125 if (err < 0) 133 if (err < 0)
126 return err; 134 return err;
127 135
128 if (on) 136 if (on)
129 buf[1] |= PCF8563_BIT_AIE; 137 buf |= PCF8563_BIT_AIE;
130 else 138 else
131 buf[1] &= ~PCF8563_BIT_AIE; 139 buf &= ~PCF8563_BIT_AIE;
132 140
133 buf[1] &= ~PCF8563_BIT_AF; 141 buf &= ~(PCF8563_BIT_AF | PCF8563_BITS_ST2_N);
134 buf[0] = PCF8563_REG_ST2;
135 142
136 err = pcf8563_write_block_data(client, PCF8563_REG_ST2, 1, buf + 1); 143 err = pcf8563_write_block_data(client, PCF8563_REG_ST2, 1, &buf);
137 if (err < 0) { 144 if (err < 0) {
138 dev_err(&client->dev, "%s: write error\n", __func__); 145 dev_err(&client->dev, "%s: write error\n", __func__);
139 return -EIO; 146 return -EIO;
@@ -336,8 +343,8 @@ static int pcf8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
336 __func__, buf[0], buf[1], buf[2], buf[3]); 343 __func__, buf[0], buf[1], buf[2], buf[3]);
337 344
338 tm->time.tm_min = bcd2bin(buf[0] & 0x7F); 345 tm->time.tm_min = bcd2bin(buf[0] & 0x7F);
339 tm->time.tm_hour = bcd2bin(buf[1] & 0x7F); 346 tm->time.tm_hour = bcd2bin(buf[1] & 0x3F);
340 tm->time.tm_mday = bcd2bin(buf[2] & 0x1F); 347 tm->time.tm_mday = bcd2bin(buf[2] & 0x3F);
341 tm->time.tm_wday = bcd2bin(buf[3] & 0x7); 348 tm->time.tm_wday = bcd2bin(buf[3] & 0x7);
342 tm->time.tm_mon = -1; 349 tm->time.tm_mon = -1;
343 tm->time.tm_year = -1; 350 tm->time.tm_year = -1;
@@ -361,6 +368,14 @@ static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
361 struct i2c_client *client = to_i2c_client(dev); 368 struct i2c_client *client = to_i2c_client(dev);
362 unsigned char buf[4]; 369 unsigned char buf[4];
363 int err; 370 int err;
371 unsigned long alarm_time;
372
373 /* The alarm has no seconds, round up to nearest minute */
374 if (tm->time.tm_sec) {
375 rtc_tm_to_time(&tm->time, &alarm_time);
376 alarm_time += 60-tm->time.tm_sec;
377 rtc_time_to_tm(alarm_time, &tm->time);
378 }
364 379
365 dev_dbg(dev, "%s, min=%d hour=%d wday=%d mday=%d " 380 dev_dbg(dev, "%s, min=%d hour=%d wday=%d mday=%d "
366 "enabled=%d pending=%d\n", __func__, 381 "enabled=%d pending=%d\n", __func__,
@@ -381,6 +396,7 @@ static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
381 396
382static int pcf8563_irq_enable(struct device *dev, unsigned int enabled) 397static int pcf8563_irq_enable(struct device *dev, unsigned int enabled)
383{ 398{
399 dev_dbg(dev, "%s: en=%d\n", __func__, enabled);
384 return pcf8563_set_alarm_mode(to_i2c_client(dev), !!enabled); 400 return pcf8563_set_alarm_mode(to_i2c_client(dev), !!enabled);
385} 401}
386 402
@@ -398,6 +414,8 @@ static int pcf8563_probe(struct i2c_client *client,
398{ 414{
399 struct pcf8563 *pcf8563; 415 struct pcf8563 *pcf8563;
400 int err; 416 int err;
417 unsigned char buf;
418 unsigned char alm_pending;
401 419
402 dev_dbg(&client->dev, "%s\n", __func__); 420 dev_dbg(&client->dev, "%s\n", __func__);
403 421
@@ -415,6 +433,22 @@ static int pcf8563_probe(struct i2c_client *client,
415 pcf8563->client = client; 433 pcf8563->client = client;
416 device_set_wakeup_capable(&client->dev, 1); 434 device_set_wakeup_capable(&client->dev, 1);
417 435
436 /* Set timer to lowest frequency to save power (ref Haoyu datasheet) */
437 buf = PCF8563_TMRC_1_60;
438 err = pcf8563_write_block_data(client, PCF8563_REG_TMRC, 1, &buf);
439 if (err < 0) {
440 dev_err(&client->dev, "%s: write error\n", __func__);
441 return err;
442 }
443
444 err = pcf8563_get_alarm_mode(client, NULL, &alm_pending);
445 if (err < 0) {
446 dev_err(&client->dev, "%s: read error\n", __func__);
447 return err;
448 }
449 if (alm_pending)
450 pcf8563_set_alarm_mode(client, 0);
451
418 pcf8563->rtc = devm_rtc_device_register(&client->dev, 452 pcf8563->rtc = devm_rtc_device_register(&client->dev,
419 pcf8563_driver.driver.name, 453 pcf8563_driver.driver.name,
420 &pcf8563_rtc_ops, THIS_MODULE); 454 &pcf8563_rtc_ops, THIS_MODULE);
@@ -435,6 +469,9 @@ static int pcf8563_probe(struct i2c_client *client,
435 469
436 } 470 }
437 471
472 /* the pcf8563 alarm only supports a minute accuracy */
473 pcf8563->rtc->uie_unsupported = 1;
474
438 return 0; 475 return 0;
439} 476}
440 477
diff --git a/drivers/rtc/rtc-sirfsoc.c b/drivers/rtc/rtc-sirfsoc.c
index 52db89e25959..edc3b43282d4 100644
--- a/drivers/rtc/rtc-sirfsoc.c
+++ b/drivers/rtc/rtc-sirfsoc.c
@@ -47,6 +47,7 @@ struct sirfsoc_rtc_drv {
47 unsigned irq_wake; 47 unsigned irq_wake;
48 /* Overflow for every 8 years extra time */ 48 /* Overflow for every 8 years extra time */
49 u32 overflow_rtc; 49 u32 overflow_rtc;
50 spinlock_t lock;
50#ifdef CONFIG_PM 51#ifdef CONFIG_PM
51 u32 saved_counter; 52 u32 saved_counter;
52 u32 saved_overflow_rtc; 53 u32 saved_overflow_rtc;
@@ -61,7 +62,7 @@ static int sirfsoc_rtc_read_alarm(struct device *dev,
61 62
62 rtcdrv = dev_get_drvdata(dev); 63 rtcdrv = dev_get_drvdata(dev);
63 64
64 local_irq_disable(); 65 spin_lock_irq(&rtcdrv->lock);
65 66
66 rtc_count = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN); 67 rtc_count = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
67 68
@@ -84,7 +85,8 @@ static int sirfsoc_rtc_read_alarm(struct device *dev,
84 if (sirfsoc_rtc_iobrg_readl( 85 if (sirfsoc_rtc_iobrg_readl(
85 rtcdrv->rtc_base + RTC_STATUS) & SIRFSOC_RTC_AL0E) 86 rtcdrv->rtc_base + RTC_STATUS) & SIRFSOC_RTC_AL0E)
86 alrm->enabled = 1; 87 alrm->enabled = 1;
87 local_irq_enable(); 88
89 spin_unlock_irq(&rtcdrv->lock);
88 90
89 return 0; 91 return 0;
90} 92}
@@ -99,7 +101,7 @@ static int sirfsoc_rtc_set_alarm(struct device *dev,
99 if (alrm->enabled) { 101 if (alrm->enabled) {
100 rtc_tm_to_time(&(alrm->time), &rtc_alarm); 102 rtc_tm_to_time(&(alrm->time), &rtc_alarm);
101 103
102 local_irq_disable(); 104 spin_lock_irq(&rtcdrv->lock);
103 105
104 rtc_status_reg = sirfsoc_rtc_iobrg_readl( 106 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
105 rtcdrv->rtc_base + RTC_STATUS); 107 rtcdrv->rtc_base + RTC_STATUS);
@@ -123,14 +125,15 @@ static int sirfsoc_rtc_set_alarm(struct device *dev,
123 rtc_status_reg |= SIRFSOC_RTC_AL0E; 125 rtc_status_reg |= SIRFSOC_RTC_AL0E;
124 sirfsoc_rtc_iobrg_writel( 126 sirfsoc_rtc_iobrg_writel(
125 rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS); 127 rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
126 local_irq_enable(); 128
129 spin_unlock_irq(&rtcdrv->lock);
127 } else { 130 } else {
128 /* 131 /*
129 * if this function was called with enabled=0 132 * if this function was called with enabled=0
130 * then it could mean that the application is 133 * then it could mean that the application is
131 * trying to cancel an ongoing alarm 134 * trying to cancel an ongoing alarm
132 */ 135 */
133 local_irq_disable(); 136 spin_lock_irq(&rtcdrv->lock);
134 137
135 rtc_status_reg = sirfsoc_rtc_iobrg_readl( 138 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
136 rtcdrv->rtc_base + RTC_STATUS); 139 rtcdrv->rtc_base + RTC_STATUS);
@@ -146,7 +149,7 @@ static int sirfsoc_rtc_set_alarm(struct device *dev,
146 rtcdrv->rtc_base + RTC_STATUS); 149 rtcdrv->rtc_base + RTC_STATUS);
147 } 150 }
148 151
149 local_irq_enable(); 152 spin_unlock_irq(&rtcdrv->lock);
150 } 153 }
151 154
152 return 0; 155 return 0;
@@ -209,12 +212,38 @@ static int sirfsoc_rtc_ioctl(struct device *dev, unsigned int cmd,
209 } 212 }
210} 213}
211 214
215static int sirfsoc_rtc_alarm_irq_enable(struct device *dev,
216 unsigned int enabled)
217{
218 unsigned long rtc_status_reg = 0x0;
219 struct sirfsoc_rtc_drv *rtcdrv;
220
221 rtcdrv = dev_get_drvdata(dev);
222
223 spin_lock_irq(&rtcdrv->lock);
224
225 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
226 rtcdrv->rtc_base + RTC_STATUS);
227 if (enabled)
228 rtc_status_reg |= SIRFSOC_RTC_AL0E;
229 else
230 rtc_status_reg &= ~SIRFSOC_RTC_AL0E;
231
232 sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
233
234 spin_unlock_irq(&rtcdrv->lock);
235
236 return 0;
237
238}
239
212static const struct rtc_class_ops sirfsoc_rtc_ops = { 240static const struct rtc_class_ops sirfsoc_rtc_ops = {
213 .read_time = sirfsoc_rtc_read_time, 241 .read_time = sirfsoc_rtc_read_time,
214 .set_time = sirfsoc_rtc_set_time, 242 .set_time = sirfsoc_rtc_set_time,
215 .read_alarm = sirfsoc_rtc_read_alarm, 243 .read_alarm = sirfsoc_rtc_read_alarm,
216 .set_alarm = sirfsoc_rtc_set_alarm, 244 .set_alarm = sirfsoc_rtc_set_alarm,
217 .ioctl = sirfsoc_rtc_ioctl 245 .ioctl = sirfsoc_rtc_ioctl,
246 .alarm_irq_enable = sirfsoc_rtc_alarm_irq_enable
218}; 247};
219 248
220static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata) 249static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
@@ -223,6 +252,8 @@ static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
223 unsigned long rtc_status_reg = 0x0; 252 unsigned long rtc_status_reg = 0x0;
224 unsigned long events = 0x0; 253 unsigned long events = 0x0;
225 254
255 spin_lock(&rtcdrv->lock);
256
226 rtc_status_reg = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_STATUS); 257 rtc_status_reg = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_STATUS);
227 /* this bit will be set ONLY if an alarm was active 258 /* this bit will be set ONLY if an alarm was active
228 * and it expired NOW 259 * and it expired NOW
@@ -240,6 +271,9 @@ static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
240 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E); 271 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
241 } 272 }
242 sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS); 273 sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
274
275 spin_unlock(&rtcdrv->lock);
276
243 /* this should wake up any apps polling/waiting on the read 277 /* this should wake up any apps polling/waiting on the read
244 * after setting the alarm 278 * after setting the alarm
245 */ 279 */
@@ -267,6 +301,8 @@ static int sirfsoc_rtc_probe(struct platform_device *pdev)
267 if (rtcdrv == NULL) 301 if (rtcdrv == NULL)
268 return -ENOMEM; 302 return -ENOMEM;
269 303
304 spin_lock_init(&rtcdrv->lock);
305
270 err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base); 306 err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base);
271 if (err) { 307 if (err) {
272 dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n"); 308 dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n");
@@ -286,14 +322,6 @@ static int sirfsoc_rtc_probe(struct platform_device *pdev)
286 rtc_div = ((32768 / RTC_HZ) / 2) - 1; 322 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
287 sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV); 323 sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
288 324
289 rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
290 &sirfsoc_rtc_ops, THIS_MODULE);
291 if (IS_ERR(rtcdrv->rtc)) {
292 err = PTR_ERR(rtcdrv->rtc);
293 dev_err(&pdev->dev, "can't register RTC device\n");
294 return err;
295 }
296
297 /* 0x3 -> RTC_CLK */ 325 /* 0x3 -> RTC_CLK */
298 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK, 326 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
299 rtcdrv->rtc_base + RTC_CLOCK_SWITCH); 327 rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
@@ -308,6 +336,14 @@ static int sirfsoc_rtc_probe(struct platform_device *pdev)
308 rtcdrv->overflow_rtc = 336 rtcdrv->overflow_rtc =
309 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE); 337 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
310 338
339 rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
340 &sirfsoc_rtc_ops, THIS_MODULE);
341 if (IS_ERR(rtcdrv->rtc)) {
342 err = PTR_ERR(rtcdrv->rtc);
343 dev_err(&pdev->dev, "can't register RTC device\n");
344 return err;
345 }
346
311 rtcdrv->irq = platform_get_irq(pdev, 0); 347 rtcdrv->irq = platform_get_irq(pdev, 0);
312 err = devm_request_irq( 348 err = devm_request_irq(
313 &pdev->dev, 349 &pdev->dev,
diff --git a/drivers/rtc/rtc-snvs.c b/drivers/rtc/rtc-snvs.c
index f04c378b781a..0479e807a776 100644
--- a/drivers/rtc/rtc-snvs.c
+++ b/drivers/rtc/rtc-snvs.c
@@ -17,6 +17,7 @@
17#include <linux/of_device.h> 17#include <linux/of_device.h>
18#include <linux/platform_device.h> 18#include <linux/platform_device.h>
19#include <linux/rtc.h> 19#include <linux/rtc.h>
20#include <linux/clk.h>
20 21
21/* These register offsets are relative to LP (Low Power) range */ 22/* These register offsets are relative to LP (Low Power) range */
22#define SNVS_LPCR 0x04 23#define SNVS_LPCR 0x04
@@ -39,6 +40,7 @@ struct snvs_rtc_data {
39 void __iomem *ioaddr; 40 void __iomem *ioaddr;
40 int irq; 41 int irq;
41 spinlock_t lock; 42 spinlock_t lock;
43 struct clk *clk;
42}; 44};
43 45
44static u32 rtc_read_lp_counter(void __iomem *ioaddr) 46static u32 rtc_read_lp_counter(void __iomem *ioaddr)
@@ -260,6 +262,18 @@ static int snvs_rtc_probe(struct platform_device *pdev)
260 if (data->irq < 0) 262 if (data->irq < 0)
261 return data->irq; 263 return data->irq;
262 264
265 data->clk = devm_clk_get(&pdev->dev, "snvs-rtc");
266 if (IS_ERR(data->clk)) {
267 data->clk = NULL;
268 } else {
269 ret = clk_prepare_enable(data->clk);
270 if (ret) {
271 dev_err(&pdev->dev,
272 "Could not prepare or enable the snvs clock\n");
273 return ret;
274 }
275 }
276
263 platform_set_drvdata(pdev, data); 277 platform_set_drvdata(pdev, data);
264 278
265 spin_lock_init(&data->lock); 279 spin_lock_init(&data->lock);
@@ -280,7 +294,7 @@ static int snvs_rtc_probe(struct platform_device *pdev)
280 if (ret) { 294 if (ret) {
281 dev_err(&pdev->dev, "failed to request irq %d: %d\n", 295 dev_err(&pdev->dev, "failed to request irq %d: %d\n",
282 data->irq, ret); 296 data->irq, ret);
283 return ret; 297 goto error_rtc_device_register;
284 } 298 }
285 299
286 data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name, 300 data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
@@ -288,10 +302,16 @@ static int snvs_rtc_probe(struct platform_device *pdev)
288 if (IS_ERR(data->rtc)) { 302 if (IS_ERR(data->rtc)) {
289 ret = PTR_ERR(data->rtc); 303 ret = PTR_ERR(data->rtc);
290 dev_err(&pdev->dev, "failed to register rtc: %d\n", ret); 304 dev_err(&pdev->dev, "failed to register rtc: %d\n", ret);
291 return ret; 305 goto error_rtc_device_register;
292 } 306 }
293 307
294 return 0; 308 return 0;
309
310error_rtc_device_register:
311 if (data->clk)
312 clk_disable_unprepare(data->clk);
313
314 return ret;
295} 315}
296 316
297#ifdef CONFIG_PM_SLEEP 317#ifdef CONFIG_PM_SLEEP
@@ -302,21 +322,41 @@ static int snvs_rtc_suspend(struct device *dev)
302 if (device_may_wakeup(dev)) 322 if (device_may_wakeup(dev))
303 enable_irq_wake(data->irq); 323 enable_irq_wake(data->irq);
304 324
325 if (data->clk)
326 clk_disable_unprepare(data->clk);
327
305 return 0; 328 return 0;
306} 329}
307 330
308static int snvs_rtc_resume(struct device *dev) 331static int snvs_rtc_resume(struct device *dev)
309{ 332{
310 struct snvs_rtc_data *data = dev_get_drvdata(dev); 333 struct snvs_rtc_data *data = dev_get_drvdata(dev);
334 int ret;
311 335
312 if (device_may_wakeup(dev)) 336 if (device_may_wakeup(dev))
313 disable_irq_wake(data->irq); 337 disable_irq_wake(data->irq);
314 338
339 if (data->clk) {
340 ret = clk_prepare_enable(data->clk);
341 if (ret)
342 return ret;
343 }
344
315 return 0; 345 return 0;
316} 346}
317#endif
318 347
319static SIMPLE_DEV_PM_OPS(snvs_rtc_pm_ops, snvs_rtc_suspend, snvs_rtc_resume); 348static const struct dev_pm_ops snvs_rtc_pm_ops = {
349 .suspend_noirq = snvs_rtc_suspend,
350 .resume_noirq = snvs_rtc_resume,
351};
352
353#define SNVS_RTC_PM_OPS (&snvs_rtc_pm_ops)
354
355#else
356
357#define SNVS_RTC_PM_OPS NULL
358
359#endif
320 360
321static const struct of_device_id snvs_dt_ids[] = { 361static const struct of_device_id snvs_dt_ids[] = {
322 { .compatible = "fsl,sec-v4.0-mon-rtc-lp", }, 362 { .compatible = "fsl,sec-v4.0-mon-rtc-lp", },
@@ -327,7 +367,7 @@ MODULE_DEVICE_TABLE(of, snvs_dt_ids);
327static struct platform_driver snvs_rtc_driver = { 367static struct platform_driver snvs_rtc_driver = {
328 .driver = { 368 .driver = {
329 .name = "snvs_rtc", 369 .name = "snvs_rtc",
330 .pm = &snvs_rtc_pm_ops, 370 .pm = SNVS_RTC_PM_OPS,
331 .of_match_table = snvs_dt_ids, 371 .of_match_table = snvs_dt_ids,
332 }, 372 },
333 .probe = snvs_rtc_probe, 373 .probe = snvs_rtc_probe,