aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig147
-rw-r--r--drivers/rtc/Makefile3
-rw-r--r--drivers/rtc/rtc-ab-b5ze-s3.c1035
-rw-r--r--drivers/rtc/rtc-armada38x.c320
-rw-r--r--drivers/rtc/rtc-at91sam9.c2
-rw-r--r--drivers/rtc/rtc-ds1685.c2252
-rw-r--r--drivers/rtc/rtc-imxdi.c50
-rw-r--r--drivers/rtc/rtc-isl12022.c3
-rw-r--r--drivers/rtc/rtc-isl12057.c351
-rw-r--r--drivers/rtc/rtc-pcf2123.c10
-rw-r--r--drivers/rtc/rtc-rk808.c10
11 files changed, 4130 insertions, 53 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index f15cddfeb897..b5b5c3d485d6 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -65,7 +65,7 @@ config RTC_DEBUG
65comment "RTC interfaces" 65comment "RTC interfaces"
66 66
67config RTC_INTF_SYSFS 67config RTC_INTF_SYSFS
68 boolean "/sys/class/rtc/rtcN (sysfs)" 68 bool "/sys/class/rtc/rtcN (sysfs)"
69 depends on SYSFS 69 depends on SYSFS
70 default RTC_CLASS 70 default RTC_CLASS
71 help 71 help
@@ -75,7 +75,7 @@ config RTC_INTF_SYSFS
75 If unsure, say Y. 75 If unsure, say Y.
76 76
77config RTC_INTF_PROC 77config RTC_INTF_PROC
78 boolean "/proc/driver/rtc (procfs for rtcN)" 78 bool "/proc/driver/rtc (procfs for rtcN)"
79 depends on PROC_FS 79 depends on PROC_FS
80 default RTC_CLASS 80 default RTC_CLASS
81 help 81 help
@@ -88,7 +88,7 @@ config RTC_INTF_PROC
88 If unsure, say Y. 88 If unsure, say Y.
89 89
90config RTC_INTF_DEV 90config RTC_INTF_DEV
91 boolean "/dev/rtcN (character devices)" 91 bool "/dev/rtcN (character devices)"
92 default RTC_CLASS 92 default RTC_CLASS
93 help 93 help
94 Say yes here if you want to use your RTCs using the /dev 94 Say yes here if you want to use your RTCs using the /dev
@@ -153,6 +153,17 @@ config RTC_DRV_88PM80X
153 This driver can also be built as a module. If so, the module 153 This driver can also be built as a module. If so, the module
154 will be called rtc-88pm80x. 154 will be called rtc-88pm80x.
155 155
156config RTC_DRV_ABB5ZES3
157 depends on I2C
158 select REGMAP_I2C
159 tristate "Abracon AB-RTCMC-32.768kHz-B5ZE-S3"
160 help
161 If you say yes here you get support for the Abracon
162 AB-RTCMC-32.768kHz-B5ZE-S3 I2C RTC chip.
163
164 This driver can also be built as a module. If so, the module
165 will be called rtc-ab-b5ze-s3.
166
156config RTC_DRV_AS3722 167config RTC_DRV_AS3722
157 tristate "ams AS3722 RTC driver" 168 tristate "ams AS3722 RTC driver"
158 depends on MFD_AS3722 169 depends on MFD_AS3722
@@ -455,7 +466,7 @@ config RTC_DRV_DM355EVM
455 Supports the RTC firmware in the MSP430 on the DM355 EVM. 466 Supports the RTC firmware in the MSP430 on the DM355 EVM.
456 467
457config RTC_DRV_TWL92330 468config RTC_DRV_TWL92330
458 boolean "TI TWL92330/Menelaus" 469 bool "TI TWL92330/Menelaus"
459 depends on MENELAUS 470 depends on MENELAUS
460 help 471 help
461 If you say yes here you get support for the RTC on the 472 If you say yes here you get support for the RTC on the
@@ -790,6 +801,96 @@ config RTC_DRV_DS1553
790 This driver can also be built as a module. If so, the module 801 This driver can also be built as a module. If so, the module
791 will be called rtc-ds1553. 802 will be called rtc-ds1553.
792 803
804config RTC_DRV_DS1685_FAMILY
805 tristate "Dallas/Maxim DS1685 Family"
806 help
807 If you say yes here you get support for the Dallas/Maxim DS1685
808 family of real time chips. This family includes the DS1685/DS1687,
809 DS1689/DS1693, DS17285/DS17287, DS17485/DS17487, and
810 DS17885/DS17887 chips.
811
812 This driver can also be built as a module. If so, the module
813 will be called rtc-ds1685.
814
815choice
816 prompt "Subtype"
817 depends on RTC_DRV_DS1685_FAMILY
818 default RTC_DRV_DS1685
819
820config RTC_DRV_DS1685
821 bool "DS1685/DS1687"
822 help
823 This enables support for the Dallas/Maxim DS1685/DS1687 real time
824 clock chip.
825
826 This chip is commonly found in SGI O2 (IP32) and SGI Octane (IP30)
827 systems, as well as EPPC-405-UC modules by electronic system design
828 GmbH.
829
830config RTC_DRV_DS1689
831 bool "DS1689/DS1693"
832 help
833 This enables support for the Dallas/Maxim DS1689/DS1693 real time
834 clock chip.
835
836 This is an older RTC chip, supplanted by the DS1685/DS1687 above,
837 which supports a few minor features such as Vcc, Vbat, and Power
838 Cycle counters, plus a customer-specific, 8-byte ROM/Serial number.
839
840 It also works for the even older DS1688/DS1691 RTC chips, which are
841 virtually the same and carry the same model number. Both chips
842 have 114 bytes of user NVRAM.
843
844config RTC_DRV_DS17285
845 bool "DS17285/DS17287"
846 help
847 This enables support for the Dallas/Maxim DS17285/DS17287 real time
848 clock chip.
849
850 This chip features 2kb of extended NV-SRAM. It may possibly be
851 found in some SGI O2 systems (rare).
852
853config RTC_DRV_DS17485
854 bool "DS17485/DS17487"
855 help
856 This enables support for the Dallas/Maxim DS17485/DS17487 real time
857 clock chip.
858
859 This chip features 4kb of extended NV-SRAM.
860
861config RTC_DRV_DS17885
862 bool "DS17885/DS17887"
863 help
864 This enables support for the Dallas/Maxim DS17885/DS17887 real time
865 clock chip.
866
867 This chip features 8kb of extended NV-SRAM.
868
869endchoice
870
871config RTC_DS1685_PROC_REGS
872 bool "Display register values in /proc"
873 depends on RTC_DRV_DS1685_FAMILY && PROC_FS
874 help
875 Enable this to display a readout of all of the RTC registers in
876 /proc/drivers/rtc. Keep in mind that this can potentially lead
877 to lost interrupts, as reading Control Register C will clear
878 all pending IRQ flags.
879
880 Unless you are debugging this driver, choose N.
881
882config RTC_DS1685_SYSFS_REGS
883 bool "SysFS access to RTC register bits"
884 depends on RTC_DRV_DS1685_FAMILY && SYSFS
885 help
886 Enable this to provide access to the RTC control register bits
887 in /sys. Some of the bits are read-write, others are read-only.
888
889 Keep in mind that reading Control C's bits automatically clears
890 all pending IRQ flags - this can cause lost interrupts.
891
892 If you know that you need access to these bits, choose Y, Else N.
893
793config RTC_DRV_DS1742 894config RTC_DRV_DS1742
794 tristate "Maxim/Dallas DS1742/1743" 895 tristate "Maxim/Dallas DS1742/1743"
795 depends on HAS_IOMEM 896 depends on HAS_IOMEM
@@ -1141,34 +1242,6 @@ config RTC_DRV_AT91SAM9
1141 probably want to use the real RTC block instead of the "RTT as an 1242 probably want to use the real RTC block instead of the "RTT as an
1142 RTC" driver. 1243 RTC" driver.
1143 1244
1144config RTC_DRV_AT91SAM9_RTT
1145 int
1146 range 0 1
1147 default 0
1148 depends on RTC_DRV_AT91SAM9
1149 help
1150 This option is only relevant for legacy board support and
1151 won't be used when booting a DT board.
1152
1153 More than one RTT module is available. You can choose which
1154 one will be used as an RTC. The default of zero is normally
1155 OK to use, though some systems use that for non-RTC purposes.
1156
1157config RTC_DRV_AT91SAM9_GPBR
1158 int
1159 range 0 3
1160 default 0
1161 prompt "Backup Register Number"
1162 depends on RTC_DRV_AT91SAM9
1163 help
1164 This option is only relevant for legacy board support and
1165 won't be used when booting a DT board.
1166
1167 The RTC driver needs to use one of the General Purpose Backup
1168 Registers (GPBRs) as well as the RTT. You can choose which one
1169 will be used. The default of zero is normally OK to use, but
1170 on some systems other software needs to use that register.
1171
1172config RTC_DRV_AU1XXX 1245config RTC_DRV_AU1XXX
1173 tristate "Au1xxx Counter0 RTC support" 1246 tristate "Au1xxx Counter0 RTC support"
1174 depends on MIPS_ALCHEMY 1247 depends on MIPS_ALCHEMY
@@ -1269,6 +1342,16 @@ config RTC_DRV_MV
1269 This driver can also be built as a module. If so, the module 1342 This driver can also be built as a module. If so, the module
1270 will be called rtc-mv. 1343 will be called rtc-mv.
1271 1344
1345config RTC_DRV_ARMADA38X
1346 tristate "Armada 38x Marvell SoC RTC"
1347 depends on ARCH_MVEBU
1348 help
1349 If you say yes here you will get support for the in-chip RTC
1350 that can be found in the Armada 38x Marvell's SoC device
1351
1352 This driver can also be built as a module. If so, the module
1353 will be called armada38x-rtc.
1354
1272config RTC_DRV_PS3 1355config RTC_DRV_PS3
1273 tristate "PS3 RTC" 1356 tristate "PS3 RTC"
1274 depends on PPC_PS3 1357 depends on PPC_PS3
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index c8ef3e1e6ccd..69c87062b098 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -24,6 +24,8 @@ obj-$(CONFIG_RTC_DRV_88PM860X) += rtc-88pm860x.o
24obj-$(CONFIG_RTC_DRV_88PM80X) += rtc-88pm80x.o 24obj-$(CONFIG_RTC_DRV_88PM80X) += rtc-88pm80x.o
25obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o 25obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o
26obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o 26obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o
27obj-$(CONFIG_RTC_DRV_ABB5ZES3) += rtc-ab-b5ze-s3.o
28obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o
27obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o 29obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o
28obj-$(CONFIG_RTC_DRV_AT32AP700X)+= rtc-at32ap700x.o 30obj-$(CONFIG_RTC_DRV_AT32AP700X)+= rtc-at32ap700x.o
29obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o 31obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o
@@ -52,6 +54,7 @@ obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o
52obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o 54obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o
53obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o 55obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o
54obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o 56obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o
57obj-$(CONFIG_RTC_DRV_DS1685_FAMILY) += rtc-ds1685.o
55obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o 58obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o
56obj-$(CONFIG_RTC_DRV_DS2404) += rtc-ds2404.o 59obj-$(CONFIG_RTC_DRV_DS2404) += rtc-ds2404.o
57obj-$(CONFIG_RTC_DRV_DS3232) += rtc-ds3232.o 60obj-$(CONFIG_RTC_DRV_DS3232) += rtc-ds3232.o
diff --git a/drivers/rtc/rtc-ab-b5ze-s3.c b/drivers/rtc/rtc-ab-b5ze-s3.c
new file mode 100644
index 000000000000..cfc2ef98d393
--- /dev/null
+++ b/drivers/rtc/rtc-ab-b5ze-s3.c
@@ -0,0 +1,1035 @@
1/*
2 * rtc-ab-b5ze-s3 - Driver for Abracon AB-RTCMC-32.768Khz-B5ZE-S3
3 * I2C RTC / Alarm chip
4 *
5 * Copyright (C) 2014, Arnaud EBALARD <arno@natisbad.org>
6 *
7 * Detailed datasheet of the chip is available here:
8 *
9 * http://www.abracon.com/realtimeclock/AB-RTCMC-32.768kHz-B5ZE-S3-Application-Manual.pdf
10 *
11 * This work is based on ISL12057 driver (drivers/rtc/rtc-isl12057.c).
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/module.h>
25#include <linux/mutex.h>
26#include <linux/rtc.h>
27#include <linux/i2c.h>
28#include <linux/bcd.h>
29#include <linux/of.h>
30#include <linux/regmap.h>
31#include <linux/interrupt.h>
32
33#define DRV_NAME "rtc-ab-b5ze-s3"
34
35/* Control section */
36#define ABB5ZES3_REG_CTRL1 0x00 /* Control 1 register */
37#define ABB5ZES3_REG_CTRL1_CIE BIT(0) /* Pulse interrupt enable */
38#define ABB5ZES3_REG_CTRL1_AIE BIT(1) /* Alarm interrupt enable */
39#define ABB5ZES3_REG_CTRL1_SIE BIT(2) /* Second interrupt enable */
40#define ABB5ZES3_REG_CTRL1_PM BIT(3) /* 24h/12h mode */
41#define ABB5ZES3_REG_CTRL1_SR BIT(4) /* Software reset */
42#define ABB5ZES3_REG_CTRL1_STOP BIT(5) /* RTC circuit enable */
43#define ABB5ZES3_REG_CTRL1_CAP BIT(7)
44
45#define ABB5ZES3_REG_CTRL2 0x01 /* Control 2 register */
46#define ABB5ZES3_REG_CTRL2_CTBIE BIT(0) /* Countdown timer B int. enable */
47#define ABB5ZES3_REG_CTRL2_CTAIE BIT(1) /* Countdown timer A int. enable */
48#define ABB5ZES3_REG_CTRL2_WTAIE BIT(2) /* Watchdog timer A int. enable */
49#define ABB5ZES3_REG_CTRL2_AF BIT(3) /* Alarm interrupt status */
50#define ABB5ZES3_REG_CTRL2_SF BIT(4) /* Second interrupt status */
51#define ABB5ZES3_REG_CTRL2_CTBF BIT(5) /* Countdown timer B int. status */
52#define ABB5ZES3_REG_CTRL2_CTAF BIT(6) /* Countdown timer A int. status */
53#define ABB5ZES3_REG_CTRL2_WTAF BIT(7) /* Watchdog timer A int. status */
54
55#define ABB5ZES3_REG_CTRL3 0x02 /* Control 3 register */
56#define ABB5ZES3_REG_CTRL3_PM2 BIT(7) /* Power Management bit 2 */
57#define ABB5ZES3_REG_CTRL3_PM1 BIT(6) /* Power Management bit 1 */
58#define ABB5ZES3_REG_CTRL3_PM0 BIT(5) /* Power Management bit 0 */
59#define ABB5ZES3_REG_CTRL3_BSF BIT(3) /* Battery switchover int. status */
60#define ABB5ZES3_REG_CTRL3_BLF BIT(2) /* Battery low int. status */
61#define ABB5ZES3_REG_CTRL3_BSIE BIT(1) /* Battery switchover int. enable */
62#define ABB5ZES3_REG_CTRL3_BLIE BIT(0) /* Battery low int. enable */
63
64#define ABB5ZES3_CTRL_SEC_LEN 3
65
66/* RTC section */
67#define ABB5ZES3_REG_RTC_SC 0x03 /* RTC Seconds register */
68#define ABB5ZES3_REG_RTC_SC_OSC BIT(7) /* Clock integrity status */
69#define ABB5ZES3_REG_RTC_MN 0x04 /* RTC Minutes register */
70#define ABB5ZES3_REG_RTC_HR 0x05 /* RTC Hours register */
71#define ABB5ZES3_REG_RTC_HR_PM BIT(5) /* RTC Hours PM bit */
72#define ABB5ZES3_REG_RTC_DT 0x06 /* RTC Date register */
73#define ABB5ZES3_REG_RTC_DW 0x07 /* RTC Day of the week register */
74#define ABB5ZES3_REG_RTC_MO 0x08 /* RTC Month register */
75#define ABB5ZES3_REG_RTC_YR 0x09 /* RTC Year register */
76
77#define ABB5ZES3_RTC_SEC_LEN 7
78
79/* Alarm section (enable bits are all active low) */
80#define ABB5ZES3_REG_ALRM_MN 0x0A /* Alarm - minute register */
81#define ABB5ZES3_REG_ALRM_MN_AE BIT(7) /* Minute enable */
82#define ABB5ZES3_REG_ALRM_HR 0x0B /* Alarm - hours register */
83#define ABB5ZES3_REG_ALRM_HR_AE BIT(7) /* Hour enable */
84#define ABB5ZES3_REG_ALRM_DT 0x0C /* Alarm - date register */
85#define ABB5ZES3_REG_ALRM_DT_AE BIT(7) /* Date (day of the month) enable */
86#define ABB5ZES3_REG_ALRM_DW 0x0D /* Alarm - day of the week reg. */
87#define ABB5ZES3_REG_ALRM_DW_AE BIT(7) /* Day of the week enable */
88
89#define ABB5ZES3_ALRM_SEC_LEN 4
90
91/* Frequency offset section */
92#define ABB5ZES3_REG_FREQ_OF 0x0E /* Frequency offset register */
93#define ABB5ZES3_REG_FREQ_OF_MODE 0x0E /* Offset mode: 2 hours / minute */
94
95/* CLOCKOUT section */
96#define ABB5ZES3_REG_TIM_CLK 0x0F /* Timer & Clockout register */
97#define ABB5ZES3_REG_TIM_CLK_TAM BIT(7) /* Permanent/pulsed timer A/int. 2 */
98#define ABB5ZES3_REG_TIM_CLK_TBM BIT(6) /* Permanent/pulsed timer B */
99#define ABB5ZES3_REG_TIM_CLK_COF2 BIT(5) /* Clkout Freq bit 2 */
100#define ABB5ZES3_REG_TIM_CLK_COF1 BIT(4) /* Clkout Freq bit 1 */
101#define ABB5ZES3_REG_TIM_CLK_COF0 BIT(3) /* Clkout Freq bit 0 */
102#define ABB5ZES3_REG_TIM_CLK_TAC1 BIT(2) /* Timer A: - 01 : countdown */
103#define ABB5ZES3_REG_TIM_CLK_TAC0 BIT(1) /* - 10 : timer */
104#define ABB5ZES3_REG_TIM_CLK_TBC BIT(0) /* Timer B enable */
105
106/* Timer A Section */
107#define ABB5ZES3_REG_TIMA_CLK 0x10 /* Timer A clock register */
108#define ABB5ZES3_REG_TIMA_CLK_TAQ2 BIT(2) /* Freq bit 2 */
109#define ABB5ZES3_REG_TIMA_CLK_TAQ1 BIT(1) /* Freq bit 1 */
110#define ABB5ZES3_REG_TIMA_CLK_TAQ0 BIT(0) /* Freq bit 0 */
111#define ABB5ZES3_REG_TIMA 0x11 /* Timer A register */
112
113#define ABB5ZES3_TIMA_SEC_LEN 2
114
115/* Timer B Section */
116#define ABB5ZES3_REG_TIMB_CLK 0x12 /* Timer B clock register */
117#define ABB5ZES3_REG_TIMB_CLK_TBW2 BIT(6)
118#define ABB5ZES3_REG_TIMB_CLK_TBW1 BIT(5)
119#define ABB5ZES3_REG_TIMB_CLK_TBW0 BIT(4)
120#define ABB5ZES3_REG_TIMB_CLK_TAQ2 BIT(2)
121#define ABB5ZES3_REG_TIMB_CLK_TAQ1 BIT(1)
122#define ABB5ZES3_REG_TIMB_CLK_TAQ0 BIT(0)
123#define ABB5ZES3_REG_TIMB 0x13 /* Timer B register */
124#define ABB5ZES3_TIMB_SEC_LEN 2
125
126#define ABB5ZES3_MEM_MAP_LEN 0x14
127
128struct abb5zes3_rtc_data {
129 struct rtc_device *rtc;
130 struct regmap *regmap;
131 struct mutex lock;
132
133 int irq;
134
135 bool battery_low;
136 bool timer_alarm; /* current alarm is via timer A */
137};
138
139/*
140 * Try and match register bits w/ fixed null values to see whether we
141 * are dealing with an ABB5ZES3. Note: this function is called early
142 * during init and hence does need mutex protection.
143 */
144static int abb5zes3_i2c_validate_chip(struct regmap *regmap)
145{
146 u8 regs[ABB5ZES3_MEM_MAP_LEN];
147 static const u8 mask[ABB5ZES3_MEM_MAP_LEN] = { 0x00, 0x00, 0x10, 0x00,
148 0x80, 0xc0, 0xc0, 0xf8,
149 0xe0, 0x00, 0x00, 0x40,
150 0x40, 0x78, 0x00, 0x00,
151 0xf8, 0x00, 0x88, 0x00 };
152 int ret, i;
153
154 ret = regmap_bulk_read(regmap, 0, regs, ABB5ZES3_MEM_MAP_LEN);
155 if (ret)
156 return ret;
157
158 for (i = 0; i < ABB5ZES3_MEM_MAP_LEN; ++i) {
159 if (regs[i] & mask[i]) /* check if bits are cleared */
160 return -ENODEV;
161 }
162
163 return 0;
164}
165
166/* Clear alarm status bit. */
167static int _abb5zes3_rtc_clear_alarm(struct device *dev)
168{
169 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
170 int ret;
171
172 ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL2,
173 ABB5ZES3_REG_CTRL2_AF, 0);
174 if (ret)
175 dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret);
176
177 return ret;
178}
179
180/* Enable or disable alarm (i.e. alarm interrupt generation) */
181static int _abb5zes3_rtc_update_alarm(struct device *dev, bool enable)
182{
183 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
184 int ret;
185
186 ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL1,
187 ABB5ZES3_REG_CTRL1_AIE,
188 enable ? ABB5ZES3_REG_CTRL1_AIE : 0);
189 if (ret)
190 dev_err(dev, "%s: writing alarm INT failed (%d)\n",
191 __func__, ret);
192
193 return ret;
194}
195
196/* Enable or disable timer (watchdog timer A interrupt generation) */
197static int _abb5zes3_rtc_update_timer(struct device *dev, bool enable)
198{
199 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
200 int ret;
201
202 ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_CTRL2,
203 ABB5ZES3_REG_CTRL2_WTAIE,
204 enable ? ABB5ZES3_REG_CTRL2_WTAIE : 0);
205 if (ret)
206 dev_err(dev, "%s: writing timer INT failed (%d)\n",
207 __func__, ret);
208
209 return ret;
210}
211
212/*
213 * Note: we only read, so regmap inner lock protection is sufficient, i.e.
214 * we do not need driver's main lock protection.
215 */
216static int _abb5zes3_rtc_read_time(struct device *dev, struct rtc_time *tm)
217{
218 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
219 u8 regs[ABB5ZES3_REG_RTC_SC + ABB5ZES3_RTC_SEC_LEN];
220 int ret;
221
222 /*
223 * As we need to read CTRL1 register anyway to access 24/12h
224 * mode bit, we do a single bulk read of both control and RTC
225 * sections (they are consecutive). This also ease indexing
226 * of register values after bulk read.
227 */
228 ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_CTRL1, regs,
229 sizeof(regs));
230 if (ret) {
231 dev_err(dev, "%s: reading RTC time failed (%d)\n",
232 __func__, ret);
233 goto err;
234 }
235
236 /* If clock integrity is not guaranteed, do not return a time value */
237 if (regs[ABB5ZES3_REG_RTC_SC] & ABB5ZES3_REG_RTC_SC_OSC) {
238 ret = -ENODATA;
239 goto err;
240 }
241
242 tm->tm_sec = bcd2bin(regs[ABB5ZES3_REG_RTC_SC] & 0x7F);
243 tm->tm_min = bcd2bin(regs[ABB5ZES3_REG_RTC_MN]);
244
245 if (regs[ABB5ZES3_REG_CTRL1] & ABB5ZES3_REG_CTRL1_PM) { /* 12hr mode */
246 tm->tm_hour = bcd2bin(regs[ABB5ZES3_REG_RTC_HR] & 0x1f);
247 if (regs[ABB5ZES3_REG_RTC_HR] & ABB5ZES3_REG_RTC_HR_PM) /* PM */
248 tm->tm_hour += 12;
249 } else { /* 24hr mode */
250 tm->tm_hour = bcd2bin(regs[ABB5ZES3_REG_RTC_HR]);
251 }
252
253 tm->tm_mday = bcd2bin(regs[ABB5ZES3_REG_RTC_DT]);
254 tm->tm_wday = bcd2bin(regs[ABB5ZES3_REG_RTC_DW]);
255 tm->tm_mon = bcd2bin(regs[ABB5ZES3_REG_RTC_MO]) - 1; /* starts at 1 */
256 tm->tm_year = bcd2bin(regs[ABB5ZES3_REG_RTC_YR]) + 100;
257
258 ret = rtc_valid_tm(tm);
259
260err:
261 return ret;
262}
263
264static int abb5zes3_rtc_set_time(struct device *dev, struct rtc_time *tm)
265{
266 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
267 u8 regs[ABB5ZES3_REG_RTC_SC + ABB5ZES3_RTC_SEC_LEN];
268 int ret;
269
270 /*
271 * Year register is 8-bit wide and bcd-coded, i.e records values
272 * between 0 and 99. tm_year is an offset from 1900 and we are
273 * interested in the 2000-2099 range, so any value less than 100
274 * is invalid.
275 */
276 if (tm->tm_year < 100)
277 return -EINVAL;
278
279 regs[ABB5ZES3_REG_RTC_SC] = bin2bcd(tm->tm_sec); /* MSB=0 clears OSC */
280 regs[ABB5ZES3_REG_RTC_MN] = bin2bcd(tm->tm_min);
281 regs[ABB5ZES3_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */
282 regs[ABB5ZES3_REG_RTC_DT] = bin2bcd(tm->tm_mday);
283 regs[ABB5ZES3_REG_RTC_DW] = bin2bcd(tm->tm_wday);
284 regs[ABB5ZES3_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1);
285 regs[ABB5ZES3_REG_RTC_YR] = bin2bcd(tm->tm_year - 100);
286
287 mutex_lock(&data->lock);
288 ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_RTC_SC,
289 regs + ABB5ZES3_REG_RTC_SC,
290 ABB5ZES3_RTC_SEC_LEN);
291 mutex_unlock(&data->lock);
292
293
294 return ret;
295}
296
297/*
298 * Set provided TAQ and Timer A registers (TIMA_CLK and TIMA) based on
299 * given number of seconds.
300 */
301static inline void sec_to_timer_a(u8 secs, u8 *taq, u8 *timer_a)
302{
303 *taq = ABB5ZES3_REG_TIMA_CLK_TAQ1; /* 1Hz */
304 *timer_a = secs;
305}
306
307/*
308 * Return current number of seconds in Timer A. As we only use
309 * timer A with a 1Hz freq, this is what we expect to have.
310 */
311static inline int sec_from_timer_a(u8 *secs, u8 taq, u8 timer_a)
312{
313 if (taq != ABB5ZES3_REG_TIMA_CLK_TAQ1) /* 1Hz */
314 return -EINVAL;
315
316 *secs = timer_a;
317
318 return 0;
319}
320
321/*
322 * Read alarm currently configured via a watchdog timer using timer A. This
323 * is done by reading current RTC time and adding remaining timer time.
324 */
325static int _abb5zes3_rtc_read_timer(struct device *dev,
326 struct rtc_wkalrm *alarm)
327{
328 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
329 struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
330 u8 regs[ABB5ZES3_TIMA_SEC_LEN + 1];
331 unsigned long rtc_secs;
332 unsigned int reg;
333 u8 timer_secs;
334 int ret;
335
336 /*
337 * Instead of doing two separate calls, because they are consecutive,
338 * we grab both clockout register and Timer A section. The latter is
339 * used to decide if timer A is enabled (as a watchdog timer).
340 */
341 ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_TIM_CLK, regs,
342 ABB5ZES3_TIMA_SEC_LEN + 1);
343 if (ret) {
344 dev_err(dev, "%s: reading Timer A section failed (%d)\n",
345 __func__, ret);
346 goto err;
347 }
348
349 /* get current time ... */
350 ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
351 if (ret)
352 goto err;
353
354 /* ... convert to seconds ... */
355 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
356 if (ret)
357 goto err;
358
359 /* ... add remaining timer A time ... */
360 ret = sec_from_timer_a(&timer_secs, regs[1], regs[2]);
361 if (ret)
362 goto err;
363
364 /* ... and convert back. */
365 rtc_time_to_tm(rtc_secs + timer_secs, alarm_tm);
366
367 ret = regmap_read(data->regmap, ABB5ZES3_REG_CTRL2, &reg);
368 if (ret) {
369 dev_err(dev, "%s: reading ctrl reg failed (%d)\n",
370 __func__, ret);
371 goto err;
372 }
373
374 alarm->enabled = !!(reg & ABB5ZES3_REG_CTRL2_WTAIE);
375
376err:
377 return ret;
378}
379
380/* Read alarm currently configured via a RTC alarm registers. */
381static int _abb5zes3_rtc_read_alarm(struct device *dev,
382 struct rtc_wkalrm *alarm)
383{
384 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
385 struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
386 unsigned long rtc_secs, alarm_secs;
387 u8 regs[ABB5ZES3_ALRM_SEC_LEN];
388 unsigned int reg;
389 int ret;
390
391 ret = regmap_bulk_read(data->regmap, ABB5ZES3_REG_ALRM_MN, regs,
392 ABB5ZES3_ALRM_SEC_LEN);
393 if (ret) {
394 dev_err(dev, "%s: reading alarm section failed (%d)\n",
395 __func__, ret);
396 goto err;
397 }
398
399 alarm_tm->tm_sec = 0;
400 alarm_tm->tm_min = bcd2bin(regs[0] & 0x7f);
401 alarm_tm->tm_hour = bcd2bin(regs[1] & 0x3f);
402 alarm_tm->tm_mday = bcd2bin(regs[2] & 0x3f);
403 alarm_tm->tm_wday = -1;
404
405 /*
406 * The alarm section does not store year/month. We use the ones in rtc
407 * section as a basis and increment month and then year if needed to get
408 * alarm after current time.
409 */
410 ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
411 if (ret)
412 goto err;
413
414 alarm_tm->tm_year = rtc_tm.tm_year;
415 alarm_tm->tm_mon = rtc_tm.tm_mon;
416
417 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
418 if (ret)
419 goto err;
420
421 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
422 if (ret)
423 goto err;
424
425 if (alarm_secs < rtc_secs) {
426 if (alarm_tm->tm_mon == 11) {
427 alarm_tm->tm_mon = 0;
428 alarm_tm->tm_year += 1;
429 } else {
430 alarm_tm->tm_mon += 1;
431 }
432 }
433
434 ret = regmap_read(data->regmap, ABB5ZES3_REG_CTRL1, &reg);
435 if (ret) {
436 dev_err(dev, "%s: reading ctrl reg failed (%d)\n",
437 __func__, ret);
438 goto err;
439 }
440
441 alarm->enabled = !!(reg & ABB5ZES3_REG_CTRL1_AIE);
442
443err:
444 return ret;
445}
446
447/*
448 * As the Alarm mechanism supported by the chip is only accurate to the
449 * minute, we use the watchdog timer mechanism provided by timer A
450 * (up to 256 seconds w/ a second accuracy) for low alarm values (below
451 * 4 minutes). Otherwise, we use the common alarm mechanism provided
452 * by the chip. In order for that to work, we keep track of currently
453 * configured timer type via 'timer_alarm' flag in our private data
454 * structure.
455 */
456static int abb5zes3_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
457{
458 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
459 int ret;
460
461 mutex_lock(&data->lock);
462 if (data->timer_alarm)
463 ret = _abb5zes3_rtc_read_timer(dev, alarm);
464 else
465 ret = _abb5zes3_rtc_read_alarm(dev, alarm);
466 mutex_unlock(&data->lock);
467
468 return ret;
469}
470
471/*
472 * Set alarm using chip alarm mechanism. It is only accurate to the
473 * minute (not the second). The function expects alarm interrupt to
474 * be disabled.
475 */
476static int _abb5zes3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
477{
478 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
479 struct rtc_time *alarm_tm = &alarm->time;
480 unsigned long rtc_secs, alarm_secs;
481 u8 regs[ABB5ZES3_ALRM_SEC_LEN];
482 struct rtc_time rtc_tm;
483 int ret, enable = 1;
484
485 ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
486 if (ret)
487 goto err;
488
489 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
490 if (ret)
491 goto err;
492
493 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
494 if (ret)
495 goto err;
496
497 /* If alarm time is before current time, disable the alarm */
498 if (!alarm->enabled || alarm_secs <= rtc_secs) {
499 enable = 0;
500 } else {
501 /*
502 * Chip only support alarms up to one month in the future. Let's
503 * return an error if we get something after that limit.
504 * Comparison is done by incrementing rtc_tm month field by one
505 * and checking alarm value is still below.
506 */
507 if (rtc_tm.tm_mon == 11) { /* handle year wrapping */
508 rtc_tm.tm_mon = 0;
509 rtc_tm.tm_year += 1;
510 } else {
511 rtc_tm.tm_mon += 1;
512 }
513
514 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
515 if (ret)
516 goto err;
517
518 if (alarm_secs > rtc_secs) {
519 dev_err(dev, "%s: alarm maximum is one month in the "
520 "future (%d)\n", __func__, ret);
521 ret = -EINVAL;
522 goto err;
523 }
524 }
525
526 /*
527 * Program all alarm registers but DW one. For each register, setting
528 * MSB to 0 enables associated alarm.
529 */
530 regs[0] = bin2bcd(alarm_tm->tm_min) & 0x7f;
531 regs[1] = bin2bcd(alarm_tm->tm_hour) & 0x3f;
532 regs[2] = bin2bcd(alarm_tm->tm_mday) & 0x3f;
533 regs[3] = ABB5ZES3_REG_ALRM_DW_AE; /* do not match day of the week */
534
535 ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_ALRM_MN, regs,
536 ABB5ZES3_ALRM_SEC_LEN);
537 if (ret < 0) {
538 dev_err(dev, "%s: writing ALARM section failed (%d)\n",
539 __func__, ret);
540 goto err;
541 }
542
543 /* Record currently configured alarm is not a timer */
544 data->timer_alarm = 0;
545
546 /* Enable or disable alarm interrupt generation */
547 ret = _abb5zes3_rtc_update_alarm(dev, enable);
548
549err:
550 return ret;
551}
552
553/*
554 * Set alarm using timer watchdog (via timer A) mechanism. The function expects
555 * timer A interrupt to be disabled.
556 */
557static int _abb5zes3_rtc_set_timer(struct device *dev, struct rtc_wkalrm *alarm,
558 u8 secs)
559{
560 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
561 u8 regs[ABB5ZES3_TIMA_SEC_LEN];
562 u8 mask = ABB5ZES3_REG_TIM_CLK_TAC0 | ABB5ZES3_REG_TIM_CLK_TAC1;
563 int ret = 0;
564
565 /* Program given number of seconds to Timer A registers */
566 sec_to_timer_a(secs, &regs[0], &regs[1]);
567 ret = regmap_bulk_write(data->regmap, ABB5ZES3_REG_TIMA_CLK, regs,
568 ABB5ZES3_TIMA_SEC_LEN);
569 if (ret < 0) {
570 dev_err(dev, "%s: writing timer section failed\n", __func__);
571 goto err;
572 }
573
574 /* Configure Timer A as a watchdog timer */
575 ret = regmap_update_bits(data->regmap, ABB5ZES3_REG_TIM_CLK,
576 mask, ABB5ZES3_REG_TIM_CLK_TAC1);
577 if (ret)
578 dev_err(dev, "%s: failed to update timer\n", __func__);
579
580 /* Record currently configured alarm is a timer */
581 data->timer_alarm = 1;
582
583 /* Enable or disable timer interrupt generation */
584 ret = _abb5zes3_rtc_update_timer(dev, alarm->enabled);
585
586err:
587 return ret;
588}
589
590/*
591 * The chip has an alarm which is only accurate to the minute. In order to
592 * handle alarms below that limit, we use the watchdog timer function of
593 * timer A. More precisely, the timer method is used for alarms below 240
594 * seconds.
595 */
596static int abb5zes3_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
597{
598 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
599 struct rtc_time *alarm_tm = &alarm->time;
600 unsigned long rtc_secs, alarm_secs;
601 struct rtc_time rtc_tm;
602 int ret;
603
604 mutex_lock(&data->lock);
605 ret = _abb5zes3_rtc_read_time(dev, &rtc_tm);
606 if (ret)
607 goto err;
608
609 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
610 if (ret)
611 goto err;
612
613 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
614 if (ret)
615 goto err;
616
617 /* Let's first disable both the alarm and the timer interrupts */
618 ret = _abb5zes3_rtc_update_alarm(dev, false);
619 if (ret < 0) {
620 dev_err(dev, "%s: unable to disable alarm (%d)\n", __func__,
621 ret);
622 goto err;
623 }
624 ret = _abb5zes3_rtc_update_timer(dev, false);
625 if (ret < 0) {
626 dev_err(dev, "%s: unable to disable timer (%d)\n", __func__,
627 ret);
628 goto err;
629 }
630
631 data->timer_alarm = 0;
632
633 /*
634 * Let's now configure the alarm; if we are expected to ring in
635 * more than 240s, then we setup an alarm. Otherwise, a timer.
636 */
637 if ((alarm_secs > rtc_secs) && ((alarm_secs - rtc_secs) <= 240))
638 ret = _abb5zes3_rtc_set_timer(dev, alarm,
639 alarm_secs - rtc_secs);
640 else
641 ret = _abb5zes3_rtc_set_alarm(dev, alarm);
642
643 err:
644 mutex_unlock(&data->lock);
645
646 if (ret)
647 dev_err(dev, "%s: unable to configure alarm (%d)\n", __func__,
648 ret);
649
650 return ret;
651 }
652
653/* Enable or disable battery low irq generation */
654static inline int _abb5zes3_rtc_battery_low_irq_enable(struct regmap *regmap,
655 bool enable)
656{
657 return regmap_update_bits(regmap, ABB5ZES3_REG_CTRL3,
658 ABB5ZES3_REG_CTRL3_BLIE,
659 enable ? ABB5ZES3_REG_CTRL3_BLIE : 0);
660}
661
662/*
663 * Check current RTC status and enable/disable what needs to be. Return 0 if
664 * everything went ok and a negative value upon error. Note: this function
665 * is called early during init and hence does need mutex protection.
666 */
667static int abb5zes3_rtc_check_setup(struct device *dev)
668{
669 struct abb5zes3_rtc_data *data = dev_get_drvdata(dev);
670 struct regmap *regmap = data->regmap;
671 unsigned int reg;
672 int ret;
673 u8 mask;
674
675 /*
676 * By default, the devices generates a 32.768KHz signal on IRQ#1 pin. It
677 * is disabled here to prevent polluting the interrupt line and
678 * uselessly triggering the IRQ handler we install for alarm and battery
679 * low events. Note: this is done before clearing int. status below
680 * in this function.
681 * We also disable all timers and set timer interrupt to permanent (not
682 * pulsed).
683 */
684 mask = (ABB5ZES3_REG_TIM_CLK_TBC | ABB5ZES3_REG_TIM_CLK_TAC0 |
685 ABB5ZES3_REG_TIM_CLK_TAC1 | ABB5ZES3_REG_TIM_CLK_COF0 |
686 ABB5ZES3_REG_TIM_CLK_COF1 | ABB5ZES3_REG_TIM_CLK_COF2 |
687 ABB5ZES3_REG_TIM_CLK_TBM | ABB5ZES3_REG_TIM_CLK_TAM);
688 ret = regmap_update_bits(regmap, ABB5ZES3_REG_TIM_CLK, mask,
689 ABB5ZES3_REG_TIM_CLK_COF0 | ABB5ZES3_REG_TIM_CLK_COF1 |
690 ABB5ZES3_REG_TIM_CLK_COF2);
691 if (ret < 0) {
692 dev_err(dev, "%s: unable to initialize clkout register (%d)\n",
693 __func__, ret);
694 return ret;
695 }
696
697 /*
698 * Each component of the alarm (MN, HR, DT, DW) can be enabled/disabled
699 * individually by clearing/setting MSB of each associated register. So,
700 * we set all alarm enable bits to disable current alarm setting.
701 */
702 mask = (ABB5ZES3_REG_ALRM_MN_AE | ABB5ZES3_REG_ALRM_HR_AE |
703 ABB5ZES3_REG_ALRM_DT_AE | ABB5ZES3_REG_ALRM_DW_AE);
704 ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL2, mask, mask);
705 if (ret < 0) {
706 dev_err(dev, "%s: unable to disable alarm setting (%d)\n",
707 __func__, ret);
708 return ret;
709 }
710
711 /* Set Control 1 register (RTC enabled, 24hr mode, all int. disabled) */
712 mask = (ABB5ZES3_REG_CTRL1_CIE | ABB5ZES3_REG_CTRL1_AIE |
713 ABB5ZES3_REG_CTRL1_SIE | ABB5ZES3_REG_CTRL1_PM |
714 ABB5ZES3_REG_CTRL1_CAP | ABB5ZES3_REG_CTRL1_STOP);
715 ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL1, mask, 0);
716 if (ret < 0) {
717 dev_err(dev, "%s: unable to initialize CTRL1 register (%d)\n",
718 __func__, ret);
719 return ret;
720 }
721
722 /*
723 * Set Control 2 register (timer int. disabled, alarm status cleared).
724 * WTAF is read-only and cleared automatically by reading the register.
725 */
726 mask = (ABB5ZES3_REG_CTRL2_CTBIE | ABB5ZES3_REG_CTRL2_CTAIE |
727 ABB5ZES3_REG_CTRL2_WTAIE | ABB5ZES3_REG_CTRL2_AF |
728 ABB5ZES3_REG_CTRL2_SF | ABB5ZES3_REG_CTRL2_CTBF |
729 ABB5ZES3_REG_CTRL2_CTAF);
730 ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL2, mask, 0);
731 if (ret < 0) {
732 dev_err(dev, "%s: unable to initialize CTRL2 register (%d)\n",
733 __func__, ret);
734 return ret;
735 }
736
737 /*
738 * Enable battery low detection function and battery switchover function
739 * (standard mode). Disable associated interrupts. Clear battery
740 * switchover flag but not battery low flag. The latter is checked
741 * later below.
742 */
743 mask = (ABB5ZES3_REG_CTRL3_PM0 | ABB5ZES3_REG_CTRL3_PM1 |
744 ABB5ZES3_REG_CTRL3_PM2 | ABB5ZES3_REG_CTRL3_BLIE |
745 ABB5ZES3_REG_CTRL3_BSIE| ABB5ZES3_REG_CTRL3_BSF);
746 ret = regmap_update_bits(regmap, ABB5ZES3_REG_CTRL3, mask, 0);
747 if (ret < 0) {
748 dev_err(dev, "%s: unable to initialize CTRL3 register (%d)\n",
749 __func__, ret);
750 return ret;
751 }
752
753 /* Check oscillator integrity flag */
754 ret = regmap_read(regmap, ABB5ZES3_REG_RTC_SC, &reg);
755 if (ret < 0) {
756 dev_err(dev, "%s: unable to read osc. integrity flag (%d)\n",
757 __func__, ret);
758 return ret;
759 }
760
761 if (reg & ABB5ZES3_REG_RTC_SC_OSC) {
762 dev_err(dev, "clock integrity not guaranteed. Osc. has stopped "
763 "or has been interrupted.\n");
764 dev_err(dev, "change battery (if not already done) and "
765 "then set time to reset osc. failure flag.\n");
766 }
767
768 /*
769 * Check battery low flag at startup: this allows reporting battery
770 * is low at startup when IRQ line is not connected. Note: we record
771 * current status to avoid reenabling this interrupt later in probe
772 * function if battery is low.
773 */
774 ret = regmap_read(regmap, ABB5ZES3_REG_CTRL3, &reg);
775 if (ret < 0) {
776 dev_err(dev, "%s: unable to read battery low flag (%d)\n",
777 __func__, ret);
778 return ret;
779 }
780
781 data->battery_low = reg & ABB5ZES3_REG_CTRL3_BLF;
782 if (data->battery_low) {
783 dev_err(dev, "RTC battery is low; please, consider "
784 "changing it!\n");
785
786 ret = _abb5zes3_rtc_battery_low_irq_enable(regmap, false);
787 if (ret)
788 dev_err(dev, "%s: disabling battery low interrupt "
789 "generation failed (%d)\n", __func__, ret);
790 }
791
792 return ret;
793}
794
795static int abb5zes3_rtc_alarm_irq_enable(struct device *dev,
796 unsigned int enable)
797{
798 struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
799 int ret = 0;
800
801 if (rtc_data->irq) {
802 mutex_lock(&rtc_data->lock);
803 if (rtc_data->timer_alarm)
804 ret = _abb5zes3_rtc_update_timer(dev, enable);
805 else
806 ret = _abb5zes3_rtc_update_alarm(dev, enable);
807 mutex_unlock(&rtc_data->lock);
808 }
809
810 return ret;
811}
812
813static irqreturn_t _abb5zes3_rtc_interrupt(int irq, void *data)
814{
815 struct i2c_client *client = data;
816 struct device *dev = &client->dev;
817 struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
818 struct rtc_device *rtc = rtc_data->rtc;
819 u8 regs[ABB5ZES3_CTRL_SEC_LEN];
820 int ret, handled = IRQ_NONE;
821
822 ret = regmap_bulk_read(rtc_data->regmap, 0, regs,
823 ABB5ZES3_CTRL_SEC_LEN);
824 if (ret) {
825 dev_err(dev, "%s: unable to read control section (%d)!\n",
826 __func__, ret);
827 return handled;
828 }
829
830 /*
831 * Check battery low detection flag and disable battery low interrupt
832 * generation if flag is set (interrupt can only be cleared when
833 * battery is replaced).
834 */
835 if (regs[ABB5ZES3_REG_CTRL3] & ABB5ZES3_REG_CTRL3_BLF) {
836 dev_err(dev, "RTC battery is low; please change it!\n");
837
838 _abb5zes3_rtc_battery_low_irq_enable(rtc_data->regmap, false);
839
840 handled = IRQ_HANDLED;
841 }
842
843 /* Check alarm flag */
844 if (regs[ABB5ZES3_REG_CTRL2] & ABB5ZES3_REG_CTRL2_AF) {
845 dev_dbg(dev, "RTC alarm!\n");
846
847 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
848
849 /* Acknowledge and disable the alarm */
850 _abb5zes3_rtc_clear_alarm(dev);
851 _abb5zes3_rtc_update_alarm(dev, 0);
852
853 handled = IRQ_HANDLED;
854 }
855
856 /* Check watchdog Timer A flag */
857 if (regs[ABB5ZES3_REG_CTRL2] & ABB5ZES3_REG_CTRL2_WTAF) {
858 dev_dbg(dev, "RTC timer!\n");
859
860 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
861
862 /*
863 * Acknowledge and disable the alarm. Note: WTAF
864 * flag had been cleared when reading CTRL2
865 */
866 _abb5zes3_rtc_update_timer(dev, 0);
867
868 rtc_data->timer_alarm = 0;
869
870 handled = IRQ_HANDLED;
871 }
872
873 return handled;
874}
875
876static const struct rtc_class_ops rtc_ops = {
877 .read_time = _abb5zes3_rtc_read_time,
878 .set_time = abb5zes3_rtc_set_time,
879 .read_alarm = abb5zes3_rtc_read_alarm,
880 .set_alarm = abb5zes3_rtc_set_alarm,
881 .alarm_irq_enable = abb5zes3_rtc_alarm_irq_enable,
882};
883
884static struct regmap_config abb5zes3_rtc_regmap_config = {
885 .reg_bits = 8,
886 .val_bits = 8,
887};
888
889static int abb5zes3_probe(struct i2c_client *client,
890 const struct i2c_device_id *id)
891{
892 struct abb5zes3_rtc_data *data = NULL;
893 struct device *dev = &client->dev;
894 struct regmap *regmap;
895 int ret;
896
897 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
898 I2C_FUNC_SMBUS_BYTE_DATA |
899 I2C_FUNC_SMBUS_I2C_BLOCK)) {
900 ret = -ENODEV;
901 goto err;
902 }
903
904 regmap = devm_regmap_init_i2c(client, &abb5zes3_rtc_regmap_config);
905 if (IS_ERR(regmap)) {
906 ret = PTR_ERR(regmap);
907 dev_err(dev, "%s: regmap allocation failed: %d\n",
908 __func__, ret);
909 goto err;
910 }
911
912 ret = abb5zes3_i2c_validate_chip(regmap);
913 if (ret)
914 goto err;
915
916 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
917 if (!data) {
918 ret = -ENOMEM;
919 goto err;
920 }
921
922 mutex_init(&data->lock);
923 data->regmap = regmap;
924 dev_set_drvdata(dev, data);
925
926 ret = abb5zes3_rtc_check_setup(dev);
927 if (ret)
928 goto err;
929
930 if (client->irq > 0) {
931 ret = devm_request_threaded_irq(dev, client->irq, NULL,
932 _abb5zes3_rtc_interrupt,
933 IRQF_SHARED|IRQF_ONESHOT,
934 DRV_NAME, client);
935 if (!ret) {
936 device_init_wakeup(dev, true);
937 data->irq = client->irq;
938 dev_dbg(dev, "%s: irq %d used by RTC\n", __func__,
939 client->irq);
940 } else {
941 dev_err(dev, "%s: irq %d unavailable (%d)\n",
942 __func__, client->irq, ret);
943 goto err;
944 }
945 }
946
947 data->rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops,
948 THIS_MODULE);
949 ret = PTR_ERR_OR_ZERO(data->rtc);
950 if (ret) {
951 dev_err(dev, "%s: unable to register RTC device (%d)\n",
952 __func__, ret);
953 goto err;
954 }
955
956 /* Enable battery low detection interrupt if battery not already low */
957 if (!data->battery_low && data->irq) {
958 ret = _abb5zes3_rtc_battery_low_irq_enable(regmap, true);
959 if (ret) {
960 dev_err(dev, "%s: enabling battery low interrupt "
961 "generation failed (%d)\n", __func__, ret);
962 goto err;
963 }
964 }
965
966err:
967 if (ret && data && data->irq)
968 device_init_wakeup(dev, false);
969 return ret;
970}
971
972static int abb5zes3_remove(struct i2c_client *client)
973{
974 struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(&client->dev);
975
976 if (rtc_data->irq > 0)
977 device_init_wakeup(&client->dev, false);
978
979 return 0;
980}
981
982#ifdef CONFIG_PM_SLEEP
983static int abb5zes3_rtc_suspend(struct device *dev)
984{
985 struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
986
987 if (device_may_wakeup(dev))
988 return enable_irq_wake(rtc_data->irq);
989
990 return 0;
991}
992
993static int abb5zes3_rtc_resume(struct device *dev)
994{
995 struct abb5zes3_rtc_data *rtc_data = dev_get_drvdata(dev);
996
997 if (device_may_wakeup(dev))
998 return disable_irq_wake(rtc_data->irq);
999
1000 return 0;
1001}
1002#endif
1003
1004static SIMPLE_DEV_PM_OPS(abb5zes3_rtc_pm_ops, abb5zes3_rtc_suspend,
1005 abb5zes3_rtc_resume);
1006
1007#ifdef CONFIG_OF
1008static const struct of_device_id abb5zes3_dt_match[] = {
1009 { .compatible = "abracon,abb5zes3" },
1010 { },
1011};
1012#endif
1013
1014static const struct i2c_device_id abb5zes3_id[] = {
1015 { "abb5zes3", 0 },
1016 { }
1017};
1018MODULE_DEVICE_TABLE(i2c, abb5zes3_id);
1019
1020static struct i2c_driver abb5zes3_driver = {
1021 .driver = {
1022 .name = DRV_NAME,
1023 .owner = THIS_MODULE,
1024 .pm = &abb5zes3_rtc_pm_ops,
1025 .of_match_table = of_match_ptr(abb5zes3_dt_match),
1026 },
1027 .probe = abb5zes3_probe,
1028 .remove = abb5zes3_remove,
1029 .id_table = abb5zes3_id,
1030};
1031module_i2c_driver(abb5zes3_driver);
1032
1033MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1034MODULE_DESCRIPTION("Abracon AB-RTCMC-32.768kHz-B5ZE-S3 RTC/Alarm driver");
1035MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-armada38x.c b/drivers/rtc/rtc-armada38x.c
new file mode 100644
index 000000000000..43e04af39e09
--- /dev/null
+++ b/drivers/rtc/rtc-armada38x.c
@@ -0,0 +1,320 @@
1/*
2 * RTC driver for the Armada 38x Marvell SoCs
3 *
4 * Copyright (C) 2015 Marvell
5 *
6 * Gregory Clement <gregory.clement@free-electrons.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 as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 */
14
15#include <linux/delay.h>
16#include <linux/io.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20#include <linux/rtc.h>
21
22#define RTC_STATUS 0x0
23#define RTC_STATUS_ALARM1 BIT(0)
24#define RTC_STATUS_ALARM2 BIT(1)
25#define RTC_IRQ1_CONF 0x4
26#define RTC_IRQ1_AL_EN BIT(0)
27#define RTC_IRQ1_FREQ_EN BIT(1)
28#define RTC_IRQ1_FREQ_1HZ BIT(2)
29#define RTC_TIME 0xC
30#define RTC_ALARM1 0x10
31
32#define SOC_RTC_INTERRUPT 0x8
33#define SOC_RTC_ALARM1 BIT(0)
34#define SOC_RTC_ALARM2 BIT(1)
35#define SOC_RTC_ALARM1_MASK BIT(2)
36#define SOC_RTC_ALARM2_MASK BIT(3)
37
38struct armada38x_rtc {
39 struct rtc_device *rtc_dev;
40 void __iomem *regs;
41 void __iomem *regs_soc;
42 spinlock_t lock;
43 int irq;
44};
45
46/*
47 * According to the datasheet, the OS should wait 5us after every
48 * register write to the RTC hard macro so that the required update
49 * can occur without holding off the system bus
50 */
51static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
52{
53 writel(val, rtc->regs + offset);
54 udelay(5);
55}
56
57static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
58{
59 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
60 unsigned long time, time_check, flags;
61
62 spin_lock_irqsave(&rtc->lock, flags);
63
64 time = readl(rtc->regs + RTC_TIME);
65 /*
66 * WA for failing time set attempts. As stated in HW ERRATA if
67 * more than one second between two time reads is detected
68 * then read once again.
69 */
70 time_check = readl(rtc->regs + RTC_TIME);
71 if ((time_check - time) > 1)
72 time_check = readl(rtc->regs + RTC_TIME);
73
74 spin_unlock_irqrestore(&rtc->lock, flags);
75
76 rtc_time_to_tm(time_check, tm);
77
78 return 0;
79}
80
81static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
82{
83 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
84 int ret = 0;
85 unsigned long time, flags;
86
87 ret = rtc_tm_to_time(tm, &time);
88
89 if (ret)
90 goto out;
91 /*
92 * Setting the RTC time not always succeeds. According to the
93 * errata we need to first write on the status register and
94 * then wait for 100ms before writing to the time register to be
95 * sure that the data will be taken into account.
96 */
97 spin_lock_irqsave(&rtc->lock, flags);
98
99 rtc_delayed_write(0, rtc, RTC_STATUS);
100
101 spin_unlock_irqrestore(&rtc->lock, flags);
102
103 msleep(100);
104
105 spin_lock_irqsave(&rtc->lock, flags);
106
107 rtc_delayed_write(time, rtc, RTC_TIME);
108
109 spin_unlock_irqrestore(&rtc->lock, flags);
110out:
111 return ret;
112}
113
114static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
115{
116 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
117 unsigned long time, flags;
118 u32 val;
119
120 spin_lock_irqsave(&rtc->lock, flags);
121
122 time = readl(rtc->regs + RTC_ALARM1);
123 val = readl(rtc->regs + RTC_IRQ1_CONF) & RTC_IRQ1_AL_EN;
124
125 spin_unlock_irqrestore(&rtc->lock, flags);
126
127 alrm->enabled = val ? 1 : 0;
128 rtc_time_to_tm(time, &alrm->time);
129
130 return 0;
131}
132
133static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
134{
135 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
136 unsigned long time, flags;
137 int ret = 0;
138 u32 val;
139
140 ret = rtc_tm_to_time(&alrm->time, &time);
141
142 if (ret)
143 goto out;
144
145 spin_lock_irqsave(&rtc->lock, flags);
146
147 rtc_delayed_write(time, rtc, RTC_ALARM1);
148
149 if (alrm->enabled) {
150 rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
151 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
152 writel(val | SOC_RTC_ALARM1_MASK,
153 rtc->regs_soc + SOC_RTC_INTERRUPT);
154 }
155
156 spin_unlock_irqrestore(&rtc->lock, flags);
157
158out:
159 return ret;
160}
161
162static int armada38x_rtc_alarm_irq_enable(struct device *dev,
163 unsigned int enabled)
164{
165 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
166 unsigned long flags;
167
168 spin_lock_irqsave(&rtc->lock, flags);
169
170 if (enabled)
171 rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
172 else
173 rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
174
175 spin_unlock_irqrestore(&rtc->lock, flags);
176
177 return 0;
178}
179
180static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
181{
182 struct armada38x_rtc *rtc = data;
183 u32 val;
184 int event = RTC_IRQF | RTC_AF;
185
186 dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
187
188 spin_lock(&rtc->lock);
189
190 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
191
192 writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
193 val = readl(rtc->regs + RTC_IRQ1_CONF);
194 /* disable all the interrupts for alarm 1 */
195 rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
196 /* Ack the event */
197 rtc_delayed_write(RTC_STATUS_ALARM1, rtc, RTC_STATUS);
198
199 spin_unlock(&rtc->lock);
200
201 if (val & RTC_IRQ1_FREQ_EN) {
202 if (val & RTC_IRQ1_FREQ_1HZ)
203 event |= RTC_UF;
204 else
205 event |= RTC_PF;
206 }
207
208 rtc_update_irq(rtc->rtc_dev, 1, event);
209
210 return IRQ_HANDLED;
211}
212
213static struct rtc_class_ops armada38x_rtc_ops = {
214 .read_time = armada38x_rtc_read_time,
215 .set_time = armada38x_rtc_set_time,
216 .read_alarm = armada38x_rtc_read_alarm,
217 .set_alarm = armada38x_rtc_set_alarm,
218 .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
219};
220
221static __init int armada38x_rtc_probe(struct platform_device *pdev)
222{
223 struct resource *res;
224 struct armada38x_rtc *rtc;
225 int ret;
226
227 rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
228 GFP_KERNEL);
229 if (!rtc)
230 return -ENOMEM;
231
232 spin_lock_init(&rtc->lock);
233
234 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
235 rtc->regs = devm_ioremap_resource(&pdev->dev, res);
236 if (IS_ERR(rtc->regs))
237 return PTR_ERR(rtc->regs);
238 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
239 rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
240 if (IS_ERR(rtc->regs_soc))
241 return PTR_ERR(rtc->regs_soc);
242
243 rtc->irq = platform_get_irq(pdev, 0);
244
245 if (rtc->irq < 0) {
246 dev_err(&pdev->dev, "no irq\n");
247 return rtc->irq;
248 }
249 if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
250 0, pdev->name, rtc) < 0) {
251 dev_warn(&pdev->dev, "Interrupt not available.\n");
252 rtc->irq = -1;
253 /*
254 * If there is no interrupt available then we can't
255 * use the alarm
256 */
257 armada38x_rtc_ops.set_alarm = NULL;
258 armada38x_rtc_ops.alarm_irq_enable = NULL;
259 }
260 platform_set_drvdata(pdev, rtc);
261 if (rtc->irq != -1)
262 device_init_wakeup(&pdev->dev, 1);
263
264 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
265 &armada38x_rtc_ops, THIS_MODULE);
266 if (IS_ERR(rtc->rtc_dev)) {
267 ret = PTR_ERR(rtc->rtc_dev);
268 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
269 return ret;
270 }
271 return 0;
272}
273
274#ifdef CONFIG_PM_SLEEP
275static int armada38x_rtc_suspend(struct device *dev)
276{
277 if (device_may_wakeup(dev)) {
278 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
279
280 return enable_irq_wake(rtc->irq);
281 }
282
283 return 0;
284}
285
286static int armada38x_rtc_resume(struct device *dev)
287{
288 if (device_may_wakeup(dev)) {
289 struct armada38x_rtc *rtc = dev_get_drvdata(dev);
290
291 return disable_irq_wake(rtc->irq);
292 }
293
294 return 0;
295}
296#endif
297
298static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
299 armada38x_rtc_suspend, armada38x_rtc_resume);
300
301#ifdef CONFIG_OF
302static const struct of_device_id armada38x_rtc_of_match_table[] = {
303 { .compatible = "marvell,armada-380-rtc", },
304 {}
305};
306#endif
307
308static struct platform_driver armada38x_rtc_driver = {
309 .driver = {
310 .name = "armada38x-rtc",
311 .pm = &armada38x_rtc_pm_ops,
312 .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
313 },
314};
315
316module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
317
318MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
319MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
320MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-at91sam9.c b/drivers/rtc/rtc-at91sam9.c
index 6b9aaf1afc72..2183fd2750ab 100644
--- a/drivers/rtc/rtc-at91sam9.c
+++ b/drivers/rtc/rtc-at91sam9.c
@@ -313,7 +313,7 @@ static const struct rtc_class_ops at91_rtc_ops = {
313 .alarm_irq_enable = at91_rtc_alarm_irq_enable, 313 .alarm_irq_enable = at91_rtc_alarm_irq_enable,
314}; 314};
315 315
316static struct regmap_config gpbr_regmap_config = { 316static const struct regmap_config gpbr_regmap_config = {
317 .reg_bits = 32, 317 .reg_bits = 32,
318 .val_bits = 32, 318 .val_bits = 32,
319 .reg_stride = 4, 319 .reg_stride = 4,
diff --git a/drivers/rtc/rtc-ds1685.c b/drivers/rtc/rtc-ds1685.c
new file mode 100644
index 000000000000..8c3bfcb115b7
--- /dev/null
+++ b/drivers/rtc/rtc-ds1685.c
@@ -0,0 +1,2252 @@
1/*
2 * An rtc driver for the Dallas/Maxim DS1685/DS1687 and related real-time
3 * chips.
4 *
5 * Copyright (C) 2011-2014 Joshua Kinard <kumba@gentoo.org>.
6 * Copyright (C) 2009 Matthias Fuchs <matthias.fuchs@esd-electronics.com>.
7 *
8 * References:
9 * DS1685/DS1687 3V/5V Real-Time Clocks, 19-5215, Rev 4/10.
10 * DS17x85/DS17x87 3V/5V Real-Time Clocks, 19-5222, Rev 4/10.
11 * DS1689/DS1693 3V/5V Serialized Real-Time Clocks, Rev 112105.
12 * Application Note 90, Using the Multiplex Bus RTC Extended Features.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/bcd.h>
20#include <linux/delay.h>
21#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/rtc.h>
25#include <linux/workqueue.h>
26
27#include <linux/rtc/ds1685.h>
28
29#ifdef CONFIG_PROC_FS
30#include <linux/proc_fs.h>
31#endif
32
33#define DRV_VERSION "0.42.0"
34
35
36/* ----------------------------------------------------------------------- */
37/* Standard read/write functions if platform does not provide overrides */
38
39/**
40 * ds1685_read - read a value from an rtc register.
41 * @rtc: pointer to the ds1685 rtc structure.
42 * @reg: the register address to read.
43 */
44static u8
45ds1685_read(struct ds1685_priv *rtc, int reg)
46{
47 return readb((u8 __iomem *)rtc->regs +
48 (reg * rtc->regstep));
49}
50
51/**
52 * ds1685_write - write a value to an rtc register.
53 * @rtc: pointer to the ds1685 rtc structure.
54 * @reg: the register address to write.
55 * @value: value to write to the register.
56 */
57static void
58ds1685_write(struct ds1685_priv *rtc, int reg, u8 value)
59{
60 writeb(value, ((u8 __iomem *)rtc->regs +
61 (reg * rtc->regstep)));
62}
63/* ----------------------------------------------------------------------- */
64
65
66/* ----------------------------------------------------------------------- */
67/* Inlined functions */
68
69/**
70 * ds1685_rtc_bcd2bin - bcd2bin wrapper in case platform doesn't support BCD.
71 * @rtc: pointer to the ds1685 rtc structure.
72 * @val: u8 time value to consider converting.
73 * @bcd_mask: u8 mask value if BCD mode is used.
74 * @bin_mask: u8 mask value if BIN mode is used.
75 *
76 * Returns the value, converted to BIN if originally in BCD and bcd_mode TRUE.
77 */
78static inline u8
79ds1685_rtc_bcd2bin(struct ds1685_priv *rtc, u8 val, u8 bcd_mask, u8 bin_mask)
80{
81 if (rtc->bcd_mode)
82 return (bcd2bin(val) & bcd_mask);
83
84 return (val & bin_mask);
85}
86
87/**
88 * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD.
89 * @rtc: pointer to the ds1685 rtc structure.
90 * @val: u8 time value to consider converting.
91 * @bin_mask: u8 mask value if BIN mode is used.
92 * @bcd_mask: u8 mask value if BCD mode is used.
93 *
94 * Returns the value, converted to BCD if originally in BIN and bcd_mode TRUE.
95 */
96static inline u8
97ds1685_rtc_bin2bcd(struct ds1685_priv *rtc, u8 val, u8 bin_mask, u8 bcd_mask)
98{
99 if (rtc->bcd_mode)
100 return (bin2bcd(val) & bcd_mask);
101
102 return (val & bin_mask);
103}
104
105/**
106 * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0.
107 * @rtc: pointer to the ds1685 rtc structure.
108 */
109static inline void
110ds1685_rtc_switch_to_bank0(struct ds1685_priv *rtc)
111{
112 rtc->write(rtc, RTC_CTRL_A,
113 (rtc->read(rtc, RTC_CTRL_A) & ~(RTC_CTRL_A_DV0)));
114}
115
116/**
117 * ds1685_rtc_switch_to_bank1 - switch the rtc to bank 1.
118 * @rtc: pointer to the ds1685 rtc structure.
119 */
120static inline void
121ds1685_rtc_switch_to_bank1(struct ds1685_priv *rtc)
122{
123 rtc->write(rtc, RTC_CTRL_A,
124 (rtc->read(rtc, RTC_CTRL_A) | RTC_CTRL_A_DV0));
125}
126
127/**
128 * ds1685_rtc_begin_data_access - prepare the rtc for data access.
129 * @rtc: pointer to the ds1685 rtc structure.
130 *
131 * This takes several steps to prepare the rtc for access to get/set time
132 * and alarm values from the rtc registers:
133 * - Sets the SET bit in Control Register B.
134 * - Reads Ext Control Register 4A and checks the INCR bit.
135 * - If INCR is active, a short delay is added before Ext Control Register 4A
136 * is read again in a loop until INCR is inactive.
137 * - Switches the rtc to bank 1. This allows access to all relevant
138 * data for normal rtc operation, as bank 0 contains only the nvram.
139 */
140static inline void
141ds1685_rtc_begin_data_access(struct ds1685_priv *rtc)
142{
143 /* Set the SET bit in Ctrl B */
144 rtc->write(rtc, RTC_CTRL_B,
145 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
146
147 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
148 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
149 cpu_relax();
150
151 /* Switch to Bank 1 */
152 ds1685_rtc_switch_to_bank1(rtc);
153}
154
155/**
156 * ds1685_rtc_end_data_access - end data access on the rtc.
157 * @rtc: pointer to the ds1685 rtc structure.
158 *
159 * This ends what was started by ds1685_rtc_begin_data_access:
160 * - Switches the rtc back to bank 0.
161 * - Clears the SET bit in Control Register B.
162 */
163static inline void
164ds1685_rtc_end_data_access(struct ds1685_priv *rtc)
165{
166 /* Switch back to Bank 0 */
167 ds1685_rtc_switch_to_bank1(rtc);
168
169 /* Clear the SET bit in Ctrl B */
170 rtc->write(rtc, RTC_CTRL_B,
171 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
172}
173
174/**
175 * ds1685_rtc_begin_ctrl_access - prepare the rtc for ctrl access.
176 * @rtc: pointer to the ds1685 rtc structure.
177 * @flags: irq flags variable for spin_lock_irqsave.
178 *
179 * This takes several steps to prepare the rtc for access to read just the
180 * control registers:
181 * - Sets a spinlock on the rtc IRQ.
182 * - Switches the rtc to bank 1. This allows access to the two extended
183 * control registers.
184 *
185 * Only use this where you are certain another lock will not be held.
186 */
187static inline void
188ds1685_rtc_begin_ctrl_access(struct ds1685_priv *rtc, unsigned long flags)
189{
190 spin_lock_irqsave(&rtc->lock, flags);
191 ds1685_rtc_switch_to_bank1(rtc);
192}
193
194/**
195 * ds1685_rtc_end_ctrl_access - end ctrl access on the rtc.
196 * @rtc: pointer to the ds1685 rtc structure.
197 * @flags: irq flags variable for spin_unlock_irqrestore.
198 *
199 * This ends what was started by ds1685_rtc_begin_ctrl_access:
200 * - Switches the rtc back to bank 0.
201 * - Unsets the spinlock on the rtc IRQ.
202 */
203static inline void
204ds1685_rtc_end_ctrl_access(struct ds1685_priv *rtc, unsigned long flags)
205{
206 ds1685_rtc_switch_to_bank0(rtc);
207 spin_unlock_irqrestore(&rtc->lock, flags);
208}
209
210/**
211 * ds1685_rtc_get_ssn - retrieve the silicon serial number.
212 * @rtc: pointer to the ds1685 rtc structure.
213 * @ssn: u8 array to hold the bits of the silicon serial number.
214 *
215 * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The
216 * first byte is the model number, the next six bytes are the serial number
217 * digits, and the final byte is a CRC check byte. Together, they form the
218 * silicon serial number.
219 *
220 * These values are stored in bank1, so ds1685_rtc_switch_to_bank1 must be
221 * called first before calling this function, else data will be read out of
222 * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done.
223 */
224static inline void
225ds1685_rtc_get_ssn(struct ds1685_priv *rtc, u8 *ssn)
226{
227 ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL);
228 ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1);
229 ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2);
230 ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3);
231 ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4);
232 ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5);
233 ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6);
234 ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC);
235}
236/* ----------------------------------------------------------------------- */
237
238
239/* ----------------------------------------------------------------------- */
240/* Read/Set Time & Alarm functions */
241
242/**
243 * ds1685_rtc_read_time - reads the time registers.
244 * @dev: pointer to device structure.
245 * @tm: pointer to rtc_time structure.
246 */
247static int
248ds1685_rtc_read_time(struct device *dev, struct rtc_time *tm)
249{
250 struct platform_device *pdev = to_platform_device(dev);
251 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
252 u8 ctrlb, century;
253 u8 seconds, minutes, hours, wday, mday, month, years;
254
255 /* Fetch the time info from the RTC registers. */
256 ds1685_rtc_begin_data_access(rtc);
257 seconds = rtc->read(rtc, RTC_SECS);
258 minutes = rtc->read(rtc, RTC_MINS);
259 hours = rtc->read(rtc, RTC_HRS);
260 wday = rtc->read(rtc, RTC_WDAY);
261 mday = rtc->read(rtc, RTC_MDAY);
262 month = rtc->read(rtc, RTC_MONTH);
263 years = rtc->read(rtc, RTC_YEAR);
264 century = rtc->read(rtc, RTC_CENTURY);
265 ctrlb = rtc->read(rtc, RTC_CTRL_B);
266 ds1685_rtc_end_data_access(rtc);
267
268 /* bcd2bin if needed, perform fixups, and store to rtc_time. */
269 years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK,
270 RTC_YEAR_BIN_MASK);
271 century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK,
272 RTC_CENTURY_MASK);
273 tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK,
274 RTC_SECS_BIN_MASK);
275 tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK,
276 RTC_MINS_BIN_MASK);
277 tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK,
278 RTC_HRS_24_BIN_MASK);
279 tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK,
280 RTC_WDAY_MASK) - 1);
281 tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
282 RTC_MDAY_BIN_MASK);
283 tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK,
284 RTC_MONTH_BIN_MASK) - 1);
285 tm->tm_year = ((years + (century * 100)) - 1900);
286 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
287 tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */
288
289 return rtc_valid_tm(tm);
290}
291
292/**
293 * ds1685_rtc_set_time - sets the time registers.
294 * @dev: pointer to device structure.
295 * @tm: pointer to rtc_time structure.
296 */
297static int
298ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm)
299{
300 struct platform_device *pdev = to_platform_device(dev);
301 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
302 u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century;
303
304 /* Fetch the time info from rtc_time. */
305 seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK,
306 RTC_SECS_BCD_MASK);
307 minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK,
308 RTC_MINS_BCD_MASK);
309 hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK,
310 RTC_HRS_24_BCD_MASK);
311 wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK,
312 RTC_WDAY_MASK);
313 mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK,
314 RTC_MDAY_BCD_MASK);
315 month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK,
316 RTC_MONTH_BCD_MASK);
317 years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100),
318 RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK);
319 century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100),
320 RTC_CENTURY_MASK, RTC_CENTURY_MASK);
321
322 /*
323 * Perform Sanity Checks:
324 * - Months: !> 12, Month Day != 0.
325 * - Month Day !> Max days in current month.
326 * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7.
327 */
328 if ((tm->tm_mon > 11) || (mday == 0))
329 return -EDOM;
330
331 if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year))
332 return -EDOM;
333
334 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) ||
335 (tm->tm_sec >= 60) || (wday > 7))
336 return -EDOM;
337
338 /*
339 * Set the data mode to use and store the time values in the
340 * RTC registers.
341 */
342 ds1685_rtc_begin_data_access(rtc);
343 ctrlb = rtc->read(rtc, RTC_CTRL_B);
344 if (rtc->bcd_mode)
345 ctrlb &= ~(RTC_CTRL_B_DM);
346 else
347 ctrlb |= RTC_CTRL_B_DM;
348 rtc->write(rtc, RTC_CTRL_B, ctrlb);
349 rtc->write(rtc, RTC_SECS, seconds);
350 rtc->write(rtc, RTC_MINS, minutes);
351 rtc->write(rtc, RTC_HRS, hours);
352 rtc->write(rtc, RTC_WDAY, wday);
353 rtc->write(rtc, RTC_MDAY, mday);
354 rtc->write(rtc, RTC_MONTH, month);
355 rtc->write(rtc, RTC_YEAR, years);
356 rtc->write(rtc, RTC_CENTURY, century);
357 ds1685_rtc_end_data_access(rtc);
358
359 return 0;
360}
361
362/**
363 * ds1685_rtc_read_alarm - reads the alarm registers.
364 * @dev: pointer to device structure.
365 * @alrm: pointer to rtc_wkalrm structure.
366 *
367 * There are three primary alarm registers: seconds, minutes, and hours.
368 * A fourth alarm register for the month date is also available in bank1 for
369 * kickstart/wakeup features. The DS1685/DS1687 manual states that a
370 * "don't care" value ranging from 0xc0 to 0xff may be written into one or
371 * more of the three alarm bytes to act as a wildcard value. The fourth
372 * byte doesn't support a "don't care" value.
373 */
374static int
375ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
376{
377 struct platform_device *pdev = to_platform_device(dev);
378 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
379 u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
380
381 /* Fetch the alarm info from the RTC alarm registers. */
382 ds1685_rtc_begin_data_access(rtc);
383 seconds = rtc->read(rtc, RTC_SECS_ALARM);
384 minutes = rtc->read(rtc, RTC_MINS_ALARM);
385 hours = rtc->read(rtc, RTC_HRS_ALARM);
386 mday = rtc->read(rtc, RTC_MDAY_ALARM);
387 ctrlb = rtc->read(rtc, RTC_CTRL_B);
388 ctrlc = rtc->read(rtc, RTC_CTRL_C);
389 ds1685_rtc_end_data_access(rtc);
390
391 /* Check month date. */
392 if (!(mday >= 1) && (mday <= 31))
393 return -EDOM;
394
395 /*
396 * Check the three alarm bytes.
397 *
398 * The Linux RTC system doesn't support the "don't care" capability
399 * of this RTC chip. We check for it anyways in case support is
400 * added in the future.
401 */
402 if (unlikely((seconds >= 0xc0) && (seconds <= 0xff)))
403 alrm->time.tm_sec = -1;
404 else
405 alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds,
406 RTC_SECS_BCD_MASK,
407 RTC_SECS_BIN_MASK);
408
409 if (unlikely((minutes >= 0xc0) && (minutes <= 0xff)))
410 alrm->time.tm_min = -1;
411 else
412 alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes,
413 RTC_MINS_BCD_MASK,
414 RTC_MINS_BIN_MASK);
415
416 if (unlikely((hours >= 0xc0) && (hours <= 0xff)))
417 alrm->time.tm_hour = -1;
418 else
419 alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours,
420 RTC_HRS_24_BCD_MASK,
421 RTC_HRS_24_BIN_MASK);
422
423 /* Write the data to rtc_wkalrm. */
424 alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
425 RTC_MDAY_BIN_MASK);
426 alrm->time.tm_mon = -1;
427 alrm->time.tm_year = -1;
428 alrm->time.tm_wday = -1;
429 alrm->time.tm_yday = -1;
430 alrm->time.tm_isdst = -1;
431 alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE);
432 alrm->pending = !!(ctrlc & RTC_CTRL_C_AF);
433
434 return 0;
435}
436
437/**
438 * ds1685_rtc_set_alarm - sets the alarm in registers.
439 * @dev: pointer to device structure.
440 * @alrm: pointer to rtc_wkalrm structure.
441 */
442static int
443ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
444{
445 struct platform_device *pdev = to_platform_device(dev);
446 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
447 u8 ctrlb, seconds, minutes, hours, mday;
448
449 /* Fetch the alarm info and convert to BCD. */
450 seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
451 RTC_SECS_BIN_MASK,
452 RTC_SECS_BCD_MASK);
453 minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min,
454 RTC_MINS_BIN_MASK,
455 RTC_MINS_BCD_MASK);
456 hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour,
457 RTC_HRS_24_BIN_MASK,
458 RTC_HRS_24_BCD_MASK);
459 mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday,
460 RTC_MDAY_BIN_MASK,
461 RTC_MDAY_BCD_MASK);
462
463 /* Check the month date for validity. */
464 if (!(mday >= 1) && (mday <= 31))
465 return -EDOM;
466
467 /*
468 * Check the three alarm bytes.
469 *
470 * The Linux RTC system doesn't support the "don't care" capability
471 * of this RTC chip because rtc_valid_tm tries to validate every
472 * field, and we only support four fields. We put the support
473 * here anyways for the future.
474 */
475 if (unlikely((seconds >= 0xc0) && (seconds <= 0xff)))
476 seconds = 0xff;
477
478 if (unlikely((minutes >= 0xc0) && (minutes <= 0xff)))
479 minutes = 0xff;
480
481 if (unlikely((hours >= 0xc0) && (hours <= 0xff)))
482 hours = 0xff;
483
484 alrm->time.tm_mon = -1;
485 alrm->time.tm_year = -1;
486 alrm->time.tm_wday = -1;
487 alrm->time.tm_yday = -1;
488 alrm->time.tm_isdst = -1;
489
490 /* Disable the alarm interrupt first. */
491 ds1685_rtc_begin_data_access(rtc);
492 ctrlb = rtc->read(rtc, RTC_CTRL_B);
493 rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE)));
494
495 /* Read ctrlc to clear RTC_CTRL_C_AF. */
496 rtc->read(rtc, RTC_CTRL_C);
497
498 /*
499 * Set the data mode to use and store the time values in the
500 * RTC registers.
501 */
502 ctrlb = rtc->read(rtc, RTC_CTRL_B);
503 if (rtc->bcd_mode)
504 ctrlb &= ~(RTC_CTRL_B_DM);
505 else
506 ctrlb |= RTC_CTRL_B_DM;
507 rtc->write(rtc, RTC_CTRL_B, ctrlb);
508 rtc->write(rtc, RTC_SECS_ALARM, seconds);
509 rtc->write(rtc, RTC_MINS_ALARM, minutes);
510 rtc->write(rtc, RTC_HRS_ALARM, hours);
511 rtc->write(rtc, RTC_MDAY_ALARM, mday);
512
513 /* Re-enable the alarm if needed. */
514 if (alrm->enabled) {
515 ctrlb = rtc->read(rtc, RTC_CTRL_B);
516 ctrlb |= RTC_CTRL_B_AIE;
517 rtc->write(rtc, RTC_CTRL_B, ctrlb);
518 }
519
520 /* Done! */
521 ds1685_rtc_end_data_access(rtc);
522
523 return 0;
524}
525/* ----------------------------------------------------------------------- */
526
527
528/* ----------------------------------------------------------------------- */
529/* /dev/rtcX Interface functions */
530
531#ifdef CONFIG_RTC_INTF_DEV
532/**
533 * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off.
534 * @dev: pointer to device structure.
535 * @enabled: flag indicating whether to enable or disable.
536 */
537static int
538ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
539{
540 struct ds1685_priv *rtc = dev_get_drvdata(dev);
541 unsigned long flags = 0;
542
543 /* Enable/disable the Alarm IRQ-Enable flag. */
544 spin_lock_irqsave(&rtc->lock, flags);
545
546 /* Flip the requisite interrupt-enable bit. */
547 if (enabled)
548 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) |
549 RTC_CTRL_B_AIE));
550 else
551 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) &
552 ~(RTC_CTRL_B_AIE)));
553
554 /* Read Control C to clear all the flag bits. */
555 rtc->read(rtc, RTC_CTRL_C);
556 spin_unlock_irqrestore(&rtc->lock, flags);
557
558 return 0;
559}
560#endif
561/* ----------------------------------------------------------------------- */
562
563
564/* ----------------------------------------------------------------------- */
565/* IRQ handler & workqueue. */
566
567/**
568 * ds1685_rtc_irq_handler - IRQ handler.
569 * @irq: IRQ number.
570 * @dev_id: platform device pointer.
571 */
572static irqreturn_t
573ds1685_rtc_irq_handler(int irq, void *dev_id)
574{
575 struct platform_device *pdev = dev_id;
576 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
577 u8 ctrlb, ctrlc;
578 unsigned long events = 0;
579 u8 num_irqs = 0;
580
581 /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
582 if (unlikely(!rtc))
583 return IRQ_HANDLED;
584
585 /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
586 spin_lock(&rtc->lock);
587 ctrlb = rtc->read(rtc, RTC_CTRL_B);
588 ctrlc = rtc->read(rtc, RTC_CTRL_C);
589
590 /* Is the IRQF bit set? */
591 if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
592 /*
593 * We need to determine if it was one of the standard
594 * events: PF, AF, or UF. If so, we handle them and
595 * update the RTC core.
596 */
597 if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
598 events = RTC_IRQF;
599
600 /* Check for a periodic interrupt. */
601 if ((ctrlb & RTC_CTRL_B_PIE) &&
602 (ctrlc & RTC_CTRL_C_PF)) {
603 events |= RTC_PF;
604 num_irqs++;
605 }
606
607 /* Check for an alarm interrupt. */
608 if ((ctrlb & RTC_CTRL_B_AIE) &&
609 (ctrlc & RTC_CTRL_C_AF)) {
610 events |= RTC_AF;
611 num_irqs++;
612 }
613
614 /* Check for an update interrupt. */
615 if ((ctrlb & RTC_CTRL_B_UIE) &&
616 (ctrlc & RTC_CTRL_C_UF)) {
617 events |= RTC_UF;
618 num_irqs++;
619 }
620
621 rtc_update_irq(rtc->dev, num_irqs, events);
622 } else {
623 /*
624 * One of the "extended" interrupts was received that
625 * is not recognized by the RTC core. These need to
626 * be handled in task context as they can call other
627 * functions and the time spent in irq context needs
628 * to be minimized. Schedule them into a workqueue
629 * and inform the RTC core that the IRQs were handled.
630 */
631 spin_unlock(&rtc->lock);
632 schedule_work(&rtc->work);
633 rtc_update_irq(rtc->dev, 0, 0);
634 return IRQ_HANDLED;
635 }
636 }
637 spin_unlock(&rtc->lock);
638
639 return events ? IRQ_HANDLED : IRQ_NONE;
640}
641
642/**
643 * ds1685_rtc_work_queue - work queue handler.
644 * @work: work_struct containing data to work on in task context.
645 */
646static void
647ds1685_rtc_work_queue(struct work_struct *work)
648{
649 struct ds1685_priv *rtc = container_of(work,
650 struct ds1685_priv, work);
651 struct platform_device *pdev = to_platform_device(&rtc->dev->dev);
652 struct mutex *rtc_mutex = &rtc->dev->ops_lock;
653 u8 ctrl4a, ctrl4b;
654
655 mutex_lock(rtc_mutex);
656
657 ds1685_rtc_switch_to_bank1(rtc);
658 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
659 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
660
661 /*
662 * Check for a kickstart interrupt. With Vcc applied, this
663 * typically means that the power button was pressed, so we
664 * begin the shutdown sequence.
665 */
666 if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) {
667 /* Briefly disable kickstarts to debounce button presses. */
668 rtc->write(rtc, RTC_EXT_CTRL_4B,
669 (rtc->read(rtc, RTC_EXT_CTRL_4B) &
670 ~(RTC_CTRL_4B_KSE)));
671
672 /* Clear the kickstart flag. */
673 rtc->write(rtc, RTC_EXT_CTRL_4A,
674 (ctrl4a & ~(RTC_CTRL_4A_KF)));
675
676
677 /*
678 * Sleep 500ms before re-enabling kickstarts. This allows
679 * adequate time to avoid reading signal jitter as additional
680 * button presses.
681 */
682 msleep(500);
683 rtc->write(rtc, RTC_EXT_CTRL_4B,
684 (rtc->read(rtc, RTC_EXT_CTRL_4B) |
685 RTC_CTRL_4B_KSE));
686
687 /* Call the platform pre-poweroff function. Else, shutdown. */
688 if (rtc->prepare_poweroff != NULL)
689 rtc->prepare_poweroff();
690 else
691 ds1685_rtc_poweroff(pdev);
692 }
693
694 /*
695 * Check for a wake-up interrupt. With Vcc applied, this is
696 * essentially a second alarm interrupt, except it takes into
697 * account the 'date' register in bank1 in addition to the
698 * standard three alarm registers.
699 */
700 if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) {
701 rtc->write(rtc, RTC_EXT_CTRL_4A,
702 (ctrl4a & ~(RTC_CTRL_4A_WF)));
703
704 /* Call the platform wake_alarm function if defined. */
705 if (rtc->wake_alarm != NULL)
706 rtc->wake_alarm();
707 else
708 dev_warn(&pdev->dev,
709 "Wake Alarm IRQ just occurred!\n");
710 }
711
712 /*
713 * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0
714 * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting
715 * each byte to a logic 1. This has no effect on any extended
716 * NV-SRAM that might be present, nor on the time/calendar/alarm
717 * registers. After a ram-clear is completed, there is a minimum
718 * recovery time of ~150ms in which all reads/writes are locked out.
719 * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot
720 * catch this scenario.
721 */
722 if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) {
723 rtc->write(rtc, RTC_EXT_CTRL_4A,
724 (ctrl4a & ~(RTC_CTRL_4A_RF)));
725 msleep(150);
726
727 /* Call the platform post_ram_clear function if defined. */
728 if (rtc->post_ram_clear != NULL)
729 rtc->post_ram_clear();
730 else
731 dev_warn(&pdev->dev,
732 "RAM-Clear IRQ just occurred!\n");
733 }
734 ds1685_rtc_switch_to_bank0(rtc);
735
736 mutex_unlock(rtc_mutex);
737}
738/* ----------------------------------------------------------------------- */
739
740
741/* ----------------------------------------------------------------------- */
742/* ProcFS interface */
743
744#ifdef CONFIG_PROC_FS
745#define NUM_REGS 6 /* Num of control registers. */
746#define NUM_BITS 8 /* Num bits per register. */
747#define NUM_SPACES 4 /* Num spaces between each bit. */
748
749/*
750 * Periodic Interrupt Rates.
751 */
752static const char *ds1685_rtc_pirq_rate[16] = {
753 "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms",
754 "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms",
755 "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms"
756};
757
758/*
759 * Square-Wave Output Frequencies.
760 */
761static const char *ds1685_rtc_sqw_freq[16] = {
762 "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz",
763 "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz"
764};
765
766#ifdef CONFIG_RTC_DS1685_PROC_REGS
767/**
768 * ds1685_rtc_print_regs - helper function to print register values.
769 * @hex: hex byte to convert into binary bits.
770 * @dest: destination char array.
771 *
772 * This is basically a hex->binary function, just with extra spacing between
773 * the digits. It only works on 1-byte values (8 bits).
774 */
775static char*
776ds1685_rtc_print_regs(u8 hex, char *dest)
777{
778 u32 i, j;
779 char *tmp = dest;
780
781 for (i = 0; i < NUM_BITS; i++) {
782 *tmp++ = ((hex & 0x80) != 0 ? '1' : '0');
783 for (j = 0; j < NUM_SPACES; j++)
784 *tmp++ = ' ';
785 hex <<= 1;
786 }
787 *tmp++ = '\0';
788
789 return dest;
790}
791#endif
792
793/**
794 * ds1685_rtc_proc - procfs access function.
795 * @dev: pointer to device structure.
796 * @seq: pointer to seq_file structure.
797 */
798static int
799ds1685_rtc_proc(struct device *dev, struct seq_file *seq)
800{
801 struct platform_device *pdev = to_platform_device(dev);
802 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
803 u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8];
804 char *model = '\0';
805#ifdef CONFIG_RTC_DS1685_PROC_REGS
806 char bits[NUM_REGS][(NUM_BITS * NUM_SPACES) + NUM_BITS + 1];
807#endif
808
809 /* Read all the relevant data from the control registers. */
810 ds1685_rtc_switch_to_bank1(rtc);
811 ds1685_rtc_get_ssn(rtc, ssn);
812 ctrla = rtc->read(rtc, RTC_CTRL_A);
813 ctrlb = rtc->read(rtc, RTC_CTRL_B);
814 ctrlc = rtc->read(rtc, RTC_CTRL_C);
815 ctrld = rtc->read(rtc, RTC_CTRL_D);
816 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
817 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
818 ds1685_rtc_switch_to_bank0(rtc);
819
820 /* Determine the RTC model. */
821 switch (ssn[0]) {
822 case RTC_MODEL_DS1685:
823 model = "DS1685/DS1687\0";
824 break;
825 case RTC_MODEL_DS1689:
826 model = "DS1689/DS1693\0";
827 break;
828 case RTC_MODEL_DS17285:
829 model = "DS17285/DS17287\0";
830 break;
831 case RTC_MODEL_DS17485:
832 model = "DS17485/DS17487\0";
833 break;
834 case RTC_MODEL_DS17885:
835 model = "DS17885/DS17887\0";
836 break;
837 default:
838 model = "Unknown\0";
839 break;
840 }
841
842 /* Print out the information. */
843 seq_printf(seq,
844 "Model\t\t: %s\n"
845 "Oscillator\t: %s\n"
846 "12/24hr\t\t: %s\n"
847 "DST\t\t: %s\n"
848 "Data mode\t: %s\n"
849 "Battery\t\t: %s\n"
850 "Aux batt\t: %s\n"
851 "Update IRQ\t: %s\n"
852 "Periodic IRQ\t: %s\n"
853 "Periodic Rate\t: %s\n"
854 "SQW Freq\t: %s\n"
855#ifdef CONFIG_RTC_DS1685_PROC_REGS
856 "Serial #\t: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"
857 "Register Status\t:\n"
858 " Ctrl A\t: UIP DV2 DV1 DV0 RS3 RS2 RS1 RS0\n"
859 "\t\t: %s\n"
860 " Ctrl B\t: SET PIE AIE UIE SQWE DM 2412 DSE\n"
861 "\t\t: %s\n"
862 " Ctrl C\t: IRQF PF AF UF --- --- --- ---\n"
863 "\t\t: %s\n"
864 " Ctrl D\t: VRT --- --- --- --- --- --- ---\n"
865 "\t\t: %s\n"
866#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
867 " Ctrl 4A\t: VRT2 INCR BME --- PAB RF WF KF\n"
868#else
869 " Ctrl 4A\t: VRT2 INCR --- --- PAB RF WF KF\n"
870#endif
871 "\t\t: %s\n"
872 " Ctrl 4B\t: ABE E32k CS RCE PRS RIE WIE KSE\n"
873 "\t\t: %s\n",
874#else
875 "Serial #\t: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
876#endif
877 model,
878 ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"),
879 ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"),
880 ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"),
881 ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"),
882 ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"),
883 ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"),
884 ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"),
885 ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"),
886 (!(ctrl4b & RTC_CTRL_4B_E32K) ?
887 ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"),
888 (!((ctrl4b & RTC_CTRL_4B_E32K)) ?
889 ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"),
890#ifdef CONFIG_RTC_DS1685_PROC_REGS
891 ssn[0], ssn[1], ssn[2], ssn[3], ssn[4], ssn[5], ssn[6], ssn[7],
892 ds1685_rtc_print_regs(ctrla, bits[0]),
893 ds1685_rtc_print_regs(ctrlb, bits[1]),
894 ds1685_rtc_print_regs(ctrlc, bits[2]),
895 ds1685_rtc_print_regs(ctrld, bits[3]),
896 ds1685_rtc_print_regs(ctrl4a, bits[4]),
897 ds1685_rtc_print_regs(ctrl4b, bits[5]));
898#else
899 ssn[0], ssn[1], ssn[2], ssn[3], ssn[4], ssn[5], ssn[6], ssn[7]);
900#endif
901 return 0;
902}
903#else
904#define ds1685_rtc_proc NULL
905#endif /* CONFIG_PROC_FS */
906/* ----------------------------------------------------------------------- */
907
908
909/* ----------------------------------------------------------------------- */
910/* RTC Class operations */
911
912static const struct rtc_class_ops
913ds1685_rtc_ops = {
914 .proc = ds1685_rtc_proc,
915 .read_time = ds1685_rtc_read_time,
916 .set_time = ds1685_rtc_set_time,
917 .read_alarm = ds1685_rtc_read_alarm,
918 .set_alarm = ds1685_rtc_set_alarm,
919 .alarm_irq_enable = ds1685_rtc_alarm_irq_enable,
920};
921/* ----------------------------------------------------------------------- */
922
923
924/* ----------------------------------------------------------------------- */
925/* SysFS interface */
926
927#ifdef CONFIG_SYSFS
928/**
929 * ds1685_rtc_sysfs_nvram_read - reads rtc nvram via sysfs.
930 * @file: pointer to file structure.
931 * @kobj: pointer to kobject structure.
932 * @bin_attr: pointer to bin_attribute structure.
933 * @buf: pointer to char array to hold the output.
934 * @pos: current file position pointer.
935 * @size: size of the data to read.
936 */
937static ssize_t
938ds1685_rtc_sysfs_nvram_read(struct file *filp, struct kobject *kobj,
939 struct bin_attribute *bin_attr, char *buf,
940 loff_t pos, size_t size)
941{
942 struct platform_device *pdev =
943 to_platform_device(container_of(kobj, struct device, kobj));
944 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
945 ssize_t count;
946 unsigned long flags = 0;
947
948 spin_lock_irqsave(&rtc->lock, flags);
949 ds1685_rtc_switch_to_bank0(rtc);
950
951 /* Read NVRAM in time and bank0 registers. */
952 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
953 count++, size--) {
954 if (count < NVRAM_SZ_TIME)
955 *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++));
956 else
957 *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++));
958 }
959
960#ifndef CONFIG_RTC_DRV_DS1689
961 if (size > 0) {
962 ds1685_rtc_switch_to_bank1(rtc);
963
964#ifndef CONFIG_RTC_DRV_DS1685
965 /* Enable burst-mode on DS17x85/DS17x87 */
966 rtc->write(rtc, RTC_EXT_CTRL_4A,
967 (rtc->read(rtc, RTC_EXT_CTRL_4A) |
968 RTC_CTRL_4A_BME));
969
970 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
971 * reading with burst-mode */
972 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
973 (pos - NVRAM_TOTAL_SZ_BANK0));
974#endif
975
976 /* Read NVRAM in bank1 registers. */
977 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
978 count++, size--) {
979#ifdef CONFIG_RTC_DRV_DS1685
980 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
981 * before each read. */
982 rtc->write(rtc, RTC_BANK1_RAM_ADDR,
983 (pos - NVRAM_TOTAL_SZ_BANK0));
984#endif
985 *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT);
986 pos++;
987 }
988
989#ifndef CONFIG_RTC_DRV_DS1685
990 /* Disable burst-mode on DS17x85/DS17x87 */
991 rtc->write(rtc, RTC_EXT_CTRL_4A,
992 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
993 ~(RTC_CTRL_4A_BME)));
994#endif
995 ds1685_rtc_switch_to_bank0(rtc);
996 }
997#endif /* !CONFIG_RTC_DRV_DS1689 */
998 spin_unlock_irqrestore(&rtc->lock, flags);
999
1000 /*
1001 * XXX: Bug? this appears to cause the function to get executed
1002 * several times in succession. But it's the only way to actually get
1003 * data written out to a file.
1004 */
1005 return count;
1006}
1007
1008/**
1009 * ds1685_rtc_sysfs_nvram_write - writes rtc nvram via sysfs.
1010 * @file: pointer to file structure.
1011 * @kobj: pointer to kobject structure.
1012 * @bin_attr: pointer to bin_attribute structure.
1013 * @buf: pointer to char array to hold the input.
1014 * @pos: current file position pointer.
1015 * @size: size of the data to write.
1016 */
1017static ssize_t
1018ds1685_rtc_sysfs_nvram_write(struct file *filp, struct kobject *kobj,
1019 struct bin_attribute *bin_attr, char *buf,
1020 loff_t pos, size_t size)
1021{
1022 struct platform_device *pdev =
1023 to_platform_device(container_of(kobj, struct device, kobj));
1024 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1025 ssize_t count;
1026 unsigned long flags = 0;
1027
1028 spin_lock_irqsave(&rtc->lock, flags);
1029 ds1685_rtc_switch_to_bank0(rtc);
1030
1031 /* Write NVRAM in time and bank0 registers. */
1032 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
1033 count++, size--)
1034 if (count < NVRAM_SZ_TIME)
1035 rtc->write(rtc, (NVRAM_TIME_BASE + pos++),
1036 *buf++);
1037 else
1038 rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++);
1039
1040#ifndef CONFIG_RTC_DRV_DS1689
1041 if (size > 0) {
1042 ds1685_rtc_switch_to_bank1(rtc);
1043
1044#ifndef CONFIG_RTC_DRV_DS1685
1045 /* Enable burst-mode on DS17x85/DS17x87 */
1046 rtc->write(rtc, RTC_EXT_CTRL_4A,
1047 (rtc->read(rtc, RTC_EXT_CTRL_4A) |
1048 RTC_CTRL_4A_BME));
1049
1050 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
1051 * writing with burst-mode */
1052 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
1053 (pos - NVRAM_TOTAL_SZ_BANK0));
1054#endif
1055
1056 /* Write NVRAM in bank1 registers. */
1057 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
1058 count++, size--) {
1059#ifdef CONFIG_RTC_DRV_DS1685
1060 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
1061 * before each read. */
1062 rtc->write(rtc, RTC_BANK1_RAM_ADDR,
1063 (pos - NVRAM_TOTAL_SZ_BANK0));
1064#endif
1065 rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++);
1066 pos++;
1067 }
1068
1069#ifndef CONFIG_RTC_DRV_DS1685
1070 /* Disable burst-mode on DS17x85/DS17x87 */
1071 rtc->write(rtc, RTC_EXT_CTRL_4A,
1072 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
1073 ~(RTC_CTRL_4A_BME)));
1074#endif
1075 ds1685_rtc_switch_to_bank0(rtc);
1076 }
1077#endif /* !CONFIG_RTC_DRV_DS1689 */
1078 spin_unlock_irqrestore(&rtc->lock, flags);
1079
1080 return count;
1081}
1082
1083/**
1084 * struct ds1685_rtc_sysfs_nvram_attr - sysfs attributes for rtc nvram.
1085 * @attr: nvram attributes.
1086 * @read: nvram read function.
1087 * @write: nvram write function.
1088 * @size: nvram total size (bank0 + extended).
1089 */
1090static struct bin_attribute
1091ds1685_rtc_sysfs_nvram_attr = {
1092 .attr = {
1093 .name = "nvram",
1094 .mode = S_IRUGO | S_IWUSR,
1095 },
1096 .read = ds1685_rtc_sysfs_nvram_read,
1097 .write = ds1685_rtc_sysfs_nvram_write,
1098 .size = NVRAM_TOTAL_SZ
1099};
1100
1101/**
1102 * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status.
1103 * @dev: pointer to device structure.
1104 * @attr: pointer to device_attribute structure.
1105 * @buf: pointer to char array to hold the output.
1106 */
1107static ssize_t
1108ds1685_rtc_sysfs_battery_show(struct device *dev,
1109 struct device_attribute *attr, char *buf)
1110{
1111 struct platform_device *pdev = to_platform_device(dev);
1112 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1113 u8 ctrld;
1114
1115 ctrld = rtc->read(rtc, RTC_CTRL_D);
1116
1117 return snprintf(buf, 13, "%s\n",
1118 (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A");
1119}
1120static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL);
1121
1122/**
1123 * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
1124 * @dev: pointer to device structure.
1125 * @attr: pointer to device_attribute structure.
1126 * @buf: pointer to char array to hold the output.
1127 */
1128static ssize_t
1129ds1685_rtc_sysfs_auxbatt_show(struct device *dev,
1130 struct device_attribute *attr, char *buf)
1131{
1132 struct platform_device *pdev = to_platform_device(dev);
1133 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1134 u8 ctrl4a;
1135
1136 ds1685_rtc_switch_to_bank1(rtc);
1137 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
1138 ds1685_rtc_switch_to_bank0(rtc);
1139
1140 return snprintf(buf, 13, "%s\n",
1141 (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A");
1142}
1143static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL);
1144
1145/**
1146 * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
1147 * @dev: pointer to device structure.
1148 * @attr: pointer to device_attribute structure.
1149 * @buf: pointer to char array to hold the output.
1150 */
1151static ssize_t
1152ds1685_rtc_sysfs_serial_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
1154{
1155 struct platform_device *pdev = to_platform_device(dev);
1156 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1157 u8 ssn[8];
1158
1159 ds1685_rtc_switch_to_bank1(rtc);
1160 ds1685_rtc_get_ssn(rtc, ssn);
1161 ds1685_rtc_switch_to_bank0(rtc);
1162
1163 return snprintf(buf, 24, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
1164 ssn[0], ssn[1], ssn[2], ssn[3], ssn[4], ssn[5],
1165 ssn[6], ssn[7]);
1166
1167 return 0;
1168}
1169static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
1170
1171/**
1172 * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
1173 */
1174static struct attribute*
1175ds1685_rtc_sysfs_misc_attrs[] = {
1176 &dev_attr_battery.attr,
1177 &dev_attr_auxbatt.attr,
1178 &dev_attr_serial.attr,
1179 NULL,
1180};
1181
1182/**
1183 * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
1184 */
1185static const struct attribute_group
1186ds1685_rtc_sysfs_misc_grp = {
1187 .name = "misc",
1188 .attrs = ds1685_rtc_sysfs_misc_attrs,
1189};
1190
1191#ifdef CONFIG_RTC_DS1685_SYSFS_REGS
1192/**
1193 * struct ds1685_rtc_ctrl_regs.
1194 * @name: char pointer for the bit name.
1195 * @reg: control register the bit is in.
1196 * @bit: the bit's offset in the register.
1197 */
1198struct ds1685_rtc_ctrl_regs {
1199 const char *name;
1200 const u8 reg;
1201 const u8 bit;
1202};
1203
1204/*
1205 * Ctrl register bit lookup table.
1206 */
1207static const struct ds1685_rtc_ctrl_regs
1208ds1685_ctrl_regs_table[] = {
1209 { "uip", RTC_CTRL_A, RTC_CTRL_A_UIP },
1210 { "dv2", RTC_CTRL_A, RTC_CTRL_A_DV2 },
1211 { "dv1", RTC_CTRL_A, RTC_CTRL_A_DV1 },
1212 { "dv0", RTC_CTRL_A, RTC_CTRL_A_DV0 },
1213 { "rs3", RTC_CTRL_A, RTC_CTRL_A_RS3 },
1214 { "rs2", RTC_CTRL_A, RTC_CTRL_A_RS2 },
1215 { "rs1", RTC_CTRL_A, RTC_CTRL_A_RS1 },
1216 { "rs0", RTC_CTRL_A, RTC_CTRL_A_RS0 },
1217 { "set", RTC_CTRL_B, RTC_CTRL_B_SET },
1218 { "pie", RTC_CTRL_B, RTC_CTRL_B_PIE },
1219 { "aie", RTC_CTRL_B, RTC_CTRL_B_AIE },
1220 { "uie", RTC_CTRL_B, RTC_CTRL_B_UIE },
1221 { "sqwe", RTC_CTRL_B, RTC_CTRL_B_SQWE },
1222 { "dm", RTC_CTRL_B, RTC_CTRL_B_DM },
1223 { "2412", RTC_CTRL_B, RTC_CTRL_B_2412 },
1224 { "dse", RTC_CTRL_B, RTC_CTRL_B_DSE },
1225 { "irqf", RTC_CTRL_C, RTC_CTRL_C_IRQF },
1226 { "pf", RTC_CTRL_C, RTC_CTRL_C_PF },
1227 { "af", RTC_CTRL_C, RTC_CTRL_C_AF },
1228 { "uf", RTC_CTRL_C, RTC_CTRL_C_UF },
1229 { "vrt", RTC_CTRL_D, RTC_CTRL_D_VRT },
1230 { "vrt2", RTC_EXT_CTRL_4A, RTC_CTRL_4A_VRT2 },
1231 { "incr", RTC_EXT_CTRL_4A, RTC_CTRL_4A_INCR },
1232 { "pab", RTC_EXT_CTRL_4A, RTC_CTRL_4A_PAB },
1233 { "rf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_RF },
1234 { "wf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_WF },
1235 { "kf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_KF },
1236#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
1237 { "bme", RTC_EXT_CTRL_4A, RTC_CTRL_4A_BME },
1238#endif
1239 { "abe", RTC_EXT_CTRL_4B, RTC_CTRL_4B_ABE },
1240 { "e32k", RTC_EXT_CTRL_4B, RTC_CTRL_4B_E32K },
1241 { "cs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_CS },
1242 { "rce", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RCE },
1243 { "prs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_PRS },
1244 { "rie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RIE },
1245 { "wie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_WIE },
1246 { "kse", RTC_EXT_CTRL_4B, RTC_CTRL_4B_KSE },
1247 { NULL, 0, 0 },
1248};
1249
1250/**
1251 * ds1685_rtc_sysfs_ctrl_regs_lookup - ctrl register bit lookup function.
1252 * @name: ctrl register bit to look up in ds1685_ctrl_regs_table.
1253 */
1254static const struct ds1685_rtc_ctrl_regs*
1255ds1685_rtc_sysfs_ctrl_regs_lookup(const char *name)
1256{
1257 const struct ds1685_rtc_ctrl_regs *p = ds1685_ctrl_regs_table;
1258
1259 for (; p->name != NULL; ++p)
1260 if (strcmp(p->name, name) == 0)
1261 return p;
1262
1263 return NULL;
1264}
1265
1266/**
1267 * ds1685_rtc_sysfs_ctrl_regs_show - reads a ctrl register bit via sysfs.
1268 * @dev: pointer to device structure.
1269 * @attr: pointer to device_attribute structure.
1270 * @buf: pointer to char array to hold the output.
1271 */
1272static ssize_t
1273ds1685_rtc_sysfs_ctrl_regs_show(struct device *dev,
1274 struct device_attribute *attr, char *buf)
1275{
1276 u8 tmp;
1277 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1278 const struct ds1685_rtc_ctrl_regs *reg_info =
1279 ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
1280
1281 /* Make sure we actually matched something. */
1282 if (!reg_info)
1283 return -EINVAL;
1284
1285 /* No spinlock during a read -- mutex is already held. */
1286 ds1685_rtc_switch_to_bank1(rtc);
1287 tmp = rtc->read(rtc, reg_info->reg) & reg_info->bit;
1288 ds1685_rtc_switch_to_bank0(rtc);
1289
1290 return snprintf(buf, 2, "%d\n", (tmp ? 1 : 0));
1291}
1292
1293/**
1294 * ds1685_rtc_sysfs_ctrl_regs_store - writes a ctrl register bit via sysfs.
1295 * @dev: pointer to device structure.
1296 * @attr: pointer to device_attribute structure.
1297 * @buf: pointer to char array to hold the output.
1298 * @count: number of bytes written.
1299 */
1300static ssize_t
1301ds1685_rtc_sysfs_ctrl_regs_store(struct device *dev,
1302 struct device_attribute *attr,
1303 const char *buf, size_t count)
1304{
1305 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1306 u8 reg = 0, bit = 0, tmp;
1307 unsigned long flags = 0;
1308 long int val = 0;
1309 const struct ds1685_rtc_ctrl_regs *reg_info =
1310 ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
1311
1312 /* We only accept numbers. */
1313 if (kstrtol(buf, 10, &val) < 0)
1314 return -EINVAL;
1315
1316 /* bits are binary, 0 or 1 only. */
1317 if ((val != 0) && (val != 1))
1318 return -ERANGE;
1319
1320 /* Make sure we actually matched something. */
1321 if (!reg_info)
1322 return -EINVAL;
1323
1324 reg = reg_info->reg;
1325 bit = reg_info->bit;
1326
1327 /* Safe to spinlock during a write. */
1328 ds1685_rtc_begin_ctrl_access(rtc, flags);
1329 tmp = rtc->read(rtc, reg);
1330 rtc->write(rtc, reg, (val ? (tmp | bit) : (tmp & ~(bit))));
1331 ds1685_rtc_end_ctrl_access(rtc, flags);
1332
1333 return count;
1334}
1335
1336/**
1337 * DS1685_RTC_SYSFS_CTRL_REG_RO - device_attribute for read-only register bit.
1338 * @bit: bit to read.
1339 */
1340#define DS1685_RTC_SYSFS_CTRL_REG_RO(bit) \
1341 static DEVICE_ATTR(bit, S_IRUGO, \
1342 ds1685_rtc_sysfs_ctrl_regs_show, NULL)
1343
1344/**
1345 * DS1685_RTC_SYSFS_CTRL_REG_RW - device_attribute for read-write register bit.
1346 * @bit: bit to read or write.
1347 */
1348#define DS1685_RTC_SYSFS_CTRL_REG_RW(bit) \
1349 static DEVICE_ATTR(bit, S_IRUGO | S_IWUSR, \
1350 ds1685_rtc_sysfs_ctrl_regs_show, \
1351 ds1685_rtc_sysfs_ctrl_regs_store)
1352
1353/*
1354 * Control Register A bits.
1355 */
1356DS1685_RTC_SYSFS_CTRL_REG_RO(uip);
1357DS1685_RTC_SYSFS_CTRL_REG_RW(dv2);
1358DS1685_RTC_SYSFS_CTRL_REG_RW(dv1);
1359DS1685_RTC_SYSFS_CTRL_REG_RO(dv0);
1360DS1685_RTC_SYSFS_CTRL_REG_RW(rs3);
1361DS1685_RTC_SYSFS_CTRL_REG_RW(rs2);
1362DS1685_RTC_SYSFS_CTRL_REG_RW(rs1);
1363DS1685_RTC_SYSFS_CTRL_REG_RW(rs0);
1364
1365static struct attribute*
1366ds1685_rtc_sysfs_ctrla_attrs[] = {
1367 &dev_attr_uip.attr,
1368 &dev_attr_dv2.attr,
1369 &dev_attr_dv1.attr,
1370 &dev_attr_dv0.attr,
1371 &dev_attr_rs3.attr,
1372 &dev_attr_rs2.attr,
1373 &dev_attr_rs1.attr,
1374 &dev_attr_rs0.attr,
1375 NULL,
1376};
1377
1378static const struct attribute_group
1379ds1685_rtc_sysfs_ctrla_grp = {
1380 .name = "ctrla",
1381 .attrs = ds1685_rtc_sysfs_ctrla_attrs,
1382};
1383
1384
1385/*
1386 * Control Register B bits.
1387 */
1388DS1685_RTC_SYSFS_CTRL_REG_RO(set);
1389DS1685_RTC_SYSFS_CTRL_REG_RW(pie);
1390DS1685_RTC_SYSFS_CTRL_REG_RW(aie);
1391DS1685_RTC_SYSFS_CTRL_REG_RW(uie);
1392DS1685_RTC_SYSFS_CTRL_REG_RW(sqwe);
1393DS1685_RTC_SYSFS_CTRL_REG_RO(dm);
1394DS1685_RTC_SYSFS_CTRL_REG_RO(2412);
1395DS1685_RTC_SYSFS_CTRL_REG_RO(dse);
1396
1397static struct attribute*
1398ds1685_rtc_sysfs_ctrlb_attrs[] = {
1399 &dev_attr_set.attr,
1400 &dev_attr_pie.attr,
1401 &dev_attr_aie.attr,
1402 &dev_attr_uie.attr,
1403 &dev_attr_sqwe.attr,
1404 &dev_attr_dm.attr,
1405 &dev_attr_2412.attr,
1406 &dev_attr_dse.attr,
1407 NULL,
1408};
1409
1410static const struct attribute_group
1411ds1685_rtc_sysfs_ctrlb_grp = {
1412 .name = "ctrlb",
1413 .attrs = ds1685_rtc_sysfs_ctrlb_attrs,
1414};
1415
1416/*
1417 * Control Register C bits.
1418 *
1419 * Reading Control C clears these bits! Reading them individually can
1420 * possibly cause an interrupt to be missed. Use the /proc interface
1421 * to see all the bits in this register simultaneously.
1422 */
1423DS1685_RTC_SYSFS_CTRL_REG_RO(irqf);
1424DS1685_RTC_SYSFS_CTRL_REG_RO(pf);
1425DS1685_RTC_SYSFS_CTRL_REG_RO(af);
1426DS1685_RTC_SYSFS_CTRL_REG_RO(uf);
1427
1428static struct attribute*
1429ds1685_rtc_sysfs_ctrlc_attrs[] = {
1430 &dev_attr_irqf.attr,
1431 &dev_attr_pf.attr,
1432 &dev_attr_af.attr,
1433 &dev_attr_uf.attr,
1434 NULL,
1435};
1436
1437static const struct attribute_group
1438ds1685_rtc_sysfs_ctrlc_grp = {
1439 .name = "ctrlc",
1440 .attrs = ds1685_rtc_sysfs_ctrlc_attrs,
1441};
1442
1443/*
1444 * Control Register D bits.
1445 */
1446DS1685_RTC_SYSFS_CTRL_REG_RO(vrt);
1447
1448static struct attribute*
1449ds1685_rtc_sysfs_ctrld_attrs[] = {
1450 &dev_attr_vrt.attr,
1451 NULL,
1452};
1453
1454static const struct attribute_group
1455ds1685_rtc_sysfs_ctrld_grp = {
1456 .name = "ctrld",
1457 .attrs = ds1685_rtc_sysfs_ctrld_attrs,
1458};
1459
1460/*
1461 * Control Register 4A bits.
1462 */
1463DS1685_RTC_SYSFS_CTRL_REG_RO(vrt2);
1464DS1685_RTC_SYSFS_CTRL_REG_RO(incr);
1465DS1685_RTC_SYSFS_CTRL_REG_RW(pab);
1466DS1685_RTC_SYSFS_CTRL_REG_RW(rf);
1467DS1685_RTC_SYSFS_CTRL_REG_RW(wf);
1468DS1685_RTC_SYSFS_CTRL_REG_RW(kf);
1469#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
1470DS1685_RTC_SYSFS_CTRL_REG_RO(bme);
1471#endif
1472
1473static struct attribute*
1474ds1685_rtc_sysfs_ctrl4a_attrs[] = {
1475 &dev_attr_vrt2.attr,
1476 &dev_attr_incr.attr,
1477 &dev_attr_pab.attr,
1478 &dev_attr_rf.attr,
1479 &dev_attr_wf.attr,
1480 &dev_attr_kf.attr,
1481#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
1482 &dev_attr_bme.attr,
1483#endif
1484 NULL,
1485};
1486
1487static const struct attribute_group
1488ds1685_rtc_sysfs_ctrl4a_grp = {
1489 .name = "ctrl4a",
1490 .attrs = ds1685_rtc_sysfs_ctrl4a_attrs,
1491};
1492
1493/*
1494 * Control Register 4B bits.
1495 */
1496DS1685_RTC_SYSFS_CTRL_REG_RW(abe);
1497DS1685_RTC_SYSFS_CTRL_REG_RW(e32k);
1498DS1685_RTC_SYSFS_CTRL_REG_RO(cs);
1499DS1685_RTC_SYSFS_CTRL_REG_RW(rce);
1500DS1685_RTC_SYSFS_CTRL_REG_RW(prs);
1501DS1685_RTC_SYSFS_CTRL_REG_RW(rie);
1502DS1685_RTC_SYSFS_CTRL_REG_RW(wie);
1503DS1685_RTC_SYSFS_CTRL_REG_RW(kse);
1504
1505static struct attribute*
1506ds1685_rtc_sysfs_ctrl4b_attrs[] = {
1507 &dev_attr_abe.attr,
1508 &dev_attr_e32k.attr,
1509 &dev_attr_cs.attr,
1510 &dev_attr_rce.attr,
1511 &dev_attr_prs.attr,
1512 &dev_attr_rie.attr,
1513 &dev_attr_wie.attr,
1514 &dev_attr_kse.attr,
1515 NULL,
1516};
1517
1518static const struct attribute_group
1519ds1685_rtc_sysfs_ctrl4b_grp = {
1520 .name = "ctrl4b",
1521 .attrs = ds1685_rtc_sysfs_ctrl4b_attrs,
1522};
1523
1524
1525/**
1526 * struct ds1685_rtc_ctrl_regs.
1527 * @name: char pointer for the bit name.
1528 * @reg: control register the bit is in.
1529 * @bit: the bit's offset in the register.
1530 */
1531struct ds1685_rtc_time_regs {
1532 const char *name;
1533 const u8 reg;
1534 const u8 mask;
1535 const u8 min;
1536 const u8 max;
1537};
1538
1539/*
1540 * Time/Date register lookup tables.
1541 */
1542static const struct ds1685_rtc_time_regs
1543ds1685_time_regs_bcd_table[] = {
1544 { "seconds", RTC_SECS, RTC_SECS_BCD_MASK, 0, 59 },
1545 { "minutes", RTC_MINS, RTC_MINS_BCD_MASK, 0, 59 },
1546 { "hours", RTC_HRS, RTC_HRS_24_BCD_MASK, 0, 23 },
1547 { "wday", RTC_WDAY, RTC_WDAY_MASK, 1, 7 },
1548 { "mday", RTC_MDAY, RTC_MDAY_BCD_MASK, 1, 31 },
1549 { "month", RTC_MONTH, RTC_MONTH_BCD_MASK, 1, 12 },
1550 { "year", RTC_YEAR, RTC_YEAR_BCD_MASK, 0, 99 },
1551 { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0, 99 },
1552 { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BCD_MASK, 0, 59 },
1553 { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BCD_MASK, 0, 59 },
1554 { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BCD_MASK, 0, 23 },
1555 { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 1, 31 },
1556 { NULL, 0, 0, 0, 0 },
1557};
1558
1559static const struct ds1685_rtc_time_regs
1560ds1685_time_regs_bin_table[] = {
1561 { "seconds", RTC_SECS, RTC_SECS_BIN_MASK, 0x00, 0x3b },
1562 { "minutes", RTC_MINS, RTC_MINS_BIN_MASK, 0x00, 0x3b },
1563 { "hours", RTC_HRS, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
1564 { "wday", RTC_WDAY, RTC_WDAY_MASK, 0x01, 0x07 },
1565 { "mday", RTC_MDAY, RTC_MDAY_BIN_MASK, 0x01, 0x1f },
1566 { "month", RTC_MONTH, RTC_MONTH_BIN_MASK, 0x01, 0x0c },
1567 { "year", RTC_YEAR, RTC_YEAR_BIN_MASK, 0x00, 0x63 },
1568 { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0x00, 0x63 },
1569 { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BIN_MASK, 0x00, 0x3b },
1570 { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BIN_MASK, 0x00, 0x3b },
1571 { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
1572 { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 0x01, 0x1f },
1573 { NULL, 0, 0, 0x00, 0x00 },
1574};
1575
1576/**
1577 * ds1685_rtc_sysfs_time_regs_bcd_lookup - time/date reg bit lookup function.
1578 * @name: register bit to look up in ds1685_time_regs_bcd_table.
1579 */
1580static const struct ds1685_rtc_time_regs*
1581ds1685_rtc_sysfs_time_regs_lookup(const char *name, bool bcd_mode)
1582{
1583 const struct ds1685_rtc_time_regs *p;
1584
1585 if (bcd_mode)
1586 p = ds1685_time_regs_bcd_table;
1587 else
1588 p = ds1685_time_regs_bin_table;
1589
1590 for (; p->name != NULL; ++p)
1591 if (strcmp(p->name, name) == 0)
1592 return p;
1593
1594 return NULL;
1595}
1596
1597/**
1598 * ds1685_rtc_sysfs_time_regs_show - reads a time/date register via sysfs.
1599 * @dev: pointer to device structure.
1600 * @attr: pointer to device_attribute structure.
1601 * @buf: pointer to char array to hold the output.
1602 */
1603static ssize_t
1604ds1685_rtc_sysfs_time_regs_show(struct device *dev,
1605 struct device_attribute *attr, char *buf)
1606{
1607 u8 tmp;
1608 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1609 const struct ds1685_rtc_time_regs *bcd_reg_info =
1610 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
1611 const struct ds1685_rtc_time_regs *bin_reg_info =
1612 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
1613
1614 /* Make sure we actually matched something. */
1615 if (!bcd_reg_info && !bin_reg_info)
1616 return -EINVAL;
1617
1618 /* bcd_reg_info->reg == bin_reg_info->reg. */
1619 ds1685_rtc_begin_data_access(rtc);
1620 tmp = rtc->read(rtc, bcd_reg_info->reg);
1621 ds1685_rtc_end_data_access(rtc);
1622
1623 tmp = ds1685_rtc_bcd2bin(rtc, tmp, bcd_reg_info->mask,
1624 bin_reg_info->mask);
1625
1626 return snprintf(buf, 4, "%d\n", tmp);
1627}
1628
1629/**
1630 * ds1685_rtc_sysfs_time_regs_store - writes a time/date register via sysfs.
1631 * @dev: pointer to device structure.
1632 * @attr: pointer to device_attribute structure.
1633 * @buf: pointer to char array to hold the output.
1634 * @count: number of bytes written.
1635 */
1636static ssize_t
1637ds1685_rtc_sysfs_time_regs_store(struct device *dev,
1638 struct device_attribute *attr,
1639 const char *buf, size_t count)
1640{
1641 long int val = 0;
1642 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1643 const struct ds1685_rtc_time_regs *bcd_reg_info =
1644 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
1645 const struct ds1685_rtc_time_regs *bin_reg_info =
1646 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
1647
1648 /* We only accept numbers. */
1649 if (kstrtol(buf, 10, &val) < 0)
1650 return -EINVAL;
1651
1652 /* Make sure we actually matched something. */
1653 if (!bcd_reg_info && !bin_reg_info)
1654 return -EINVAL;
1655
1656 /* Check for a valid range. */
1657 if (rtc->bcd_mode) {
1658 if ((val < bcd_reg_info->min) || (val > bcd_reg_info->max))
1659 return -ERANGE;
1660 } else {
1661 if ((val < bin_reg_info->min) || (val > bin_reg_info->max))
1662 return -ERANGE;
1663 }
1664
1665 val = ds1685_rtc_bin2bcd(rtc, val, bin_reg_info->mask,
1666 bcd_reg_info->mask);
1667
1668 /* bcd_reg_info->reg == bin_reg_info->reg. */
1669 ds1685_rtc_begin_data_access(rtc);
1670 rtc->write(rtc, bcd_reg_info->reg, val);
1671 ds1685_rtc_end_data_access(rtc);
1672
1673 return count;
1674}
1675
1676/**
1677 * DS1685_RTC_SYSFS_REG_RW - device_attribute for a read-write time register.
1678 * @reg: time/date register to read or write.
1679 */
1680#define DS1685_RTC_SYSFS_TIME_REG_RW(reg) \
1681 static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, \
1682 ds1685_rtc_sysfs_time_regs_show, \
1683 ds1685_rtc_sysfs_time_regs_store)
1684
1685/*
1686 * Time/Date Register bits.
1687 */
1688DS1685_RTC_SYSFS_TIME_REG_RW(seconds);
1689DS1685_RTC_SYSFS_TIME_REG_RW(minutes);
1690DS1685_RTC_SYSFS_TIME_REG_RW(hours);
1691DS1685_RTC_SYSFS_TIME_REG_RW(wday);
1692DS1685_RTC_SYSFS_TIME_REG_RW(mday);
1693DS1685_RTC_SYSFS_TIME_REG_RW(month);
1694DS1685_RTC_SYSFS_TIME_REG_RW(year);
1695DS1685_RTC_SYSFS_TIME_REG_RW(century);
1696DS1685_RTC_SYSFS_TIME_REG_RW(alarm_seconds);
1697DS1685_RTC_SYSFS_TIME_REG_RW(alarm_minutes);
1698DS1685_RTC_SYSFS_TIME_REG_RW(alarm_hours);
1699DS1685_RTC_SYSFS_TIME_REG_RW(alarm_mday);
1700
1701static struct attribute*
1702ds1685_rtc_sysfs_time_attrs[] = {
1703 &dev_attr_seconds.attr,
1704 &dev_attr_minutes.attr,
1705 &dev_attr_hours.attr,
1706 &dev_attr_wday.attr,
1707 &dev_attr_mday.attr,
1708 &dev_attr_month.attr,
1709 &dev_attr_year.attr,
1710 &dev_attr_century.attr,
1711 NULL,
1712};
1713
1714static const struct attribute_group
1715ds1685_rtc_sysfs_time_grp = {
1716 .name = "datetime",
1717 .attrs = ds1685_rtc_sysfs_time_attrs,
1718};
1719
1720static struct attribute*
1721ds1685_rtc_sysfs_alarm_attrs[] = {
1722 &dev_attr_alarm_seconds.attr,
1723 &dev_attr_alarm_minutes.attr,
1724 &dev_attr_alarm_hours.attr,
1725 &dev_attr_alarm_mday.attr,
1726 NULL,
1727};
1728
1729static const struct attribute_group
1730ds1685_rtc_sysfs_alarm_grp = {
1731 .name = "alarm",
1732 .attrs = ds1685_rtc_sysfs_alarm_attrs,
1733};
1734#endif /* CONFIG_RTC_DS1685_SYSFS_REGS */
1735
1736
1737/**
1738 * ds1685_rtc_sysfs_register - register sysfs files.
1739 * @dev: pointer to device structure.
1740 */
1741static int
1742ds1685_rtc_sysfs_register(struct device *dev)
1743{
1744 int ret = 0;
1745
1746 sysfs_bin_attr_init(&ds1685_rtc_sysfs_nvram_attr);
1747 ret = sysfs_create_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
1748 if (ret)
1749 return ret;
1750
1751 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
1752 if (ret)
1753 return ret;
1754
1755#ifdef CONFIG_RTC_DS1685_SYSFS_REGS
1756 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
1757 if (ret)
1758 return ret;
1759
1760 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
1761 if (ret)
1762 return ret;
1763
1764 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
1765 if (ret)
1766 return ret;
1767
1768 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
1769 if (ret)
1770 return ret;
1771
1772 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
1773 if (ret)
1774 return ret;
1775
1776 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
1777 if (ret)
1778 return ret;
1779
1780 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
1781 if (ret)
1782 return ret;
1783
1784 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
1785 if (ret)
1786 return ret;
1787#endif
1788 return 0;
1789}
1790
1791/**
1792 * ds1685_rtc_sysfs_unregister - unregister sysfs files.
1793 * @dev: pointer to device structure.
1794 */
1795static int
1796ds1685_rtc_sysfs_unregister(struct device *dev)
1797{
1798 sysfs_remove_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
1799 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
1800
1801#ifdef CONFIG_RTC_DS1685_SYSFS_REGS
1802 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
1803 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
1804 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
1805 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
1806 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
1807 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
1808 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
1809 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
1810#endif
1811
1812 return 0;
1813}
1814#endif /* CONFIG_SYSFS */
1815
1816
1817
1818/* ----------------------------------------------------------------------- */
1819/* Driver Probe/Removal */
1820
1821/**
1822 * ds1685_rtc_probe - initializes rtc driver.
1823 * @pdev: pointer to platform_device structure.
1824 */
1825static int
1826ds1685_rtc_probe(struct platform_device *pdev)
1827{
1828 struct rtc_device *rtc_dev;
1829 struct resource *res;
1830 struct ds1685_priv *rtc;
1831 struct ds1685_rtc_platform_data *pdata;
1832 u8 ctrla, ctrlb, hours;
1833 unsigned char am_pm;
1834 int ret = 0;
1835
1836 /* Get the platform data. */
1837 pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
1838 if (!pdata)
1839 return -ENODEV;
1840
1841 /* Allocate memory for the rtc device. */
1842 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
1843 if (!rtc)
1844 return -ENOMEM;
1845
1846 /*
1847 * Allocate/setup any IORESOURCE_MEM resources, if required. Not all
1848 * platforms put the RTC in an easy-access place. Like the SGI Octane,
1849 * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip
1850 * that sits behind the IOC3 PCI metadevice.
1851 */
1852 if (pdata->alloc_io_resources) {
1853 /* Get the platform resources. */
1854 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1855 if (!res)
1856 return -ENXIO;
1857 rtc->size = resource_size(res);
1858
1859 /* Request a memory region. */
1860 /* XXX: mmio-only for now. */
1861 if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size,
1862 pdev->name))
1863 return -EBUSY;
1864
1865 /*
1866 * Set the base address for the rtc, and ioremap its
1867 * registers.
1868 */
1869 rtc->baseaddr = res->start;
1870 rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size);
1871 if (!rtc->regs)
1872 return -ENOMEM;
1873 }
1874 rtc->alloc_io_resources = pdata->alloc_io_resources;
1875
1876 /* Get the register step size. */
1877 if (pdata->regstep > 0)
1878 rtc->regstep = pdata->regstep;
1879 else
1880 rtc->regstep = 1;
1881
1882 /* Platform read function, else default if mmio setup */
1883 if (pdata->plat_read)
1884 rtc->read = pdata->plat_read;
1885 else
1886 if (pdata->alloc_io_resources)
1887 rtc->read = ds1685_read;
1888 else
1889 return -ENXIO;
1890
1891 /* Platform write function, else default if mmio setup */
1892 if (pdata->plat_write)
1893 rtc->write = pdata->plat_write;
1894 else
1895 if (pdata->alloc_io_resources)
1896 rtc->write = ds1685_write;
1897 else
1898 return -ENXIO;
1899
1900 /* Platform pre-shutdown function, if defined. */
1901 if (pdata->plat_prepare_poweroff)
1902 rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
1903
1904 /* Platform wake_alarm function, if defined. */
1905 if (pdata->plat_wake_alarm)
1906 rtc->wake_alarm = pdata->plat_wake_alarm;
1907
1908 /* Platform post_ram_clear function, if defined. */
1909 if (pdata->plat_post_ram_clear)
1910 rtc->post_ram_clear = pdata->plat_post_ram_clear;
1911
1912 /* Init the spinlock, workqueue, & set the driver data. */
1913 spin_lock_init(&rtc->lock);
1914 INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
1915 platform_set_drvdata(pdev, rtc);
1916
1917 /* Turn the oscillator on if is not already on (DV1 = 1). */
1918 ctrla = rtc->read(rtc, RTC_CTRL_A);
1919 if (!(ctrla & RTC_CTRL_A_DV1))
1920 ctrla |= RTC_CTRL_A_DV1;
1921
1922 /* Enable the countdown chain (DV2 = 0) */
1923 ctrla &= ~(RTC_CTRL_A_DV2);
1924
1925 /* Clear RS3-RS0 in Control A. */
1926 ctrla &= ~(RTC_CTRL_A_RS_MASK);
1927
1928 /*
1929 * All done with Control A. Switch to Bank 1 for the remainder of
1930 * the RTC setup so we have access to the extended functions.
1931 */
1932 ctrla |= RTC_CTRL_A_DV0;
1933 rtc->write(rtc, RTC_CTRL_A, ctrla);
1934
1935 /* Default to 32768kHz output. */
1936 rtc->write(rtc, RTC_EXT_CTRL_4B,
1937 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
1938
1939 /* Set the SET bit in Control B so we can do some housekeeping. */
1940 rtc->write(rtc, RTC_CTRL_B,
1941 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
1942
1943 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
1944 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
1945 cpu_relax();
1946
1947 /*
1948 * If the platform supports BCD mode, then set DM=0 in Control B.
1949 * Otherwise, set DM=1 for BIN mode.
1950 */
1951 ctrlb = rtc->read(rtc, RTC_CTRL_B);
1952 if (pdata->bcd_mode)
1953 ctrlb &= ~(RTC_CTRL_B_DM);
1954 else
1955 ctrlb |= RTC_CTRL_B_DM;
1956 rtc->bcd_mode = pdata->bcd_mode;
1957
1958 /*
1959 * Disable Daylight Savings Time (DSE = 0).
1960 * The RTC has hardcoded timezone information that is rendered
1961 * obselete. We'll let the OS deal with DST settings instead.
1962 */
1963 if (ctrlb & RTC_CTRL_B_DSE)
1964 ctrlb &= ~(RTC_CTRL_B_DSE);
1965
1966 /* Force 24-hour mode (2412 = 1). */
1967 if (!(ctrlb & RTC_CTRL_B_2412)) {
1968 /* Reinitialize the time hours. */
1969 hours = rtc->read(rtc, RTC_HRS);
1970 am_pm = hours & RTC_HRS_AMPM_MASK;
1971 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
1972 RTC_HRS_12_BIN_MASK);
1973 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
1974
1975 /* Enable 24-hour mode. */
1976 ctrlb |= RTC_CTRL_B_2412;
1977
1978 /* Write back to Control B, including DM & DSE bits. */
1979 rtc->write(rtc, RTC_CTRL_B, ctrlb);
1980
1981 /* Write the time hours back. */
1982 rtc->write(rtc, RTC_HRS,
1983 ds1685_rtc_bin2bcd(rtc, hours,
1984 RTC_HRS_24_BIN_MASK,
1985 RTC_HRS_24_BCD_MASK));
1986
1987 /* Reinitialize the alarm hours. */
1988 hours = rtc->read(rtc, RTC_HRS_ALARM);
1989 am_pm = hours & RTC_HRS_AMPM_MASK;
1990 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
1991 RTC_HRS_12_BIN_MASK);
1992 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
1993
1994 /* Write the alarm hours back. */
1995 rtc->write(rtc, RTC_HRS_ALARM,
1996 ds1685_rtc_bin2bcd(rtc, hours,
1997 RTC_HRS_24_BIN_MASK,
1998 RTC_HRS_24_BCD_MASK));
1999 } else {
2000 /* 24-hour mode is already set, so write Control B back. */
2001 rtc->write(rtc, RTC_CTRL_B, ctrlb);
2002 }
2003
2004 /* Unset the SET bit in Control B so the RTC can update. */
2005 rtc->write(rtc, RTC_CTRL_B,
2006 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
2007
2008 /* Check the main battery. */
2009 if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
2010 dev_warn(&pdev->dev,
2011 "Main battery is exhausted! RTC may be invalid!\n");
2012
2013 /* Check the auxillary battery. It is optional. */
2014 if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
2015 dev_warn(&pdev->dev,
2016 "Aux battery is exhausted or not available.\n");
2017
2018 /* Read Ctrl B and clear PIE/AIE/UIE. */
2019 rtc->write(rtc, RTC_CTRL_B,
2020 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
2021
2022 /* Reading Ctrl C auto-clears PF/AF/UF. */
2023 rtc->read(rtc, RTC_CTRL_C);
2024
2025 /* Read Ctrl 4B and clear RIE/WIE/KSE. */
2026 rtc->write(rtc, RTC_EXT_CTRL_4B,
2027 (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
2028
2029 /* Clear RF/WF/KF in Ctrl 4A. */
2030 rtc->write(rtc, RTC_EXT_CTRL_4A,
2031 (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
2032
2033 /*
2034 * Re-enable KSE to handle power button events. We do not enable
2035 * WIE or RIE by default.
2036 */
2037 rtc->write(rtc, RTC_EXT_CTRL_4B,
2038 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
2039
2040 /*
2041 * Fetch the IRQ and setup the interrupt handler.
2042 *
2043 * Not all platforms have the IRQF pin tied to something. If not, the
2044 * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but
2045 * there won't be an automatic way of notifying the kernel about it,
2046 * unless ctrlc is explicitly polled.
2047 */
2048 if (!pdata->no_irq) {
2049 ret = platform_get_irq(pdev, 0);
2050 if (ret > 0) {
2051 rtc->irq_num = ret;
2052
2053 /* Request an IRQ. */
2054 ret = devm_request_irq(&pdev->dev, rtc->irq_num,
2055 ds1685_rtc_irq_handler,
2056 IRQF_SHARED, pdev->name, pdev);
2057
2058 /* Check to see if something came back. */
2059 if (unlikely(ret)) {
2060 dev_warn(&pdev->dev,
2061 "RTC interrupt not available\n");
2062 rtc->irq_num = 0;
2063 }
2064 } else
2065 return ret;
2066 }
2067 rtc->no_irq = pdata->no_irq;
2068
2069 /* Setup complete. */
2070 ds1685_rtc_switch_to_bank0(rtc);
2071
2072 /* Register the device as an RTC. */
2073 rtc_dev = rtc_device_register(pdev->name, &pdev->dev,
2074 &ds1685_rtc_ops, THIS_MODULE);
2075
2076 /* Success? */
2077 if (IS_ERR(rtc_dev))
2078 return PTR_ERR(rtc_dev);
2079
2080 /* Maximum periodic rate is 8192Hz (0.122070ms). */
2081 rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
2082
2083 /* See if the platform doesn't support UIE. */
2084 if (pdata->uie_unsupported)
2085 rtc_dev->uie_unsupported = 1;
2086 rtc->uie_unsupported = pdata->uie_unsupported;
2087
2088 rtc->dev = rtc_dev;
2089
2090#ifdef CONFIG_SYSFS
2091 ret = ds1685_rtc_sysfs_register(&pdev->dev);
2092 if (ret)
2093 rtc_device_unregister(rtc->dev);
2094#endif
2095
2096 /* Done! */
2097 return ret;
2098}
2099
2100/**
2101 * ds1685_rtc_remove - removes rtc driver.
2102 * @pdev: pointer to platform_device structure.
2103 */
2104static int
2105ds1685_rtc_remove(struct platform_device *pdev)
2106{
2107 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
2108
2109#ifdef CONFIG_SYSFS
2110 ds1685_rtc_sysfs_unregister(&pdev->dev);
2111#endif
2112
2113 rtc_device_unregister(rtc->dev);
2114
2115 /* Read Ctrl B and clear PIE/AIE/UIE. */
2116 rtc->write(rtc, RTC_CTRL_B,
2117 (rtc->read(rtc, RTC_CTRL_B) &
2118 ~(RTC_CTRL_B_PAU_MASK)));
2119
2120 /* Reading Ctrl C auto-clears PF/AF/UF. */
2121 rtc->read(rtc, RTC_CTRL_C);
2122
2123 /* Read Ctrl 4B and clear RIE/WIE/KSE. */
2124 rtc->write(rtc, RTC_EXT_CTRL_4B,
2125 (rtc->read(rtc, RTC_EXT_CTRL_4B) &
2126 ~(RTC_CTRL_4B_RWK_MASK)));
2127
2128 /* Manually clear RF/WF/KF in Ctrl 4A. */
2129 rtc->write(rtc, RTC_EXT_CTRL_4A,
2130 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
2131 ~(RTC_CTRL_4A_RWK_MASK)));
2132
2133 cancel_work_sync(&rtc->work);
2134
2135 return 0;
2136}
2137
2138/**
2139 * ds1685_rtc_driver - rtc driver properties.
2140 */
2141static struct platform_driver ds1685_rtc_driver = {
2142 .driver = {
2143 .name = "rtc-ds1685",
2144 .owner = THIS_MODULE,
2145 },
2146 .probe = ds1685_rtc_probe,
2147 .remove = ds1685_rtc_remove,
2148};
2149
2150/**
2151 * ds1685_rtc_init - rtc module init.
2152 */
2153static int __init
2154ds1685_rtc_init(void)
2155{
2156 return platform_driver_register(&ds1685_rtc_driver);
2157}
2158
2159/**
2160 * ds1685_rtc_exit - rtc module exit.
2161 */
2162static void __exit
2163ds1685_rtc_exit(void)
2164{
2165 platform_driver_unregister(&ds1685_rtc_driver);
2166}
2167
2168module_init(ds1685_rtc_init);
2169module_exit(ds1685_rtc_exit);
2170/* ----------------------------------------------------------------------- */
2171
2172
2173/* ----------------------------------------------------------------------- */
2174/* Poweroff function */
2175
2176/**
2177 * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
2178 * @pdev: pointer to platform_device structure.
2179 */
2180extern void __noreturn
2181ds1685_rtc_poweroff(struct platform_device *pdev)
2182{
2183 u8 ctrla, ctrl4a, ctrl4b;
2184 struct ds1685_priv *rtc;
2185
2186 /* Check for valid RTC data, else, spin forever. */
2187 if (unlikely(!pdev)) {
2188 pr_emerg("rtc-ds1685: platform device data not available, spinning forever ...\n");
2189 unreachable();
2190 } else {
2191 /* Get the rtc data. */
2192 rtc = platform_get_drvdata(pdev);
2193
2194 /*
2195 * Disable our IRQ. We're powering down, so we're not
2196 * going to worry about cleaning up. Most of that should
2197 * have been taken care of by the shutdown scripts and this
2198 * is the final function call.
2199 */
2200 if (!rtc->no_irq)
2201 disable_irq_nosync(rtc->irq_num);
2202
2203 /* Oscillator must be on and the countdown chain enabled. */
2204 ctrla = rtc->read(rtc, RTC_CTRL_A);
2205 ctrla |= RTC_CTRL_A_DV1;
2206 ctrla &= ~(RTC_CTRL_A_DV2);
2207 rtc->write(rtc, RTC_CTRL_A, ctrla);
2208
2209 /*
2210 * Read Control 4A and check the status of the auxillary
2211 * battery. This must be present and working (VRT2 = 1)
2212 * for wakeup and kickstart functionality to be useful.
2213 */
2214 ds1685_rtc_switch_to_bank1(rtc);
2215 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
2216 if (ctrl4a & RTC_CTRL_4A_VRT2) {
2217 /* Clear all of the interrupt flags on Control 4A. */
2218 ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
2219 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
2220
2221 /*
2222 * The auxillary battery is present and working.
2223 * Enable extended functions (ABE=1), enable
2224 * wake-up (WIE=1), and enable kickstart (KSE=1)
2225 * in Control 4B.
2226 */
2227 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
2228 ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
2229 RTC_CTRL_4B_KSE);
2230 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
2231 }
2232
2233 /* Set PAB to 1 in Control 4A to power the system down. */
2234 dev_warn(&pdev->dev, "Powerdown.\n");
2235 msleep(20);
2236 rtc->write(rtc, RTC_EXT_CTRL_4A,
2237 (ctrl4a | RTC_CTRL_4A_PAB));
2238
2239 /* Spin ... we do not switch back to bank0. */
2240 unreachable();
2241 }
2242}
2243EXPORT_SYMBOL(ds1685_rtc_poweroff);
2244/* ----------------------------------------------------------------------- */
2245
2246
2247MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
2248MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
2249MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
2250MODULE_LICENSE("GPL");
2251MODULE_VERSION(DRV_VERSION);
2252MODULE_ALIAS("platform:rtc-ds1685");
diff --git a/drivers/rtc/rtc-imxdi.c b/drivers/rtc/rtc-imxdi.c
index 42f5570f42f8..c666eab98273 100644
--- a/drivers/rtc/rtc-imxdi.c
+++ b/drivers/rtc/rtc-imxdi.c
@@ -50,22 +50,58 @@
50#define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */ 50#define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */
51 51
52#define DCR 0x10 /* Control Reg */ 52#define DCR 0x10 /* Control Reg */
53#define DCR_TDCHL (1 << 30) /* Tamper-detect configuration hard lock */
54#define DCR_TDCSL (1 << 29) /* Tamper-detect configuration soft lock */
55#define DCR_KSSL (1 << 27) /* Key-select soft lock */
56#define DCR_MCHL (1 << 20) /* Monotonic-counter hard lock */
57#define DCR_MCSL (1 << 19) /* Monotonic-counter soft lock */
58#define DCR_TCHL (1 << 18) /* Timer-counter hard lock */
59#define DCR_TCSL (1 << 17) /* Timer-counter soft lock */
60#define DCR_FSHL (1 << 16) /* Failure state hard lock */
53#define DCR_TCE (1 << 3) /* Time Counter Enable */ 61#define DCR_TCE (1 << 3) /* Time Counter Enable */
62#define DCR_MCE (1 << 2) /* Monotonic Counter Enable */
54 63
55#define DSR 0x14 /* Status Reg */ 64#define DSR 0x14 /* Status Reg */
56#define DSR_WBF (1 << 10) /* Write Busy Flag */ 65#define DSR_WTD (1 << 23) /* Wire-mesh tamper detected */
57#define DSR_WNF (1 << 9) /* Write Next Flag */ 66#define DSR_ETBD (1 << 22) /* External tamper B detected */
58#define DSR_WCF (1 << 8) /* Write Complete Flag */ 67#define DSR_ETAD (1 << 21) /* External tamper A detected */
68#define DSR_EBD (1 << 20) /* External boot detected */
69#define DSR_SAD (1 << 19) /* SCC alarm detected */
70#define DSR_TTD (1 << 18) /* Temperatur tamper detected */
71#define DSR_CTD (1 << 17) /* Clock tamper detected */
72#define DSR_VTD (1 << 16) /* Voltage tamper detected */
73#define DSR_WBF (1 << 10) /* Write Busy Flag (synchronous) */
74#define DSR_WNF (1 << 9) /* Write Next Flag (synchronous) */
75#define DSR_WCF (1 << 8) /* Write Complete Flag (synchronous)*/
59#define DSR_WEF (1 << 7) /* Write Error Flag */ 76#define DSR_WEF (1 << 7) /* Write Error Flag */
60#define DSR_CAF (1 << 4) /* Clock Alarm Flag */ 77#define DSR_CAF (1 << 4) /* Clock Alarm Flag */
78#define DSR_MCO (1 << 3) /* monotonic counter overflow */
79#define DSR_TCO (1 << 2) /* time counter overflow */
61#define DSR_NVF (1 << 1) /* Non-Valid Flag */ 80#define DSR_NVF (1 << 1) /* Non-Valid Flag */
62#define DSR_SVF (1 << 0) /* Security Violation Flag */ 81#define DSR_SVF (1 << 0) /* Security Violation Flag */
63 82
64#define DIER 0x18 /* Interrupt Enable Reg */ 83#define DIER 0x18 /* Interrupt Enable Reg (synchronous) */
65#define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */ 84#define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */
66#define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */ 85#define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */
67#define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */ 86#define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */
68#define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */ 87#define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */
88#define DIER_SVIE (1 << 0) /* Security-violation Interrupt Enable */
89
90#define DMCR 0x1c /* DryIce Monotonic Counter Reg */
91
92#define DTCR 0x28 /* DryIce Tamper Configuration Reg */
93#define DTCR_MOE (1 << 9) /* monotonic overflow enabled */
94#define DTCR_TOE (1 << 8) /* time overflow enabled */
95#define DTCR_WTE (1 << 7) /* wire-mesh tamper enabled */
96#define DTCR_ETBE (1 << 6) /* external B tamper enabled */
97#define DTCR_ETAE (1 << 5) /* external A tamper enabled */
98#define DTCR_EBE (1 << 4) /* external boot tamper enabled */
99#define DTCR_SAIE (1 << 3) /* SCC enabled */
100#define DTCR_TTE (1 << 2) /* temperature tamper enabled */
101#define DTCR_CTE (1 << 1) /* clock tamper enabled */
102#define DTCR_VTE (1 << 0) /* voltage tamper enabled */
103
104#define DGPR 0x3c /* DryIce General Purpose Reg */
69 105
70/** 106/**
71 * struct imxdi_dev - private imxdi rtc data 107 * struct imxdi_dev - private imxdi rtc data
@@ -313,7 +349,7 @@ static irqreturn_t dryice_norm_irq(int irq, void *dev_id)
313 dier = __raw_readl(imxdi->ioaddr + DIER); 349 dier = __raw_readl(imxdi->ioaddr + DIER);
314 350
315 /* handle write complete and write error cases */ 351 /* handle write complete and write error cases */
316 if ((dier & DIER_WCIE)) { 352 if (dier & DIER_WCIE) {
317 /*If the write wait queue is empty then there is no pending 353 /*If the write wait queue is empty then there is no pending
318 operations. It means the interrupt is for DryIce -Security. 354 operations. It means the interrupt is for DryIce -Security.
319 IRQ must be returned as none.*/ 355 IRQ must be returned as none.*/
@@ -322,7 +358,7 @@ static irqreturn_t dryice_norm_irq(int irq, void *dev_id)
322 358
323 /* DSR_WCF clears itself on DSR read */ 359 /* DSR_WCF clears itself on DSR read */
324 dsr = __raw_readl(imxdi->ioaddr + DSR); 360 dsr = __raw_readl(imxdi->ioaddr + DSR);
325 if ((dsr & (DSR_WCF | DSR_WEF))) { 361 if (dsr & (DSR_WCF | DSR_WEF)) {
326 /* mask the interrupt */ 362 /* mask the interrupt */
327 di_int_disable(imxdi, DIER_WCIE); 363 di_int_disable(imxdi, DIER_WCIE);
328 364
@@ -335,7 +371,7 @@ static irqreturn_t dryice_norm_irq(int irq, void *dev_id)
335 } 371 }
336 372
337 /* handle the alarm case */ 373 /* handle the alarm case */
338 if ((dier & DIER_CAIE)) { 374 if (dier & DIER_CAIE) {
339 /* DSR_WCF clears itself on DSR read */ 375 /* DSR_WCF clears itself on DSR read */
340 dsr = __raw_readl(imxdi->ioaddr + DSR); 376 dsr = __raw_readl(imxdi->ioaddr + DSR);
341 if (dsr & DSR_CAF) { 377 if (dsr & DSR_CAF) {
diff --git a/drivers/rtc/rtc-isl12022.c b/drivers/rtc/rtc-isl12022.c
index ee3ba7e6b45e..f9b082784b90 100644
--- a/drivers/rtc/rtc-isl12022.c
+++ b/drivers/rtc/rtc-isl12022.c
@@ -275,7 +275,8 @@ static int isl12022_probe(struct i2c_client *client,
275 275
276#ifdef CONFIG_OF 276#ifdef CONFIG_OF
277static const struct of_device_id isl12022_dt_match[] = { 277static const struct of_device_id isl12022_dt_match[] = {
278 { .compatible = "isl,isl12022" }, 278 { .compatible = "isl,isl12022" }, /* for backward compat., don't use */
279 { .compatible = "isil,isl12022" },
279 { }, 280 { },
280}; 281};
281#endif 282#endif
diff --git a/drivers/rtc/rtc-isl12057.c b/drivers/rtc/rtc-isl12057.c
index 6e1fcfb5d7e6..da818d3337ce 100644
--- a/drivers/rtc/rtc-isl12057.c
+++ b/drivers/rtc/rtc-isl12057.c
@@ -79,8 +79,10 @@
79#define ISL12057_MEM_MAP_LEN 0x10 79#define ISL12057_MEM_MAP_LEN 0x10
80 80
81struct isl12057_rtc_data { 81struct isl12057_rtc_data {
82 struct rtc_device *rtc;
82 struct regmap *regmap; 83 struct regmap *regmap;
83 struct mutex lock; 84 struct mutex lock;
85 int irq;
84}; 86};
85 87
86static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs) 88static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs)
@@ -160,14 +162,47 @@ static int isl12057_i2c_validate_chip(struct regmap *regmap)
160 return 0; 162 return 0;
161} 163}
162 164
163static int isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm) 165static int _isl12057_rtc_clear_alarm(struct device *dev)
166{
167 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
168 int ret;
169
170 ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
171 ISL12057_REG_SR_A1F, 0);
172 if (ret)
173 dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret);
174
175 return ret;
176}
177
178static int _isl12057_rtc_update_alarm(struct device *dev, int enable)
179{
180 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
181 int ret;
182
183 ret = regmap_update_bits(data->regmap, ISL12057_REG_INT,
184 ISL12057_REG_INT_A1IE,
185 enable ? ISL12057_REG_INT_A1IE : 0);
186 if (ret)
187 dev_err(dev, "%s: changing alarm interrupt flag failed (%d)\n",
188 __func__, ret);
189
190 return ret;
191}
192
193/*
194 * Note: as we only read from device and do not perform any update, there is
195 * no need for an equivalent function which would try and get driver's main
196 * lock. Here, it is safe for everyone if we just use regmap internal lock
197 * on the device when reading.
198 */
199static int _isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
164{ 200{
165 struct isl12057_rtc_data *data = dev_get_drvdata(dev); 201 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
166 u8 regs[ISL12057_RTC_SEC_LEN]; 202 u8 regs[ISL12057_RTC_SEC_LEN];
167 unsigned int sr; 203 unsigned int sr;
168 int ret; 204 int ret;
169 205
170 mutex_lock(&data->lock);
171 ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr); 206 ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr);
172 if (ret) { 207 if (ret) {
173 dev_err(dev, "%s: unable to read oscillator status flag (%d)\n", 208 dev_err(dev, "%s: unable to read oscillator status flag (%d)\n",
@@ -187,8 +222,6 @@ static int isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
187 __func__, ret); 222 __func__, ret);
188 223
189out: 224out:
190 mutex_unlock(&data->lock);
191
192 if (ret) 225 if (ret)
193 return ret; 226 return ret;
194 227
@@ -197,6 +230,168 @@ out:
197 return rtc_valid_tm(tm); 230 return rtc_valid_tm(tm);
198} 231}
199 232
233static int isl12057_rtc_update_alarm(struct device *dev, int enable)
234{
235 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
236 int ret;
237
238 mutex_lock(&data->lock);
239 ret = _isl12057_rtc_update_alarm(dev, enable);
240 mutex_unlock(&data->lock);
241
242 return ret;
243}
244
245static int isl12057_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
246{
247 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
248 struct rtc_time rtc_tm, *alarm_tm = &alarm->time;
249 unsigned long rtc_secs, alarm_secs;
250 u8 regs[ISL12057_A1_SEC_LEN];
251 unsigned int ir;
252 int ret;
253
254 mutex_lock(&data->lock);
255 ret = regmap_bulk_read(data->regmap, ISL12057_REG_A1_SC, regs,
256 ISL12057_A1_SEC_LEN);
257 if (ret) {
258 dev_err(dev, "%s: reading alarm section failed (%d)\n",
259 __func__, ret);
260 goto err_unlock;
261 }
262
263 alarm_tm->tm_sec = bcd2bin(regs[0] & 0x7f);
264 alarm_tm->tm_min = bcd2bin(regs[1] & 0x7f);
265 alarm_tm->tm_hour = bcd2bin(regs[2] & 0x3f);
266 alarm_tm->tm_mday = bcd2bin(regs[3] & 0x3f);
267 alarm_tm->tm_wday = -1;
268
269 /*
270 * The alarm section does not store year/month. We use the ones in rtc
271 * section as a basis and increment month and then year if needed to get
272 * alarm after current time.
273 */
274 ret = _isl12057_rtc_read_time(dev, &rtc_tm);
275 if (ret)
276 goto err_unlock;
277
278 alarm_tm->tm_year = rtc_tm.tm_year;
279 alarm_tm->tm_mon = rtc_tm.tm_mon;
280
281 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
282 if (ret)
283 goto err_unlock;
284
285 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
286 if (ret)
287 goto err_unlock;
288
289 if (alarm_secs < rtc_secs) {
290 if (alarm_tm->tm_mon == 11) {
291 alarm_tm->tm_mon = 0;
292 alarm_tm->tm_year += 1;
293 } else {
294 alarm_tm->tm_mon += 1;
295 }
296 }
297
298 ret = regmap_read(data->regmap, ISL12057_REG_INT, &ir);
299 if (ret) {
300 dev_err(dev, "%s: reading alarm interrupt flag failed (%d)\n",
301 __func__, ret);
302 goto err_unlock;
303 }
304
305 alarm->enabled = !!(ir & ISL12057_REG_INT_A1IE);
306
307err_unlock:
308 mutex_unlock(&data->lock);
309
310 return ret;
311}
312
313static int isl12057_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
314{
315 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
316 struct rtc_time *alarm_tm = &alarm->time;
317 unsigned long rtc_secs, alarm_secs;
318 u8 regs[ISL12057_A1_SEC_LEN];
319 struct rtc_time rtc_tm;
320 int ret, enable = 1;
321
322 mutex_lock(&data->lock);
323 ret = _isl12057_rtc_read_time(dev, &rtc_tm);
324 if (ret)
325 goto err_unlock;
326
327 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
328 if (ret)
329 goto err_unlock;
330
331 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
332 if (ret)
333 goto err_unlock;
334
335 /* If alarm time is before current time, disable the alarm */
336 if (!alarm->enabled || alarm_secs <= rtc_secs) {
337 enable = 0;
338 } else {
339 /*
340 * Chip only support alarms up to one month in the future. Let's
341 * return an error if we get something after that limit.
342 * Comparison is done by incrementing rtc_tm month field by one
343 * and checking alarm value is still below.
344 */
345 if (rtc_tm.tm_mon == 11) { /* handle year wrapping */
346 rtc_tm.tm_mon = 0;
347 rtc_tm.tm_year += 1;
348 } else {
349 rtc_tm.tm_mon += 1;
350 }
351
352 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
353 if (ret)
354 goto err_unlock;
355
356 if (alarm_secs > rtc_secs) {
357 dev_err(dev, "%s: max for alarm is one month (%d)\n",
358 __func__, ret);
359 ret = -EINVAL;
360 goto err_unlock;
361 }
362 }
363
364 /* Disable the alarm before modifying it */
365 ret = _isl12057_rtc_update_alarm(dev, 0);
366 if (ret < 0) {
367 dev_err(dev, "%s: unable to disable the alarm (%d)\n",
368 __func__, ret);
369 goto err_unlock;
370 }
371
372 /* Program alarm registers */
373 regs[0] = bin2bcd(alarm_tm->tm_sec) & 0x7f;
374 regs[1] = bin2bcd(alarm_tm->tm_min) & 0x7f;
375 regs[2] = bin2bcd(alarm_tm->tm_hour) & 0x3f;
376 regs[3] = bin2bcd(alarm_tm->tm_mday) & 0x3f;
377
378 ret = regmap_bulk_write(data->regmap, ISL12057_REG_A1_SC, regs,
379 ISL12057_A1_SEC_LEN);
380 if (ret < 0) {
381 dev_err(dev, "%s: writing alarm section failed (%d)\n",
382 __func__, ret);
383 goto err_unlock;
384 }
385
386 /* Enable or disable alarm */
387 ret = _isl12057_rtc_update_alarm(dev, enable);
388
389err_unlock:
390 mutex_unlock(&data->lock);
391
392 return ret;
393}
394
200static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm) 395static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm)
201{ 396{
202 struct isl12057_rtc_data *data = dev_get_drvdata(dev); 397 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
@@ -262,12 +457,85 @@ static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
262 return 0; 457 return 0;
263} 458}
264 459
460#ifdef CONFIG_OF
461/*
462 * One would expect the device to be marked as a wakeup source only
463 * when an IRQ pin of the RTC is routed to an interrupt line of the
464 * CPU. In practice, such an IRQ pin can be connected to a PMIC and
465 * this allows the device to be powered up when RTC alarm rings. This
466 * is for instance the case on ReadyNAS 102, 104 and 2120. On those
467 * devices with no IRQ driectly connected to the SoC, the RTC chip
468 * can be forced as a wakeup source by stating that explicitly in
469 * the device's .dts file using the "isil,irq2-can-wakeup-machine"
470 * boolean property. This will guarantee 'wakealarm' sysfs entry is
471 * available on the device.
472 *
473 * The function below returns 1, i.e. the capability of the chip to
474 * wakeup the device, based on IRQ availability or if the boolean
475 * property has been set in the .dts file. Otherwise, it returns 0.
476 */
477
478static bool isl12057_can_wakeup_machine(struct device *dev)
479{
480 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
481
482 return (data->irq || of_property_read_bool(dev->of_node,
483 "isil,irq2-can-wakeup-machine"));
484}
485#else
486static bool isl12057_can_wakeup_machine(struct device *dev)
487{
488 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
489
490 return !!data->irq;
491}
492#endif
493
494static int isl12057_rtc_alarm_irq_enable(struct device *dev,
495 unsigned int enable)
496{
497 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
498 int ret = -ENOTTY;
499
500 if (rtc_data->irq)
501 ret = isl12057_rtc_update_alarm(dev, enable);
502
503 return ret;
504}
505
506static irqreturn_t isl12057_rtc_interrupt(int irq, void *data)
507{
508 struct i2c_client *client = data;
509 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(&client->dev);
510 struct rtc_device *rtc = rtc_data->rtc;
511 int ret, handled = IRQ_NONE;
512 unsigned int sr;
513
514 ret = regmap_read(rtc_data->regmap, ISL12057_REG_SR, &sr);
515 if (!ret && (sr & ISL12057_REG_SR_A1F)) {
516 dev_dbg(&client->dev, "RTC alarm!\n");
517
518 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
519
520 /* Acknowledge and disable the alarm */
521 _isl12057_rtc_clear_alarm(&client->dev);
522 _isl12057_rtc_update_alarm(&client->dev, 0);
523
524 handled = IRQ_HANDLED;
525 }
526
527 return handled;
528}
529
265static const struct rtc_class_ops rtc_ops = { 530static const struct rtc_class_ops rtc_ops = {
266 .read_time = isl12057_rtc_read_time, 531 .read_time = _isl12057_rtc_read_time,
267 .set_time = isl12057_rtc_set_time, 532 .set_time = isl12057_rtc_set_time,
533 .read_alarm = isl12057_rtc_read_alarm,
534 .set_alarm = isl12057_rtc_set_alarm,
535 .alarm_irq_enable = isl12057_rtc_alarm_irq_enable,
268}; 536};
269 537
270static struct regmap_config isl12057_rtc_regmap_config = { 538static const struct regmap_config isl12057_rtc_regmap_config = {
271 .reg_bits = 8, 539 .reg_bits = 8,
272 .val_bits = 8, 540 .val_bits = 8,
273}; 541};
@@ -277,7 +545,6 @@ static int isl12057_probe(struct i2c_client *client,
277{ 545{
278 struct device *dev = &client->dev; 546 struct device *dev = &client->dev;
279 struct isl12057_rtc_data *data; 547 struct isl12057_rtc_data *data;
280 struct rtc_device *rtc;
281 struct regmap *regmap; 548 struct regmap *regmap;
282 int ret; 549 int ret;
283 550
@@ -310,13 +577,75 @@ static int isl12057_probe(struct i2c_client *client,
310 data->regmap = regmap; 577 data->regmap = regmap;
311 dev_set_drvdata(dev, data); 578 dev_set_drvdata(dev, data);
312 579
313 rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops, THIS_MODULE); 580 if (client->irq > 0) {
314 return PTR_ERR_OR_ZERO(rtc); 581 ret = devm_request_threaded_irq(dev, client->irq, NULL,
582 isl12057_rtc_interrupt,
583 IRQF_SHARED|IRQF_ONESHOT,
584 DRV_NAME, client);
585 if (!ret)
586 data->irq = client->irq;
587 else
588 dev_err(dev, "%s: irq %d unavailable (%d)\n", __func__,
589 client->irq, ret);
590 }
591
592 if (isl12057_can_wakeup_machine(dev))
593 device_init_wakeup(dev, true);
594
595 data->rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops,
596 THIS_MODULE);
597 ret = PTR_ERR_OR_ZERO(data->rtc);
598 if (ret) {
599 dev_err(dev, "%s: unable to register RTC device (%d)\n",
600 __func__, ret);
601 goto err;
602 }
603
604 /* We cannot support UIE mode if we do not have an IRQ line */
605 if (!data->irq)
606 data->rtc->uie_unsupported = 1;
607
608err:
609 return ret;
610}
611
612static int isl12057_remove(struct i2c_client *client)
613{
614 if (isl12057_can_wakeup_machine(&client->dev))
615 device_init_wakeup(&client->dev, false);
616
617 return 0;
618}
619
620#ifdef CONFIG_PM_SLEEP
621static int isl12057_rtc_suspend(struct device *dev)
622{
623 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
624
625 if (rtc_data->irq && device_may_wakeup(dev))
626 return enable_irq_wake(rtc_data->irq);
627
628 return 0;
629}
630
631static int isl12057_rtc_resume(struct device *dev)
632{
633 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
634
635 if (rtc_data->irq && device_may_wakeup(dev))
636 return disable_irq_wake(rtc_data->irq);
637
638 return 0;
315} 639}
640#endif
641
642static SIMPLE_DEV_PM_OPS(isl12057_rtc_pm_ops, isl12057_rtc_suspend,
643 isl12057_rtc_resume);
316 644
317#ifdef CONFIG_OF 645#ifdef CONFIG_OF
318static const struct of_device_id isl12057_dt_match[] = { 646static const struct of_device_id isl12057_dt_match[] = {
319 { .compatible = "isl,isl12057" }, 647 { .compatible = "isl,isl12057" }, /* for backward compat., don't use */
648 { .compatible = "isil,isl12057" },
320 { }, 649 { },
321}; 650};
322#endif 651#endif
@@ -331,9 +660,11 @@ static struct i2c_driver isl12057_driver = {
331 .driver = { 660 .driver = {
332 .name = DRV_NAME, 661 .name = DRV_NAME,
333 .owner = THIS_MODULE, 662 .owner = THIS_MODULE,
663 .pm = &isl12057_rtc_pm_ops,
334 .of_match_table = of_match_ptr(isl12057_dt_match), 664 .of_match_table = of_match_ptr(isl12057_dt_match),
335 }, 665 },
336 .probe = isl12057_probe, 666 .probe = isl12057_probe,
667 .remove = isl12057_remove,
337 .id_table = isl12057_id, 668 .id_table = isl12057_id,
338}; 669};
339module_i2c_driver(isl12057_driver); 670module_i2c_driver(isl12057_driver);
diff --git a/drivers/rtc/rtc-pcf2123.c b/drivers/rtc/rtc-pcf2123.c
index d1953bb244c5..8a7556cbcb7f 100644
--- a/drivers/rtc/rtc-pcf2123.c
+++ b/drivers/rtc/rtc-pcf2123.c
@@ -38,6 +38,7 @@
38#include <linux/errno.h> 38#include <linux/errno.h>
39#include <linux/init.h> 39#include <linux/init.h>
40#include <linux/kernel.h> 40#include <linux/kernel.h>
41#include <linux/of.h>
41#include <linux/string.h> 42#include <linux/string.h>
42#include <linux/slab.h> 43#include <linux/slab.h>
43#include <linux/rtc.h> 44#include <linux/rtc.h>
@@ -340,10 +341,19 @@ static int pcf2123_remove(struct spi_device *spi)
340 return 0; 341 return 0;
341} 342}
342 343
344#ifdef CONFIG_OF
345static const struct of_device_id pcf2123_dt_ids[] = {
346 { .compatible = "nxp,rtc-pcf2123", },
347 { /* sentinel */ }
348};
349MODULE_DEVICE_TABLE(of, pcf2123_dt_ids);
350#endif
351
343static struct spi_driver pcf2123_driver = { 352static struct spi_driver pcf2123_driver = {
344 .driver = { 353 .driver = {
345 .name = "rtc-pcf2123", 354 .name = "rtc-pcf2123",
346 .owner = THIS_MODULE, 355 .owner = THIS_MODULE,
356 .of_match_table = of_match_ptr(pcf2123_dt_ids),
347 }, 357 },
348 .probe = pcf2123_probe, 358 .probe = pcf2123_probe,
349 .remove = pcf2123_remove, 359 .remove = pcf2123_remove,
diff --git a/drivers/rtc/rtc-rk808.c b/drivers/rtc/rtc-rk808.c
index df42257668ac..91ca0bc1b484 100644
--- a/drivers/rtc/rtc-rk808.c
+++ b/drivers/rtc/rtc-rk808.c
@@ -67,15 +67,21 @@ static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm)
67 /* Force an update of the shadowed registers right now */ 67 /* Force an update of the shadowed registers right now */
68 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 68 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
69 BIT_RTC_CTRL_REG_RTC_GET_TIME, 69 BIT_RTC_CTRL_REG_RTC_GET_TIME,
70 0); 70 BIT_RTC_CTRL_REG_RTC_GET_TIME);
71 if (ret) { 71 if (ret) {
72 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); 72 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
73 return ret; 73 return ret;
74 } 74 }
75 75
76 /*
77 * After we set the GET_TIME bit, the rtc time can't be read
78 * immediately. So we should wait up to 31.25 us, about one cycle of
79 * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer
80 * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency.
81 */
76 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 82 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
77 BIT_RTC_CTRL_REG_RTC_GET_TIME, 83 BIT_RTC_CTRL_REG_RTC_GET_TIME,
78 BIT_RTC_CTRL_REG_RTC_GET_TIME); 84 0);
79 if (ret) { 85 if (ret) {
80 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); 86 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
81 return ret; 87 return ret;