aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig55
-rw-r--r--drivers/rtc/Makefile5
-rw-r--r--drivers/rtc/class.c4
-rw-r--r--drivers/rtc/rtc-ab3100.c2
-rw-r--r--drivers/rtc/rtc-ab8500.c103
-rw-r--r--drivers/rtc/rtc-bfin.c58
-rw-r--r--drivers/rtc/rtc-ds1302.c2
-rw-r--r--drivers/rtc/rtc-ds1305.c2
-rw-r--r--drivers/rtc/rtc-ds1307.c12
-rw-r--r--drivers/rtc/rtc-ds1374.c2
-rw-r--r--drivers/rtc/rtc-ds3232.c185
-rw-r--r--drivers/rtc/rtc-jz4740.c45
-rw-r--r--drivers/rtc/rtc-lpc32xx.c414
-rw-r--r--drivers/rtc/rtc-m41t80.c16
-rw-r--r--drivers/rtc/rtc-max8998.c300
-rw-r--r--drivers/rtc/rtc-mc13783.c428
-rw-r--r--drivers/rtc/rtc-mc13xxx.c437
-rw-r--r--drivers/rtc/rtc-mrst.c582
-rw-r--r--drivers/rtc/rtc-nuc900.c2
-rw-r--r--drivers/rtc/rtc-omap.c12
-rw-r--r--drivers/rtc/rtc-pl031.c2
-rw-r--r--drivers/rtc/rtc-rs5c313.c34
-rw-r--r--drivers/rtc/rtc-rs5c372.c2
-rw-r--r--drivers/rtc/rtc-rx8025.c2
-rw-r--r--drivers/rtc/rtc-s3c.c105
-rw-r--r--drivers/rtc/rtc-sa1100.c161
-rw-r--r--drivers/rtc/rtc-sh.c4
27 files changed, 2336 insertions, 640 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 48ca7132cc05..4941cade319f 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -171,7 +171,8 @@ config RTC_DRV_DS3232
171 depends on RTC_CLASS && I2C 171 depends on RTC_CLASS && I2C
172 help 172 help
173 If you say yes here you get support for Dallas Semiconductor 173 If you say yes here you get support for Dallas Semiconductor
174 DS3232 real-time clock chips. 174 DS3232 real-time clock chips. If an interrupt is associated
175 with the device, the alarm functionality is supported.
175 176
176 This driver can also be built as a module. If so, the module 177 This driver can also be built as a module. If so, the module
177 will be called rtc-ds3232. 178 will be called rtc-ds3232.
@@ -195,6 +196,16 @@ config RTC_DRV_MAX8925
195 This driver can also be built as a module. If so, the module 196 This driver can also be built as a module. If so, the module
196 will be called rtc-max8925. 197 will be called rtc-max8925.
197 198
199config RTC_DRV_MAX8998
200 tristate "Maxim MAX8998"
201 depends on MFD_MAX8998
202 help
203 If you say yes here you will get support for the
204 RTC of Maxim MAX8998 PMIC.
205
206 This driver can also be built as a module. If so, the module
207 will be called rtc-max8998.
208
198config RTC_DRV_RS5C372 209config RTC_DRV_RS5C372
199 tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A" 210 tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A"
200 help 211 help
@@ -452,6 +463,18 @@ config RTC_DRV_CMOS
452 This driver can also be built as a module. If so, the module 463 This driver can also be built as a module. If so, the module
453 will be called rtc-cmos. 464 will be called rtc-cmos.
454 465
466config RTC_DRV_VRTC
467 tristate "Virtual RTC for Moorestown platforms"
468 depends on X86_MRST
469 default y if X86_MRST
470
471 help
472 Say "yes" here to get direct support for the real time clock
473 found on Moorestown platforms. The VRTC is a emulated RTC that
474 derives its clock source from a real RTC in the PMIC. The MC146818
475 style programming interface is mostly conserved, but any
476 updates are done via IPC calls to the system controller FW.
477
455config RTC_DRV_DS1216 478config RTC_DRV_DS1216
456 tristate "Dallas DS1216" 479 tristate "Dallas DS1216"
457 depends on SNI_RM 480 depends on SNI_RM
@@ -765,15 +788,15 @@ config RTC_DRV_AT32AP700X
765 AT32AP700x family processors. 788 AT32AP700x family processors.
766 789
767config RTC_DRV_AT91RM9200 790config RTC_DRV_AT91RM9200
768 tristate "AT91RM9200 or AT91SAM9RL" 791 tristate "AT91RM9200 or some AT91SAM9 RTC"
769 depends on ARCH_AT91RM9200 || ARCH_AT91SAM9RL 792 depends on ARCH_AT91RM9200 || ARCH_AT91SAM9RL || ARCH_AT91SAM9G45
770 help 793 help
771 Driver for the internal RTC (Realtime Clock) module found on 794 Driver for the internal RTC (Realtime Clock) module found on
772 Atmel AT91RM9200's and AT91SAM9RL chips. On SAM9RL chips 795 Atmel AT91RM9200's and some AT91SAM9 chips. On AT91SAM9 chips
773 this is powered by the backup power supply. 796 this is powered by the backup power supply.
774 797
775config RTC_DRV_AT91SAM9 798config RTC_DRV_AT91SAM9
776 tristate "AT91SAM9x/AT91CAP9" 799 tristate "AT91SAM9x/AT91CAP9 RTT as RTC"
777 depends on ARCH_AT91 && !(ARCH_AT91RM9200 || ARCH_AT91X40) 800 depends on ARCH_AT91 && !(ARCH_AT91RM9200 || ARCH_AT91X40)
778 help 801 help
779 RTC driver for the Atmel AT91SAM9x and AT91CAP9 internal RTT 802 RTC driver for the Atmel AT91SAM9x and AT91CAP9 internal RTT
@@ -781,8 +804,8 @@ config RTC_DRV_AT91SAM9
781 supply (such as a small coin cell battery), but do not need to 804 supply (such as a small coin cell battery), but do not need to
782 be used as RTCs. 805 be used as RTCs.
783 806
784 (On AT91SAM9rl chips you probably want to use the dedicated RTC 807 (On AT91SAM9rl and AT91SAM9G45 chips you probably want to use the
785 module and leave the RTT available for other uses.) 808 dedicated RTC module and leave the RTT available for other uses.)
786 809
787config RTC_DRV_AT91SAM9_RTT 810config RTC_DRV_AT91SAM9_RTT
788 int 811 int
@@ -925,11 +948,12 @@ config RTC_DRV_PCAP
925 If you say Y here you will get support for the RTC found on 948 If you say Y here you will get support for the RTC found on
926 the PCAP2 ASIC used on some Motorola phones. 949 the PCAP2 ASIC used on some Motorola phones.
927 950
928config RTC_DRV_MC13783 951config RTC_DRV_MC13XXX
929 depends on MFD_MC13783 952 depends on MFD_MC13XXX
930 tristate "Freescale MC13783 RTC" 953 tristate "Freescale MC13xxx RTC"
931 help 954 help
932 This enables support for the Freescale MC13783 PMIC RTC 955 This enables support for the RTCs found on Freescale's PMICs
956 MC13783 and MC13892.
933 957
934config RTC_DRV_MPC5121 958config RTC_DRV_MPC5121
935 tristate "Freescale MPC5121 built-in RTC" 959 tristate "Freescale MPC5121 built-in RTC"
@@ -952,4 +976,13 @@ config RTC_DRV_JZ4740
952 This driver can also be buillt as a module. If so, the module 976 This driver can also be buillt as a module. If so, the module
953 will be called rtc-jz4740. 977 will be called rtc-jz4740.
954 978
979config RTC_DRV_LPC32XX
980 depends on ARCH_LPC32XX
981 tristate "NXP LPC32XX RTC"
982 help
983 This enables support for the NXP RTC in the LPC32XX
984
985 This driver can also be buillt as a module. If so, the module
986 will be called rtc-lpc32xx.
987
955endif # RTC_CLASS 988endif # RTC_CLASS
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 0f207b3b5833..2afdaf3ff986 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_RTC_DRV_CMOS) += rtc-cmos.o
30obj-$(CONFIG_RTC_DRV_COH901331) += rtc-coh901331.o 30obj-$(CONFIG_RTC_DRV_COH901331) += rtc-coh901331.o
31obj-$(CONFIG_RTC_DRV_DAVINCI) += rtc-davinci.o 31obj-$(CONFIG_RTC_DRV_DAVINCI) += rtc-davinci.o
32obj-$(CONFIG_RTC_DRV_DM355EVM) += rtc-dm355evm.o 32obj-$(CONFIG_RTC_DRV_DM355EVM) += rtc-dm355evm.o
33obj-$(CONFIG_RTC_DRV_VRTC) += rtc-mrst.o
33obj-$(CONFIG_RTC_DRV_DS1216) += rtc-ds1216.o 34obj-$(CONFIG_RTC_DRV_DS1216) += rtc-ds1216.o
34obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o 35obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o
35obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o 36obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o
@@ -51,6 +52,7 @@ obj-$(CONFIG_RTC_DRV_IMXDI) += rtc-imxdi.o
51obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o 52obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
52obj-$(CONFIG_RTC_DRV_ISL12022) += rtc-isl12022.o 53obj-$(CONFIG_RTC_DRV_ISL12022) += rtc-isl12022.o
53obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o 54obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o
55obj-$(CONFIG_RTC_DRV_LPC32XX) += rtc-lpc32xx.o
54obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o 56obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o
55obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o 57obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o
56obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o 58obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
@@ -59,8 +61,9 @@ obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o
59obj-$(CONFIG_RTC_MXC) += rtc-mxc.o 61obj-$(CONFIG_RTC_MXC) += rtc-mxc.o
60obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o 62obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o
61obj-$(CONFIG_RTC_DRV_MAX8925) += rtc-max8925.o 63obj-$(CONFIG_RTC_DRV_MAX8925) += rtc-max8925.o
64obj-$(CONFIG_RTC_DRV_MAX8998) += rtc-max8998.o
62obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o 65obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o
63obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o 66obj-$(CONFIG_RTC_DRV_MC13XXX) += rtc-mc13xxx.o
64obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o 67obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o
65obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o 68obj-$(CONFIG_RTC_DRV_MPC5121) += rtc-mpc5121.o
66obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o 69obj-$(CONFIG_RTC_DRV_MV) += rtc-mv.o
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 3243832a17cd..9583cbcc6b79 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -171,8 +171,10 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
171 rtc_dev_prepare(rtc); 171 rtc_dev_prepare(rtc);
172 172
173 err = device_register(&rtc->dev); 173 err = device_register(&rtc->dev);
174 if (err) 174 if (err) {
175 put_device(&rtc->dev);
175 goto exit_kfree; 176 goto exit_kfree;
177 }
176 178
177 rtc_dev_add_device(rtc); 179 rtc_dev_add_device(rtc);
178 rtc_sysfs_add_device(rtc); 180 rtc_sysfs_add_device(rtc);
diff --git a/drivers/rtc/rtc-ab3100.c b/drivers/rtc/rtc-ab3100.c
index d26780ea254b..261a07e0fb24 100644
--- a/drivers/rtc/rtc-ab3100.c
+++ b/drivers/rtc/rtc-ab3100.c
@@ -235,6 +235,7 @@ static int __init ab3100_rtc_probe(struct platform_device *pdev)
235 err = PTR_ERR(rtc); 235 err = PTR_ERR(rtc);
236 return err; 236 return err;
237 } 237 }
238 platform_set_drvdata(pdev, rtc);
238 239
239 return 0; 240 return 0;
240} 241}
@@ -244,6 +245,7 @@ static int __exit ab3100_rtc_remove(struct platform_device *pdev)
244 struct rtc_device *rtc = platform_get_drvdata(pdev); 245 struct rtc_device *rtc = platform_get_drvdata(pdev);
245 246
246 rtc_device_unregister(rtc); 247 rtc_device_unregister(rtc);
248 platform_set_drvdata(pdev, NULL);
247 return 0; 249 return 0;
248} 250}
249 251
diff --git a/drivers/rtc/rtc-ab8500.c b/drivers/rtc/rtc-ab8500.c
index 2fda03125e55..e346705aae92 100644
--- a/drivers/rtc/rtc-ab8500.c
+++ b/drivers/rtc/rtc-ab8500.c
@@ -14,26 +14,26 @@
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/platform_device.h> 15#include <linux/platform_device.h>
16#include <linux/rtc.h> 16#include <linux/rtc.h>
17#include <linux/mfd/abx500.h>
17#include <linux/mfd/ab8500.h> 18#include <linux/mfd/ab8500.h>
18#include <linux/delay.h> 19#include <linux/delay.h>
19 20
20#define AB8500_RTC_SOFF_STAT_REG 0x0F00 21#define AB8500_RTC_SOFF_STAT_REG 0x00
21#define AB8500_RTC_CC_CONF_REG 0x0F01 22#define AB8500_RTC_CC_CONF_REG 0x01
22#define AB8500_RTC_READ_REQ_REG 0x0F02 23#define AB8500_RTC_READ_REQ_REG 0x02
23#define AB8500_RTC_WATCH_TSECMID_REG 0x0F03 24#define AB8500_RTC_WATCH_TSECMID_REG 0x03
24#define AB8500_RTC_WATCH_TSECHI_REG 0x0F04 25#define AB8500_RTC_WATCH_TSECHI_REG 0x04
25#define AB8500_RTC_WATCH_TMIN_LOW_REG 0x0F05 26#define AB8500_RTC_WATCH_TMIN_LOW_REG 0x05
26#define AB8500_RTC_WATCH_TMIN_MID_REG 0x0F06 27#define AB8500_RTC_WATCH_TMIN_MID_REG 0x06
27#define AB8500_RTC_WATCH_TMIN_HI_REG 0x0F07 28#define AB8500_RTC_WATCH_TMIN_HI_REG 0x07
28#define AB8500_RTC_ALRM_MIN_LOW_REG 0x0F08 29#define AB8500_RTC_ALRM_MIN_LOW_REG 0x08
29#define AB8500_RTC_ALRM_MIN_MID_REG 0x0F09 30#define AB8500_RTC_ALRM_MIN_MID_REG 0x09
30#define AB8500_RTC_ALRM_MIN_HI_REG 0x0F0A 31#define AB8500_RTC_ALRM_MIN_HI_REG 0x0A
31#define AB8500_RTC_STAT_REG 0x0F0B 32#define AB8500_RTC_STAT_REG 0x0B
32#define AB8500_RTC_BKUP_CHG_REG 0x0F0C 33#define AB8500_RTC_BKUP_CHG_REG 0x0C
33#define AB8500_RTC_FORCE_BKUP_REG 0x0F0D 34#define AB8500_RTC_FORCE_BKUP_REG 0x0D
34#define AB8500_RTC_CALIB_REG 0x0F0E 35#define AB8500_RTC_CALIB_REG 0x0E
35#define AB8500_RTC_SWITCH_STAT_REG 0x0F0F 36#define AB8500_RTC_SWITCH_STAT_REG 0x0F
36#define AB8500_REV_REG 0x1080
37 37
38/* RtcReadRequest bits */ 38/* RtcReadRequest bits */
39#define RTC_READ_REQUEST 0x01 39#define RTC_READ_REQUEST 0x01
@@ -46,13 +46,13 @@
46#define COUNTS_PER_SEC (0xF000 / 60) 46#define COUNTS_PER_SEC (0xF000 / 60)
47#define AB8500_RTC_EPOCH 2000 47#define AB8500_RTC_EPOCH 2000
48 48
49static const unsigned long ab8500_rtc_time_regs[] = { 49static const u8 ab8500_rtc_time_regs[] = {
50 AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG, 50 AB8500_RTC_WATCH_TMIN_HI_REG, AB8500_RTC_WATCH_TMIN_MID_REG,
51 AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG, 51 AB8500_RTC_WATCH_TMIN_LOW_REG, AB8500_RTC_WATCH_TSECHI_REG,
52 AB8500_RTC_WATCH_TSECMID_REG 52 AB8500_RTC_WATCH_TSECMID_REG
53}; 53};
54 54
55static const unsigned long ab8500_rtc_alarm_regs[] = { 55static const u8 ab8500_rtc_alarm_regs[] = {
56 AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG, 56 AB8500_RTC_ALRM_MIN_HI_REG, AB8500_RTC_ALRM_MIN_MID_REG,
57 AB8500_RTC_ALRM_MIN_LOW_REG 57 AB8500_RTC_ALRM_MIN_LOW_REG
58}; 58};
@@ -76,29 +76,30 @@ static unsigned long get_elapsed_seconds(int year)
76 76
77static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm) 77static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
78{ 78{
79 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
80 unsigned long timeout = jiffies + HZ; 79 unsigned long timeout = jiffies + HZ;
81 int retval, i; 80 int retval, i;
82 unsigned long mins, secs; 81 unsigned long mins, secs;
83 unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 82 unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
83 u8 value;
84 84
85 /* Request a data read */ 85 /* Request a data read */
86 retval = ab8500_write(ab8500, AB8500_RTC_READ_REQ_REG, 86 retval = abx500_set_register_interruptible(dev,
87 RTC_READ_REQUEST); 87 AB8500_RTC, AB8500_RTC_READ_REQ_REG, RTC_READ_REQUEST);
88 if (retval < 0) 88 if (retval < 0)
89 return retval; 89 return retval;
90 90
91 /* Early AB8500 chips will not clear the rtc read request bit */ 91 /* Early AB8500 chips will not clear the rtc read request bit */
92 if (ab8500->revision == 0) { 92 if (abx500_get_chip_id(dev) == 0) {
93 msleep(1); 93 msleep(1);
94 } else { 94 } else {
95 /* Wait for some cycles after enabling the rtc read in ab8500 */ 95 /* Wait for some cycles after enabling the rtc read in ab8500 */
96 while (time_before(jiffies, timeout)) { 96 while (time_before(jiffies, timeout)) {
97 retval = ab8500_read(ab8500, AB8500_RTC_READ_REQ_REG); 97 retval = abx500_get_register_interruptible(dev,
98 AB8500_RTC, AB8500_RTC_READ_REQ_REG, &value);
98 if (retval < 0) 99 if (retval < 0)
99 return retval; 100 return retval;
100 101
101 if (!(retval & RTC_READ_REQUEST)) 102 if (!(value & RTC_READ_REQUEST))
102 break; 103 break;
103 104
104 msleep(1); 105 msleep(1);
@@ -107,10 +108,11 @@ static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
107 108
108 /* Read the Watchtime registers */ 109 /* Read the Watchtime registers */
109 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 110 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
110 retval = ab8500_read(ab8500, ab8500_rtc_time_regs[i]); 111 retval = abx500_get_register_interruptible(dev,
112 AB8500_RTC, ab8500_rtc_time_regs[i], &value);
111 if (retval < 0) 113 if (retval < 0)
112 return retval; 114 return retval;
113 buf[i] = retval; 115 buf[i] = value;
114 } 116 }
115 117
116 mins = (buf[0] << 16) | (buf[1] << 8) | buf[2]; 118 mins = (buf[0] << 16) | (buf[1] << 8) | buf[2];
@@ -128,7 +130,6 @@ static int ab8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
128 130
129static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm) 131static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
130{ 132{
131 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
132 int retval, i; 133 int retval, i;
133 unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)]; 134 unsigned char buf[ARRAY_SIZE(ab8500_rtc_time_regs)];
134 unsigned long no_secs, no_mins, secs = 0; 135 unsigned long no_secs, no_mins, secs = 0;
@@ -162,27 +163,29 @@ static int ab8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
162 buf[0] = (no_mins >> 16) & 0xFF; 163 buf[0] = (no_mins >> 16) & 0xFF;
163 164
164 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) { 165 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_time_regs); i++) {
165 retval = ab8500_write(ab8500, ab8500_rtc_time_regs[i], buf[i]); 166 retval = abx500_set_register_interruptible(dev, AB8500_RTC,
167 ab8500_rtc_time_regs[i], buf[i]);
166 if (retval < 0) 168 if (retval < 0)
167 return retval; 169 return retval;
168 } 170 }
169 171
170 /* Request a data write */ 172 /* Request a data write */
171 return ab8500_write(ab8500, AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST); 173 return abx500_set_register_interruptible(dev, AB8500_RTC,
174 AB8500_RTC_READ_REQ_REG, RTC_WRITE_REQUEST);
172} 175}
173 176
174static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 177static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
175{ 178{
176 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
177 int retval, i; 179 int retval, i;
178 int rtc_ctrl; 180 u8 rtc_ctrl, value;
179 unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 181 unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
180 unsigned long secs, mins; 182 unsigned long secs, mins;
181 183
182 /* Check if the alarm is enabled or not */ 184 /* Check if the alarm is enabled or not */
183 rtc_ctrl = ab8500_read(ab8500, AB8500_RTC_STAT_REG); 185 retval = abx500_get_register_interruptible(dev, AB8500_RTC,
184 if (rtc_ctrl < 0) 186 AB8500_RTC_STAT_REG, &rtc_ctrl);
185 return rtc_ctrl; 187 if (retval < 0)
188 return retval;
186 189
187 if (rtc_ctrl & RTC_ALARM_ENA) 190 if (rtc_ctrl & RTC_ALARM_ENA)
188 alarm->enabled = 1; 191 alarm->enabled = 1;
@@ -192,10 +195,11 @@ static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
192 alarm->pending = 0; 195 alarm->pending = 0;
193 196
194 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 197 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
195 retval = ab8500_read(ab8500, ab8500_rtc_alarm_regs[i]); 198 retval = abx500_get_register_interruptible(dev, AB8500_RTC,
199 ab8500_rtc_alarm_regs[i], &value);
196 if (retval < 0) 200 if (retval < 0)
197 return retval; 201 return retval;
198 buf[i] = retval; 202 buf[i] = value;
199 } 203 }
200 204
201 mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]); 205 mins = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
@@ -211,15 +215,13 @@ static int ab8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
211 215
212static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled) 216static int ab8500_rtc_irq_enable(struct device *dev, unsigned int enabled)
213{ 217{
214 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent); 218 return abx500_mask_and_set_register_interruptible(dev, AB8500_RTC,
215 219 AB8500_RTC_STAT_REG, RTC_ALARM_ENA,
216 return ab8500_set_bits(ab8500, AB8500_RTC_STAT_REG, RTC_ALARM_ENA, 220 enabled ? RTC_ALARM_ENA : 0);
217 enabled ? RTC_ALARM_ENA : 0);
218} 221}
219 222
220static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 223static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
221{ 224{
222 struct ab8500 *ab8500 = dev_get_drvdata(dev->parent);
223 int retval, i; 225 int retval, i;
224 unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)]; 226 unsigned char buf[ARRAY_SIZE(ab8500_rtc_alarm_regs)];
225 unsigned long mins, secs = 0; 227 unsigned long mins, secs = 0;
@@ -247,7 +249,8 @@ static int ab8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
247 249
248 /* Set the alarm time */ 250 /* Set the alarm time */
249 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) { 251 for (i = 0; i < ARRAY_SIZE(ab8500_rtc_alarm_regs); i++) {
250 retval = ab8500_write(ab8500, ab8500_rtc_alarm_regs[i], buf[i]); 252 retval = abx500_set_register_interruptible(dev, AB8500_RTC,
253 ab8500_rtc_alarm_regs[i], buf[i]);
251 if (retval < 0) 254 if (retval < 0)
252 return retval; 255 return retval;
253 } 256 }
@@ -276,10 +279,9 @@ static const struct rtc_class_ops ab8500_rtc_ops = {
276 279
277static int __devinit ab8500_rtc_probe(struct platform_device *pdev) 280static int __devinit ab8500_rtc_probe(struct platform_device *pdev)
278{ 281{
279 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
280 int err; 282 int err;
281 struct rtc_device *rtc; 283 struct rtc_device *rtc;
282 int rtc_ctrl; 284 u8 rtc_ctrl;
283 int irq; 285 int irq;
284 286
285 irq = platform_get_irq_byname(pdev, "ALARM"); 287 irq = platform_get_irq_byname(pdev, "ALARM");
@@ -287,17 +289,18 @@ static int __devinit ab8500_rtc_probe(struct platform_device *pdev)
287 return irq; 289 return irq;
288 290
289 /* For RTC supply test */ 291 /* For RTC supply test */
290 err = ab8500_set_bits(ab8500, AB8500_RTC_STAT_REG, RTC_STATUS_DATA, 292 err = abx500_mask_and_set_register_interruptible(&pdev->dev, AB8500_RTC,
291 RTC_STATUS_DATA); 293 AB8500_RTC_STAT_REG, RTC_STATUS_DATA, RTC_STATUS_DATA);
292 if (err < 0) 294 if (err < 0)
293 return err; 295 return err;
294 296
295 /* Wait for reset by the PorRtc */ 297 /* Wait for reset by the PorRtc */
296 msleep(1); 298 msleep(1);
297 299
298 rtc_ctrl = ab8500_read(ab8500, AB8500_RTC_STAT_REG); 300 err = abx500_get_register_interruptible(&pdev->dev, AB8500_RTC,
299 if (rtc_ctrl < 0) 301 AB8500_RTC_STAT_REG, &rtc_ctrl);
300 return rtc_ctrl; 302 if (err < 0)
303 return err;
301 304
302 /* Check if the RTC Supply fails */ 305 /* Check if the RTC Supply fails */
303 if (!(rtc_ctrl & RTC_STATUS_DATA)) { 306 if (!(rtc_ctrl & RTC_STATUS_DATA)) {
diff --git a/drivers/rtc/rtc-bfin.c b/drivers/rtc/rtc-bfin.c
index 72b2bcc2c224..b4b6087f2234 100644
--- a/drivers/rtc/rtc-bfin.c
+++ b/drivers/rtc/rtc-bfin.c
@@ -2,7 +2,7 @@
2 * Blackfin On-Chip Real Time Clock Driver 2 * Blackfin On-Chip Real Time Clock Driver
3 * Supports BF51x/BF52x/BF53[123]/BF53[467]/BF54x 3 * Supports BF51x/BF52x/BF53[123]/BF53[467]/BF54x
4 * 4 *
5 * Copyright 2004-2009 Analog Devices Inc. 5 * Copyright 2004-2010 Analog Devices Inc.
6 * 6 *
7 * Enter bugs at http://blackfin.uclinux.org/ 7 * Enter bugs at http://blackfin.uclinux.org/
8 * 8 *
@@ -183,29 +183,33 @@ static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
183 struct bfin_rtc *rtc = dev_get_drvdata(dev); 183 struct bfin_rtc *rtc = dev_get_drvdata(dev);
184 unsigned long events = 0; 184 unsigned long events = 0;
185 bool write_complete = false; 185 bool write_complete = false;
186 u16 rtc_istat, rtc_ictl; 186 u16 rtc_istat, rtc_istat_clear, rtc_ictl, bits;
187 187
188 dev_dbg_stamp(dev); 188 dev_dbg_stamp(dev);
189 189
190 rtc_istat = bfin_read_RTC_ISTAT(); 190 rtc_istat = bfin_read_RTC_ISTAT();
191 rtc_ictl = bfin_read_RTC_ICTL(); 191 rtc_ictl = bfin_read_RTC_ICTL();
192 rtc_istat_clear = 0;
192 193
193 if (rtc_istat & RTC_ISTAT_WRITE_COMPLETE) { 194 bits = RTC_ISTAT_WRITE_COMPLETE;
194 bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE); 195 if (rtc_istat & bits) {
196 rtc_istat_clear |= bits;
195 write_complete = true; 197 write_complete = true;
196 complete(&bfin_write_complete); 198 complete(&bfin_write_complete);
197 } 199 }
198 200
199 if (rtc_ictl & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) { 201 bits = (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
200 if (rtc_istat & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) { 202 if (rtc_ictl & bits) {
201 bfin_write_RTC_ISTAT(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY); 203 if (rtc_istat & bits) {
204 rtc_istat_clear |= bits;
202 events |= RTC_AF | RTC_IRQF; 205 events |= RTC_AF | RTC_IRQF;
203 } 206 }
204 } 207 }
205 208
206 if (rtc_ictl & RTC_ISTAT_SEC) { 209 bits = RTC_ISTAT_SEC;
207 if (rtc_istat & RTC_ISTAT_SEC) { 210 if (rtc_ictl & bits) {
208 bfin_write_RTC_ISTAT(RTC_ISTAT_SEC); 211 if (rtc_istat & bits) {
212 rtc_istat_clear |= bits;
209 events |= RTC_UF | RTC_IRQF; 213 events |= RTC_UF | RTC_IRQF;
210 } 214 }
211 } 215 }
@@ -213,9 +217,10 @@ static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
213 if (events) 217 if (events)
214 rtc_update_irq(rtc->rtc_dev, 1, events); 218 rtc_update_irq(rtc->rtc_dev, 1, events);
215 219
216 if (write_complete || events) 220 if (write_complete || events) {
221 bfin_write_RTC_ISTAT(rtc_istat_clear);
217 return IRQ_HANDLED; 222 return IRQ_HANDLED;
218 else 223 } else
219 return IRQ_NONE; 224 return IRQ_NONE;
220} 225}
221 226
@@ -422,21 +427,38 @@ static int __devexit bfin_rtc_remove(struct platform_device *pdev)
422#ifdef CONFIG_PM 427#ifdef CONFIG_PM
423static int bfin_rtc_suspend(struct platform_device *pdev, pm_message_t state) 428static int bfin_rtc_suspend(struct platform_device *pdev, pm_message_t state)
424{ 429{
425 if (device_may_wakeup(&pdev->dev)) { 430 struct device *dev = &pdev->dev;
431
432 dev_dbg_stamp(dev);
433
434 if (device_may_wakeup(dev)) {
426 enable_irq_wake(IRQ_RTC); 435 enable_irq_wake(IRQ_RTC);
427 bfin_rtc_sync_pending(&pdev->dev); 436 bfin_rtc_sync_pending(dev);
428 } else 437 } else
429 bfin_rtc_int_clear(-1); 438 bfin_rtc_int_clear(0);
430 439
431 return 0; 440 return 0;
432} 441}
433 442
434static int bfin_rtc_resume(struct platform_device *pdev) 443static int bfin_rtc_resume(struct platform_device *pdev)
435{ 444{
436 if (device_may_wakeup(&pdev->dev)) 445 struct device *dev = &pdev->dev;
446
447 dev_dbg_stamp(dev);
448
449 if (device_may_wakeup(dev))
437 disable_irq_wake(IRQ_RTC); 450 disable_irq_wake(IRQ_RTC);
438 else 451
439 bfin_write_RTC_ISTAT(-1); 452 /*
453 * Since only some of the RTC bits are maintained externally in the
454 * Vbat domain, we need to wait for the RTC MMRs to be synced into
455 * the core after waking up. This happens every RTC 1HZ. Once that
456 * has happened, we can go ahead and re-enable the important write
457 * complete interrupt event.
458 */
459 while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_SEC))
460 continue;
461 bfin_rtc_int_set(RTC_ISTAT_WRITE_COMPLETE);
440 462
441 return 0; 463 return 0;
442} 464}
diff --git a/drivers/rtc/rtc-ds1302.c b/drivers/rtc/rtc-ds1302.c
index 359d1e04626c..f0d638922644 100644
--- a/drivers/rtc/rtc-ds1302.c
+++ b/drivers/rtc/rtc-ds1302.c
@@ -35,7 +35,7 @@
35 35
36#ifdef CONFIG_SH_SECUREEDGE5410 36#ifdef CONFIG_SH_SECUREEDGE5410
37#include <asm/rtc.h> 37#include <asm/rtc.h>
38#include <mach/snapgear.h> 38#include <mach/secureedge5410.h>
39 39
40#define RTC_RESET 0x1000 40#define RTC_RESET 0x1000
41#define RTC_IODATA 0x0800 41#define RTC_IODATA 0x0800
diff --git a/drivers/rtc/rtc-ds1305.c b/drivers/rtc/rtc-ds1305.c
index 48da85e97ca4..077af1d7b9e4 100644
--- a/drivers/rtc/rtc-ds1305.c
+++ b/drivers/rtc/rtc-ds1305.c
@@ -813,7 +813,7 @@ static int __devexit ds1305_remove(struct spi_device *spi)
813 if (spi->irq) { 813 if (spi->irq) {
814 set_bit(FLAG_EXITING, &ds1305->flags); 814 set_bit(FLAG_EXITING, &ds1305->flags);
815 free_irq(spi->irq, ds1305); 815 free_irq(spi->irq, ds1305);
816 flush_scheduled_work(); 816 cancel_work_sync(&ds1305->work);
817 } 817 }
818 818
819 rtc_device_unregister(ds1305->rtc); 819 rtc_device_unregister(ds1305->rtc);
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index d827ce570a8c..0d559b6416dd 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -106,9 +106,9 @@ struct ds1307 {
106 struct i2c_client *client; 106 struct i2c_client *client;
107 struct rtc_device *rtc; 107 struct rtc_device *rtc;
108 struct work_struct work; 108 struct work_struct work;
109 s32 (*read_block_data)(struct i2c_client *client, u8 command, 109 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
110 u8 length, u8 *values); 110 u8 length, u8 *values);
111 s32 (*write_block_data)(struct i2c_client *client, u8 command, 111 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
112 u8 length, const u8 *values); 112 u8 length, const u8 *values);
113}; 113};
114 114
@@ -158,8 +158,8 @@ MODULE_DEVICE_TABLE(i2c, ds1307_id);
158 158
159#define BLOCK_DATA_MAX_TRIES 10 159#define BLOCK_DATA_MAX_TRIES 10
160 160
161static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command, 161static s32 ds1307_read_block_data_once(const struct i2c_client *client,
162 u8 length, u8 *values) 162 u8 command, u8 length, u8 *values)
163{ 163{
164 s32 i, data; 164 s32 i, data;
165 165
@@ -172,7 +172,7 @@ static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command,
172 return i; 172 return i;
173} 173}
174 174
175static s32 ds1307_read_block_data(struct i2c_client *client, u8 command, 175static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
176 u8 length, u8 *values) 176 u8 length, u8 *values)
177{ 177{
178 u8 oldvalues[I2C_SMBUS_BLOCK_MAX]; 178 u8 oldvalues[I2C_SMBUS_BLOCK_MAX];
@@ -198,7 +198,7 @@ static s32 ds1307_read_block_data(struct i2c_client *client, u8 command,
198 return length; 198 return length;
199} 199}
200 200
201static s32 ds1307_write_block_data(struct i2c_client *client, u8 command, 201static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
202 u8 length, const u8 *values) 202 u8 length, const u8 *values)
203{ 203{
204 u8 currvalues[I2C_SMBUS_BLOCK_MAX]; 204 u8 currvalues[I2C_SMBUS_BLOCK_MAX];
diff --git a/drivers/rtc/rtc-ds1374.c b/drivers/rtc/rtc-ds1374.c
index 1f0007fd4431..47fb6357c346 100644
--- a/drivers/rtc/rtc-ds1374.c
+++ b/drivers/rtc/rtc-ds1374.c
@@ -417,7 +417,7 @@ static int __devexit ds1374_remove(struct i2c_client *client)
417 mutex_unlock(&ds1374->mutex); 417 mutex_unlock(&ds1374->mutex);
418 418
419 free_irq(client->irq, client); 419 free_irq(client->irq, client);
420 flush_scheduled_work(); 420 cancel_work_sync(&ds1374->work);
421 } 421 }
422 422
423 rtc_device_unregister(ds1374->rtc); 423 rtc_device_unregister(ds1374->rtc);
diff --git a/drivers/rtc/rtc-ds3232.c b/drivers/rtc/rtc-ds3232.c
index 9daed8db83d3..23a9ee19764c 100644
--- a/drivers/rtc/rtc-ds3232.c
+++ b/drivers/rtc/rtc-ds3232.c
@@ -2,6 +2,7 @@
2 * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C 2 * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
3 * 3 *
4 * Copyright (C) 2009-2010 Freescale Semiconductor. 4 * Copyright (C) 2009-2010 Freescale Semiconductor.
5 * Author: Jack Lan <jack.lan@freescale.com>
5 * 6 *
6 * This program is free software; you can redistribute it and/or modify it 7 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the 8 * under the terms of the GNU General Public License as published by the
@@ -175,6 +176,182 @@ static int ds3232_set_time(struct device *dev, struct rtc_time *time)
175 DS3232_REG_SECONDS, 7, buf); 176 DS3232_REG_SECONDS, 7, buf);
176} 177}
177 178
179/*
180 * DS3232 has two alarm, we only use alarm1
181 * According to linux specification, only support one-shot alarm
182 * no periodic alarm mode
183 */
184static int ds3232_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
185{
186 struct i2c_client *client = to_i2c_client(dev);
187 struct ds3232 *ds3232 = i2c_get_clientdata(client);
188 int control, stat;
189 int ret;
190 u8 buf[4];
191
192 mutex_lock(&ds3232->mutex);
193
194 ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
195 if (ret < 0)
196 goto out;
197 stat = ret;
198 ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
199 if (ret < 0)
200 goto out;
201 control = ret;
202 ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
203 if (ret < 0)
204 goto out;
205
206 alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
207 alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
208 alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
209 alarm->time.tm_mday = bcd2bin(buf[3] & 0x7F);
210
211 alarm->time.tm_mon = -1;
212 alarm->time.tm_year = -1;
213 alarm->time.tm_wday = -1;
214 alarm->time.tm_yday = -1;
215 alarm->time.tm_isdst = -1;
216
217 alarm->enabled = !!(control & DS3232_REG_CR_A1IE);
218 alarm->pending = !!(stat & DS3232_REG_SR_A1F);
219
220 ret = 0;
221out:
222 mutex_unlock(&ds3232->mutex);
223 return ret;
224}
225
226/*
227 * linux rtc-module does not support wday alarm
228 * and only 24h time mode supported indeed
229 */
230static int ds3232_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
231{
232 struct i2c_client *client = to_i2c_client(dev);
233 struct ds3232 *ds3232 = i2c_get_clientdata(client);
234 int control, stat;
235 int ret;
236 u8 buf[4];
237
238 if (client->irq <= 0)
239 return -EINVAL;
240
241 mutex_lock(&ds3232->mutex);
242
243 buf[0] = bin2bcd(alarm->time.tm_sec);
244 buf[1] = bin2bcd(alarm->time.tm_min);
245 buf[2] = bin2bcd(alarm->time.tm_hour);
246 buf[3] = bin2bcd(alarm->time.tm_mday);
247
248 /* clear alarm interrupt enable bit */
249 ret = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
250 if (ret < 0)
251 goto out;
252 control = ret;
253 control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
254 ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
255 if (ret < 0)
256 goto out;
257
258 /* clear any pending alarm flag */
259 ret = i2c_smbus_read_byte_data(client, DS3232_REG_SR);
260 if (ret < 0)
261 goto out;
262 stat = ret;
263 stat &= ~(DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
264 ret = i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat);
265 if (ret < 0)
266 goto out;
267
268 ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
269
270 if (alarm->enabled) {
271 control |= DS3232_REG_CR_A1IE;
272 ret = i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
273 }
274out:
275 mutex_unlock(&ds3232->mutex);
276 return ret;
277}
278
279static void ds3232_update_alarm(struct i2c_client *client)
280{
281 struct ds3232 *ds3232 = i2c_get_clientdata(client);
282 int control;
283 int ret;
284 u8 buf[4];
285
286 mutex_lock(&ds3232->mutex);
287
288 ret = i2c_smbus_read_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
289 if (ret < 0)
290 goto unlock;
291
292 buf[0] = bcd2bin(buf[0]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
293 0x80 : buf[0];
294 buf[1] = bcd2bin(buf[1]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
295 0x80 : buf[1];
296 buf[2] = bcd2bin(buf[2]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
297 0x80 : buf[2];
298 buf[3] = bcd2bin(buf[3]) < 0 || (ds3232->rtc->irq_data & RTC_UF) ?
299 0x80 : buf[3];
300
301 ret = i2c_smbus_write_i2c_block_data(client, DS3232_REG_ALARM1, 4, buf);
302 if (ret < 0)
303 goto unlock;
304
305 control = i2c_smbus_read_byte_data(client, DS3232_REG_CR);
306 if (control < 0)
307 goto unlock;
308
309 if (ds3232->rtc->irq_data & (RTC_AF | RTC_UF))
310 /* enable alarm1 interrupt */
311 control |= DS3232_REG_CR_A1IE;
312 else
313 /* disable alarm1 interrupt */
314 control &= ~(DS3232_REG_CR_A1IE);
315 i2c_smbus_write_byte_data(client, DS3232_REG_CR, control);
316
317unlock:
318 mutex_unlock(&ds3232->mutex);
319}
320
321static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled)
322{
323 struct i2c_client *client = to_i2c_client(dev);
324 struct ds3232 *ds3232 = i2c_get_clientdata(client);
325
326 if (client->irq <= 0)
327 return -EINVAL;
328
329 if (enabled)
330 ds3232->rtc->irq_data |= RTC_AF;
331 else
332 ds3232->rtc->irq_data &= ~RTC_AF;
333
334 ds3232_update_alarm(client);
335 return 0;
336}
337
338static int ds3232_update_irq_enable(struct device *dev, unsigned int enabled)
339{
340 struct i2c_client *client = to_i2c_client(dev);
341 struct ds3232 *ds3232 = i2c_get_clientdata(client);
342
343 if (client->irq <= 0)
344 return -EINVAL;
345
346 if (enabled)
347 ds3232->rtc->irq_data |= RTC_UF;
348 else
349 ds3232->rtc->irq_data &= ~RTC_UF;
350
351 ds3232_update_alarm(client);
352 return 0;
353}
354
178static irqreturn_t ds3232_irq(int irq, void *dev_id) 355static irqreturn_t ds3232_irq(int irq, void *dev_id)
179{ 356{
180 struct i2c_client *client = dev_id; 357 struct i2c_client *client = dev_id;
@@ -222,6 +399,10 @@ unlock:
222static const struct rtc_class_ops ds3232_rtc_ops = { 399static const struct rtc_class_ops ds3232_rtc_ops = {
223 .read_time = ds3232_read_time, 400 .read_time = ds3232_read_time,
224 .set_time = ds3232_set_time, 401 .set_time = ds3232_set_time,
402 .read_alarm = ds3232_read_alarm,
403 .set_alarm = ds3232_set_alarm,
404 .alarm_irq_enable = ds3232_alarm_irq_enable,
405 .update_irq_enable = ds3232_update_irq_enable,
225}; 406};
226 407
227static int __devinit ds3232_probe(struct i2c_client *client, 408static int __devinit ds3232_probe(struct i2c_client *client,
@@ -268,7 +449,6 @@ out_irq:
268 free_irq(client->irq, client); 449 free_irq(client->irq, client);
269 450
270out_free: 451out_free:
271 i2c_set_clientdata(client, NULL);
272 kfree(ds3232); 452 kfree(ds3232);
273 return ret; 453 return ret;
274} 454}
@@ -283,11 +463,10 @@ static int __devexit ds3232_remove(struct i2c_client *client)
283 mutex_unlock(&ds3232->mutex); 463 mutex_unlock(&ds3232->mutex);
284 464
285 free_irq(client->irq, client); 465 free_irq(client->irq, client);
286 flush_scheduled_work(); 466 cancel_work_sync(&ds3232->work);
287 } 467 }
288 468
289 rtc_device_unregister(ds3232->rtc); 469 rtc_device_unregister(ds3232->rtc);
290 i2c_set_clientdata(client, NULL);
291 kfree(ds3232); 470 kfree(ds3232);
292 return 0; 471 return 0;
293} 472}
diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c
index 2619d57b91d7..2e16f72c9056 100644
--- a/drivers/rtc/rtc-jz4740.c
+++ b/drivers/rtc/rtc-jz4740.c
@@ -1,5 +1,6 @@
1/* 1/*
2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de> 2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
3 * JZ4740 SoC RTC driver 4 * JZ4740 SoC RTC driver
4 * 5 *
5 * This program is free software; you can redistribute it and/or modify it 6 * This program is free software; you can redistribute it and/or modify it
@@ -161,7 +162,8 @@ static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
161 162
162 ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs); 163 ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs);
163 if (!ret) 164 if (!ret)
164 ret = jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AE, alrm->enabled); 165 ret = jz4740_rtc_ctrl_set_bits(rtc,
166 JZ_RTC_CTRL_AE | JZ_RTC_CTRL_AF_IRQ, alrm->enabled);
165 167
166 return ret; 168 return ret;
167} 169}
@@ -258,6 +260,8 @@ static int __devinit jz4740_rtc_probe(struct platform_device *pdev)
258 260
259 platform_set_drvdata(pdev, rtc); 261 platform_set_drvdata(pdev, rtc);
260 262
263 device_init_wakeup(&pdev->dev, 1);
264
261 rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &jz4740_rtc_ops, 265 rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &jz4740_rtc_ops,
262 THIS_MODULE); 266 THIS_MODULE);
263 if (IS_ERR(rtc->rtc)) { 267 if (IS_ERR(rtc->rtc)) {
@@ -318,12 +322,43 @@ static int __devexit jz4740_rtc_remove(struct platform_device *pdev)
318 return 0; 322 return 0;
319} 323}
320 324
325
326#ifdef CONFIG_PM
327static int jz4740_rtc_suspend(struct device *dev)
328{
329 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
330
331 if (device_may_wakeup(dev))
332 enable_irq_wake(rtc->irq);
333 return 0;
334}
335
336static int jz4740_rtc_resume(struct device *dev)
337{
338 struct jz4740_rtc *rtc = dev_get_drvdata(dev);
339
340 if (device_may_wakeup(dev))
341 disable_irq_wake(rtc->irq);
342 return 0;
343}
344
345static const struct dev_pm_ops jz4740_pm_ops = {
346 .suspend = jz4740_rtc_suspend,
347 .resume = jz4740_rtc_resume,
348};
349#define JZ4740_RTC_PM_OPS (&jz4740_pm_ops)
350
351#else
352#define JZ4740_RTC_PM_OPS NULL
353#endif /* CONFIG_PM */
354
321struct platform_driver jz4740_rtc_driver = { 355struct platform_driver jz4740_rtc_driver = {
322 .probe = jz4740_rtc_probe, 356 .probe = jz4740_rtc_probe,
323 .remove = __devexit_p(jz4740_rtc_remove), 357 .remove = __devexit_p(jz4740_rtc_remove),
324 .driver = { 358 .driver = {
325 .name = "jz4740-rtc", 359 .name = "jz4740-rtc",
326 .owner = THIS_MODULE, 360 .owner = THIS_MODULE,
361 .pm = JZ4740_RTC_PM_OPS,
327 }, 362 },
328}; 363};
329 364
diff --git a/drivers/rtc/rtc-lpc32xx.c b/drivers/rtc/rtc-lpc32xx.c
new file mode 100644
index 000000000000..ec8701ce99f9
--- /dev/null
+++ b/drivers/rtc/rtc-lpc32xx.c
@@ -0,0 +1,414 @@
1/*
2 * Copyright (C) 2010 NXP Semiconductors
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * You should have received a copy of the GNU General Public License along
10 * with this program; if not, write to the Free Software Foundation, Inc.,
11 * 675 Mass Ave, Cambridge, MA 02139, USA.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/platform_device.h>
18#include <linux/spinlock.h>
19#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/io.h>
22
23/*
24 * Clock and Power control register offsets
25 */
26#define LPC32XX_RTC_UCOUNT 0x00
27#define LPC32XX_RTC_DCOUNT 0x04
28#define LPC32XX_RTC_MATCH0 0x08
29#define LPC32XX_RTC_MATCH1 0x0C
30#define LPC32XX_RTC_CTRL 0x10
31#define LPC32XX_RTC_INTSTAT 0x14
32#define LPC32XX_RTC_KEY 0x18
33#define LPC32XX_RTC_SRAM 0x80
34
35#define LPC32XX_RTC_CTRL_MATCH0 (1 << 0)
36#define LPC32XX_RTC_CTRL_MATCH1 (1 << 1)
37#define LPC32XX_RTC_CTRL_ONSW_MATCH0 (1 << 2)
38#define LPC32XX_RTC_CTRL_ONSW_MATCH1 (1 << 3)
39#define LPC32XX_RTC_CTRL_SW_RESET (1 << 4)
40#define LPC32XX_RTC_CTRL_CNTR_DIS (1 << 6)
41#define LPC32XX_RTC_CTRL_ONSW_FORCE_HI (1 << 7)
42
43#define LPC32XX_RTC_INTSTAT_MATCH0 (1 << 0)
44#define LPC32XX_RTC_INTSTAT_MATCH1 (1 << 1)
45#define LPC32XX_RTC_INTSTAT_ONSW (1 << 2)
46
47#define LPC32XX_RTC_KEY_ONSW_LOADVAL 0xB5C13F27
48
49#define RTC_NAME "rtc-lpc32xx"
50
51#define rtc_readl(dev, reg) \
52 __raw_readl((dev)->rtc_base + (reg))
53#define rtc_writel(dev, reg, val) \
54 __raw_writel((val), (dev)->rtc_base + (reg))
55
56struct lpc32xx_rtc {
57 void __iomem *rtc_base;
58 int irq;
59 unsigned char alarm_enabled;
60 struct rtc_device *rtc;
61 spinlock_t lock;
62};
63
64static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
65{
66 unsigned long elapsed_sec;
67 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
68
69 elapsed_sec = rtc_readl(rtc, LPC32XX_RTC_UCOUNT);
70 rtc_time_to_tm(elapsed_sec, time);
71
72 return rtc_valid_tm(time);
73}
74
75static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
76{
77 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
78 u32 tmp;
79
80 spin_lock_irq(&rtc->lock);
81
82 /* RTC must be disabled during count update */
83 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
84 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp | LPC32XX_RTC_CTRL_CNTR_DIS);
85 rtc_writel(rtc, LPC32XX_RTC_UCOUNT, secs);
86 rtc_writel(rtc, LPC32XX_RTC_DCOUNT, 0xFFFFFFFF - secs);
87 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp &= ~LPC32XX_RTC_CTRL_CNTR_DIS);
88
89 spin_unlock_irq(&rtc->lock);
90
91 return 0;
92}
93
94static int lpc32xx_rtc_read_alarm(struct device *dev,
95 struct rtc_wkalrm *wkalrm)
96{
97 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
98
99 rtc_time_to_tm(rtc_readl(rtc, LPC32XX_RTC_MATCH0), &wkalrm->time);
100 wkalrm->enabled = rtc->alarm_enabled;
101 wkalrm->pending = !!(rtc_readl(rtc, LPC32XX_RTC_INTSTAT) &
102 LPC32XX_RTC_INTSTAT_MATCH0);
103
104 return rtc_valid_tm(&wkalrm->time);
105}
106
107static int lpc32xx_rtc_set_alarm(struct device *dev,
108 struct rtc_wkalrm *wkalrm)
109{
110 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
111 unsigned long alarmsecs;
112 u32 tmp;
113 int ret;
114
115 ret = rtc_tm_to_time(&wkalrm->time, &alarmsecs);
116 if (ret < 0) {
117 dev_warn(dev, "Failed to convert time: %d\n", ret);
118 return ret;
119 }
120
121 spin_lock_irq(&rtc->lock);
122
123 /* Disable alarm during update */
124 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
125 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp & ~LPC32XX_RTC_CTRL_MATCH0);
126
127 rtc_writel(rtc, LPC32XX_RTC_MATCH0, alarmsecs);
128
129 rtc->alarm_enabled = wkalrm->enabled;
130 if (wkalrm->enabled) {
131 rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
132 LPC32XX_RTC_INTSTAT_MATCH0);
133 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp |
134 LPC32XX_RTC_CTRL_MATCH0);
135 }
136
137 spin_unlock_irq(&rtc->lock);
138
139 return 0;
140}
141
142static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
143 unsigned int enabled)
144{
145 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
146 u32 tmp;
147
148 spin_lock_irq(&rtc->lock);
149 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
150
151 if (enabled) {
152 rtc->alarm_enabled = 1;
153 tmp |= LPC32XX_RTC_CTRL_MATCH0;
154 } else {
155 rtc->alarm_enabled = 0;
156 tmp &= ~LPC32XX_RTC_CTRL_MATCH0;
157 }
158
159 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
160 spin_unlock_irq(&rtc->lock);
161
162 return 0;
163}
164
165static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
166{
167 struct lpc32xx_rtc *rtc = dev;
168
169 spin_lock(&rtc->lock);
170
171 /* Disable alarm interrupt */
172 rtc_writel(rtc, LPC32XX_RTC_CTRL,
173 rtc_readl(rtc, LPC32XX_RTC_CTRL) &
174 ~LPC32XX_RTC_CTRL_MATCH0);
175 rtc->alarm_enabled = 0;
176
177 /*
178 * Write a large value to the match value so the RTC won't
179 * keep firing the match status
180 */
181 rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
182 rtc_writel(rtc, LPC32XX_RTC_INTSTAT, LPC32XX_RTC_INTSTAT_MATCH0);
183
184 spin_unlock(&rtc->lock);
185
186 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
187
188 return IRQ_HANDLED;
189}
190
191static const struct rtc_class_ops lpc32xx_rtc_ops = {
192 .read_time = lpc32xx_rtc_read_time,
193 .set_mmss = lpc32xx_rtc_set_mmss,
194 .read_alarm = lpc32xx_rtc_read_alarm,
195 .set_alarm = lpc32xx_rtc_set_alarm,
196 .alarm_irq_enable = lpc32xx_rtc_alarm_irq_enable,
197};
198
199static int __devinit lpc32xx_rtc_probe(struct platform_device *pdev)
200{
201 struct resource *res;
202 struct lpc32xx_rtc *rtc;
203 resource_size_t size;
204 int rtcirq;
205 u32 tmp;
206
207 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
208 if (!res) {
209 dev_err(&pdev->dev, "Can't get memory resource\n");
210 return -ENOENT;
211 }
212
213 rtcirq = platform_get_irq(pdev, 0);
214 if (rtcirq < 0 || rtcirq >= NR_IRQS) {
215 dev_warn(&pdev->dev, "Can't get interrupt resource\n");
216 rtcirq = -1;
217 }
218
219 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
220 if (unlikely(!rtc)) {
221 dev_err(&pdev->dev, "Can't allocate memory\n");
222 return -ENOMEM;
223 }
224 rtc->irq = rtcirq;
225
226 size = resource_size(res);
227
228 if (!devm_request_mem_region(&pdev->dev, res->start, size,
229 pdev->name)) {
230 dev_err(&pdev->dev, "RTC registers are not free\n");
231 return -EBUSY;
232 }
233
234 rtc->rtc_base = devm_ioremap(&pdev->dev, res->start, size);
235 if (!rtc->rtc_base) {
236 dev_err(&pdev->dev, "Can't map memory\n");
237 return -ENOMEM;
238 }
239
240 spin_lock_init(&rtc->lock);
241
242 /*
243 * The RTC is on a seperate power domain and can keep it's state
244 * across a chip power cycle. If the RTC has never been previously
245 * setup, then set it up now for the first time.
246 */
247 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
248 if (rtc_readl(rtc, LPC32XX_RTC_KEY) != LPC32XX_RTC_KEY_ONSW_LOADVAL) {
249 tmp &= ~(LPC32XX_RTC_CTRL_SW_RESET |
250 LPC32XX_RTC_CTRL_CNTR_DIS |
251 LPC32XX_RTC_CTRL_MATCH0 |
252 LPC32XX_RTC_CTRL_MATCH1 |
253 LPC32XX_RTC_CTRL_ONSW_MATCH0 |
254 LPC32XX_RTC_CTRL_ONSW_MATCH1 |
255 LPC32XX_RTC_CTRL_ONSW_FORCE_HI);
256 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
257
258 /* Clear latched interrupt states */
259 rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
260 rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
261 LPC32XX_RTC_INTSTAT_MATCH0 |
262 LPC32XX_RTC_INTSTAT_MATCH1 |
263 LPC32XX_RTC_INTSTAT_ONSW);
264
265 /* Write key value to RTC so it won't reload on reset */
266 rtc_writel(rtc, LPC32XX_RTC_KEY,
267 LPC32XX_RTC_KEY_ONSW_LOADVAL);
268 } else {
269 rtc_writel(rtc, LPC32XX_RTC_CTRL,
270 tmp & ~LPC32XX_RTC_CTRL_MATCH0);
271 }
272
273 platform_set_drvdata(pdev, rtc);
274
275 rtc->rtc = rtc_device_register(RTC_NAME, &pdev->dev, &lpc32xx_rtc_ops,
276 THIS_MODULE);
277 if (IS_ERR(rtc->rtc)) {
278 dev_err(&pdev->dev, "Can't get RTC\n");
279 platform_set_drvdata(pdev, NULL);
280 return PTR_ERR(rtc->rtc);
281 }
282
283 /*
284 * IRQ is enabled after device registration in case alarm IRQ
285 * is pending upon suspend exit.
286 */
287 if (rtc->irq >= 0) {
288 if (devm_request_irq(&pdev->dev, rtc->irq,
289 lpc32xx_rtc_alarm_interrupt,
290 IRQF_DISABLED, pdev->name, rtc) < 0) {
291 dev_warn(&pdev->dev, "Can't request interrupt.\n");
292 rtc->irq = -1;
293 } else {
294 device_init_wakeup(&pdev->dev, 1);
295 }
296 }
297
298 return 0;
299}
300
301static int __devexit lpc32xx_rtc_remove(struct platform_device *pdev)
302{
303 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
304
305 if (rtc->irq >= 0)
306 device_init_wakeup(&pdev->dev, 0);
307
308 platform_set_drvdata(pdev, NULL);
309 rtc_device_unregister(rtc->rtc);
310
311 return 0;
312}
313
314#ifdef CONFIG_PM
315static int lpc32xx_rtc_suspend(struct device *dev)
316{
317 struct platform_device *pdev = to_platform_device(dev);
318 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
319
320 if (rtc->irq >= 0) {
321 if (device_may_wakeup(&pdev->dev))
322 enable_irq_wake(rtc->irq);
323 else
324 disable_irq_wake(rtc->irq);
325 }
326
327 return 0;
328}
329
330static int lpc32xx_rtc_resume(struct device *dev)
331{
332 struct platform_device *pdev = to_platform_device(dev);
333 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
334
335 if (rtc->irq >= 0 && device_may_wakeup(&pdev->dev))
336 disable_irq_wake(rtc->irq);
337
338 return 0;
339}
340
341/* Unconditionally disable the alarm */
342static int lpc32xx_rtc_freeze(struct device *dev)
343{
344 struct platform_device *pdev = to_platform_device(dev);
345 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
346
347 spin_lock_irq(&rtc->lock);
348
349 rtc_writel(rtc, LPC32XX_RTC_CTRL,
350 rtc_readl(rtc, LPC32XX_RTC_CTRL) &
351 ~LPC32XX_RTC_CTRL_MATCH0);
352
353 spin_unlock_irq(&rtc->lock);
354
355 return 0;
356}
357
358static int lpc32xx_rtc_thaw(struct device *dev)
359{
360 struct platform_device *pdev = to_platform_device(dev);
361 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
362
363 if (rtc->alarm_enabled) {
364 spin_lock_irq(&rtc->lock);
365
366 rtc_writel(rtc, LPC32XX_RTC_CTRL,
367 rtc_readl(rtc, LPC32XX_RTC_CTRL) |
368 LPC32XX_RTC_CTRL_MATCH0);
369
370 spin_unlock_irq(&rtc->lock);
371 }
372
373 return 0;
374}
375
376static const struct dev_pm_ops lpc32xx_rtc_pm_ops = {
377 .suspend = lpc32xx_rtc_suspend,
378 .resume = lpc32xx_rtc_resume,
379 .freeze = lpc32xx_rtc_freeze,
380 .thaw = lpc32xx_rtc_thaw,
381 .restore = lpc32xx_rtc_resume
382};
383
384#define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
385#else
386#define LPC32XX_RTC_PM_OPS NULL
387#endif
388
389static struct platform_driver lpc32xx_rtc_driver = {
390 .probe = lpc32xx_rtc_probe,
391 .remove = __devexit_p(lpc32xx_rtc_remove),
392 .driver = {
393 .name = RTC_NAME,
394 .owner = THIS_MODULE,
395 .pm = LPC32XX_RTC_PM_OPS
396 },
397};
398
399static int __init lpc32xx_rtc_init(void)
400{
401 return platform_driver_register(&lpc32xx_rtc_driver);
402}
403module_init(lpc32xx_rtc_init);
404
405static void __exit lpc32xx_rtc_exit(void)
406{
407 platform_driver_unregister(&lpc32xx_rtc_driver);
408}
409module_exit(lpc32xx_rtc_exit);
410
411MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
412MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
413MODULE_LICENSE("GPL");
414MODULE_ALIAS("platform:rtc-lpc32xx");
diff --git a/drivers/rtc/rtc-m41t80.c b/drivers/rtc/rtc-m41t80.c
index 66377f3e28b8..5a8daa358066 100644
--- a/drivers/rtc/rtc-m41t80.c
+++ b/drivers/rtc/rtc-m41t80.c
@@ -20,7 +20,7 @@
20#include <linux/module.h> 20#include <linux/module.h>
21#include <linux/rtc.h> 21#include <linux/rtc.h>
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/smp_lock.h> 23#include <linux/mutex.h>
24#include <linux/string.h> 24#include <linux/string.h>
25#ifdef CONFIG_RTC_DRV_M41T80_WDT 25#ifdef CONFIG_RTC_DRV_M41T80_WDT
26#include <linux/fs.h> 26#include <linux/fs.h>
@@ -68,6 +68,7 @@
68 68
69#define DRV_VERSION "0.05" 69#define DRV_VERSION "0.05"
70 70
71static DEFINE_MUTEX(m41t80_rtc_mutex);
71static const struct i2c_device_id m41t80_id[] = { 72static const struct i2c_device_id m41t80_id[] = {
72 { "m41t62", M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT }, 73 { "m41t62", M41T80_FEATURE_SQ | M41T80_FEATURE_SQ_ALT },
73 { "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD }, 74 { "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD },
@@ -364,7 +365,7 @@ static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
364 t->time.tm_isdst = -1; 365 t->time.tm_isdst = -1;
365 t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE); 366 t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE);
366 t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF); 367 t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF);
367 return rtc_valid_tm(t); 368 return 0;
368} 369}
369 370
370static struct rtc_class_ops m41t80_rtc_ops = { 371static struct rtc_class_ops m41t80_rtc_ops = {
@@ -677,9 +678,9 @@ static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
677{ 678{
678 int ret; 679 int ret;
679 680
680 lock_kernel(); 681 mutex_lock(&m41t80_rtc_mutex);
681 ret = wdt_ioctl(file, cmd, arg); 682 ret = wdt_ioctl(file, cmd, arg);
682 unlock_kernel(); 683 mutex_unlock(&m41t80_rtc_mutex);
683 684
684 return ret; 685 return ret;
685} 686}
@@ -693,16 +694,16 @@ static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
693static int wdt_open(struct inode *inode, struct file *file) 694static int wdt_open(struct inode *inode, struct file *file)
694{ 695{
695 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) { 696 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
696 lock_kernel(); 697 mutex_lock(&m41t80_rtc_mutex);
697 if (test_and_set_bit(0, &wdt_is_open)) { 698 if (test_and_set_bit(0, &wdt_is_open)) {
698 unlock_kernel(); 699 mutex_unlock(&m41t80_rtc_mutex);
699 return -EBUSY; 700 return -EBUSY;
700 } 701 }
701 /* 702 /*
702 * Activate 703 * Activate
703 */ 704 */
704 wdt_is_open = 1; 705 wdt_is_open = 1;
705 unlock_kernel(); 706 mutex_unlock(&m41t80_rtc_mutex);
706 return nonseekable_open(inode, file); 707 return nonseekable_open(inode, file);
707 } 708 }
708 return -ENODEV; 709 return -ENODEV;
@@ -748,6 +749,7 @@ static const struct file_operations wdt_fops = {
748 .write = wdt_write, 749 .write = wdt_write,
749 .open = wdt_open, 750 .open = wdt_open,
750 .release = wdt_release, 751 .release = wdt_release,
752 .llseek = no_llseek,
751}; 753};
752 754
753static struct miscdevice wdt_dev = { 755static struct miscdevice wdt_dev = {
diff --git a/drivers/rtc/rtc-max8998.c b/drivers/rtc/rtc-max8998.c
new file mode 100644
index 000000000000..f22dee35f330
--- /dev/null
+++ b/drivers/rtc/rtc-max8998.c
@@ -0,0 +1,300 @@
1/*
2 * RTC driver for Maxim MAX8998
3 *
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Minkyu Kang <mk7.kang@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/slab.h>
18#include <linux/bcd.h>
19#include <linux/rtc.h>
20#include <linux/platform_device.h>
21#include <linux/mfd/max8998.h>
22#include <linux/mfd/max8998-private.h>
23
24#define MAX8998_RTC_SEC 0x00
25#define MAX8998_RTC_MIN 0x01
26#define MAX8998_RTC_HOUR 0x02
27#define MAX8998_RTC_WEEKDAY 0x03
28#define MAX8998_RTC_DATE 0x04
29#define MAX8998_RTC_MONTH 0x05
30#define MAX8998_RTC_YEAR1 0x06
31#define MAX8998_RTC_YEAR2 0x07
32#define MAX8998_ALARM0_SEC 0x08
33#define MAX8998_ALARM0_MIN 0x09
34#define MAX8998_ALARM0_HOUR 0x0a
35#define MAX8998_ALARM0_WEEKDAY 0x0b
36#define MAX8998_ALARM0_DATE 0x0c
37#define MAX8998_ALARM0_MONTH 0x0d
38#define MAX8998_ALARM0_YEAR1 0x0e
39#define MAX8998_ALARM0_YEAR2 0x0f
40#define MAX8998_ALARM1_SEC 0x10
41#define MAX8998_ALARM1_MIN 0x11
42#define MAX8998_ALARM1_HOUR 0x12
43#define MAX8998_ALARM1_WEEKDAY 0x13
44#define MAX8998_ALARM1_DATE 0x14
45#define MAX8998_ALARM1_MONTH 0x15
46#define MAX8998_ALARM1_YEAR1 0x16
47#define MAX8998_ALARM1_YEAR2 0x17
48#define MAX8998_ALARM0_CONF 0x18
49#define MAX8998_ALARM1_CONF 0x19
50#define MAX8998_RTC_STATUS 0x1a
51#define MAX8998_WTSR_SMPL_CNTL 0x1b
52#define MAX8998_TEST 0x1f
53
54#define HOUR_12 (1 << 7)
55#define HOUR_PM (1 << 5)
56#define ALARM0_STATUS (1 << 1)
57#define ALARM1_STATUS (1 << 2)
58
59enum {
60 RTC_SEC = 0,
61 RTC_MIN,
62 RTC_HOUR,
63 RTC_WEEKDAY,
64 RTC_DATE,
65 RTC_MONTH,
66 RTC_YEAR1,
67 RTC_YEAR2,
68};
69
70struct max8998_rtc_info {
71 struct device *dev;
72 struct max8998_dev *max8998;
73 struct i2c_client *rtc;
74 struct rtc_device *rtc_dev;
75 int irq;
76};
77
78static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
79{
80 tm->tm_sec = bcd2bin(data[RTC_SEC]);
81 tm->tm_min = bcd2bin(data[RTC_MIN]);
82 if (data[RTC_HOUR] & HOUR_12) {
83 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
84 if (data[RTC_HOUR] & HOUR_PM)
85 tm->tm_hour += 12;
86 } else
87 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
88
89 tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
90 tm->tm_mday = bcd2bin(data[RTC_DATE]);
91 tm->tm_mon = bcd2bin(data[RTC_MONTH]);
92 tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
93 tm->tm_year -= 1900;
94}
95
96static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
97{
98 data[RTC_SEC] = bin2bcd(tm->tm_sec);
99 data[RTC_MIN] = bin2bcd(tm->tm_min);
100 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
101 data[RTC_WEEKDAY] = tm->tm_wday;
102 data[RTC_DATE] = bin2bcd(tm->tm_mday);
103 data[RTC_MONTH] = bin2bcd(tm->tm_mon);
104 data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
105 data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
106}
107
108static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
109{
110 struct max8998_rtc_info *info = dev_get_drvdata(dev);
111 u8 data[8];
112 int ret;
113
114 ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
115 if (ret < 0)
116 return ret;
117
118 max8998_data_to_tm(data, tm);
119
120 return rtc_valid_tm(tm);
121}
122
123static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
124{
125 struct max8998_rtc_info *info = dev_get_drvdata(dev);
126 u8 data[8];
127
128 max8998_tm_to_data(tm, data);
129
130 return max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
131}
132
133static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
134{
135 struct max8998_rtc_info *info = dev_get_drvdata(dev);
136 u8 data[8];
137 u8 val;
138 int ret;
139
140 ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
141 if (ret < 0)
142 return ret;
143
144 max8998_data_to_tm(data, &alrm->time);
145
146 ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
147 if (ret < 0)
148 return ret;
149
150 alrm->enabled = !!val;
151
152 ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
153 if (ret < 0)
154 return ret;
155
156 if (val & ALARM0_STATUS)
157 alrm->pending = 1;
158 else
159 alrm->pending = 0;
160
161 return 0;
162}
163
164static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
165{
166 return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
167}
168
169static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
170{
171 return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0x77);
172}
173
174static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
175{
176 struct max8998_rtc_info *info = dev_get_drvdata(dev);
177 u8 data[8];
178 int ret;
179
180 max8998_tm_to_data(&alrm->time, data);
181
182 ret = max8998_rtc_stop_alarm(info);
183 if (ret < 0)
184 return ret;
185
186 ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
187 if (ret < 0)
188 return ret;
189
190 if (alrm->enabled)
191 return max8998_rtc_start_alarm(info);
192
193 return 0;
194}
195
196static int max8998_rtc_alarm_irq_enable(struct device *dev,
197 unsigned int enabled)
198{
199 struct max8998_rtc_info *info = dev_get_drvdata(dev);
200
201 if (enabled)
202 return max8998_rtc_start_alarm(info);
203 else
204 return max8998_rtc_stop_alarm(info);
205}
206
207static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
208{
209 struct max8998_rtc_info *info = data;
210
211 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
212
213 return IRQ_HANDLED;
214}
215
216static const struct rtc_class_ops max8998_rtc_ops = {
217 .read_time = max8998_rtc_read_time,
218 .set_time = max8998_rtc_set_time,
219 .read_alarm = max8998_rtc_read_alarm,
220 .set_alarm = max8998_rtc_set_alarm,
221 .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
222};
223
224static int __devinit max8998_rtc_probe(struct platform_device *pdev)
225{
226 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
227 struct max8998_rtc_info *info;
228 int ret;
229
230 info = kzalloc(sizeof(struct max8998_rtc_info), GFP_KERNEL);
231 if (!info)
232 return -ENOMEM;
233
234 info->dev = &pdev->dev;
235 info->max8998 = max8998;
236 info->rtc = max8998->rtc;
237 info->irq = max8998->irq_base + MAX8998_IRQ_ALARM0;
238
239 info->rtc_dev = rtc_device_register("max8998-rtc", &pdev->dev,
240 &max8998_rtc_ops, THIS_MODULE);
241
242 if (IS_ERR(info->rtc_dev)) {
243 ret = PTR_ERR(info->rtc_dev);
244 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
245 goto out_rtc;
246 }
247
248 platform_set_drvdata(pdev, info);
249
250 ret = request_threaded_irq(info->irq, NULL, max8998_rtc_alarm_irq, 0,
251 "rtc-alarm0", info);
252 if (ret < 0)
253 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
254 info->irq, ret);
255
256 return 0;
257
258out_rtc:
259 kfree(info);
260 return ret;
261}
262
263static int __devexit max8998_rtc_remove(struct platform_device *pdev)
264{
265 struct max8998_rtc_info *info = platform_get_drvdata(pdev);
266
267 if (info) {
268 free_irq(info->irq, info);
269 rtc_device_unregister(info->rtc_dev);
270 kfree(info);
271 }
272
273 return 0;
274}
275
276static struct platform_driver max8998_rtc_driver = {
277 .driver = {
278 .name = "max8998-rtc",
279 .owner = THIS_MODULE,
280 },
281 .probe = max8998_rtc_probe,
282 .remove = __devexit_p(max8998_rtc_remove),
283};
284
285static int __init max8998_rtc_init(void)
286{
287 return platform_driver_register(&max8998_rtc_driver);
288}
289module_init(max8998_rtc_init);
290
291static void __exit max8998_rtc_exit(void)
292{
293 platform_driver_unregister(&max8998_rtc_driver);
294}
295module_exit(max8998_rtc_exit);
296
297MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
298MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
299MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
300MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-mc13783.c b/drivers/rtc/rtc-mc13783.c
deleted file mode 100644
index 675bfb515367..000000000000
--- a/drivers/rtc/rtc-mc13783.c
+++ /dev/null
@@ -1,428 +0,0 @@
1/*
2 * Real Time Clock driver for Freescale MC13783 PMIC
3 *
4 * (C) 2009 Sascha Hauer, Pengutronix
5 * (C) 2009 Uwe Kleine-Koenig, Pengutronix
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/mfd/mc13783.h>
13#include <linux/platform_device.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/rtc.h>
18
19#define DRIVER_NAME "mc13783-rtc"
20
21#define MC13783_RTCTOD 20
22#define MC13783_RTCTODA 21
23#define MC13783_RTCDAY 22
24#define MC13783_RTCDAYA 23
25
26struct mc13783_rtc {
27 struct rtc_device *rtc;
28 struct mc13783 *mc13783;
29 int valid;
30};
31
32static int mc13783_rtc_irq_enable_unlocked(struct device *dev,
33 unsigned int enabled, int irq)
34{
35 struct mc13783_rtc *priv = dev_get_drvdata(dev);
36 int (*func)(struct mc13783 *mc13783, int irq);
37
38 if (!priv->valid)
39 return -ENODATA;
40
41 func = enabled ? mc13783_irq_unmask : mc13783_irq_mask;
42 return func(priv->mc13783, irq);
43}
44
45static int mc13783_rtc_irq_enable(struct device *dev,
46 unsigned int enabled, int irq)
47{
48 struct mc13783_rtc *priv = dev_get_drvdata(dev);
49 int ret;
50
51 mc13783_lock(priv->mc13783);
52
53 ret = mc13783_rtc_irq_enable_unlocked(dev, enabled, irq);
54
55 mc13783_unlock(priv->mc13783);
56
57 return ret;
58}
59
60static int mc13783_rtc_read_time(struct device *dev, struct rtc_time *tm)
61{
62 struct mc13783_rtc *priv = dev_get_drvdata(dev);
63 unsigned int seconds, days1, days2;
64 unsigned long s1970;
65 int ret;
66
67 mc13783_lock(priv->mc13783);
68
69 if (!priv->valid) {
70 ret = -ENODATA;
71 goto out;
72 }
73
74 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1);
75 if (unlikely(ret))
76 goto out;
77
78 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds);
79 if (unlikely(ret))
80 goto out;
81
82 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2);
83out:
84 mc13783_unlock(priv->mc13783);
85
86 if (ret)
87 return ret;
88
89 if (days2 == days1 + 1) {
90 if (seconds >= 86400 / 2)
91 days2 = days1;
92 else
93 days1 = days2;
94 }
95
96 if (days1 != days2)
97 return -EIO;
98
99 s1970 = days1 * 86400 + seconds;
100
101 rtc_time_to_tm(s1970, tm);
102
103 return rtc_valid_tm(tm);
104}
105
106static int mc13783_rtc_set_mmss(struct device *dev, unsigned long secs)
107{
108 struct mc13783_rtc *priv = dev_get_drvdata(dev);
109 unsigned int seconds, days;
110 unsigned int alarmseconds;
111 int ret;
112
113 seconds = secs % 86400;
114 days = secs / 86400;
115
116 mc13783_lock(priv->mc13783);
117
118 /*
119 * temporarily invalidate alarm to prevent triggering it when the day is
120 * already updated while the time isn't yet.
121 */
122 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &alarmseconds);
123 if (unlikely(ret))
124 goto out;
125
126 if (alarmseconds < 86400) {
127 ret = mc13783_reg_write(priv->mc13783,
128 MC13783_RTCTODA, 0x1ffff);
129 if (unlikely(ret))
130 goto out;
131 }
132
133 /*
134 * write seconds=0 to prevent a day switch between writing days
135 * and seconds below
136 */
137 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0);
138 if (unlikely(ret))
139 goto out;
140
141 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days);
142 if (unlikely(ret))
143 goto out;
144
145 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds);
146 if (unlikely(ret))
147 goto out;
148
149 /* restore alarm */
150 if (alarmseconds < 86400) {
151 ret = mc13783_reg_write(priv->mc13783,
152 MC13783_RTCTODA, alarmseconds);
153 if (unlikely(ret))
154 goto out;
155 }
156
157 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_RTCRST);
158 if (unlikely(ret))
159 goto out;
160
161 ret = mc13783_irq_unmask(priv->mc13783, MC13783_IRQ_RTCRST);
162out:
163 priv->valid = !ret;
164
165 mc13783_unlock(priv->mc13783);
166
167 return ret;
168}
169
170static int mc13783_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
171{
172 struct mc13783_rtc *priv = dev_get_drvdata(dev);
173 unsigned seconds, days;
174 unsigned long s1970;
175 int enabled, pending;
176 int ret;
177
178 mc13783_lock(priv->mc13783);
179
180 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &seconds);
181 if (unlikely(ret))
182 goto out;
183 if (seconds >= 86400) {
184 ret = -ENODATA;
185 goto out;
186 }
187
188 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days);
189 if (unlikely(ret))
190 goto out;
191
192 ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_TODA,
193 &enabled, &pending);
194
195out:
196 mc13783_unlock(priv->mc13783);
197
198 if (ret)
199 return ret;
200
201 alarm->enabled = enabled;
202 alarm->pending = pending;
203
204 s1970 = days * 86400 + seconds;
205
206 rtc_time_to_tm(s1970, &alarm->time);
207 dev_dbg(dev, "%s: %lu\n", __func__, s1970);
208
209 return 0;
210}
211
212static int mc13783_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
213{
214 struct mc13783_rtc *priv = dev_get_drvdata(dev);
215 unsigned long s1970;
216 unsigned seconds, days;
217 int ret;
218
219 mc13783_lock(priv->mc13783);
220
221 /* disable alarm to prevent false triggering */
222 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, 0x1ffff);
223 if (unlikely(ret))
224 goto out;
225
226 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_TODA);
227 if (unlikely(ret))
228 goto out;
229
230 ret = rtc_tm_to_time(&alarm->time, &s1970);
231 if (unlikely(ret))
232 goto out;
233
234 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
235 s1970);
236
237 ret = mc13783_rtc_irq_enable_unlocked(dev, alarm->enabled,
238 MC13783_IRQ_TODA);
239 if (unlikely(ret))
240 goto out;
241
242 seconds = s1970 % 86400;
243 days = s1970 / 86400;
244
245 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAYA, days);
246 if (unlikely(ret))
247 goto out;
248
249 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, seconds);
250
251out:
252 mc13783_unlock(priv->mc13783);
253
254 return ret;
255}
256
257static irqreturn_t mc13783_rtc_alarm_handler(int irq, void *dev)
258{
259 struct mc13783_rtc *priv = dev;
260 struct mc13783 *mc13783 = priv->mc13783;
261
262 dev_dbg(&priv->rtc->dev, "Alarm\n");
263
264 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
265
266 mc13783_irq_ack(mc13783, irq);
267
268 return IRQ_HANDLED;
269}
270
271static irqreturn_t mc13783_rtc_update_handler(int irq, void *dev)
272{
273 struct mc13783_rtc *priv = dev;
274 struct mc13783 *mc13783 = priv->mc13783;
275
276 dev_dbg(&priv->rtc->dev, "1HZ\n");
277
278 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF);
279
280 mc13783_irq_ack(mc13783, irq);
281
282 return IRQ_HANDLED;
283}
284
285static int mc13783_rtc_update_irq_enable(struct device *dev,
286 unsigned int enabled)
287{
288 return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_1HZ);
289}
290
291static int mc13783_rtc_alarm_irq_enable(struct device *dev,
292 unsigned int enabled)
293{
294 return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_TODA);
295}
296
297static const struct rtc_class_ops mc13783_rtc_ops = {
298 .read_time = mc13783_rtc_read_time,
299 .set_mmss = mc13783_rtc_set_mmss,
300 .read_alarm = mc13783_rtc_read_alarm,
301 .set_alarm = mc13783_rtc_set_alarm,
302 .alarm_irq_enable = mc13783_rtc_alarm_irq_enable,
303 .update_irq_enable = mc13783_rtc_update_irq_enable,
304};
305
306static irqreturn_t mc13783_rtc_reset_handler(int irq, void *dev)
307{
308 struct mc13783_rtc *priv = dev;
309 struct mc13783 *mc13783 = priv->mc13783;
310
311 dev_dbg(&priv->rtc->dev, "RTCRST\n");
312 priv->valid = 0;
313
314 mc13783_irq_mask(mc13783, irq);
315
316 return IRQ_HANDLED;
317}
318
319static int __devinit mc13783_rtc_probe(struct platform_device *pdev)
320{
321 int ret;
322 struct mc13783_rtc *priv;
323 struct mc13783 *mc13783;
324 int rtcrst_pending;
325
326 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
327 if (!priv)
328 return -ENOMEM;
329
330 mc13783 = dev_get_drvdata(pdev->dev.parent);
331 priv->mc13783 = mc13783;
332
333 platform_set_drvdata(pdev, priv);
334
335 mc13783_lock(mc13783);
336
337 ret = mc13783_irq_request(mc13783, MC13783_IRQ_RTCRST,
338 mc13783_rtc_reset_handler, DRIVER_NAME, priv);
339 if (ret)
340 goto err_reset_irq_request;
341
342 ret = mc13783_irq_status(mc13783, MC13783_IRQ_RTCRST,
343 NULL, &rtcrst_pending);
344 if (ret)
345 goto err_reset_irq_status;
346
347 priv->valid = !rtcrst_pending;
348
349 ret = mc13783_irq_request_nounmask(mc13783, MC13783_IRQ_1HZ,
350 mc13783_rtc_update_handler, DRIVER_NAME, priv);
351 if (ret)
352 goto err_update_irq_request;
353
354 ret = mc13783_irq_request_nounmask(mc13783, MC13783_IRQ_TODA,
355 mc13783_rtc_alarm_handler, DRIVER_NAME, priv);
356 if (ret)
357 goto err_alarm_irq_request;
358
359 priv->rtc = rtc_device_register(pdev->name,
360 &pdev->dev, &mc13783_rtc_ops, THIS_MODULE);
361 if (IS_ERR(priv->rtc)) {
362 ret = PTR_ERR(priv->rtc);
363
364 mc13783_irq_free(mc13783, MC13783_IRQ_TODA, priv);
365err_alarm_irq_request:
366
367 mc13783_irq_free(mc13783, MC13783_IRQ_1HZ, priv);
368err_update_irq_request:
369
370err_reset_irq_status:
371
372 mc13783_irq_free(mc13783, MC13783_IRQ_RTCRST, priv);
373err_reset_irq_request:
374
375 platform_set_drvdata(pdev, NULL);
376 kfree(priv);
377 }
378
379 mc13783_unlock(mc13783);
380
381 return ret;
382}
383
384static int __devexit mc13783_rtc_remove(struct platform_device *pdev)
385{
386 struct mc13783_rtc *priv = platform_get_drvdata(pdev);
387
388 mc13783_lock(priv->mc13783);
389
390 rtc_device_unregister(priv->rtc);
391
392 mc13783_irq_free(priv->mc13783, MC13783_IRQ_TODA, priv);
393 mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv);
394 mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv);
395
396 mc13783_unlock(priv->mc13783);
397
398 platform_set_drvdata(pdev, NULL);
399
400 kfree(priv);
401
402 return 0;
403}
404
405static struct platform_driver mc13783_rtc_driver = {
406 .remove = __devexit_p(mc13783_rtc_remove),
407 .driver = {
408 .name = DRIVER_NAME,
409 .owner = THIS_MODULE,
410 },
411};
412
413static int __init mc13783_rtc_init(void)
414{
415 return platform_driver_probe(&mc13783_rtc_driver, &mc13783_rtc_probe);
416}
417module_init(mc13783_rtc_init);
418
419static void __exit mc13783_rtc_exit(void)
420{
421 platform_driver_unregister(&mc13783_rtc_driver);
422}
423module_exit(mc13783_rtc_exit);
424
425MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
426MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC");
427MODULE_LICENSE("GPL v2");
428MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/rtc/rtc-mc13xxx.c b/drivers/rtc/rtc-mc13xxx.c
new file mode 100644
index 000000000000..5314b153bfba
--- /dev/null
+++ b/drivers/rtc/rtc-mc13xxx.c
@@ -0,0 +1,437 @@
1/*
2 * Real Time Clock driver for Freescale MC13XXX PMIC
3 *
4 * (C) 2009 Sascha Hauer, Pengutronix
5 * (C) 2009 Uwe Kleine-Koenig, Pengutronix
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/mfd/mc13xxx.h>
13#include <linux/platform_device.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/rtc.h>
18
19#define DRIVER_NAME "mc13xxx-rtc"
20
21#define MC13XXX_RTCTOD 20
22#define MC13XXX_RTCTODA 21
23#define MC13XXX_RTCDAY 22
24#define MC13XXX_RTCDAYA 23
25
26struct mc13xxx_rtc {
27 struct rtc_device *rtc;
28 struct mc13xxx *mc13xxx;
29 int valid;
30};
31
32static int mc13xxx_rtc_irq_enable_unlocked(struct device *dev,
33 unsigned int enabled, int irq)
34{
35 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
36 int (*func)(struct mc13xxx *mc13xxx, int irq);
37
38 if (!priv->valid)
39 return -ENODATA;
40
41 func = enabled ? mc13xxx_irq_unmask : mc13xxx_irq_mask;
42 return func(priv->mc13xxx, irq);
43}
44
45static int mc13xxx_rtc_irq_enable(struct device *dev,
46 unsigned int enabled, int irq)
47{
48 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
49 int ret;
50
51 mc13xxx_lock(priv->mc13xxx);
52
53 ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, irq);
54
55 mc13xxx_unlock(priv->mc13xxx);
56
57 return ret;
58}
59
60static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
61{
62 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
63 unsigned int seconds, days1, days2;
64 unsigned long s1970;
65 int ret;
66
67 mc13xxx_lock(priv->mc13xxx);
68
69 if (!priv->valid) {
70 ret = -ENODATA;
71 goto out;
72 }
73
74 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
75 if (unlikely(ret))
76 goto out;
77
78 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
79 if (unlikely(ret))
80 goto out;
81
82 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
83out:
84 mc13xxx_unlock(priv->mc13xxx);
85
86 if (ret)
87 return ret;
88
89 if (days2 == days1 + 1) {
90 if (seconds >= 86400 / 2)
91 days2 = days1;
92 else
93 days1 = days2;
94 }
95
96 if (days1 != days2)
97 return -EIO;
98
99 s1970 = days1 * 86400 + seconds;
100
101 rtc_time_to_tm(s1970, tm);
102
103 return rtc_valid_tm(tm);
104}
105
106static int mc13xxx_rtc_set_mmss(struct device *dev, unsigned long secs)
107{
108 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
109 unsigned int seconds, days;
110 unsigned int alarmseconds;
111 int ret;
112
113 seconds = secs % 86400;
114 days = secs / 86400;
115
116 mc13xxx_lock(priv->mc13xxx);
117
118 /*
119 * temporarily invalidate alarm to prevent triggering it when the day is
120 * already updated while the time isn't yet.
121 */
122 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds);
123 if (unlikely(ret))
124 goto out;
125
126 if (alarmseconds < 86400) {
127 ret = mc13xxx_reg_write(priv->mc13xxx,
128 MC13XXX_RTCTODA, 0x1ffff);
129 if (unlikely(ret))
130 goto out;
131 }
132
133 /*
134 * write seconds=0 to prevent a day switch between writing days
135 * and seconds below
136 */
137 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
138 if (unlikely(ret))
139 goto out;
140
141 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
142 if (unlikely(ret))
143 goto out;
144
145 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
146 if (unlikely(ret))
147 goto out;
148
149 /* restore alarm */
150 if (alarmseconds < 86400) {
151 ret = mc13xxx_reg_write(priv->mc13xxx,
152 MC13XXX_RTCTODA, alarmseconds);
153 if (unlikely(ret))
154 goto out;
155 }
156
157 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
158 if (unlikely(ret))
159 goto out;
160
161 ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
162out:
163 priv->valid = !ret;
164
165 mc13xxx_unlock(priv->mc13xxx);
166
167 return ret;
168}
169
170static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
171{
172 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
173 unsigned seconds, days;
174 unsigned long s1970;
175 int enabled, pending;
176 int ret;
177
178 mc13xxx_lock(priv->mc13xxx);
179
180 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
181 if (unlikely(ret))
182 goto out;
183 if (seconds >= 86400) {
184 ret = -ENODATA;
185 goto out;
186 }
187
188 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
189 if (unlikely(ret))
190 goto out;
191
192 ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
193 &enabled, &pending);
194
195out:
196 mc13xxx_unlock(priv->mc13xxx);
197
198 if (ret)
199 return ret;
200
201 alarm->enabled = enabled;
202 alarm->pending = pending;
203
204 s1970 = days * 86400 + seconds;
205
206 rtc_time_to_tm(s1970, &alarm->time);
207 dev_dbg(dev, "%s: %lu\n", __func__, s1970);
208
209 return 0;
210}
211
212static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
213{
214 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
215 unsigned long s1970;
216 unsigned seconds, days;
217 int ret;
218
219 mc13xxx_lock(priv->mc13xxx);
220
221 /* disable alarm to prevent false triggering */
222 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
223 if (unlikely(ret))
224 goto out;
225
226 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
227 if (unlikely(ret))
228 goto out;
229
230 ret = rtc_tm_to_time(&alarm->time, &s1970);
231 if (unlikely(ret))
232 goto out;
233
234 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
235 s1970);
236
237 ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
238 MC13XXX_IRQ_TODA);
239 if (unlikely(ret))
240 goto out;
241
242 seconds = s1970 % 86400;
243 days = s1970 / 86400;
244
245 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
246 if (unlikely(ret))
247 goto out;
248
249 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
250
251out:
252 mc13xxx_unlock(priv->mc13xxx);
253
254 return ret;
255}
256
257static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev)
258{
259 struct mc13xxx_rtc *priv = dev;
260 struct mc13xxx *mc13xxx = priv->mc13xxx;
261
262 dev_dbg(&priv->rtc->dev, "Alarm\n");
263
264 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
265
266 mc13xxx_irq_ack(mc13xxx, irq);
267
268 return IRQ_HANDLED;
269}
270
271static irqreturn_t mc13xxx_rtc_update_handler(int irq, void *dev)
272{
273 struct mc13xxx_rtc *priv = dev;
274 struct mc13xxx *mc13xxx = priv->mc13xxx;
275
276 dev_dbg(&priv->rtc->dev, "1HZ\n");
277
278 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF);
279
280 mc13xxx_irq_ack(mc13xxx, irq);
281
282 return IRQ_HANDLED;
283}
284
285static int mc13xxx_rtc_update_irq_enable(struct device *dev,
286 unsigned int enabled)
287{
288 return mc13xxx_rtc_irq_enable(dev, enabled, MC13XXX_IRQ_1HZ);
289}
290
291static int mc13xxx_rtc_alarm_irq_enable(struct device *dev,
292 unsigned int enabled)
293{
294 return mc13xxx_rtc_irq_enable(dev, enabled, MC13XXX_IRQ_TODA);
295}
296
297static const struct rtc_class_ops mc13xxx_rtc_ops = {
298 .read_time = mc13xxx_rtc_read_time,
299 .set_mmss = mc13xxx_rtc_set_mmss,
300 .read_alarm = mc13xxx_rtc_read_alarm,
301 .set_alarm = mc13xxx_rtc_set_alarm,
302 .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
303 .update_irq_enable = mc13xxx_rtc_update_irq_enable,
304};
305
306static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
307{
308 struct mc13xxx_rtc *priv = dev;
309 struct mc13xxx *mc13xxx = priv->mc13xxx;
310
311 dev_dbg(&priv->rtc->dev, "RTCRST\n");
312 priv->valid = 0;
313
314 mc13xxx_irq_mask(mc13xxx, irq);
315
316 return IRQ_HANDLED;
317}
318
319static int __devinit mc13xxx_rtc_probe(struct platform_device *pdev)
320{
321 int ret;
322 struct mc13xxx_rtc *priv;
323 struct mc13xxx *mc13xxx;
324 int rtcrst_pending;
325
326 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
327 if (!priv)
328 return -ENOMEM;
329
330 mc13xxx = dev_get_drvdata(pdev->dev.parent);
331 priv->mc13xxx = mc13xxx;
332
333 platform_set_drvdata(pdev, priv);
334
335 mc13xxx_lock(mc13xxx);
336
337 ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
338 mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
339 if (ret)
340 goto err_reset_irq_request;
341
342 ret = mc13xxx_irq_status(mc13xxx, MC13XXX_IRQ_RTCRST,
343 NULL, &rtcrst_pending);
344 if (ret)
345 goto err_reset_irq_status;
346
347 priv->valid = !rtcrst_pending;
348
349 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_1HZ,
350 mc13xxx_rtc_update_handler, DRIVER_NAME, priv);
351 if (ret)
352 goto err_update_irq_request;
353
354 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
355 mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
356 if (ret)
357 goto err_alarm_irq_request;
358
359 priv->rtc = rtc_device_register(pdev->name,
360 &pdev->dev, &mc13xxx_rtc_ops, THIS_MODULE);
361 if (IS_ERR(priv->rtc)) {
362 ret = PTR_ERR(priv->rtc);
363
364 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv);
365err_alarm_irq_request:
366
367 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_1HZ, priv);
368err_update_irq_request:
369
370err_reset_irq_status:
371
372 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
373err_reset_irq_request:
374
375 platform_set_drvdata(pdev, NULL);
376 kfree(priv);
377 }
378
379 mc13xxx_unlock(mc13xxx);
380
381 return ret;
382}
383
384static int __devexit mc13xxx_rtc_remove(struct platform_device *pdev)
385{
386 struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
387
388 mc13xxx_lock(priv->mc13xxx);
389
390 rtc_device_unregister(priv->rtc);
391
392 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
393 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_1HZ, priv);
394 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
395
396 mc13xxx_unlock(priv->mc13xxx);
397
398 platform_set_drvdata(pdev, NULL);
399
400 kfree(priv);
401
402 return 0;
403}
404
405const struct platform_device_id mc13xxx_rtc_idtable[] = {
406 {
407 .name = "mc13783-rtc",
408 }, {
409 .name = "mc13892-rtc",
410 },
411};
412
413static struct platform_driver mc13xxx_rtc_driver = {
414 .id_table = mc13xxx_rtc_idtable,
415 .remove = __devexit_p(mc13xxx_rtc_remove),
416 .driver = {
417 .name = DRIVER_NAME,
418 .owner = THIS_MODULE,
419 },
420};
421
422static int __init mc13xxx_rtc_init(void)
423{
424 return platform_driver_probe(&mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
425}
426module_init(mc13xxx_rtc_init);
427
428static void __exit mc13xxx_rtc_exit(void)
429{
430 platform_driver_unregister(&mc13xxx_rtc_driver);
431}
432module_exit(mc13xxx_rtc_exit);
433
434MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
435MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
436MODULE_LICENSE("GPL v2");
437MODULE_ALIAS("platform:" DRIVER_NAME);
diff --git a/drivers/rtc/rtc-mrst.c b/drivers/rtc/rtc-mrst.c
new file mode 100644
index 000000000000..bcd0cf63eb16
--- /dev/null
+++ b/drivers/rtc/rtc-mrst.c
@@ -0,0 +1,582 @@
1/*
2 * rtc-mrst.c: Driver for Moorestown virtual RTC
3 *
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Jacob Pan (jacob.jun.pan@intel.com)
6 * Feng Tang (feng.tang@intel.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
11 * of the License.
12 *
13 * Note:
14 * VRTC is emulated by system controller firmware, the real HW
15 * RTC is located in the PMIC device. SCU FW shadows PMIC RTC
16 * in a memory mapped IO space that is visible to the host IA
17 * processor.
18 *
19 * This driver is based upon drivers/rtc/rtc-cmos.c
20 */
21
22/*
23 * Note:
24 * * vRTC only supports binary mode and 24H mode
25 * * vRTC only support PIE and AIE, no UIE, and its PIE only happens
26 * at 23:59:59pm everyday, no support for adjustable frequency
27 * * Alarm function is also limited to hr/min/sec.
28 */
29
30#include <linux/mod_devicetable.h>
31#include <linux/platform_device.h>
32#include <linux/interrupt.h>
33#include <linux/spinlock.h>
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/sfi.h>
38
39#include <asm-generic/rtc.h>
40#include <asm/intel_scu_ipc.h>
41#include <asm/mrst.h>
42#include <asm/mrst-vrtc.h>
43
44struct mrst_rtc {
45 struct rtc_device *rtc;
46 struct device *dev;
47 int irq;
48 struct resource *iomem;
49
50 u8 enabled_wake;
51 u8 suspend_ctrl;
52};
53
54static const char driver_name[] = "rtc_mrst";
55
56#define RTC_IRQMASK (RTC_PF | RTC_AF)
57
58static inline int is_intr(u8 rtc_intr)
59{
60 if (!(rtc_intr & RTC_IRQF))
61 return 0;
62 return rtc_intr & RTC_IRQMASK;
63}
64
65/*
66 * rtc_time's year contains the increment over 1900, but vRTC's YEAR
67 * register can't be programmed to value larger than 0x64, so vRTC
68 * driver chose to use 1960 (1970 is UNIX time start point) as the base,
69 * and does the translation at read/write time.
70 *
71 * Why not just use 1970 as the offset? it's because using 1960 will
72 * make it consistent in leap year setting for both vrtc and low-level
73 * physical rtc devices.
74 */
75static int mrst_read_time(struct device *dev, struct rtc_time *time)
76{
77 unsigned long flags;
78
79 if (rtc_is_updating())
80 mdelay(20);
81
82 spin_lock_irqsave(&rtc_lock, flags);
83 time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
84 time->tm_min = vrtc_cmos_read(RTC_MINUTES);
85 time->tm_hour = vrtc_cmos_read(RTC_HOURS);
86 time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
87 time->tm_mon = vrtc_cmos_read(RTC_MONTH);
88 time->tm_year = vrtc_cmos_read(RTC_YEAR);
89 spin_unlock_irqrestore(&rtc_lock, flags);
90
91 /* Adjust for the 1960/1900 */
92 time->tm_year += 60;
93 time->tm_mon--;
94 return RTC_24H;
95}
96
97static int mrst_set_time(struct device *dev, struct rtc_time *time)
98{
99 int ret;
100 unsigned long flags;
101 unsigned char mon, day, hrs, min, sec;
102 unsigned int yrs;
103
104 yrs = time->tm_year;
105 mon = time->tm_mon + 1; /* tm_mon starts at zero */
106 day = time->tm_mday;
107 hrs = time->tm_hour;
108 min = time->tm_min;
109 sec = time->tm_sec;
110
111 if (yrs < 70 || yrs > 138)
112 return -EINVAL;
113 yrs -= 60;
114
115 spin_lock_irqsave(&rtc_lock, flags);
116
117 vrtc_cmos_write(yrs, RTC_YEAR);
118 vrtc_cmos_write(mon, RTC_MONTH);
119 vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
120 vrtc_cmos_write(hrs, RTC_HOURS);
121 vrtc_cmos_write(min, RTC_MINUTES);
122 vrtc_cmos_write(sec, RTC_SECONDS);
123
124 spin_unlock_irqrestore(&rtc_lock, flags);
125
126 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
127 return ret;
128}
129
130static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
131{
132 struct mrst_rtc *mrst = dev_get_drvdata(dev);
133 unsigned char rtc_control;
134
135 if (mrst->irq <= 0)
136 return -EIO;
137
138 /* Basic alarms only support hour, minute, and seconds fields.
139 * Some also support day and month, for alarms up to a year in
140 * the future.
141 */
142 t->time.tm_mday = -1;
143 t->time.tm_mon = -1;
144 t->time.tm_year = -1;
145
146 /* vRTC only supports binary mode */
147 spin_lock_irq(&rtc_lock);
148 t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
149 t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
150 t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
151
152 rtc_control = vrtc_cmos_read(RTC_CONTROL);
153 spin_unlock_irq(&rtc_lock);
154
155 t->enabled = !!(rtc_control & RTC_AIE);
156 t->pending = 0;
157
158 return 0;
159}
160
161static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
162{
163 unsigned char rtc_intr;
164
165 /*
166 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
167 * allegedly some older rtcs need that to handle irqs properly
168 */
169 rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
170 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
171 if (is_intr(rtc_intr))
172 rtc_update_irq(mrst->rtc, 1, rtc_intr);
173}
174
175static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
176{
177 unsigned char rtc_control;
178
179 /*
180 * Flush any pending IRQ status, notably for update irqs,
181 * before we enable new IRQs
182 */
183 rtc_control = vrtc_cmos_read(RTC_CONTROL);
184 mrst_checkintr(mrst, rtc_control);
185
186 rtc_control |= mask;
187 vrtc_cmos_write(rtc_control, RTC_CONTROL);
188
189 mrst_checkintr(mrst, rtc_control);
190}
191
192static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
193{
194 unsigned char rtc_control;
195
196 rtc_control = vrtc_cmos_read(RTC_CONTROL);
197 rtc_control &= ~mask;
198 vrtc_cmos_write(rtc_control, RTC_CONTROL);
199 mrst_checkintr(mrst, rtc_control);
200}
201
202static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
203{
204 struct mrst_rtc *mrst = dev_get_drvdata(dev);
205 unsigned char hrs, min, sec;
206 int ret = 0;
207
208 if (!mrst->irq)
209 return -EIO;
210
211 hrs = t->time.tm_hour;
212 min = t->time.tm_min;
213 sec = t->time.tm_sec;
214
215 spin_lock_irq(&rtc_lock);
216 /* Next rtc irq must not be from previous alarm setting */
217 mrst_irq_disable(mrst, RTC_AIE);
218
219 /* Update alarm */
220 vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
221 vrtc_cmos_write(min, RTC_MINUTES_ALARM);
222 vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
223
224 spin_unlock_irq(&rtc_lock);
225
226 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
227 if (ret)
228 return ret;
229
230 spin_lock_irq(&rtc_lock);
231 if (t->enabled)
232 mrst_irq_enable(mrst, RTC_AIE);
233
234 spin_unlock_irq(&rtc_lock);
235
236 return 0;
237}
238
239static int mrst_irq_set_state(struct device *dev, int enabled)
240{
241 struct mrst_rtc *mrst = dev_get_drvdata(dev);
242 unsigned long flags;
243
244 if (!mrst->irq)
245 return -ENXIO;
246
247 spin_lock_irqsave(&rtc_lock, flags);
248
249 if (enabled)
250 mrst_irq_enable(mrst, RTC_PIE);
251 else
252 mrst_irq_disable(mrst, RTC_PIE);
253
254 spin_unlock_irqrestore(&rtc_lock, flags);
255 return 0;
256}
257
258#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
259
260/* Currently, the vRTC doesn't support UIE ON/OFF */
261static int
262mrst_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
263{
264 struct mrst_rtc *mrst = dev_get_drvdata(dev);
265 unsigned long flags;
266
267 switch (cmd) {
268 case RTC_AIE_OFF:
269 case RTC_AIE_ON:
270 if (!mrst->irq)
271 return -EINVAL;
272 break;
273 default:
274 /* PIE ON/OFF is handled by mrst_irq_set_state() */
275 return -ENOIOCTLCMD;
276 }
277
278 spin_lock_irqsave(&rtc_lock, flags);
279 switch (cmd) {
280 case RTC_AIE_OFF: /* alarm off */
281 mrst_irq_disable(mrst, RTC_AIE);
282 break;
283 case RTC_AIE_ON: /* alarm on */
284 mrst_irq_enable(mrst, RTC_AIE);
285 break;
286 }
287 spin_unlock_irqrestore(&rtc_lock, flags);
288 return 0;
289}
290
291#else
292#define mrst_rtc_ioctl NULL
293#endif
294
295#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
296
297static int mrst_procfs(struct device *dev, struct seq_file *seq)
298{
299 unsigned char rtc_control, valid;
300
301 spin_lock_irq(&rtc_lock);
302 rtc_control = vrtc_cmos_read(RTC_CONTROL);
303 valid = vrtc_cmos_read(RTC_VALID);
304 spin_unlock_irq(&rtc_lock);
305
306 return seq_printf(seq,
307 "periodic_IRQ\t: %s\n"
308 "alarm\t\t: %s\n"
309 "BCD\t\t: no\n"
310 "periodic_freq\t: daily (not adjustable)\n",
311 (rtc_control & RTC_PIE) ? "on" : "off",
312 (rtc_control & RTC_AIE) ? "on" : "off");
313}
314
315#else
316#define mrst_procfs NULL
317#endif
318
319static const struct rtc_class_ops mrst_rtc_ops = {
320 .ioctl = mrst_rtc_ioctl,
321 .read_time = mrst_read_time,
322 .set_time = mrst_set_time,
323 .read_alarm = mrst_read_alarm,
324 .set_alarm = mrst_set_alarm,
325 .proc = mrst_procfs,
326 .irq_set_state = mrst_irq_set_state,
327};
328
329static struct mrst_rtc mrst_rtc;
330
331/*
332 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
333 * Reg B, so no need for this driver to clear it
334 */
335static irqreturn_t mrst_rtc_irq(int irq, void *p)
336{
337 u8 irqstat;
338
339 spin_lock(&rtc_lock);
340 /* This read will clear all IRQ flags inside Reg C */
341 irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
342 spin_unlock(&rtc_lock);
343
344 irqstat &= RTC_IRQMASK | RTC_IRQF;
345 if (is_intr(irqstat)) {
346 rtc_update_irq(p, 1, irqstat);
347 return IRQ_HANDLED;
348 }
349 return IRQ_NONE;
350}
351
352static int __init
353vrtc_mrst_do_probe(struct device *dev, struct resource *iomem, int rtc_irq)
354{
355 int retval = 0;
356 unsigned char rtc_control;
357
358 /* There can be only one ... */
359 if (mrst_rtc.dev)
360 return -EBUSY;
361
362 if (!iomem)
363 return -ENODEV;
364
365 iomem = request_mem_region(iomem->start,
366 iomem->end + 1 - iomem->start,
367 driver_name);
368 if (!iomem) {
369 dev_dbg(dev, "i/o mem already in use.\n");
370 return -EBUSY;
371 }
372
373 mrst_rtc.irq = rtc_irq;
374 mrst_rtc.iomem = iomem;
375
376 mrst_rtc.rtc = rtc_device_register(driver_name, dev,
377 &mrst_rtc_ops, THIS_MODULE);
378 if (IS_ERR(mrst_rtc.rtc)) {
379 retval = PTR_ERR(mrst_rtc.rtc);
380 goto cleanup0;
381 }
382
383 mrst_rtc.dev = dev;
384 dev_set_drvdata(dev, &mrst_rtc);
385 rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
386
387 spin_lock_irq(&rtc_lock);
388 mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
389 rtc_control = vrtc_cmos_read(RTC_CONTROL);
390 spin_unlock_irq(&rtc_lock);
391
392 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
393 dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
394
395 if (rtc_irq) {
396 retval = request_irq(rtc_irq, mrst_rtc_irq,
397 IRQF_DISABLED, dev_name(&mrst_rtc.rtc->dev),
398 mrst_rtc.rtc);
399 if (retval < 0) {
400 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
401 rtc_irq, retval);
402 goto cleanup1;
403 }
404 }
405 dev_dbg(dev, "initialised\n");
406 return 0;
407
408cleanup1:
409 mrst_rtc.dev = NULL;
410 rtc_device_unregister(mrst_rtc.rtc);
411cleanup0:
412 release_region(iomem->start, iomem->end + 1 - iomem->start);
413 dev_err(dev, "rtc-mrst: unable to initialise\n");
414 return retval;
415}
416
417static void rtc_mrst_do_shutdown(void)
418{
419 spin_lock_irq(&rtc_lock);
420 mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
421 spin_unlock_irq(&rtc_lock);
422}
423
424static void __exit rtc_mrst_do_remove(struct device *dev)
425{
426 struct mrst_rtc *mrst = dev_get_drvdata(dev);
427 struct resource *iomem;
428
429 rtc_mrst_do_shutdown();
430
431 if (mrst->irq)
432 free_irq(mrst->irq, mrst->rtc);
433
434 rtc_device_unregister(mrst->rtc);
435 mrst->rtc = NULL;
436
437 iomem = mrst->iomem;
438 release_region(iomem->start, iomem->end + 1 - iomem->start);
439 mrst->iomem = NULL;
440
441 mrst->dev = NULL;
442 dev_set_drvdata(dev, NULL);
443}
444
445#ifdef CONFIG_PM
446static int mrst_suspend(struct device *dev, pm_message_t mesg)
447{
448 struct mrst_rtc *mrst = dev_get_drvdata(dev);
449 unsigned char tmp;
450
451 /* Only the alarm might be a wakeup event source */
452 spin_lock_irq(&rtc_lock);
453 mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
454 if (tmp & (RTC_PIE | RTC_AIE)) {
455 unsigned char mask;
456
457 if (device_may_wakeup(dev))
458 mask = RTC_IRQMASK & ~RTC_AIE;
459 else
460 mask = RTC_IRQMASK;
461 tmp &= ~mask;
462 vrtc_cmos_write(tmp, RTC_CONTROL);
463
464 mrst_checkintr(mrst, tmp);
465 }
466 spin_unlock_irq(&rtc_lock);
467
468 if (tmp & RTC_AIE) {
469 mrst->enabled_wake = 1;
470 enable_irq_wake(mrst->irq);
471 }
472
473 dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
474 (tmp & RTC_AIE) ? ", alarm may wake" : "",
475 tmp);
476
477 return 0;
478}
479
480/*
481 * We want RTC alarms to wake us from the deep power saving state
482 */
483static inline int mrst_poweroff(struct device *dev)
484{
485 return mrst_suspend(dev, PMSG_HIBERNATE);
486}
487
488static int mrst_resume(struct device *dev)
489{
490 struct mrst_rtc *mrst = dev_get_drvdata(dev);
491 unsigned char tmp = mrst->suspend_ctrl;
492
493 /* Re-enable any irqs previously active */
494 if (tmp & RTC_IRQMASK) {
495 unsigned char mask;
496
497 if (mrst->enabled_wake) {
498 disable_irq_wake(mrst->irq);
499 mrst->enabled_wake = 0;
500 }
501
502 spin_lock_irq(&rtc_lock);
503 do {
504 vrtc_cmos_write(tmp, RTC_CONTROL);
505
506 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
507 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
508 if (!is_intr(mask))
509 break;
510
511 rtc_update_irq(mrst->rtc, 1, mask);
512 tmp &= ~RTC_AIE;
513 } while (mask & RTC_AIE);
514 spin_unlock_irq(&rtc_lock);
515 }
516
517 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
518
519 return 0;
520}
521
522#else
523#define mrst_suspend NULL
524#define mrst_resume NULL
525
526static inline int mrst_poweroff(struct device *dev)
527{
528 return -ENOSYS;
529}
530
531#endif
532
533static int __init vrtc_mrst_platform_probe(struct platform_device *pdev)
534{
535 return vrtc_mrst_do_probe(&pdev->dev,
536 platform_get_resource(pdev, IORESOURCE_MEM, 0),
537 platform_get_irq(pdev, 0));
538}
539
540static int __exit vrtc_mrst_platform_remove(struct platform_device *pdev)
541{
542 rtc_mrst_do_remove(&pdev->dev);
543 return 0;
544}
545
546static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
547{
548 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
549 return;
550
551 rtc_mrst_do_shutdown();
552}
553
554MODULE_ALIAS("platform:vrtc_mrst");
555
556static struct platform_driver vrtc_mrst_platform_driver = {
557 .probe = vrtc_mrst_platform_probe,
558 .remove = __exit_p(vrtc_mrst_platform_remove),
559 .shutdown = vrtc_mrst_platform_shutdown,
560 .driver = {
561 .name = (char *) driver_name,
562 .suspend = mrst_suspend,
563 .resume = mrst_resume,
564 }
565};
566
567static int __init vrtc_mrst_init(void)
568{
569 return platform_driver_register(&vrtc_mrst_platform_driver);
570}
571
572static void __exit vrtc_mrst_exit(void)
573{
574 platform_driver_unregister(&vrtc_mrst_platform_driver);
575}
576
577module_init(vrtc_mrst_init);
578module_exit(vrtc_mrst_exit);
579
580MODULE_AUTHOR("Jacob Pan; Feng Tang");
581MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
582MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-nuc900.c b/drivers/rtc/rtc-nuc900.c
index 62de66af0a68..ddb0857e15a4 100644
--- a/drivers/rtc/rtc-nuc900.c
+++ b/drivers/rtc/rtc-nuc900.c
@@ -274,7 +274,7 @@ static int __devinit nuc900_rtc_probe(struct platform_device *pdev)
274 nuc900_rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev, 274 nuc900_rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev,
275 &nuc900_rtc_ops, THIS_MODULE); 275 &nuc900_rtc_ops, THIS_MODULE);
276 if (IS_ERR(nuc900_rtc->rtcdev)) { 276 if (IS_ERR(nuc900_rtc->rtcdev)) {
277 dev_err(&pdev->dev, "rtc device register faild\n"); 277 dev_err(&pdev->dev, "rtc device register failed\n");
278 err = PTR_ERR(nuc900_rtc->rtcdev); 278 err = PTR_ERR(nuc900_rtc->rtcdev);
279 goto fail3; 279 goto fail3;
280 } 280 }
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
index 64d9727b7229..73377b0d65da 100644
--- a/drivers/rtc/rtc-omap.c
+++ b/drivers/rtc/rtc-omap.c
@@ -34,7 +34,8 @@
34 * Board-specific wiring options include using split power mode with 34 * Board-specific wiring options include using split power mode with
35 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset), 35 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
36 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from 36 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
37 * low power modes). See the BOARD-SPECIFIC CUSTOMIZATION comment. 37 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
38 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
38 */ 39 */
39 40
40#define OMAP_RTC_BASE 0xfffb4800 41#define OMAP_RTC_BASE 0xfffb4800
@@ -401,16 +402,17 @@ static int __init omap_rtc_probe(struct platform_device *pdev)
401 402
402 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE: 403 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
403 * 404 *
404 * - Boards wired so that RTC_WAKE_INT does something, and muxed 405 * - Device wake-up capability setting should come through chip
405 * right (W13_1610_RTC_WAKE_INT is the default after chip reset), 406 * init logic. OMAP1 boards should initialize the "wakeup capable"
406 * should initialize the device wakeup flag appropriately. 407 * flag in the platform device if the board is wired right for
408 * being woken up by RTC alarm. For OMAP-L138, this capability
409 * is built into the SoC by the "Deep Sleep" capability.
407 * 410 *
408 * - Boards wired so RTC_ON_nOFF is used as the reset signal, 411 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
409 * rather than nPWRON_RESET, should forcibly enable split 412 * rather than nPWRON_RESET, should forcibly enable split
410 * power mode. (Some chip errata report that RTC_CTRL_SPLIT 413 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
411 * is write-only, and always reads as zero...) 414 * is write-only, and always reads as zero...)
412 */ 415 */
413 device_init_wakeup(&pdev->dev, 0);
414 416
415 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT) 417 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
416 pr_info("%s: split power mode\n", pdev->name); 418 pr_info("%s: split power mode\n", pdev->name);
diff --git a/drivers/rtc/rtc-pl031.c b/drivers/rtc/rtc-pl031.c
index 6c418fe7f288..b7a6690e5b35 100644
--- a/drivers/rtc/rtc-pl031.c
+++ b/drivers/rtc/rtc-pl031.c
@@ -403,7 +403,7 @@ static int pl031_probe(struct amba_device *adev, struct amba_id *id)
403 } 403 }
404 404
405 if (request_irq(adev->irq[0], pl031_interrupt, 405 if (request_irq(adev->irq[0], pl031_interrupt,
406 IRQF_DISABLED | IRQF_SHARED, "rtc-pl031", ldata)) { 406 IRQF_DISABLED, "rtc-pl031", ldata)) {
407 ret = -EIO; 407 ret = -EIO;
408 goto out_no_irq; 408 goto out_no_irq;
409 } 409 }
diff --git a/drivers/rtc/rtc-rs5c313.c b/drivers/rtc/rtc-rs5c313.c
index e6ea3f5ee1eb..e3ff179b99ca 100644
--- a/drivers/rtc/rtc-rs5c313.c
+++ b/drivers/rtc/rtc-rs5c313.c
@@ -80,21 +80,21 @@
80/* SCSPTR1 data */ 80/* SCSPTR1 data */
81unsigned char scsptr1_data; 81unsigned char scsptr1_data;
82 82
83#define RS5C313_CEENABLE ctrl_outb(RS5C313_CE_RTCCE, RS5C313_CE); 83#define RS5C313_CEENABLE __raw_writeb(RS5C313_CE_RTCCE, RS5C313_CE);
84#define RS5C313_CEDISABLE ctrl_outb(0x00, RS5C313_CE) 84#define RS5C313_CEDISABLE __raw_writeb(0x00, RS5C313_CE)
85#define RS5C313_MISCOP ctrl_outb(0x02, 0xB0000008) 85#define RS5C313_MISCOP __raw_writeb(0x02, 0xB0000008)
86 86
87static void rs5c313_init_port(void) 87static void rs5c313_init_port(void)
88{ 88{
89 /* Set SCK as I/O port and Initialize SCSPTR1 data & I/O port. */ 89 /* Set SCK as I/O port and Initialize SCSPTR1 data & I/O port. */
90 ctrl_outb(ctrl_inb(SCSMR1) & ~SCSMR1_CA, SCSMR1); 90 __raw_writeb(__raw_readb(SCSMR1) & ~SCSMR1_CA, SCSMR1);
91 ctrl_outb(ctrl_inb(SCSCR1) & ~SCSCR1_CKE, SCSCR1); 91 __raw_writeb(__raw_readb(SCSCR1) & ~SCSCR1_CKE, SCSCR1);
92 92
93 /* And Initialize SCL for RS5C313 clock */ 93 /* And Initialize SCL for RS5C313 clock */
94 scsptr1_data = ctrl_inb(SCSPTR1) | SCL; /* SCL:H */ 94 scsptr1_data = __raw_readb(SCSPTR1) | SCL; /* SCL:H */
95 ctrl_outb(scsptr1_data, SCSPTR1); 95 __raw_writeb(scsptr1_data, SCSPTR1);
96 scsptr1_data = ctrl_inb(SCSPTR1) | SCL_OEN; /* SCL output enable */ 96 scsptr1_data = __raw_readb(SCSPTR1) | SCL_OEN; /* SCL output enable */
97 ctrl_outb(scsptr1_data, SCSPTR1); 97 __raw_writeb(scsptr1_data, SCSPTR1);
98 RS5C313_CEDISABLE; /* CE:L */ 98 RS5C313_CEDISABLE; /* CE:L */
99} 99}
100 100
@@ -106,21 +106,21 @@ static void rs5c313_write_data(unsigned char data)
106 /* SDA:Write Data */ 106 /* SDA:Write Data */
107 scsptr1_data = (scsptr1_data & ~SDA) | 107 scsptr1_data = (scsptr1_data & ~SDA) |
108 ((((0x80 >> i) & data) >> (7 - i)) << 2); 108 ((((0x80 >> i) & data) >> (7 - i)) << 2);
109 ctrl_outb(scsptr1_data, SCSPTR1); 109 __raw_writeb(scsptr1_data, SCSPTR1);
110 if (i == 0) { 110 if (i == 0) {
111 scsptr1_data |= SDA_OEN; /* SDA:output enable */ 111 scsptr1_data |= SDA_OEN; /* SDA:output enable */
112 ctrl_outb(scsptr1_data, SCSPTR1); 112 __raw_writeb(scsptr1_data, SCSPTR1);
113 } 113 }
114 ndelay(700); 114 ndelay(700);
115 scsptr1_data &= ~SCL; /* SCL:L */ 115 scsptr1_data &= ~SCL; /* SCL:L */
116 ctrl_outb(scsptr1_data, SCSPTR1); 116 __raw_writeb(scsptr1_data, SCSPTR1);
117 ndelay(700); 117 ndelay(700);
118 scsptr1_data |= SCL; /* SCL:H */ 118 scsptr1_data |= SCL; /* SCL:H */
119 ctrl_outb(scsptr1_data, SCSPTR1); 119 __raw_writeb(scsptr1_data, SCSPTR1);
120 } 120 }
121 121
122 scsptr1_data &= ~SDA_OEN; /* SDA:output disable */ 122 scsptr1_data &= ~SDA_OEN; /* SDA:output disable */
123 ctrl_outb(scsptr1_data, SCSPTR1); 123 __raw_writeb(scsptr1_data, SCSPTR1);
124} 124}
125 125
126static unsigned char rs5c313_read_data(void) 126static unsigned char rs5c313_read_data(void)
@@ -131,12 +131,12 @@ static unsigned char rs5c313_read_data(void)
131 for (i = 0; i < 8; i++) { 131 for (i = 0; i < 8; i++) {
132 ndelay(700); 132 ndelay(700);
133 /* SDA:Read Data */ 133 /* SDA:Read Data */
134 data |= ((ctrl_inb(SCSPTR1) & SDA) >> 2) << (7 - i); 134 data |= ((__raw_readb(SCSPTR1) & SDA) >> 2) << (7 - i);
135 scsptr1_data &= ~SCL; /* SCL:L */ 135 scsptr1_data &= ~SCL; /* SCL:L */
136 ctrl_outb(scsptr1_data, SCSPTR1); 136 __raw_writeb(scsptr1_data, SCSPTR1);
137 ndelay(700); 137 ndelay(700);
138 scsptr1_data |= SCL; /* SCL:H */ 138 scsptr1_data |= SCL; /* SCL:H */
139 ctrl_outb(scsptr1_data, SCSPTR1); 139 __raw_writeb(scsptr1_data, SCSPTR1);
140 } 140 }
141 return data & 0x0F; 141 return data & 0x0F;
142} 142}
diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c
index 90cf0a6ff23e..dd14e202c2c8 100644
--- a/drivers/rtc/rtc-rs5c372.c
+++ b/drivers/rtc/rtc-rs5c372.c
@@ -207,7 +207,7 @@ static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
207static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm) 207static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
208{ 208{
209 struct rs5c372 *rs5c = i2c_get_clientdata(client); 209 struct rs5c372 *rs5c = i2c_get_clientdata(client);
210 unsigned char buf[8]; 210 unsigned char buf[7];
211 int addr; 211 int addr;
212 212
213 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d " 213 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
diff --git a/drivers/rtc/rtc-rx8025.c b/drivers/rtc/rtc-rx8025.c
index 1146e3522d3c..af32a62e12a8 100644
--- a/drivers/rtc/rtc-rx8025.c
+++ b/drivers/rtc/rtc-rx8025.c
@@ -650,7 +650,7 @@ static int __devexit rx8025_remove(struct i2c_client *client)
650 mutex_unlock(lock); 650 mutex_unlock(lock);
651 651
652 free_irq(client->irq, client); 652 free_irq(client->irq, client);
653 flush_scheduled_work(); 653 cancel_work_sync(&rx8025->work);
654 } 654 }
655 655
656 rx8025_sysfs_unregister(&client->dev); 656 rx8025_sysfs_unregister(&client->dev);
diff --git a/drivers/rtc/rtc-s3c.c b/drivers/rtc/rtc-s3c.c
index a0d3ec89d412..cf953ecbfca9 100644
--- a/drivers/rtc/rtc-s3c.c
+++ b/drivers/rtc/rtc-s3c.c
@@ -100,7 +100,7 @@ static int s3c_rtc_setpie(struct device *dev, int enabled)
100 spin_lock_irq(&s3c_rtc_pie_lock); 100 spin_lock_irq(&s3c_rtc_pie_lock);
101 101
102 if (s3c_rtc_cpu_type == TYPE_S3C64XX) { 102 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
103 tmp = readb(s3c_rtc_base + S3C2410_RTCCON); 103 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
104 tmp &= ~S3C64XX_RTCCON_TICEN; 104 tmp &= ~S3C64XX_RTCCON_TICEN;
105 105
106 if (enabled) 106 if (enabled)
@@ -171,8 +171,8 @@ static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
171 goto retry_get_time; 171 goto retry_get_time;
172 } 172 }
173 173
174 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n", 174 pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
175 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday, 175 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
176 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec); 176 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
177 177
178 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec); 178 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
@@ -185,7 +185,7 @@ static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
185 rtc_tm->tm_year += 100; 185 rtc_tm->tm_year += 100;
186 rtc_tm->tm_mon -= 1; 186 rtc_tm->tm_mon -= 1;
187 187
188 return 0; 188 return rtc_valid_tm(rtc_tm);
189} 189}
190 190
191static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm) 191static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
@@ -193,8 +193,8 @@ static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
193 void __iomem *base = s3c_rtc_base; 193 void __iomem *base = s3c_rtc_base;
194 int year = tm->tm_year - 100; 194 int year = tm->tm_year - 100;
195 195
196 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n", 196 pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
197 tm->tm_year, tm->tm_mon, tm->tm_mday, 197 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
198 tm->tm_hour, tm->tm_min, tm->tm_sec); 198 tm->tm_hour, tm->tm_min, tm->tm_sec);
199 199
200 /* we get around y2k by simply not supporting it */ 200 /* we get around y2k by simply not supporting it */
@@ -231,9 +231,9 @@ static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
231 231
232 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0; 232 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
233 233
234 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n", 234 pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
235 alm_en, 235 alm_en,
236 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday, 236 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
237 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec); 237 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
238 238
239 239
@@ -242,34 +242,34 @@ static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
242 if (alm_en & S3C2410_RTCALM_SECEN) 242 if (alm_en & S3C2410_RTCALM_SECEN)
243 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec); 243 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
244 else 244 else
245 alm_tm->tm_sec = 0xff; 245 alm_tm->tm_sec = -1;
246 246
247 if (alm_en & S3C2410_RTCALM_MINEN) 247 if (alm_en & S3C2410_RTCALM_MINEN)
248 alm_tm->tm_min = bcd2bin(alm_tm->tm_min); 248 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
249 else 249 else
250 alm_tm->tm_min = 0xff; 250 alm_tm->tm_min = -1;
251 251
252 if (alm_en & S3C2410_RTCALM_HOUREN) 252 if (alm_en & S3C2410_RTCALM_HOUREN)
253 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour); 253 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
254 else 254 else
255 alm_tm->tm_hour = 0xff; 255 alm_tm->tm_hour = -1;
256 256
257 if (alm_en & S3C2410_RTCALM_DAYEN) 257 if (alm_en & S3C2410_RTCALM_DAYEN)
258 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday); 258 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
259 else 259 else
260 alm_tm->tm_mday = 0xff; 260 alm_tm->tm_mday = -1;
261 261
262 if (alm_en & S3C2410_RTCALM_MONEN) { 262 if (alm_en & S3C2410_RTCALM_MONEN) {
263 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon); 263 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
264 alm_tm->tm_mon -= 1; 264 alm_tm->tm_mon -= 1;
265 } else { 265 } else {
266 alm_tm->tm_mon = 0xff; 266 alm_tm->tm_mon = -1;
267 } 267 }
268 268
269 if (alm_en & S3C2410_RTCALM_YEAREN) 269 if (alm_en & S3C2410_RTCALM_YEAREN)
270 alm_tm->tm_year = bcd2bin(alm_tm->tm_year); 270 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
271 else 271 else
272 alm_tm->tm_year = 0xffff; 272 alm_tm->tm_year = -1;
273 273
274 return 0; 274 return 0;
275} 275}
@@ -280,10 +280,10 @@ static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
280 void __iomem *base = s3c_rtc_base; 280 void __iomem *base = s3c_rtc_base;
281 unsigned int alrm_en; 281 unsigned int alrm_en;
282 282
283 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n", 283 pr_debug("s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
284 alrm->enabled, 284 alrm->enabled,
285 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff, 285 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
286 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec); 286 tm->tm_hour, tm->tm_min, tm->tm_sec);
287 287
288 288
289 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN; 289 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
@@ -310,11 +310,6 @@ static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
310 310
311 s3c_rtc_setaie(alrm->enabled); 311 s3c_rtc_setaie(alrm->enabled);
312 312
313 if (alrm->enabled)
314 enable_irq_wake(s3c_rtc_alarmno);
315 else
316 disable_irq_wake(s3c_rtc_alarmno);
317
318 return 0; 313 return 0;
319} 314}
320 315
@@ -323,7 +318,7 @@ static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
323 unsigned int ticnt; 318 unsigned int ticnt;
324 319
325 if (s3c_rtc_cpu_type == TYPE_S3C64XX) { 320 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
326 ticnt = readb(s3c_rtc_base + S3C2410_RTCCON); 321 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
327 ticnt &= S3C64XX_RTCCON_TICEN; 322 ticnt &= S3C64XX_RTCCON_TICEN;
328 } else { 323 } else {
329 ticnt = readb(s3c_rtc_base + S3C2410_TICNT); 324 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
@@ -384,7 +379,8 @@ static const struct rtc_class_ops s3c_rtcops = {
384 .set_alarm = s3c_rtc_setalarm, 379 .set_alarm = s3c_rtc_setalarm,
385 .irq_set_freq = s3c_rtc_setfreq, 380 .irq_set_freq = s3c_rtc_setfreq,
386 .irq_set_state = s3c_rtc_setpie, 381 .irq_set_state = s3c_rtc_setpie,
387 .proc = s3c_rtc_proc, 382 .proc = s3c_rtc_proc,
383 .alarm_irq_enable = s3c_rtc_setaie,
388}; 384};
389 385
390static void s3c_rtc_enable(struct platform_device *pdev, int en) 386static void s3c_rtc_enable(struct platform_device *pdev, int en)
@@ -396,11 +392,11 @@ static void s3c_rtc_enable(struct platform_device *pdev, int en)
396 return; 392 return;
397 393
398 if (!en) { 394 if (!en) {
399 tmp = readb(base + S3C2410_RTCCON); 395 tmp = readw(base + S3C2410_RTCCON);
400 if (s3c_rtc_cpu_type == TYPE_S3C64XX) 396 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
401 tmp &= ~S3C64XX_RTCCON_TICEN; 397 tmp &= ~S3C64XX_RTCCON_TICEN;
402 tmp &= ~S3C2410_RTCCON_RTCEN; 398 tmp &= ~S3C2410_RTCCON_RTCEN;
403 writeb(tmp, base + S3C2410_RTCCON); 399 writew(tmp, base + S3C2410_RTCCON);
404 400
405 if (s3c_rtc_cpu_type == TYPE_S3C2410) { 401 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
406 tmp = readb(base + S3C2410_TICNT); 402 tmp = readb(base + S3C2410_TICNT);
@@ -410,25 +406,28 @@ static void s3c_rtc_enable(struct platform_device *pdev, int en)
410 } else { 406 } else {
411 /* re-enable the device, and check it is ok */ 407 /* re-enable the device, and check it is ok */
412 408
413 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){ 409 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
414 dev_info(&pdev->dev, "rtc disabled, re-enabling\n"); 410 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
415 411
416 tmp = readb(base + S3C2410_RTCCON); 412 tmp = readw(base + S3C2410_RTCCON);
417 writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON); 413 writew(tmp | S3C2410_RTCCON_RTCEN,
414 base + S3C2410_RTCCON);
418 } 415 }
419 416
420 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){ 417 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
421 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n"); 418 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
422 419
423 tmp = readb(base + S3C2410_RTCCON); 420 tmp = readw(base + S3C2410_RTCCON);
424 writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON); 421 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
422 base + S3C2410_RTCCON);
425 } 423 }
426 424
427 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){ 425 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
428 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n"); 426 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
429 427
430 tmp = readb(base + S3C2410_RTCCON); 428 tmp = readw(base + S3C2410_RTCCON);
431 writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON); 429 writew(tmp & ~S3C2410_RTCCON_CLKRST,
430 base + S3C2410_RTCCON);
432 } 431 }
433 } 432 }
434} 433}
@@ -457,8 +456,8 @@ static int __devexit s3c_rtc_remove(struct platform_device *dev)
457static int __devinit s3c_rtc_probe(struct platform_device *pdev) 456static int __devinit s3c_rtc_probe(struct platform_device *pdev)
458{ 457{
459 struct rtc_device *rtc; 458 struct rtc_device *rtc;
459 struct rtc_time rtc_tm;
460 struct resource *res; 460 struct resource *res;
461 unsigned int tmp, i;
462 int ret; 461 int ret;
463 462
464 pr_debug("%s: probe=%p\n", __func__, pdev); 463 pr_debug("%s: probe=%p\n", __func__, pdev);
@@ -519,8 +518,8 @@ static int __devinit s3c_rtc_probe(struct platform_device *pdev)
519 518
520 s3c_rtc_enable(pdev, 1); 519 s3c_rtc_enable(pdev, 1);
521 520
522 pr_debug("s3c2410_rtc: RTCCON=%02x\n", 521 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
523 readb(s3c_rtc_base + S3C2410_RTCCON)); 522 readw(s3c_rtc_base + S3C2410_RTCCON));
524 523
525 device_init_wakeup(&pdev->dev, 1); 524 device_init_wakeup(&pdev->dev, 1);
526 525
@@ -539,11 +538,19 @@ static int __devinit s3c_rtc_probe(struct platform_device *pdev)
539 538
540 /* Check RTC Time */ 539 /* Check RTC Time */
541 540
542 for (i = S3C2410_RTCSEC; i <= S3C2410_RTCYEAR; i += 0x4) { 541 s3c_rtc_gettime(NULL, &rtc_tm);
543 tmp = readb(s3c_rtc_base + i); 542
543 if (rtc_valid_tm(&rtc_tm)) {
544 rtc_tm.tm_year = 100;
545 rtc_tm.tm_mon = 0;
546 rtc_tm.tm_mday = 1;
547 rtc_tm.tm_hour = 0;
548 rtc_tm.tm_min = 0;
549 rtc_tm.tm_sec = 0;
544 550
545 if ((tmp & 0xf) > 0x9 || ((tmp >> 4) & 0xf) > 0x9) 551 s3c_rtc_settime(NULL, &rtc_tm);
546 writeb(0, s3c_rtc_base + i); 552
553 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
547 } 554 }
548 555
549 if (s3c_rtc_cpu_type == TYPE_S3C64XX) 556 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
@@ -583,10 +590,14 @@ static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
583 /* save TICNT for anyone using periodic interrupts */ 590 /* save TICNT for anyone using periodic interrupts */
584 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT); 591 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
585 if (s3c_rtc_cpu_type == TYPE_S3C64XX) { 592 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
586 ticnt_en_save = readb(s3c_rtc_base + S3C2410_RTCCON); 593 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
587 ticnt_en_save &= S3C64XX_RTCCON_TICEN; 594 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
588 } 595 }
589 s3c_rtc_enable(pdev, 0); 596 s3c_rtc_enable(pdev, 0);
597
598 if (device_may_wakeup(&pdev->dev))
599 enable_irq_wake(s3c_rtc_alarmno);
600
590 return 0; 601 return 0;
591} 602}
592 603
@@ -597,9 +608,13 @@ static int s3c_rtc_resume(struct platform_device *pdev)
597 s3c_rtc_enable(pdev, 1); 608 s3c_rtc_enable(pdev, 1);
598 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT); 609 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
599 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) { 610 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
600 tmp = readb(s3c_rtc_base + S3C2410_RTCCON); 611 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
601 writeb(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON); 612 writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
602 } 613 }
614
615 if (device_may_wakeup(&pdev->dev))
616 disable_irq_wake(s3c_rtc_alarmno);
617
603 return 0; 618 return 0;
604} 619}
605#else 620#else
diff --git a/drivers/rtc/rtc-sa1100.c b/drivers/rtc/rtc-sa1100.c
index e4a44b641702..88ea52b8647a 100644
--- a/drivers/rtc/rtc-sa1100.c
+++ b/drivers/rtc/rtc-sa1100.c
@@ -39,10 +39,10 @@
39#include <mach/regs-ost.h> 39#include <mach/regs-ost.h>
40#endif 40#endif
41 41
42#define RTC_DEF_DIVIDER 32768 - 1 42#define RTC_DEF_DIVIDER (32768 - 1)
43#define RTC_DEF_TRIM 0 43#define RTC_DEF_TRIM 0
44 44
45static unsigned long rtc_freq = 1024; 45static const unsigned long RTC_FREQ = 1024;
46static unsigned long timer_freq; 46static unsigned long timer_freq;
47static struct rtc_time rtc_alarm; 47static struct rtc_time rtc_alarm;
48static DEFINE_SPINLOCK(sa1100_rtc_lock); 48static DEFINE_SPINLOCK(sa1100_rtc_lock);
@@ -61,7 +61,8 @@ static inline int rtc_periodic_alarm(struct rtc_time *tm)
61 * Calculate the next alarm time given the requested alarm time mask 61 * Calculate the next alarm time given the requested alarm time mask
62 * and the current time. 62 * and the current time.
63 */ 63 */
64static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm) 64static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
65 struct rtc_time *alrm)
65{ 66{
66 unsigned long next_time; 67 unsigned long next_time;
67 unsigned long now_time; 68 unsigned long now_time;
@@ -116,7 +117,23 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
116 rtsr = RTSR; 117 rtsr = RTSR;
117 /* clear interrupt sources */ 118 /* clear interrupt sources */
118 RTSR = 0; 119 RTSR = 0;
119 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2); 120 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
121 * See also the comments in sa1100_rtc_probe(). */
122 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
123 /* This is the original code, before there was the if test
124 * above. This code does not clear interrupts that were not
125 * enabled. */
126 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
127 } else {
128 /* For some reason, it is possible to enter this routine
129 * without interruptions enabled, it has been tested with
130 * several units (Bug in SA11xx chip?).
131 *
132 * This situation leads to an infinite "loop" of interrupt
133 * routine calling and as a result the processor seems to
134 * lock on its first call to open(). */
135 RTSR = RTSR_AL | RTSR_HZ;
136 }
120 137
121 /* clear alarm interrupt if it has occurred */ 138 /* clear alarm interrupt if it has occurred */
122 if (rtsr & RTSR_AL) 139 if (rtsr & RTSR_AL)
@@ -139,8 +156,58 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
139 return IRQ_HANDLED; 156 return IRQ_HANDLED;
140} 157}
141 158
159static int sa1100_irq_set_freq(struct device *dev, int freq)
160{
161 if (freq < 1 || freq > timer_freq) {
162 return -EINVAL;
163 } else {
164 struct rtc_device *rtc = (struct rtc_device *)dev;
165
166 rtc->irq_freq = freq;
167
168 return 0;
169 }
170}
171
142static int rtc_timer1_count; 172static int rtc_timer1_count;
143 173
174static int sa1100_irq_set_state(struct device *dev, int enabled)
175{
176 spin_lock_irq(&sa1100_rtc_lock);
177 if (enabled) {
178 struct rtc_device *rtc = (struct rtc_device *)dev;
179
180 OSMR1 = timer_freq / rtc->irq_freq + OSCR;
181 OIER |= OIER_E1;
182 rtc_timer1_count = 1;
183 } else {
184 OIER &= ~OIER_E1;
185 }
186 spin_unlock_irq(&sa1100_rtc_lock);
187
188 return 0;
189}
190
191static inline int sa1100_timer1_retrigger(struct rtc_device *rtc)
192{
193 unsigned long diff;
194 unsigned long period = timer_freq / rtc->irq_freq;
195
196 spin_lock_irq(&sa1100_rtc_lock);
197
198 do {
199 OSMR1 += period;
200 diff = OSMR1 - OSCR;
201 /* If OSCR > OSMR1, diff is a very large number (unsigned
202 * math). This means we have a lost interrupt. */
203 } while (diff > period);
204 OIER |= OIER_E1;
205
206 spin_unlock_irq(&sa1100_rtc_lock);
207
208 return 0;
209}
210
144static irqreturn_t timer1_interrupt(int irq, void *dev_id) 211static irqreturn_t timer1_interrupt(int irq, void *dev_id)
145{ 212{
146 struct platform_device *pdev = to_platform_device(dev_id); 213 struct platform_device *pdev = to_platform_device(dev_id);
@@ -158,7 +225,11 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id)
158 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF); 225 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
159 226
160 if (rtc_timer1_count == 1) 227 if (rtc_timer1_count == 1)
161 rtc_timer1_count = (rtc_freq * ((1 << 30) / (timer_freq >> 2))); 228 rtc_timer1_count =
229 (rtc->irq_freq * ((1 << 30) / (timer_freq >> 2)));
230
231 /* retrigger. */
232 sa1100_timer1_retrigger(rtc);
162 233
163 return IRQ_HANDLED; 234 return IRQ_HANDLED;
164} 235}
@@ -166,8 +237,10 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id)
166static int sa1100_rtc_read_callback(struct device *dev, int data) 237static int sa1100_rtc_read_callback(struct device *dev, int data)
167{ 238{
168 if (data & RTC_PF) { 239 if (data & RTC_PF) {
240 struct rtc_device *rtc = (struct rtc_device *)dev;
241
169 /* interpolate missed periods and set match for the next */ 242 /* interpolate missed periods and set match for the next */
170 unsigned long period = timer_freq / rtc_freq; 243 unsigned long period = timer_freq / rtc->irq_freq;
171 unsigned long oscr = OSCR; 244 unsigned long oscr = OSCR;
172 unsigned long osmr1 = OSMR1; 245 unsigned long osmr1 = OSMR1;
173 unsigned long missed = (oscr - osmr1)/period; 246 unsigned long missed = (oscr - osmr1)/period;
@@ -178,7 +251,7 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
178 * Here we compare (match - OSCR) 8 instead of 0 -- 251 * Here we compare (match - OSCR) 8 instead of 0 --
179 * see comment in pxa_timer_interrupt() for explanation. 252 * see comment in pxa_timer_interrupt() for explanation.
180 */ 253 */
181 while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) { 254 while ((signed long)((osmr1 = OSMR1) - OSCR) <= 8) {
182 data += 0x100; 255 data += 0x100;
183 OSSR = OSSR_M1; /* clear match on timer 1 */ 256 OSSR = OSSR_M1; /* clear match on timer 1 */
184 OSMR1 = osmr1 + period; 257 OSMR1 = osmr1 + period;
@@ -190,25 +263,29 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
190static int sa1100_rtc_open(struct device *dev) 263static int sa1100_rtc_open(struct device *dev)
191{ 264{
192 int ret; 265 int ret;
266 struct rtc_device *rtc = (struct rtc_device *)dev;
193 267
194 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED, 268 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
195 "rtc 1Hz", dev); 269 "rtc 1Hz", dev);
196 if (ret) { 270 if (ret) {
197 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz); 271 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
198 goto fail_ui; 272 goto fail_ui;
199 } 273 }
200 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED, 274 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
201 "rtc Alrm", dev); 275 "rtc Alrm", dev);
202 if (ret) { 276 if (ret) {
203 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm); 277 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
204 goto fail_ai; 278 goto fail_ai;
205 } 279 }
206 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED, 280 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
207 "rtc timer", dev); 281 "rtc timer", dev);
208 if (ret) { 282 if (ret) {
209 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1); 283 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
210 goto fail_pi; 284 goto fail_pi;
211 } 285 }
286 rtc->max_user_freq = RTC_FREQ;
287 sa1100_irq_set_freq(dev, RTC_FREQ);
288
212 return 0; 289 return 0;
213 290
214 fail_pi: 291 fail_pi:
@@ -236,7 +313,7 @@ static void sa1100_rtc_release(struct device *dev)
236static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd, 313static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
237 unsigned long arg) 314 unsigned long arg)
238{ 315{
239 switch(cmd) { 316 switch (cmd) {
240 case RTC_AIE_OFF: 317 case RTC_AIE_OFF:
241 spin_lock_irq(&sa1100_rtc_lock); 318 spin_lock_irq(&sa1100_rtc_lock);
242 RTSR &= ~RTSR_ALE; 319 RTSR &= ~RTSR_ALE;
@@ -257,25 +334,6 @@ static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
257 RTSR |= RTSR_HZE; 334 RTSR |= RTSR_HZE;
258 spin_unlock_irq(&sa1100_rtc_lock); 335 spin_unlock_irq(&sa1100_rtc_lock);
259 return 0; 336 return 0;
260 case RTC_PIE_OFF:
261 spin_lock_irq(&sa1100_rtc_lock);
262 OIER &= ~OIER_E1;
263 spin_unlock_irq(&sa1100_rtc_lock);
264 return 0;
265 case RTC_PIE_ON:
266 spin_lock_irq(&sa1100_rtc_lock);
267 OSMR1 = timer_freq / rtc_freq + OSCR;
268 OIER |= OIER_E1;
269 rtc_timer1_count = 1;
270 spin_unlock_irq(&sa1100_rtc_lock);
271 return 0;
272 case RTC_IRQP_READ:
273 return put_user(rtc_freq, (unsigned long *)arg);
274 case RTC_IRQP_SET:
275 if (arg < 1 || arg > timer_freq)
276 return -EINVAL;
277 rtc_freq = arg;
278 return 0;
279 } 337 }
280 return -ENOIOCTLCMD; 338 return -ENOIOCTLCMD;
281} 339}
@@ -327,12 +385,15 @@ static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
327 385
328static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) 386static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
329{ 387{
388 struct rtc_device *rtc = (struct rtc_device *)dev;
389
330 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR); 390 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
331 seq_printf(seq, "update_IRQ\t: %s\n", 391 seq_printf(seq, "update_IRQ\t: %s\n",
332 (RTSR & RTSR_HZE) ? "yes" : "no"); 392 (RTSR & RTSR_HZE) ? "yes" : "no");
333 seq_printf(seq, "periodic_IRQ\t: %s\n", 393 seq_printf(seq, "periodic_IRQ\t: %s\n",
334 (OIER & OIER_E1) ? "yes" : "no"); 394 (OIER & OIER_E1) ? "yes" : "no");
335 seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq); 395 seq_printf(seq, "periodic_freq\t: %d\n", rtc->irq_freq);
396 seq_printf(seq, "RTSR\t\t: 0x%08x\n", (u32)RTSR);
336 397
337 return 0; 398 return 0;
338} 399}
@@ -347,6 +408,8 @@ static const struct rtc_class_ops sa1100_rtc_ops = {
347 .read_alarm = sa1100_rtc_read_alarm, 408 .read_alarm = sa1100_rtc_read_alarm,
348 .set_alarm = sa1100_rtc_set_alarm, 409 .set_alarm = sa1100_rtc_set_alarm,
349 .proc = sa1100_rtc_proc, 410 .proc = sa1100_rtc_proc,
411 .irq_set_freq = sa1100_irq_set_freq,
412 .irq_set_state = sa1100_irq_set_state,
350}; 413};
351 414
352static int sa1100_rtc_probe(struct platform_device *pdev) 415static int sa1100_rtc_probe(struct platform_device *pdev)
@@ -364,7 +427,8 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
364 */ 427 */
365 if (RTTR == 0) { 428 if (RTTR == 0) {
366 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); 429 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
367 dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n"); 430 dev_warn(&pdev->dev, "warning: "
431 "initializing default clock divider/trim value\n");
368 /* The current RTC value probably doesn't make sense either */ 432 /* The current RTC value probably doesn't make sense either */
369 RCNR = 0; 433 RCNR = 0;
370 } 434 }
@@ -372,13 +436,42 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
372 device_init_wakeup(&pdev->dev, 1); 436 device_init_wakeup(&pdev->dev, 1);
373 437
374 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops, 438 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
375 THIS_MODULE); 439 THIS_MODULE);
376 440
377 if (IS_ERR(rtc)) 441 if (IS_ERR(rtc))
378 return PTR_ERR(rtc); 442 return PTR_ERR(rtc);
379 443
380 platform_set_drvdata(pdev, rtc); 444 platform_set_drvdata(pdev, rtc);
381 445
446 /* Set the irq_freq */
447 /*TODO: Find out who is messing with this value after we initialize
448 * it here.*/
449 rtc->irq_freq = RTC_FREQ;
450
451 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
452 * See also the comments in sa1100_rtc_interrupt().
453 *
454 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
455 * interrupt pending, even though interrupts were never enabled.
456 * In this case, this bit it must be reset before enabling
457 * interruptions to avoid a nonexistent interrupt to occur.
458 *
459 * In principle, the same problem would apply to bit 0, although it has
460 * never been observed to happen.
461 *
462 * This issue is addressed both here and in sa1100_rtc_interrupt().
463 * If the issue is not addressed here, in the times when the processor
464 * wakes up with the bit set there will be one spurious interrupt.
465 *
466 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
467 * safe side, once the condition that lead to this strange
468 * initialization is unknown and could in principle happen during
469 * normal processing.
470 *
471 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
472 * the corresponding bits in RTSR. */
473 RTSR = RTSR_AL | RTSR_HZ;
474
382 return 0; 475 return 0;
383} 476}
384 477
@@ -386,7 +479,7 @@ static int sa1100_rtc_remove(struct platform_device *pdev)
386{ 479{
387 struct rtc_device *rtc = platform_get_drvdata(pdev); 480 struct rtc_device *rtc = platform_get_drvdata(pdev);
388 481
389 if (rtc) 482 if (rtc)
390 rtc_device_unregister(rtc); 483 rtc_device_unregister(rtc);
391 484
392 return 0; 485 return 0;
diff --git a/drivers/rtc/rtc-sh.c b/drivers/rtc/rtc-sh.c
index 5efbd5990ff8..06e41ed93230 100644
--- a/drivers/rtc/rtc-sh.c
+++ b/drivers/rtc/rtc-sh.c
@@ -761,7 +761,7 @@ err_unmap:
761 clk_put(rtc->clk); 761 clk_put(rtc->clk);
762 iounmap(rtc->regbase); 762 iounmap(rtc->regbase);
763err_badmap: 763err_badmap:
764 release_resource(rtc->res); 764 release_mem_region(rtc->res->start, rtc->regsize);
765err_badres: 765err_badres:
766 kfree(rtc); 766 kfree(rtc);
767 767
@@ -786,7 +786,7 @@ static int __exit sh_rtc_remove(struct platform_device *pdev)
786 } 786 }
787 787
788 iounmap(rtc->regbase); 788 iounmap(rtc->regbase);
789 release_resource(rtc->res); 789 release_mem_region(rtc->res->start, rtc->regsize);
790 790
791 clk_disable(rtc->clk); 791 clk_disable(rtc->clk);
792 clk_put(rtc->clk); 792 clk_put(rtc->clk);