aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorJiri Kosina <jkosina@suse.cz>2014-11-20 08:42:02 -0500
committerJiri Kosina <jkosina@suse.cz>2014-11-20 08:42:02 -0500
commita02001086bbfb4da35d1228bebc2f1b442db455f (patch)
tree62ab47936cef06fd08657ca5b6cd1df98c19be57 /drivers/rtc
parenteff264efeeb0898408e8c9df72d8a32621035bed (diff)
parentfc14f9c1272f62c3e8d01300f52467c0d9af50f9 (diff)
Merge Linus' tree to be be to apply submitted patches to newer code than
current trivial.git base
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig73
-rw-r--r--drivers/rtc/Makefile8
-rw-r--r--drivers/rtc/class.c16
-rw-r--r--drivers/rtc/interface.c2
-rw-r--r--drivers/rtc/rtc-au1xxx.c18
-rw-r--r--drivers/rtc/rtc-bq32k.c62
-rw-r--r--drivers/rtc/rtc-cmos.c5
-rw-r--r--drivers/rtc/rtc-da9063.c54
-rw-r--r--drivers/rtc/rtc-ds1307.c67
-rw-r--r--drivers/rtc/rtc-ds1343.c75
-rw-r--r--drivers/rtc/rtc-ds1742.c2
-rw-r--r--drivers/rtc/rtc-efi-platform.c31
-rw-r--r--drivers/rtc/rtc-efi.c34
-rw-r--r--drivers/rtc/rtc-isl12022.c12
-rw-r--r--drivers/rtc/rtc-max77686.c167
-rw-r--r--drivers/rtc/rtc-max77802.c502
-rw-r--r--drivers/rtc/rtc-mpc5121.c2
-rw-r--r--drivers/rtc/rtc-pcf85063.c204
-rw-r--r--drivers/rtc/rtc-pcf8563.c231
-rw-r--r--drivers/rtc/rtc-pcf8583.c18
-rw-r--r--drivers/rtc/rtc-pm8xxx.c222
-rw-r--r--drivers/rtc/rtc-rk808.c414
-rw-r--r--drivers/rtc/rtc-rs5c372.c11
-rw-r--r--drivers/rtc/rtc-s3c.c855
-rw-r--r--drivers/rtc/rtc-s5m.c21
-rw-r--r--drivers/rtc/rtc-sun6i.c447
-rw-r--r--drivers/rtc/rtc-tps65910.c4
27 files changed, 2916 insertions, 641 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 0754f5c7cb3b..6dd12ddbabc6 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -288,6 +288,26 @@ config RTC_DRV_MAX77686
288 This driver can also be built as a module. If so, the module 288 This driver can also be built as a module. If so, the module
289 will be called rtc-max77686. 289 will be called rtc-max77686.
290 290
291config RTC_DRV_RK808
292 tristate "Rockchip RK808 RTC"
293 depends on MFD_RK808
294 help
295 If you say yes here you will get support for the
296 RTC of RK808 PMIC.
297
298 This driver can also be built as a module. If so, the module
299 will be called rk808-rtc.
300
301config RTC_DRV_MAX77802
302 tristate "Maxim 77802 RTC"
303 depends on MFD_MAX77686
304 help
305 If you say yes here you will get support for the
306 RTC of Maxim MAX77802 PMIC.
307
308 This driver can also be built as a module. If so, the module
309 will be called rtc-max77802.
310
291config RTC_DRV_RS5C372 311config RTC_DRV_RS5C372
292 tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A" 312 tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A"
293 help 313 help
@@ -373,6 +393,14 @@ config RTC_DRV_PCF8563
373 This driver can also be built as a module. If so, the module 393 This driver can also be built as a module. If so, the module
374 will be called rtc-pcf8563. 394 will be called rtc-pcf8563.
375 395
396config RTC_DRV_PCF85063
397 tristate "nxp PCF85063"
398 help
399 If you say yes here you get support for the PCF85063 RTC chip
400
401 This driver can also be built as a module. If so, the module
402 will be called rtc-pcf85063.
403
376config RTC_DRV_PCF8583 404config RTC_DRV_PCF8583
377 tristate "Philips PCF8583" 405 tristate "Philips PCF8583"
378 help 406 help
@@ -724,6 +752,7 @@ config RTC_DRV_DS1216
724 752
725config RTC_DRV_DS1286 753config RTC_DRV_DS1286
726 tristate "Dallas DS1286" 754 tristate "Dallas DS1286"
755 depends on HAS_IOMEM
727 help 756 help
728 If you say yes here you get support for the Dallas DS1286 RTC chips. 757 If you say yes here you get support for the Dallas DS1286 RTC chips.
729 758
@@ -735,6 +764,7 @@ config RTC_DRV_DS1302
735 764
736config RTC_DRV_DS1511 765config RTC_DRV_DS1511
737 tristate "Dallas DS1511" 766 tristate "Dallas DS1511"
767 depends on HAS_IOMEM
738 help 768 help
739 If you say yes here you get support for the 769 If you say yes here you get support for the
740 Dallas DS1511 timekeeping/watchdog chip. 770 Dallas DS1511 timekeeping/watchdog chip.
@@ -744,6 +774,7 @@ config RTC_DRV_DS1511
744 774
745config RTC_DRV_DS1553 775config RTC_DRV_DS1553
746 tristate "Maxim/Dallas DS1553" 776 tristate "Maxim/Dallas DS1553"
777 depends on HAS_IOMEM
747 help 778 help
748 If you say yes here you get support for the 779 If you say yes here you get support for the
749 Maxim/Dallas DS1553 timekeeping chip. 780 Maxim/Dallas DS1553 timekeeping chip.
@@ -753,6 +784,7 @@ config RTC_DRV_DS1553
753 784
754config RTC_DRV_DS1742 785config RTC_DRV_DS1742
755 tristate "Maxim/Dallas DS1742/1743" 786 tristate "Maxim/Dallas DS1742/1743"
787 depends on HAS_IOMEM
756 help 788 help
757 If you say yes here you get support for the 789 If you say yes here you get support for the
758 Maxim/Dallas DS1742/1743 timekeeping chip. 790 Maxim/Dallas DS1742/1743 timekeeping chip.
@@ -760,6 +792,15 @@ config RTC_DRV_DS1742
760 This driver can also be built as a module. If so, the module 792 This driver can also be built as a module. If so, the module
761 will be called rtc-ds1742. 793 will be called rtc-ds1742.
762 794
795config RTC_DRV_DS2404
796 tristate "Maxim/Dallas DS2404"
797 help
798 If you say yes here you get support for the
799 Dallas DS2404 RTC chip.
800
801 This driver can also be built as a module. If so, the module
802 will be called rtc-ds2404.
803
763config RTC_DRV_DA9052 804config RTC_DRV_DA9052
764 tristate "Dialog DA9052/DA9053 RTC" 805 tristate "Dialog DA9052/DA9053 RTC"
765 depends on PMIC_DA9052 806 depends on PMIC_DA9052
@@ -789,7 +830,7 @@ config RTC_DRV_DA9063
789 830
790config RTC_DRV_EFI 831config RTC_DRV_EFI
791 tristate "EFI RTC" 832 tristate "EFI RTC"
792 depends on IA64 833 depends on EFI && !X86
793 help 834 help
794 If you say yes here you will get support for the EFI 835 If you say yes here you will get support for the EFI
795 Real Time Clock. 836 Real Time Clock.
@@ -799,6 +840,7 @@ config RTC_DRV_EFI
799 840
800config RTC_DRV_STK17TA8 841config RTC_DRV_STK17TA8
801 tristate "Simtek STK17TA8" 842 tristate "Simtek STK17TA8"
843 depends on HAS_IOMEM
802 help 844 help
803 If you say yes here you get support for the 845 If you say yes here you get support for the
804 Simtek STK17TA8 timekeeping chip. 846 Simtek STK17TA8 timekeeping chip.
@@ -817,6 +859,7 @@ config RTC_DRV_M48T86
817 859
818config RTC_DRV_M48T35 860config RTC_DRV_M48T35
819 tristate "ST M48T35" 861 tristate "ST M48T35"
862 depends on HAS_IOMEM
820 help 863 help
821 If you say Y here you will get support for the 864 If you say Y here you will get support for the
822 ST M48T35 RTC chip. 865 ST M48T35 RTC chip.
@@ -826,6 +869,7 @@ config RTC_DRV_M48T35
826 869
827config RTC_DRV_M48T59 870config RTC_DRV_M48T59
828 tristate "ST M48T59/M48T08/M48T02" 871 tristate "ST M48T59/M48T08/M48T02"
872 depends on HAS_IOMEM
829 help 873 help
830 If you say Y here you will get support for the 874 If you say Y here you will get support for the
831 ST M48T59 RTC chip and compatible ST M48T08 and M48T02. 875 ST M48T59 RTC chip and compatible ST M48T08 and M48T02.
@@ -838,6 +882,7 @@ config RTC_DRV_M48T59
838 882
839config RTC_DRV_MSM6242 883config RTC_DRV_MSM6242
840 tristate "Oki MSM6242" 884 tristate "Oki MSM6242"
885 depends on HAS_IOMEM
841 help 886 help
842 If you say yes here you get support for the Oki MSM6242 887 If you say yes here you get support for the Oki MSM6242
843 timekeeping chip. It is used in some Amiga models (e.g. A2000). 888 timekeeping chip. It is used in some Amiga models (e.g. A2000).
@@ -847,6 +892,7 @@ config RTC_DRV_MSM6242
847 892
848config RTC_DRV_BQ4802 893config RTC_DRV_BQ4802
849 tristate "TI BQ4802" 894 tristate "TI BQ4802"
895 depends on HAS_IOMEM
850 help 896 help
851 If you say Y here you will get support for the TI 897 If you say Y here you will get support for the TI
852 BQ4802 RTC chip. 898 BQ4802 RTC chip.
@@ -856,6 +902,7 @@ config RTC_DRV_BQ4802
856 902
857config RTC_DRV_RP5C01 903config RTC_DRV_RP5C01
858 tristate "Ricoh RP5C01" 904 tristate "Ricoh RP5C01"
905 depends on HAS_IOMEM
859 help 906 help
860 If you say yes here you get support for the Ricoh RP5C01 907 If you say yes here you get support for the Ricoh RP5C01
861 timekeeping chip. It is used in some Amiga models (e.g. A3000 908 timekeeping chip. It is used in some Amiga models (e.g. A3000
@@ -873,15 +920,6 @@ config RTC_DRV_V3020
873 This driver can also be built as a module. If so, the module 920 This driver can also be built as a module. If so, the module
874 will be called rtc-v3020. 921 will be called rtc-v3020.
875 922
876config RTC_DRV_DS2404
877 tristate "Dallas DS2404"
878 help
879 If you say yes here you get support for the
880 Dallas DS2404 RTC chip.
881
882 This driver can also be built as a module. If so, the module
883 will be called rtc-ds2404.
884
885config RTC_DRV_WM831X 923config RTC_DRV_WM831X
886 tristate "Wolfson Microelectronics WM831x RTC" 924 tristate "Wolfson Microelectronics WM831x RTC"
887 depends on MFD_WM831X 925 depends on MFD_WM831X
@@ -1167,9 +1205,16 @@ config RTC_DRV_SUN4V
1167 If you say Y here you will get support for the Hypervisor 1205 If you say Y here you will get support for the Hypervisor
1168 based RTC on SUN4V systems. 1206 based RTC on SUN4V systems.
1169 1207
1208config RTC_DRV_SUN6I
1209 tristate "Allwinner A31 RTC"
1210 depends on MACH_SUN6I || MACH_SUN8I
1211 help
1212 If you say Y here you will get support for the RTC found on
1213 Allwinner A31.
1214
1170config RTC_DRV_SUNXI 1215config RTC_DRV_SUNXI
1171 tristate "Allwinner sun4i/sun7i RTC" 1216 tristate "Allwinner sun4i/sun7i RTC"
1172 depends on ARCH_SUNXI 1217 depends on MACH_SUN4I || MACH_SUN7I
1173 help 1218 help
1174 If you say Y here you will get support for the RTC found on 1219 If you say Y here you will get support for the RTC found on
1175 Allwinner A10/A20. 1220 Allwinner A10/A20.
@@ -1190,7 +1235,7 @@ config RTC_DRV_TX4939
1190 1235
1191config RTC_DRV_MV 1236config RTC_DRV_MV
1192 tristate "Marvell SoC RTC" 1237 tristate "Marvell SoC RTC"
1193 depends on ARCH_KIRKWOOD || ARCH_DOVE || ARCH_MVEBU 1238 depends on ARCH_DOVE || ARCH_MVEBU
1194 help 1239 help
1195 If you say yes here you will get support for the in-chip RTC 1240 If you say yes here you will get support for the in-chip RTC
1196 that can be found in some of Marvell's SoC devices, such as 1241 that can be found in some of Marvell's SoC devices, such as
@@ -1275,7 +1320,7 @@ config RTC_DRV_LPC32XX
1275 1320
1276config RTC_DRV_PM8XXX 1321config RTC_DRV_PM8XXX
1277 tristate "Qualcomm PMIC8XXX RTC" 1322 tristate "Qualcomm PMIC8XXX RTC"
1278 depends on MFD_PM8XXX 1323 depends on MFD_PM8XXX || MFD_SPMI_PMIC
1279 help 1324 help
1280 If you say yes here you get support for the 1325 If you say yes here you get support for the
1281 Qualcomm PMIC8XXX RTC. 1326 Qualcomm PMIC8XXX RTC.
@@ -1349,6 +1394,7 @@ config RTC_DRV_SIRFSOC
1349 1394
1350config RTC_DRV_MOXART 1395config RTC_DRV_MOXART
1351 tristate "MOXA ART RTC" 1396 tristate "MOXA ART RTC"
1397 depends on ARCH_MOXART || COMPILE_TEST
1352 help 1398 help
1353 If you say yes here you get support for the MOXA ART 1399 If you say yes here you get support for the MOXA ART
1354 RTC module. 1400 RTC module.
@@ -1358,6 +1404,7 @@ config RTC_DRV_MOXART
1358 1404
1359config RTC_DRV_XGENE 1405config RTC_DRV_XGENE
1360 tristate "APM X-Gene RTC" 1406 tristate "APM X-Gene RTC"
1407 depends on HAS_IOMEM
1361 help 1408 help
1362 If you say yes here you get support for the APM X-Gene SoC real time 1409 If you say yes here you get support for the APM X-Gene SoC real time
1363 clock. 1410 clock.
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 70347d041d10..b188323c096a 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -10,6 +10,10 @@ obj-$(CONFIG_RTC_SYSTOHC) += systohc.o
10obj-$(CONFIG_RTC_CLASS) += rtc-core.o 10obj-$(CONFIG_RTC_CLASS) += rtc-core.o
11rtc-core-y := class.o interface.o 11rtc-core-y := class.o interface.o
12 12
13ifdef CONFIG_RTC_DRV_EFI
14rtc-core-y += rtc-efi-platform.o
15endif
16
13rtc-core-$(CONFIG_RTC_INTF_DEV) += rtc-dev.o 17rtc-core-$(CONFIG_RTC_INTF_DEV) += rtc-dev.o
14rtc-core-$(CONFIG_RTC_INTF_PROC) += rtc-proc.o 18rtc-core-$(CONFIG_RTC_INTF_PROC) += rtc-proc.o
15rtc-core-$(CONFIG_RTC_INTF_SYSFS) += rtc-sysfs.o 19rtc-core-$(CONFIG_RTC_INTF_SYSFS) += rtc-sysfs.o
@@ -81,6 +85,7 @@ obj-$(CONFIG_RTC_DRV_MAX8998) += rtc-max8998.o
81obj-$(CONFIG_RTC_DRV_MAX8997) += rtc-max8997.o 85obj-$(CONFIG_RTC_DRV_MAX8997) += rtc-max8997.o
82obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o 86obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o
83obj-$(CONFIG_RTC_DRV_MAX77686) += rtc-max77686.o 87obj-$(CONFIG_RTC_DRV_MAX77686) += rtc-max77686.o
88obj-$(CONFIG_RTC_DRV_MAX77802) += rtc-max77802.o
84obj-$(CONFIG_RTC_DRV_MC13XXX) += rtc-mc13xxx.o 89obj-$(CONFIG_RTC_DRV_MC13XXX) += rtc-mc13xxx.o
85obj-$(CONFIG_RTC_DRV_MCP795) += rtc-mcp795.o 90obj-$(CONFIG_RTC_DRV_MCP795) += rtc-mcp795.o
86obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o 91obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o
@@ -93,6 +98,7 @@ obj-$(CONFIG_RTC_DRV_PCAP) += rtc-pcap.o
93obj-$(CONFIG_RTC_DRV_PCF2127) += rtc-pcf2127.o 98obj-$(CONFIG_RTC_DRV_PCF2127) += rtc-pcf2127.o
94obj-$(CONFIG_RTC_DRV_PCF8523) += rtc-pcf8523.o 99obj-$(CONFIG_RTC_DRV_PCF8523) += rtc-pcf8523.o
95obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o 100obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o
101obj-$(CONFIG_RTC_DRV_PCF85063) += rtc-pcf85063.o
96obj-$(CONFIG_RTC_DRV_PCF8583) += rtc-pcf8583.o 102obj-$(CONFIG_RTC_DRV_PCF8583) += rtc-pcf8583.o
97obj-$(CONFIG_RTC_DRV_PCF2123) += rtc-pcf2123.o 103obj-$(CONFIG_RTC_DRV_PCF2123) += rtc-pcf2123.o
98obj-$(CONFIG_RTC_DRV_PCF50633) += rtc-pcf50633.o 104obj-$(CONFIG_RTC_DRV_PCF50633) += rtc-pcf50633.o
@@ -104,6 +110,7 @@ obj-$(CONFIG_RTC_DRV_PUV3) += rtc-puv3.o
104obj-$(CONFIG_RTC_DRV_PXA) += rtc-pxa.o 110obj-$(CONFIG_RTC_DRV_PXA) += rtc-pxa.o
105obj-$(CONFIG_RTC_DRV_R9701) += rtc-r9701.o 111obj-$(CONFIG_RTC_DRV_R9701) += rtc-r9701.o
106obj-$(CONFIG_RTC_DRV_RC5T583) += rtc-rc5t583.o 112obj-$(CONFIG_RTC_DRV_RC5T583) += rtc-rc5t583.o
113obj-$(CONFIG_RTC_DRV_RK808) += rtc-rk808.o
107obj-$(CONFIG_RTC_DRV_RP5C01) += rtc-rp5c01.o 114obj-$(CONFIG_RTC_DRV_RP5C01) += rtc-rp5c01.o
108obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o 115obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o
109obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o 116obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o
@@ -123,6 +130,7 @@ obj-$(CONFIG_RTC_DRV_STARFIRE) += rtc-starfire.o
123obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o 130obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o
124obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o 131obj-$(CONFIG_RTC_DRV_STMP) += rtc-stmp3xxx.o
125obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o 132obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o
133obj-$(CONFIG_RTC_DRV_SUN6I) += rtc-sun6i.o
126obj-$(CONFIG_RTC_DRV_SUNXI) += rtc-sunxi.o 134obj-$(CONFIG_RTC_DRV_SUNXI) += rtc-sunxi.o
127obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o 135obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o
128obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o 136obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 589351ef75d0..38e26be705be 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -53,6 +53,7 @@ static int rtc_suspend(struct device *dev)
53 struct rtc_device *rtc = to_rtc_device(dev); 53 struct rtc_device *rtc = to_rtc_device(dev);
54 struct rtc_time tm; 54 struct rtc_time tm;
55 struct timespec delta, delta_delta; 55 struct timespec delta, delta_delta;
56 int err;
56 57
57 if (has_persistent_clock()) 58 if (has_persistent_clock())
58 return 0; 59 return 0;
@@ -61,7 +62,12 @@ static int rtc_suspend(struct device *dev)
61 return 0; 62 return 0;
62 63
63 /* snapshot the current RTC and system time at suspend*/ 64 /* snapshot the current RTC and system time at suspend*/
64 rtc_read_time(rtc, &tm); 65 err = rtc_read_time(rtc, &tm);
66 if (err < 0) {
67 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
68 return 0;
69 }
70
65 getnstimeofday(&old_system); 71 getnstimeofday(&old_system);
66 rtc_tm_to_time(&tm, &old_rtc.tv_sec); 72 rtc_tm_to_time(&tm, &old_rtc.tv_sec);
67 73
@@ -94,6 +100,7 @@ static int rtc_resume(struct device *dev)
94 struct rtc_time tm; 100 struct rtc_time tm;
95 struct timespec new_system, new_rtc; 101 struct timespec new_system, new_rtc;
96 struct timespec sleep_time; 102 struct timespec sleep_time;
103 int err;
97 104
98 if (has_persistent_clock()) 105 if (has_persistent_clock())
99 return 0; 106 return 0;
@@ -104,7 +111,12 @@ static int rtc_resume(struct device *dev)
104 111
105 /* snapshot the current rtc and system time at resume */ 112 /* snapshot the current rtc and system time at resume */
106 getnstimeofday(&new_system); 113 getnstimeofday(&new_system);
107 rtc_read_time(rtc, &tm); 114 err = rtc_read_time(rtc, &tm);
115 if (err < 0) {
116 pr_debug("%s: fail to read rtc time\n", dev_name(&rtc->dev));
117 return 0;
118 }
119
108 if (rtc_valid_tm(&tm) != 0) { 120 if (rtc_valid_tm(&tm) != 0) {
109 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev)); 121 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
110 return 0; 122 return 0;
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 5813fa52c3d4..5b2717f5dafa 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -348,6 +348,8 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
348 348
349 /* Make sure we're not setting alarms in the past */ 349 /* Make sure we're not setting alarms in the past */
350 err = __rtc_read_time(rtc, &tm); 350 err = __rtc_read_time(rtc, &tm);
351 if (err)
352 return err;
351 rtc_tm_to_time(&tm, &now); 353 rtc_tm_to_time(&tm, &now);
352 if (scheduled <= now) 354 if (scheduled <= now)
353 return -ETIME; 355 return -ETIME;
diff --git a/drivers/rtc/rtc-au1xxx.c b/drivers/rtc/rtc-au1xxx.c
index ed526a192ce0..fd25e2374d4e 100644
--- a/drivers/rtc/rtc-au1xxx.c
+++ b/drivers/rtc/rtc-au1xxx.c
@@ -32,7 +32,7 @@ static int au1xtoy_rtc_read_time(struct device *dev, struct rtc_time *tm)
32{ 32{
33 unsigned long t; 33 unsigned long t;
34 34
35 t = au_readl(SYS_TOYREAD); 35 t = alchemy_rdsys(AU1000_SYS_TOYREAD);
36 36
37 rtc_time_to_tm(t, tm); 37 rtc_time_to_tm(t, tm);
38 38
@@ -45,13 +45,12 @@ static int au1xtoy_rtc_set_time(struct device *dev, struct rtc_time *tm)
45 45
46 rtc_tm_to_time(tm, &t); 46 rtc_tm_to_time(tm, &t);
47 47
48 au_writel(t, SYS_TOYWRITE); 48 alchemy_wrsys(t, AU1000_SYS_TOYWRITE);
49 au_sync();
50 49
51 /* wait for the pending register write to succeed. This can 50 /* wait for the pending register write to succeed. This can
52 * take up to 6 seconds... 51 * take up to 6 seconds...
53 */ 52 */
54 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S) 53 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
55 msleep(1); 54 msleep(1);
56 55
57 return 0; 56 return 0;
@@ -68,7 +67,7 @@ static int au1xtoy_rtc_probe(struct platform_device *pdev)
68 unsigned long t; 67 unsigned long t;
69 int ret; 68 int ret;
70 69
71 t = au_readl(SYS_COUNTER_CNTRL); 70 t = alchemy_rdsys(AU1000_SYS_CNTRCTRL);
72 if (!(t & CNTR_OK)) { 71 if (!(t & CNTR_OK)) {
73 dev_err(&pdev->dev, "counters not working; aborting.\n"); 72 dev_err(&pdev->dev, "counters not working; aborting.\n");
74 ret = -ENODEV; 73 ret = -ENODEV;
@@ -78,10 +77,10 @@ static int au1xtoy_rtc_probe(struct platform_device *pdev)
78 ret = -ETIMEDOUT; 77 ret = -ETIMEDOUT;
79 78
80 /* set counter0 tickrate to 1Hz if necessary */ 79 /* set counter0 tickrate to 1Hz if necessary */
81 if (au_readl(SYS_TOYTRIM) != 32767) { 80 if (alchemy_rdsys(AU1000_SYS_TOYTRIM) != 32767) {
82 /* wait until hardware gives access to TRIM register */ 81 /* wait until hardware gives access to TRIM register */
83 t = 0x00100000; 82 t = 0x00100000;
84 while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T0S) && --t) 83 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T0S) && --t)
85 msleep(1); 84 msleep(1);
86 85
87 if (!t) { 86 if (!t) {
@@ -93,12 +92,11 @@ static int au1xtoy_rtc_probe(struct platform_device *pdev)
93 } 92 }
94 93
95 /* set 1Hz TOY tick rate */ 94 /* set 1Hz TOY tick rate */
96 au_writel(32767, SYS_TOYTRIM); 95 alchemy_wrsys(32767, AU1000_SYS_TOYTRIM);
97 au_sync();
98 } 96 }
99 97
100 /* wait until the hardware allows writes to the counter reg */ 98 /* wait until the hardware allows writes to the counter reg */
101 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S) 99 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
102 msleep(1); 100 msleep(1);
103 101
104 rtcdev = devm_rtc_device_register(&pdev->dev, "rtc-au1xxx", 102 rtcdev = devm_rtc_device_register(&pdev->dev, "rtc-au1xxx",
diff --git a/drivers/rtc/rtc-bq32k.c b/drivers/rtc/rtc-bq32k.c
index c74bf0dc52cc..92679df6d6e2 100644
--- a/drivers/rtc/rtc-bq32k.c
+++ b/drivers/rtc/rtc-bq32k.c
@@ -2,10 +2,14 @@
2 * Driver for TI BQ32000 RTC. 2 * Driver for TI BQ32000 RTC.
3 * 3 *
4 * Copyright (C) 2009 Semihalf. 4 * Copyright (C) 2009 Semihalf.
5 * Copyright (C) 2014 Pavel Machek <pavel@denx.de>
5 * 6 *
6 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as 8 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
10 *
11 * You can get hardware description at
12 * http://www.ti.com/lit/ds/symlink/bq32000.pdf
9 */ 13 */
10 14
11#include <linux/module.h> 15#include <linux/module.h>
@@ -27,6 +31,10 @@
27#define BQ32K_CENT 0x40 /* Century flag */ 31#define BQ32K_CENT 0x40 /* Century flag */
28#define BQ32K_CENT_EN 0x80 /* Century flag enable bit */ 32#define BQ32K_CENT_EN 0x80 /* Century flag enable bit */
29 33
34#define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */
35#define BQ32K_TCH2 0x08 /* Trickle charge enable */
36#define BQ32K_CFG2 0x09 /* Trickle charger control */
37
30struct bq32k_regs { 38struct bq32k_regs {
31 uint8_t seconds; 39 uint8_t seconds;
32 uint8_t minutes; 40 uint8_t minutes;
@@ -122,6 +130,57 @@ static const struct rtc_class_ops bq32k_rtc_ops = {
122 .set_time = bq32k_rtc_set_time, 130 .set_time = bq32k_rtc_set_time,
123}; 131};
124 132
133static int trickle_charger_of_init(struct device *dev, struct device_node *node)
134{
135 unsigned char reg;
136 int error;
137 u32 ohms = 0;
138
139 if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
140 return 0;
141
142 switch (ohms) {
143 case 180+940:
144 /*
145 * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
146 * over diode and 940ohm resistor)
147 */
148
149 if (of_property_read_bool(node, "trickle-diode-disable")) {
150 dev_err(dev, "diode and resistor mismatch\n");
151 return -EINVAL;
152 }
153 reg = 0x05;
154 break;
155
156 case 180+20000:
157 /* diode disabled */
158
159 if (!of_property_read_bool(node, "trickle-diode-disable")) {
160 dev_err(dev, "bq32k: diode and resistor mismatch\n");
161 return -EINVAL;
162 }
163 reg = 0x45;
164 break;
165
166 default:
167 dev_err(dev, "invalid resistor value (%d)\n", ohms);
168 return -EINVAL;
169 }
170
171 error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
172 if (error)
173 return error;
174
175 reg = 0x20;
176 error = bq32k_write(dev, &reg, BQ32K_TCH2, 1);
177 if (error)
178 return error;
179
180 dev_info(dev, "Enabled trickle RTC battery charge.\n");
181 return 0;
182}
183
125static int bq32k_probe(struct i2c_client *client, 184static int bq32k_probe(struct i2c_client *client,
126 const struct i2c_device_id *id) 185 const struct i2c_device_id *id)
127{ 186{
@@ -153,6 +212,9 @@ static int bq32k_probe(struct i2c_client *client,
153 if (error) 212 if (error)
154 return error; 213 return error;
155 214
215 if (client && client->dev.of_node)
216 trickle_charger_of_init(dev, client->dev.of_node);
217
156 rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name, 218 rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
157 &bq32k_rtc_ops, THIS_MODULE); 219 &bq32k_rtc_ops, THIS_MODULE);
158 if (IS_ERR(rtc)) 220 if (IS_ERR(rtc))
diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index b0e4a3eb33c7..5b2e76159b41 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -856,7 +856,7 @@ static void __exit cmos_do_remove(struct device *dev)
856 cmos->dev = NULL; 856 cmos->dev = NULL;
857} 857}
858 858
859#ifdef CONFIG_PM_SLEEP 859#ifdef CONFIG_PM
860 860
861static int cmos_suspend(struct device *dev) 861static int cmos_suspend(struct device *dev)
862{ 862{
@@ -907,6 +907,8 @@ static inline int cmos_poweroff(struct device *dev)
907 return cmos_suspend(dev); 907 return cmos_suspend(dev);
908} 908}
909 909
910#ifdef CONFIG_PM_SLEEP
911
910static int cmos_resume(struct device *dev) 912static int cmos_resume(struct device *dev)
911{ 913{
912 struct cmos_rtc *cmos = dev_get_drvdata(dev); 914 struct cmos_rtc *cmos = dev_get_drvdata(dev);
@@ -954,6 +956,7 @@ static int cmos_resume(struct device *dev)
954 return 0; 956 return 0;
955} 957}
956 958
959#endif
957#else 960#else
958 961
959static inline int cmos_poweroff(struct device *dev) 962static inline int cmos_poweroff(struct device *dev)
diff --git a/drivers/rtc/rtc-da9063.c b/drivers/rtc/rtc-da9063.c
index 595393098b09..731ed1a97f59 100644
--- a/drivers/rtc/rtc-da9063.c
+++ b/drivers/rtc/rtc-da9063.c
@@ -29,6 +29,8 @@
29#define YEARS_FROM_DA9063(year) ((year) + 100) 29#define YEARS_FROM_DA9063(year) ((year) + 100)
30#define MONTHS_FROM_DA9063(month) ((month) - 1) 30#define MONTHS_FROM_DA9063(month) ((month) - 1)
31 31
32#define RTC_ALARM_DATA_LEN (DA9063_AD_REG_ALARM_Y - DA9063_AD_REG_ALARM_MI + 1)
33
32#define RTC_DATA_LEN (DA9063_REG_COUNT_Y - DA9063_REG_COUNT_S + 1) 34#define RTC_DATA_LEN (DA9063_REG_COUNT_Y - DA9063_REG_COUNT_S + 1)
33#define RTC_SEC 0 35#define RTC_SEC 0
34#define RTC_MIN 1 36#define RTC_MIN 1
@@ -42,6 +44,10 @@ struct da9063_rtc {
42 struct da9063 *hw; 44 struct da9063 *hw;
43 struct rtc_time alarm_time; 45 struct rtc_time alarm_time;
44 bool rtc_sync; 46 bool rtc_sync;
47 int alarm_year;
48 int alarm_start;
49 int alarm_len;
50 int data_start;
45}; 51};
46 52
47static void da9063_data_to_tm(u8 *data, struct rtc_time *tm) 53static void da9063_data_to_tm(u8 *data, struct rtc_time *tm)
@@ -83,7 +89,7 @@ static int da9063_rtc_stop_alarm(struct device *dev)
83{ 89{
84 struct da9063_rtc *rtc = dev_get_drvdata(dev); 90 struct da9063_rtc *rtc = dev_get_drvdata(dev);
85 91
86 return regmap_update_bits(rtc->hw->regmap, DA9063_REG_ALARM_Y, 92 return regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
87 DA9063_ALARM_ON, 0); 93 DA9063_ALARM_ON, 0);
88} 94}
89 95
@@ -91,7 +97,7 @@ static int da9063_rtc_start_alarm(struct device *dev)
91{ 97{
92 struct da9063_rtc *rtc = dev_get_drvdata(dev); 98 struct da9063_rtc *rtc = dev_get_drvdata(dev);
93 99
94 return regmap_update_bits(rtc->hw->regmap, DA9063_REG_ALARM_Y, 100 return regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
95 DA9063_ALARM_ON, DA9063_ALARM_ON); 101 DA9063_ALARM_ON, DA9063_ALARM_ON);
96} 102}
97 103
@@ -151,8 +157,9 @@ static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
151 int ret; 157 int ret;
152 unsigned int val; 158 unsigned int val;
153 159
154 ret = regmap_bulk_read(rtc->hw->regmap, DA9063_REG_ALARM_S, 160 data[RTC_SEC] = 0;
155 &data[RTC_SEC], RTC_DATA_LEN); 161 ret = regmap_bulk_read(rtc->hw->regmap, rtc->alarm_start,
162 &data[rtc->data_start], rtc->alarm_len);
156 if (ret < 0) 163 if (ret < 0)
157 return ret; 164 return ret;
158 165
@@ -186,14 +193,14 @@ static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
186 return ret; 193 return ret;
187 } 194 }
188 195
189 ret = regmap_bulk_write(rtc->hw->regmap, DA9063_REG_ALARM_S, 196 ret = regmap_bulk_write(rtc->hw->regmap, rtc->alarm_start,
190 data, RTC_DATA_LEN); 197 &data[rtc->data_start], rtc->alarm_len);
191 if (ret < 0) { 198 if (ret < 0) {
192 dev_err(dev, "Failed to write alarm: %d\n", ret); 199 dev_err(dev, "Failed to write alarm: %d\n", ret);
193 return ret; 200 return ret;
194 } 201 }
195 202
196 rtc->alarm_time = alrm->time; 203 da9063_data_to_tm(data, &rtc->alarm_time);
197 204
198 if (alrm->enabled) { 205 if (alrm->enabled) {
199 ret = da9063_rtc_start_alarm(dev); 206 ret = da9063_rtc_start_alarm(dev);
@@ -218,7 +225,7 @@ static irqreturn_t da9063_alarm_event(int irq, void *data)
218{ 225{
219 struct da9063_rtc *rtc = data; 226 struct da9063_rtc *rtc = data;
220 227
221 regmap_update_bits(rtc->hw->regmap, DA9063_REG_ALARM_Y, 228 regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
222 DA9063_ALARM_ON, 0); 229 DA9063_ALARM_ON, 0);
223 230
224 rtc->rtc_sync = true; 231 rtc->rtc_sync = true;
@@ -257,7 +264,23 @@ static int da9063_rtc_probe(struct platform_device *pdev)
257 goto err; 264 goto err;
258 } 265 }
259 266
260 ret = regmap_update_bits(da9063->regmap, DA9063_REG_ALARM_S, 267 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
268 if (!rtc)
269 return -ENOMEM;
270
271 if (da9063->variant_code == PMIC_DA9063_AD) {
272 rtc->alarm_year = DA9063_AD_REG_ALARM_Y;
273 rtc->alarm_start = DA9063_AD_REG_ALARM_MI;
274 rtc->alarm_len = RTC_ALARM_DATA_LEN;
275 rtc->data_start = RTC_MIN;
276 } else {
277 rtc->alarm_year = DA9063_BB_REG_ALARM_Y;
278 rtc->alarm_start = DA9063_BB_REG_ALARM_S;
279 rtc->alarm_len = RTC_DATA_LEN;
280 rtc->data_start = RTC_SEC;
281 }
282
283 ret = regmap_update_bits(da9063->regmap, rtc->alarm_start,
261 DA9063_ALARM_STATUS_TICK | DA9063_ALARM_STATUS_ALARM, 284 DA9063_ALARM_STATUS_TICK | DA9063_ALARM_STATUS_ALARM,
262 0); 285 0);
263 if (ret < 0) { 286 if (ret < 0) {
@@ -265,7 +288,7 @@ static int da9063_rtc_probe(struct platform_device *pdev)
265 goto err; 288 goto err;
266 } 289 }
267 290
268 ret = regmap_update_bits(da9063->regmap, DA9063_REG_ALARM_S, 291 ret = regmap_update_bits(da9063->regmap, rtc->alarm_start,
269 DA9063_ALARM_STATUS_ALARM, 292 DA9063_ALARM_STATUS_ALARM,
270 DA9063_ALARM_STATUS_ALARM); 293 DA9063_ALARM_STATUS_ALARM);
271 if (ret < 0) { 294 if (ret < 0) {
@@ -273,25 +296,22 @@ static int da9063_rtc_probe(struct platform_device *pdev)
273 goto err; 296 goto err;
274 } 297 }
275 298
276 ret = regmap_update_bits(da9063->regmap, DA9063_REG_ALARM_Y, 299 ret = regmap_update_bits(da9063->regmap, rtc->alarm_year,
277 DA9063_TICK_ON, 0); 300 DA9063_TICK_ON, 0);
278 if (ret < 0) { 301 if (ret < 0) {
279 dev_err(&pdev->dev, "Failed to disable TICKs\n"); 302 dev_err(&pdev->dev, "Failed to disable TICKs\n");
280 goto err; 303 goto err;
281 } 304 }
282 305
283 ret = regmap_bulk_read(da9063->regmap, DA9063_REG_ALARM_S, 306 data[RTC_SEC] = 0;
284 data, RTC_DATA_LEN); 307 ret = regmap_bulk_read(da9063->regmap, rtc->alarm_start,
308 &data[rtc->data_start], rtc->alarm_len);
285 if (ret < 0) { 309 if (ret < 0) {
286 dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n", 310 dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
287 ret); 311 ret);
288 goto err; 312 goto err;
289 } 313 }
290 314
291 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
292 if (!rtc)
293 return -ENOMEM;
294
295 platform_set_drvdata(pdev, rtc); 315 platform_set_drvdata(pdev, rtc);
296 316
297 irq_alarm = platform_get_irq_byname(pdev, "ALARM"); 317 irq_alarm = platform_get_irq_byname(pdev, "ALARM");
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index f03d5ba96db1..bb43cf703efc 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -126,9 +126,14 @@ struct chip_desc {
126 u16 nvram_offset; 126 u16 nvram_offset;
127 u16 nvram_size; 127 u16 nvram_size;
128 u16 trickle_charger_reg; 128 u16 trickle_charger_reg;
129 u8 trickle_charger_setup;
130 u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
129}; 131};
130 132
131static const struct chip_desc chips[last_ds_type] = { 133static u8 do_trickle_setup_ds1339(struct i2c_client *,
134 uint32_t ohms, bool diode);
135
136static struct chip_desc chips[last_ds_type] = {
132 [ds_1307] = { 137 [ds_1307] = {
133 .nvram_offset = 8, 138 .nvram_offset = 8,
134 .nvram_size = 56, 139 .nvram_size = 56,
@@ -143,6 +148,7 @@ static const struct chip_desc chips[last_ds_type] = {
143 [ds_1339] = { 148 [ds_1339] = {
144 .alarm = 1, 149 .alarm = 1,
145 .trickle_charger_reg = 0x10, 150 .trickle_charger_reg = 0x10,
151 .do_trickle_setup = &do_trickle_setup_ds1339,
146 }, 152 },
147 [ds_1340] = { 153 [ds_1340] = {
148 .trickle_charger_reg = 0x08, 154 .trickle_charger_reg = 0x08,
@@ -833,15 +839,58 @@ ds1307_nvram_write(struct file *filp, struct kobject *kobj,
833 return count; 839 return count;
834} 840}
835 841
842
836/*----------------------------------------------------------------------*/ 843/*----------------------------------------------------------------------*/
837 844
845static u8 do_trickle_setup_ds1339(struct i2c_client *client,
846 uint32_t ohms, bool diode)
847{
848 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
849 DS1307_TRICKLE_CHARGER_NO_DIODE;
850
851 switch (ohms) {
852 case 250:
853 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
854 break;
855 case 2000:
856 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
857 break;
858 case 4000:
859 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
860 break;
861 default:
862 dev_warn(&client->dev,
863 "Unsupported ohm value %u in dt\n", ohms);
864 return 0;
865 }
866 return setup;
867}
868
869static void ds1307_trickle_of_init(struct i2c_client *client,
870 struct chip_desc *chip)
871{
872 uint32_t ohms = 0;
873 bool diode = true;
874
875 if (!chip->do_trickle_setup)
876 goto out;
877 if (of_property_read_u32(client->dev.of_node, "trickle-resistor-ohms" , &ohms))
878 goto out;
879 if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
880 diode = false;
881 chip->trickle_charger_setup = chip->do_trickle_setup(client,
882 ohms, diode);
883out:
884 return;
885}
886
838static int ds1307_probe(struct i2c_client *client, 887static int ds1307_probe(struct i2c_client *client,
839 const struct i2c_device_id *id) 888 const struct i2c_device_id *id)
840{ 889{
841 struct ds1307 *ds1307; 890 struct ds1307 *ds1307;
842 int err = -ENODEV; 891 int err = -ENODEV;
843 int tmp; 892 int tmp;
844 const struct chip_desc *chip = &chips[id->driver_data]; 893 struct chip_desc *chip = &chips[id->driver_data];
845 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 894 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
846 bool want_irq = false; 895 bool want_irq = false;
847 unsigned char *buf; 896 unsigned char *buf;
@@ -866,9 +915,19 @@ static int ds1307_probe(struct i2c_client *client,
866 ds1307->client = client; 915 ds1307->client = client;
867 ds1307->type = id->driver_data; 916 ds1307->type = id->driver_data;
868 917
869 if (pdata && pdata->trickle_charger_setup && chip->trickle_charger_reg) 918 if (!pdata && client->dev.of_node)
919 ds1307_trickle_of_init(client, chip);
920 else if (pdata && pdata->trickle_charger_setup)
921 chip->trickle_charger_setup = pdata->trickle_charger_setup;
922
923 if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
924 dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
925 DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
926 chip->trickle_charger_reg);
870 i2c_smbus_write_byte_data(client, chip->trickle_charger_reg, 927 i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
871 DS13XX_TRICKLE_CHARGER_MAGIC | pdata->trickle_charger_setup); 928 DS13XX_TRICKLE_CHARGER_MAGIC |
929 chip->trickle_charger_setup);
930 }
872 931
873 buf = ds1307->regs; 932 buf = ds1307->regs;
874 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) { 933 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
diff --git a/drivers/rtc/rtc-ds1343.c b/drivers/rtc/rtc-ds1343.c
index c3719189dd96..ae9f997223b1 100644
--- a/drivers/rtc/rtc-ds1343.c
+++ b/drivers/rtc/rtc-ds1343.c
@@ -4,6 +4,7 @@
4 * Real Time Clock 4 * Real Time Clock
5 * 5 *
6 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com> 6 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
7 * Ankur Srivastava <sankurece@gmail.com> : DS1343 Nvram Support
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as 10 * it under the terms of the GNU General Public License version 2 as
@@ -45,6 +46,9 @@
45#define DS1343_CONTROL_REG 0x0F 46#define DS1343_CONTROL_REG 0x0F
46#define DS1343_STATUS_REG 0x10 47#define DS1343_STATUS_REG 0x10
47#define DS1343_TRICKLE_REG 0x11 48#define DS1343_TRICKLE_REG 0x11
49#define DS1343_NVRAM 0x20
50
51#define DS1343_NVRAM_LEN 96
48 52
49/* DS1343 Control Registers bits */ 53/* DS1343 Control Registers bits */
50#define DS1343_EOSC 0x80 54#define DS1343_EOSC 0x80
@@ -149,6 +153,64 @@ static ssize_t ds1343_store_glitchfilter(struct device *dev,
149static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter, 153static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
150 ds1343_store_glitchfilter); 154 ds1343_store_glitchfilter);
151 155
156static ssize_t ds1343_nvram_write(struct file *filp, struct kobject *kobj,
157 struct bin_attribute *attr,
158 char *buf, loff_t off, size_t count)
159{
160 int ret;
161 unsigned char address;
162 struct device *dev = kobj_to_dev(kobj);
163 struct ds1343_priv *priv = dev_get_drvdata(dev);
164
165 if (unlikely(!count))
166 return count;
167
168 if ((count + off) > DS1343_NVRAM_LEN)
169 count = DS1343_NVRAM_LEN - off;
170
171 address = DS1343_NVRAM + off;
172
173 ret = regmap_bulk_write(priv->map, address, buf, count);
174 if (ret < 0)
175 dev_err(&priv->spi->dev, "Error in nvram write %d", ret);
176
177 return (ret < 0) ? ret : count;
178}
179
180
181static ssize_t ds1343_nvram_read(struct file *filp, struct kobject *kobj,
182 struct bin_attribute *attr,
183 char *buf, loff_t off, size_t count)
184{
185 int ret;
186 unsigned char address;
187 struct device *dev = kobj_to_dev(kobj);
188 struct ds1343_priv *priv = dev_get_drvdata(dev);
189
190 if (unlikely(!count))
191 return count;
192
193 if ((count + off) > DS1343_NVRAM_LEN)
194 count = DS1343_NVRAM_LEN - off;
195
196 address = DS1343_NVRAM + off;
197
198 ret = regmap_bulk_read(priv->map, address, buf, count);
199 if (ret < 0)
200 dev_err(&priv->spi->dev, "Error in nvram read %d\n", ret);
201
202 return (ret < 0) ? ret : count;
203}
204
205
206static struct bin_attribute nvram_attr = {
207 .attr.name = "nvram",
208 .attr.mode = S_IRUGO | S_IWUSR,
209 .read = ds1343_nvram_read,
210 .write = ds1343_nvram_write,
211 .size = DS1343_NVRAM_LEN,
212};
213
152static ssize_t ds1343_show_alarmstatus(struct device *dev, 214static ssize_t ds1343_show_alarmstatus(struct device *dev,
153 struct device_attribute *attr, char *buf) 215 struct device_attribute *attr, char *buf)
154{ 216{
@@ -274,12 +336,16 @@ static int ds1343_sysfs_register(struct device *dev)
274 if (err) 336 if (err)
275 goto error1; 337 goto error1;
276 338
339 err = device_create_bin_file(dev, &nvram_attr);
340 if (err)
341 goto error2;
342
277 if (priv->irq <= 0) 343 if (priv->irq <= 0)
278 return err; 344 return err;
279 345
280 err = device_create_file(dev, &dev_attr_alarm_mode); 346 err = device_create_file(dev, &dev_attr_alarm_mode);
281 if (err) 347 if (err)
282 goto error2; 348 goto error3;
283 349
284 err = device_create_file(dev, &dev_attr_alarm_status); 350 err = device_create_file(dev, &dev_attr_alarm_status);
285 if (!err) 351 if (!err)
@@ -287,6 +353,9 @@ static int ds1343_sysfs_register(struct device *dev)
287 353
288 device_remove_file(dev, &dev_attr_alarm_mode); 354 device_remove_file(dev, &dev_attr_alarm_mode);
289 355
356error3:
357 device_remove_bin_file(dev, &nvram_attr);
358
290error2: 359error2:
291 device_remove_file(dev, &dev_attr_trickle_charger); 360 device_remove_file(dev, &dev_attr_trickle_charger);
292 361
@@ -302,6 +371,7 @@ static void ds1343_sysfs_unregister(struct device *dev)
302 371
303 device_remove_file(dev, &dev_attr_glitch_filter); 372 device_remove_file(dev, &dev_attr_glitch_filter);
304 device_remove_file(dev, &dev_attr_trickle_charger); 373 device_remove_file(dev, &dev_attr_trickle_charger);
374 device_remove_bin_file(dev, &nvram_attr);
305 375
306 if (priv->irq <= 0) 376 if (priv->irq <= 0)
307 return; 377 return;
@@ -684,6 +754,7 @@ static struct spi_driver ds1343_driver = {
684module_spi_driver(ds1343_driver); 754module_spi_driver(ds1343_driver);
685 755
686MODULE_DESCRIPTION("DS1343 RTC SPI Driver"); 756MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
687MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>"); 757MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>,"
758 "Ankur Srivastava <sankurece@gmail.com>");
688MODULE_LICENSE("GPL v2"); 759MODULE_LICENSE("GPL v2");
689MODULE_VERSION(DS1343_DRV_VERSION); 760MODULE_VERSION(DS1343_DRV_VERSION);
diff --git a/drivers/rtc/rtc-ds1742.c b/drivers/rtc/rtc-ds1742.c
index c6b2191a4128..9822715db8ba 100644
--- a/drivers/rtc/rtc-ds1742.c
+++ b/drivers/rtc/rtc-ds1742.c
@@ -231,7 +231,7 @@ static struct platform_driver ds1742_rtc_driver = {
231 .driver = { 231 .driver = {
232 .name = "rtc-ds1742", 232 .name = "rtc-ds1742",
233 .owner = THIS_MODULE, 233 .owner = THIS_MODULE,
234 .of_match_table = ds1742_rtc_of_match, 234 .of_match_table = of_match_ptr(ds1742_rtc_of_match),
235 }, 235 },
236}; 236};
237 237
diff --git a/drivers/rtc/rtc-efi-platform.c b/drivers/rtc/rtc-efi-platform.c
new file mode 100644
index 000000000000..b40fbe332af4
--- /dev/null
+++ b/drivers/rtc/rtc-efi-platform.c
@@ -0,0 +1,31 @@
1/*
2 * Moved from arch/ia64/kernel/time.c
3 *
4 * Copyright (C) 1998-2003 Hewlett-Packard Co
5 * Stephane Eranian <eranian@hpl.hp.com>
6 * David Mosberger <davidm@hpl.hp.com>
7 * Copyright (C) 1999 Don Dugger <don.dugger@intel.com>
8 * Copyright (C) 1999-2000 VA Linux Systems
9 * Copyright (C) 1999-2000 Walt Drummond <drummond@valinux.com>
10 */
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/efi.h>
15#include <linux/platform_device.h>
16
17static struct platform_device rtc_efi_dev = {
18 .name = "rtc-efi",
19 .id = -1,
20};
21
22static int __init rtc_init(void)
23{
24 if (efi_enabled(EFI_RUNTIME_SERVICES))
25 if (platform_device_register(&rtc_efi_dev) < 0)
26 pr_err("unable to register rtc device...\n");
27
28 /* not necessarily an error */
29 return 0;
30}
31module_init(rtc_init);
diff --git a/drivers/rtc/rtc-efi.c b/drivers/rtc/rtc-efi.c
index c4c38431012e..53b589dc34eb 100644
--- a/drivers/rtc/rtc-efi.c
+++ b/drivers/rtc/rtc-efi.c
@@ -17,6 +17,7 @@
17 17
18#include <linux/kernel.h> 18#include <linux/kernel.h>
19#include <linux/module.h> 19#include <linux/module.h>
20#include <linux/stringify.h>
20#include <linux/time.h> 21#include <linux/time.h>
21#include <linux/platform_device.h> 22#include <linux/platform_device.h>
22#include <linux/rtc.h> 23#include <linux/rtc.h>
@@ -48,8 +49,8 @@ compute_wday(efi_time_t *eft)
48 int y; 49 int y;
49 int ndays = 0; 50 int ndays = 0;
50 51
51 if (eft->year < 1998) { 52 if (eft->year < EFI_RTC_EPOCH) {
52 pr_err("EFI year < 1998, invalid date\n"); 53 pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n");
53 return -1; 54 return -1;
54 } 55 }
55 56
@@ -78,19 +79,36 @@ convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
78 eft->timezone = EFI_UNSPECIFIED_TIMEZONE; 79 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
79} 80}
80 81
81static void 82static bool
82convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime) 83convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
83{ 84{
84 memset(wtime, 0, sizeof(*wtime)); 85 memset(wtime, 0, sizeof(*wtime));
86
87 if (eft->second >= 60)
88 return false;
85 wtime->tm_sec = eft->second; 89 wtime->tm_sec = eft->second;
90
91 if (eft->minute >= 60)
92 return false;
86 wtime->tm_min = eft->minute; 93 wtime->tm_min = eft->minute;
94
95 if (eft->hour >= 24)
96 return false;
87 wtime->tm_hour = eft->hour; 97 wtime->tm_hour = eft->hour;
98
99 if (!eft->day || eft->day > 31)
100 return false;
88 wtime->tm_mday = eft->day; 101 wtime->tm_mday = eft->day;
102
103 if (!eft->month || eft->month > 12)
104 return false;
89 wtime->tm_mon = eft->month - 1; 105 wtime->tm_mon = eft->month - 1;
90 wtime->tm_year = eft->year - 1900; 106 wtime->tm_year = eft->year - 1900;
91 107
92 /* day of the week [0-6], Sunday=0 */ 108 /* day of the week [0-6], Sunday=0 */
93 wtime->tm_wday = compute_wday(eft); 109 wtime->tm_wday = compute_wday(eft);
110 if (wtime->tm_wday < 0)
111 return false;
94 112
95 /* day in the year [1-365]*/ 113 /* day in the year [1-365]*/
96 wtime->tm_yday = compute_yday(eft); 114 wtime->tm_yday = compute_yday(eft);
@@ -106,6 +124,8 @@ convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
106 default: 124 default:
107 wtime->tm_isdst = -1; 125 wtime->tm_isdst = -1;
108 } 126 }
127
128 return true;
109} 129}
110 130
111static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) 131static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
@@ -122,7 +142,8 @@ static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
122 if (status != EFI_SUCCESS) 142 if (status != EFI_SUCCESS)
123 return -EINVAL; 143 return -EINVAL;
124 144
125 convert_from_efi_time(&eft, &wkalrm->time); 145 if (!convert_from_efi_time(&eft, &wkalrm->time))
146 return -EIO;
126 147
127 return rtc_valid_tm(&wkalrm->time); 148 return rtc_valid_tm(&wkalrm->time);
128} 149}
@@ -163,7 +184,8 @@ static int efi_read_time(struct device *dev, struct rtc_time *tm)
163 return -EINVAL; 184 return -EINVAL;
164 } 185 }
165 186
166 convert_from_efi_time(&eft, tm); 187 if (!convert_from_efi_time(&eft, tm))
188 return -EIO;
167 189
168 return rtc_valid_tm(tm); 190 return rtc_valid_tm(tm);
169} 191}
@@ -210,6 +232,8 @@ static struct platform_driver efi_rtc_driver = {
210 232
211module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe); 233module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
212 234
235MODULE_ALIAS("platform:rtc-efi");
213MODULE_AUTHOR("dann frazier <dannf@hp.com>"); 236MODULE_AUTHOR("dann frazier <dannf@hp.com>");
214MODULE_LICENSE("GPL"); 237MODULE_LICENSE("GPL");
215MODULE_DESCRIPTION("EFI RTC driver"); 238MODULE_DESCRIPTION("EFI RTC driver");
239MODULE_ALIAS("platform:rtc-efi");
diff --git a/drivers/rtc/rtc-isl12022.c b/drivers/rtc/rtc-isl12022.c
index 03b891129428..ee3ba7e6b45e 100644
--- a/drivers/rtc/rtc-isl12022.c
+++ b/drivers/rtc/rtc-isl12022.c
@@ -17,6 +17,8 @@
17#include <linux/slab.h> 17#include <linux/slab.h>
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/err.h> 19#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
20 22
21#define DRV_VERSION "0.1" 23#define DRV_VERSION "0.1"
22 24
@@ -271,6 +273,13 @@ static int isl12022_probe(struct i2c_client *client,
271 return PTR_ERR_OR_ZERO(isl12022->rtc); 273 return PTR_ERR_OR_ZERO(isl12022->rtc);
272} 274}
273 275
276#ifdef CONFIG_OF
277static const struct of_device_id isl12022_dt_match[] = {
278 { .compatible = "isl,isl12022" },
279 { },
280};
281#endif
282
274static const struct i2c_device_id isl12022_id[] = { 283static const struct i2c_device_id isl12022_id[] = {
275 { "isl12022", 0 }, 284 { "isl12022", 0 },
276 { } 285 { }
@@ -280,6 +289,9 @@ MODULE_DEVICE_TABLE(i2c, isl12022_id);
280static struct i2c_driver isl12022_driver = { 289static struct i2c_driver isl12022_driver = {
281 .driver = { 290 .driver = {
282 .name = "rtc-isl12022", 291 .name = "rtc-isl12022",
292#ifdef CONFIG_OF
293 .of_match_table = of_match_ptr(isl12022_dt_match),
294#endif
283 }, 295 },
284 .probe = isl12022_probe, 296 .probe = isl12022_probe,
285 .id_table = isl12022_id, 297 .id_table = isl12022_id,
diff --git a/drivers/rtc/rtc-max77686.c b/drivers/rtc/rtc-max77686.c
index 9efe118a28ba..cf73e969c8cc 100644
--- a/drivers/rtc/rtc-max77686.c
+++ b/drivers/rtc/rtc-max77686.c
@@ -32,15 +32,6 @@
32#define RTC_UDR_MASK (1 << RTC_UDR_SHIFT) 32#define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
33#define RTC_RBUDR_SHIFT 4 33#define RTC_RBUDR_SHIFT 4
34#define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT) 34#define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
35/* WTSR and SMPL Register */
36#define WTSRT_SHIFT 0
37#define SMPLT_SHIFT 2
38#define WTSR_EN_SHIFT 6
39#define SMPL_EN_SHIFT 7
40#define WTSRT_MASK (3 << WTSRT_SHIFT)
41#define SMPLT_MASK (3 << SMPLT_SHIFT)
42#define WTSR_EN_MASK (1 << WTSR_EN_SHIFT)
43#define SMPL_EN_MASK (1 << SMPL_EN_SHIFT)
44/* RTC Hour register */ 35/* RTC Hour register */
45#define HOUR_PM_SHIFT 6 36#define HOUR_PM_SHIFT 6
46#define HOUR_PM_MASK (1 << HOUR_PM_SHIFT) 37#define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
@@ -49,7 +40,6 @@
49#define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT) 40#define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
50 41
51#define MAX77686_RTC_UPDATE_DELAY 16 42#define MAX77686_RTC_UPDATE_DELAY 16
52#undef MAX77686_RTC_WTSR_SMPL
53 43
54enum { 44enum {
55 RTC_SEC = 0, 45 RTC_SEC = 0,
@@ -80,16 +70,6 @@ enum MAX77686_RTC_OP {
80 MAX77686_RTC_READ, 70 MAX77686_RTC_READ,
81}; 71};
82 72
83static inline int max77686_rtc_calculate_wday(u8 shifted)
84{
85 int counter = -1;
86 while (shifted) {
87 shifted >>= 1;
88 counter++;
89 }
90 return counter;
91}
92
93static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm, 73static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
94 int rtc_24hr_mode) 74 int rtc_24hr_mode)
95{ 75{
@@ -103,7 +83,8 @@ static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
103 tm->tm_hour += 12; 83 tm->tm_hour += 12;
104 } 84 }
105 85
106 tm->tm_wday = max77686_rtc_calculate_wday(data[RTC_WEEKDAY] & 0x7f); 86 /* Only a single bit is set in data[], so fls() would be equivalent */
87 tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f) - 1;
107 tm->tm_mday = data[RTC_DATE] & 0x1f; 88 tm->tm_mday = data[RTC_DATE] & 0x1f;
108 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1; 89 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
109 tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100; 90 tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
@@ -412,64 +393,6 @@ static const struct rtc_class_ops max77686_rtc_ops = {
412 .alarm_irq_enable = max77686_rtc_alarm_irq_enable, 393 .alarm_irq_enable = max77686_rtc_alarm_irq_enable,
413}; 394};
414 395
415#ifdef MAX77686_RTC_WTSR_SMPL
416static void max77686_rtc_enable_wtsr(struct max77686_rtc_info *info, bool enable)
417{
418 int ret;
419 unsigned int val, mask;
420
421 if (enable)
422 val = (1 << WTSR_EN_SHIFT) | (3 << WTSRT_SHIFT);
423 else
424 val = 0;
425
426 mask = WTSR_EN_MASK | WTSRT_MASK;
427
428 dev_info(info->dev, "%s: %s WTSR\n", __func__,
429 enable ? "enable" : "disable");
430
431 ret = regmap_update_bits(info->max77686->rtc_regmap,
432 MAX77686_WTSR_SMPL_CNTL, mask, val);
433 if (ret < 0) {
434 dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
435 __func__, ret);
436 return;
437 }
438
439 max77686_rtc_update(info, MAX77686_RTC_WRITE);
440}
441
442static void max77686_rtc_enable_smpl(struct max77686_rtc_info *info, bool enable)
443{
444 int ret;
445 unsigned int val, mask;
446
447 if (enable)
448 val = (1 << SMPL_EN_SHIFT) | (0 << SMPLT_SHIFT);
449 else
450 val = 0;
451
452 mask = SMPL_EN_MASK | SMPLT_MASK;
453
454 dev_info(info->dev, "%s: %s SMPL\n", __func__,
455 enable ? "enable" : "disable");
456
457 ret = regmap_update_bits(info->max77686->rtc_regmap,
458 MAX77686_WTSR_SMPL_CNTL, mask, val);
459 if (ret < 0) {
460 dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
461 __func__, ret);
462 return;
463 }
464
465 max77686_rtc_update(info, MAX77686_RTC_WRITE);
466
467 val = 0;
468 regmap_read(info->max77686->rtc_regmap, MAX77686_WTSR_SMPL_CNTL, &val);
469 dev_info(info->dev, "%s: WTSR_SMPL(0x%02x)\n", __func__, val);
470}
471#endif /* MAX77686_RTC_WTSR_SMPL */
472
473static int max77686_rtc_init_reg(struct max77686_rtc_info *info) 396static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
474{ 397{
475 u8 data[2]; 398 u8 data[2];
@@ -492,16 +415,11 @@ static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
492 return ret; 415 return ret;
493} 416}
494 417
495static struct regmap_config max77686_rtc_regmap_config = {
496 .reg_bits = 8,
497 .val_bits = 8,
498};
499
500static int max77686_rtc_probe(struct platform_device *pdev) 418static int max77686_rtc_probe(struct platform_device *pdev)
501{ 419{
502 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent); 420 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
503 struct max77686_rtc_info *info; 421 struct max77686_rtc_info *info;
504 int ret, virq; 422 int ret;
505 423
506 dev_info(&pdev->dev, "%s\n", __func__); 424 dev_info(&pdev->dev, "%s\n", __func__);
507 425
@@ -514,14 +432,7 @@ static int max77686_rtc_probe(struct platform_device *pdev)
514 info->dev = &pdev->dev; 432 info->dev = &pdev->dev;
515 info->max77686 = max77686; 433 info->max77686 = max77686;
516 info->rtc = max77686->rtc; 434 info->rtc = max77686->rtc;
517 info->max77686->rtc_regmap = devm_regmap_init_i2c(info->max77686->rtc, 435
518 &max77686_rtc_regmap_config);
519 if (IS_ERR(info->max77686->rtc_regmap)) {
520 ret = PTR_ERR(info->max77686->rtc_regmap);
521 dev_err(info->max77686->dev, "Failed to allocate register map: %d\n",
522 ret);
523 return ret;
524 }
525 platform_set_drvdata(pdev, info); 436 platform_set_drvdata(pdev, info);
526 437
527 ret = max77686_rtc_init_reg(info); 438 ret = max77686_rtc_init_reg(info);
@@ -531,34 +442,34 @@ static int max77686_rtc_probe(struct platform_device *pdev)
531 goto err_rtc; 442 goto err_rtc;
532 } 443 }
533 444
534#ifdef MAX77686_RTC_WTSR_SMPL
535 max77686_rtc_enable_wtsr(info, true);
536 max77686_rtc_enable_smpl(info, true);
537#endif
538
539 device_init_wakeup(&pdev->dev, 1); 445 device_init_wakeup(&pdev->dev, 1);
540 446
541 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc", 447 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
542 &max77686_rtc_ops, THIS_MODULE); 448 &max77686_rtc_ops, THIS_MODULE);
543 449
544 if (IS_ERR(info->rtc_dev)) { 450 if (IS_ERR(info->rtc_dev)) {
545 dev_info(&pdev->dev, "%s: fail\n", __func__);
546
547 ret = PTR_ERR(info->rtc_dev); 451 ret = PTR_ERR(info->rtc_dev);
548 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); 452 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
549 if (ret == 0) 453 if (ret == 0)
550 ret = -EINVAL; 454 ret = -EINVAL;
551 goto err_rtc; 455 goto err_rtc;
552 } 456 }
553 virq = irq_create_mapping(max77686->irq_domain, MAX77686_RTCIRQ_RTCA1); 457
554 if (!virq) { 458 if (!max77686->rtc_irq_data) {
459 ret = -EINVAL;
460 dev_err(&pdev->dev, "%s: no RTC regmap IRQ chip\n", __func__);
461 goto err_rtc;
462 }
463
464 info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
465 MAX77686_RTCIRQ_RTCA1);
466 if (!info->virq) {
555 ret = -ENXIO; 467 ret = -ENXIO;
556 goto err_rtc; 468 goto err_rtc;
557 } 469 }
558 info->virq = virq;
559 470
560 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL, 471 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
561 max77686_rtc_alarm_irq, 0, "rtc-alarm0", info); 472 max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
562 if (ret < 0) 473 if (ret < 0)
563 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", 474 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
564 info->virq, ret); 475 info->virq, ret);
@@ -567,33 +478,33 @@ err_rtc:
567 return ret; 478 return ret;
568} 479}
569 480
570static void max77686_rtc_shutdown(struct platform_device *pdev) 481#ifdef CONFIG_PM_SLEEP
482static int max77686_rtc_suspend(struct device *dev)
571{ 483{
572#ifdef MAX77686_RTC_WTSR_SMPL 484 if (device_may_wakeup(dev)) {
573 struct max77686_rtc_info *info = platform_get_drvdata(pdev); 485 struct max77686_rtc_info *info = dev_get_drvdata(dev);
574 int i; 486
575 u8 val = 0; 487 return enable_irq_wake(info->virq);
576
577 for (i = 0; i < 3; i++) {
578 max77686_rtc_enable_wtsr(info, false);
579 regmap_read(info->max77686->rtc_regmap, MAX77686_WTSR_SMPL_CNTL, &val);
580 dev_info(info->dev, "%s: WTSR_SMPL reg(0x%02x)\n", __func__,
581 val);
582 if (val & WTSR_EN_MASK) {
583 dev_emerg(info->dev, "%s: fail to disable WTSR\n",
584 __func__);
585 } else {
586 dev_info(info->dev, "%s: success to disable WTSR\n",
587 __func__);
588 break;
589 }
590 } 488 }
591 489
592 /* Disable SMPL when power off */ 490 return 0;
593 max77686_rtc_enable_smpl(info, false);
594#endif /* MAX77686_RTC_WTSR_SMPL */
595} 491}
596 492
493static int max77686_rtc_resume(struct device *dev)
494{
495 if (device_may_wakeup(dev)) {
496 struct max77686_rtc_info *info = dev_get_drvdata(dev);
497
498 return disable_irq_wake(info->virq);
499 }
500
501 return 0;
502}
503#endif
504
505static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
506 max77686_rtc_suspend, max77686_rtc_resume);
507
597static const struct platform_device_id rtc_id[] = { 508static const struct platform_device_id rtc_id[] = {
598 { "max77686-rtc", 0 }, 509 { "max77686-rtc", 0 },
599 {}, 510 {},
@@ -603,9 +514,9 @@ static struct platform_driver max77686_rtc_driver = {
603 .driver = { 514 .driver = {
604 .name = "max77686-rtc", 515 .name = "max77686-rtc",
605 .owner = THIS_MODULE, 516 .owner = THIS_MODULE,
517 .pm = &max77686_rtc_pm_ops,
606 }, 518 },
607 .probe = max77686_rtc_probe, 519 .probe = max77686_rtc_probe,
608 .shutdown = max77686_rtc_shutdown,
609 .id_table = rtc_id, 520 .id_table = rtc_id,
610}; 521};
611 522
diff --git a/drivers/rtc/rtc-max77802.c b/drivers/rtc/rtc-max77802.c
new file mode 100644
index 000000000000..566471335b33
--- /dev/null
+++ b/drivers/rtc/rtc-max77802.c
@@ -0,0 +1,502 @@
1/*
2 * RTC driver for Maxim MAX77802
3 *
4 * Copyright (C) 2013 Google, Inc
5 *
6 * Copyright (C) 2012 Samsung Electronics Co.Ltd
7 *
8 * based on rtc-max8997.c
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/slab.h>
18#include <linux/rtc.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/mfd/max77686-private.h>
24#include <linux/irqdomain.h>
25#include <linux/regmap.h>
26
27/* RTC Control Register */
28#define BCD_EN_SHIFT 0
29#define BCD_EN_MASK (1 << BCD_EN_SHIFT)
30#define MODEL24_SHIFT 1
31#define MODEL24_MASK (1 << MODEL24_SHIFT)
32/* RTC Update Register1 */
33#define RTC_UDR_SHIFT 0
34#define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
35#define RTC_RBUDR_SHIFT 4
36#define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
37/* RTC Hour register */
38#define HOUR_PM_SHIFT 6
39#define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
40/* RTC Alarm Enable */
41#define ALARM_ENABLE_SHIFT 7
42#define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
43
44/* For the RTCAE1 register, we write this value to enable the alarm */
45#define ALARM_ENABLE_VALUE 0x77
46
47#define MAX77802_RTC_UPDATE_DELAY_US 200
48
49enum {
50 RTC_SEC = 0,
51 RTC_MIN,
52 RTC_HOUR,
53 RTC_WEEKDAY,
54 RTC_MONTH,
55 RTC_YEAR,
56 RTC_DATE,
57 RTC_NR_TIME
58};
59
60struct max77802_rtc_info {
61 struct device *dev;
62 struct max77686_dev *max77802;
63 struct i2c_client *rtc;
64 struct rtc_device *rtc_dev;
65 struct mutex lock;
66
67 struct regmap *regmap;
68
69 int virq;
70 int rtc_24hr_mode;
71};
72
73enum MAX77802_RTC_OP {
74 MAX77802_RTC_WRITE,
75 MAX77802_RTC_READ,
76};
77
78static void max77802_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
79 int rtc_24hr_mode)
80{
81 tm->tm_sec = data[RTC_SEC] & 0xff;
82 tm->tm_min = data[RTC_MIN] & 0xff;
83 if (rtc_24hr_mode)
84 tm->tm_hour = data[RTC_HOUR] & 0x1f;
85 else {
86 tm->tm_hour = data[RTC_HOUR] & 0x0f;
87 if (data[RTC_HOUR] & HOUR_PM_MASK)
88 tm->tm_hour += 12;
89 }
90
91 /* Only a single bit is set in data[], so fls() would be equivalent */
92 tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0xff) - 1;
93 tm->tm_mday = data[RTC_DATE] & 0x1f;
94 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
95
96 tm->tm_year = data[RTC_YEAR] & 0xff;
97 tm->tm_yday = 0;
98 tm->tm_isdst = 0;
99}
100
101static int max77802_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
102{
103 data[RTC_SEC] = tm->tm_sec;
104 data[RTC_MIN] = tm->tm_min;
105 data[RTC_HOUR] = tm->tm_hour;
106 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
107 data[RTC_DATE] = tm->tm_mday;
108 data[RTC_MONTH] = tm->tm_mon + 1;
109 data[RTC_YEAR] = tm->tm_year;
110
111 return 0;
112}
113
114static int max77802_rtc_update(struct max77802_rtc_info *info,
115 enum MAX77802_RTC_OP op)
116{
117 int ret;
118 unsigned int data;
119
120 if (op == MAX77802_RTC_WRITE)
121 data = 1 << RTC_UDR_SHIFT;
122 else
123 data = 1 << RTC_RBUDR_SHIFT;
124
125 ret = regmap_update_bits(info->max77802->regmap,
126 MAX77802_RTC_UPDATE0, data, data);
127 if (ret < 0)
128 dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
129 __func__, ret, data);
130 else {
131 /* Minimum delay required before RTC update. */
132 usleep_range(MAX77802_RTC_UPDATE_DELAY_US,
133 MAX77802_RTC_UPDATE_DELAY_US * 2);
134 }
135
136 return ret;
137}
138
139static int max77802_rtc_read_time(struct device *dev, struct rtc_time *tm)
140{
141 struct max77802_rtc_info *info = dev_get_drvdata(dev);
142 u8 data[RTC_NR_TIME];
143 int ret;
144
145 mutex_lock(&info->lock);
146
147 ret = max77802_rtc_update(info, MAX77802_RTC_READ);
148 if (ret < 0)
149 goto out;
150
151 ret = regmap_bulk_read(info->max77802->regmap,
152 MAX77802_RTC_SEC, data, RTC_NR_TIME);
153 if (ret < 0) {
154 dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,
155 ret);
156 goto out;
157 }
158
159 max77802_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
160
161 ret = rtc_valid_tm(tm);
162
163out:
164 mutex_unlock(&info->lock);
165 return ret;
166}
167
168static int max77802_rtc_set_time(struct device *dev, struct rtc_time *tm)
169{
170 struct max77802_rtc_info *info = dev_get_drvdata(dev);
171 u8 data[RTC_NR_TIME];
172 int ret;
173
174 ret = max77802_rtc_tm_to_data(tm, data);
175 if (ret < 0)
176 return ret;
177
178 mutex_lock(&info->lock);
179
180 ret = regmap_bulk_write(info->max77802->regmap,
181 MAX77802_RTC_SEC, data, RTC_NR_TIME);
182 if (ret < 0) {
183 dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
184 ret);
185 goto out;
186 }
187
188 ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
189
190out:
191 mutex_unlock(&info->lock);
192 return ret;
193}
194
195static int max77802_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
196{
197 struct max77802_rtc_info *info = dev_get_drvdata(dev);
198 u8 data[RTC_NR_TIME];
199 unsigned int val;
200 int ret;
201
202 mutex_lock(&info->lock);
203
204 ret = max77802_rtc_update(info, MAX77802_RTC_READ);
205 if (ret < 0)
206 goto out;
207
208 ret = regmap_bulk_read(info->max77802->regmap,
209 MAX77802_ALARM1_SEC, data, RTC_NR_TIME);
210 if (ret < 0) {
211 dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
212 __func__, __LINE__, ret);
213 goto out;
214 }
215
216 max77802_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
217
218 alrm->enabled = 0;
219 ret = regmap_read(info->max77802->regmap,
220 MAX77802_RTC_AE1, &val);
221 if (ret < 0) {
222 dev_err(info->dev, "%s:%d fail to read alarm enable(%d)\n",
223 __func__, __LINE__, ret);
224 goto out;
225 }
226 if (val)
227 alrm->enabled = 1;
228
229 alrm->pending = 0;
230 ret = regmap_read(info->max77802->regmap, MAX77802_REG_STATUS2, &val);
231 if (ret < 0) {
232 dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
233 __func__, __LINE__, ret);
234 goto out;
235 }
236
237 if (val & (1 << 2)) /* RTCA1 */
238 alrm->pending = 1;
239
240out:
241 mutex_unlock(&info->lock);
242 return 0;
243}
244
245static int max77802_rtc_stop_alarm(struct max77802_rtc_info *info)
246{
247 int ret;
248
249 if (!mutex_is_locked(&info->lock))
250 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
251
252 ret = max77802_rtc_update(info, MAX77802_RTC_READ);
253 if (ret < 0)
254 goto out;
255
256 ret = regmap_write(info->max77802->regmap,
257 MAX77802_RTC_AE1, 0);
258 if (ret < 0) {
259 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
260 __func__, ret);
261 goto out;
262 }
263
264 ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
265out:
266 return ret;
267}
268
269static int max77802_rtc_start_alarm(struct max77802_rtc_info *info)
270{
271 int ret;
272
273 if (!mutex_is_locked(&info->lock))
274 dev_warn(info->dev, "%s: should have mutex locked\n",
275 __func__);
276
277 ret = max77802_rtc_update(info, MAX77802_RTC_READ);
278 if (ret < 0)
279 goto out;
280
281 ret = regmap_write(info->max77802->regmap,
282 MAX77802_RTC_AE1,
283 ALARM_ENABLE_VALUE);
284
285 if (ret < 0) {
286 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
287 __func__, ret);
288 goto out;
289 }
290
291 ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
292out:
293 return ret;
294}
295
296static int max77802_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
297{
298 struct max77802_rtc_info *info = dev_get_drvdata(dev);
299 u8 data[RTC_NR_TIME];
300 int ret;
301
302 ret = max77802_rtc_tm_to_data(&alrm->time, data);
303 if (ret < 0)
304 return ret;
305
306 mutex_lock(&info->lock);
307
308 ret = max77802_rtc_stop_alarm(info);
309 if (ret < 0)
310 goto out;
311
312 ret = regmap_bulk_write(info->max77802->regmap,
313 MAX77802_ALARM1_SEC, data, RTC_NR_TIME);
314
315 if (ret < 0) {
316 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
317 __func__, ret);
318 goto out;
319 }
320
321 ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
322 if (ret < 0)
323 goto out;
324
325 if (alrm->enabled)
326 ret = max77802_rtc_start_alarm(info);
327out:
328 mutex_unlock(&info->lock);
329 return ret;
330}
331
332static int max77802_rtc_alarm_irq_enable(struct device *dev,
333 unsigned int enabled)
334{
335 struct max77802_rtc_info *info = dev_get_drvdata(dev);
336 int ret;
337
338 mutex_lock(&info->lock);
339 if (enabled)
340 ret = max77802_rtc_start_alarm(info);
341 else
342 ret = max77802_rtc_stop_alarm(info);
343 mutex_unlock(&info->lock);
344
345 return ret;
346}
347
348static irqreturn_t max77802_rtc_alarm_irq(int irq, void *data)
349{
350 struct max77802_rtc_info *info = data;
351
352 dev_dbg(info->dev, "%s:irq(%d)\n", __func__, irq);
353
354 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
355
356 return IRQ_HANDLED;
357}
358
359static const struct rtc_class_ops max77802_rtc_ops = {
360 .read_time = max77802_rtc_read_time,
361 .set_time = max77802_rtc_set_time,
362 .read_alarm = max77802_rtc_read_alarm,
363 .set_alarm = max77802_rtc_set_alarm,
364 .alarm_irq_enable = max77802_rtc_alarm_irq_enable,
365};
366
367static int max77802_rtc_init_reg(struct max77802_rtc_info *info)
368{
369 u8 data[2];
370 int ret;
371
372 max77802_rtc_update(info, MAX77802_RTC_READ);
373
374 /* Set RTC control register : Binary mode, 24hour mdoe */
375 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
376 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
377
378 info->rtc_24hr_mode = 1;
379
380 ret = regmap_bulk_write(info->max77802->regmap,
381 MAX77802_RTC_CONTROLM, data, ARRAY_SIZE(data));
382 if (ret < 0) {
383 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
384 __func__, ret);
385 return ret;
386 }
387
388 ret = max77802_rtc_update(info, MAX77802_RTC_WRITE);
389 return ret;
390}
391
392static int max77802_rtc_probe(struct platform_device *pdev)
393{
394 struct max77686_dev *max77802 = dev_get_drvdata(pdev->dev.parent);
395 struct max77802_rtc_info *info;
396 int ret;
397
398 dev_dbg(&pdev->dev, "%s\n", __func__);
399
400 info = devm_kzalloc(&pdev->dev, sizeof(struct max77802_rtc_info),
401 GFP_KERNEL);
402 if (!info)
403 return -ENOMEM;
404
405 mutex_init(&info->lock);
406 info->dev = &pdev->dev;
407 info->max77802 = max77802;
408 info->rtc = max77802->i2c;
409
410 platform_set_drvdata(pdev, info);
411
412 ret = max77802_rtc_init_reg(info);
413
414 if (ret < 0) {
415 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
416 return ret;
417 }
418
419 device_init_wakeup(&pdev->dev, 1);
420
421 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77802-rtc",
422 &max77802_rtc_ops, THIS_MODULE);
423
424 if (IS_ERR(info->rtc_dev)) {
425 ret = PTR_ERR(info->rtc_dev);
426 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
427 if (ret == 0)
428 ret = -EINVAL;
429 return ret;
430 }
431
432 if (!max77802->rtc_irq_data) {
433 dev_err(&pdev->dev, "No RTC regmap IRQ chip\n");
434 return -EINVAL;
435 }
436
437 info->virq = regmap_irq_get_virq(max77802->rtc_irq_data,
438 MAX77686_RTCIRQ_RTCA1);
439
440 if (info->virq <= 0) {
441 dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
442 MAX77686_RTCIRQ_RTCA1);
443 return -EINVAL;
444 }
445
446 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
447 max77802_rtc_alarm_irq, 0, "rtc-alarm1",
448 info);
449 if (ret < 0)
450 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
451 info->virq, ret);
452
453 return ret;
454}
455
456#ifdef CONFIG_PM_SLEEP
457static int max77802_rtc_suspend(struct device *dev)
458{
459 if (device_may_wakeup(dev)) {
460 struct max77802_rtc_info *info = dev_get_drvdata(dev);
461
462 return enable_irq_wake(info->virq);
463 }
464
465 return 0;
466}
467
468static int max77802_rtc_resume(struct device *dev)
469{
470 if (device_may_wakeup(dev)) {
471 struct max77802_rtc_info *info = dev_get_drvdata(dev);
472
473 return disable_irq_wake(info->virq);
474 }
475
476 return 0;
477}
478#endif
479
480static SIMPLE_DEV_PM_OPS(max77802_rtc_pm_ops,
481 max77802_rtc_suspend, max77802_rtc_resume);
482
483static const struct platform_device_id rtc_id[] = {
484 { "max77802-rtc", 0 },
485 {},
486};
487
488static struct platform_driver max77802_rtc_driver = {
489 .driver = {
490 .name = "max77802-rtc",
491 .owner = THIS_MODULE,
492 .pm = &max77802_rtc_pm_ops,
493 },
494 .probe = max77802_rtc_probe,
495 .id_table = rtc_id,
496};
497
498module_platform_driver(max77802_rtc_driver);
499
500MODULE_DESCRIPTION("Maxim MAX77802 RTC driver");
501MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
502MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-mpc5121.c b/drivers/rtc/rtc-mpc5121.c
index dc4f14255cc3..3b965ad6f4d5 100644
--- a/drivers/rtc/rtc-mpc5121.c
+++ b/drivers/rtc/rtc-mpc5121.c
@@ -401,7 +401,7 @@ static int mpc5121_rtc_remove(struct platform_device *op)
401} 401}
402 402
403#ifdef CONFIG_OF 403#ifdef CONFIG_OF
404static struct of_device_id mpc5121_rtc_match[] = { 404static const struct of_device_id mpc5121_rtc_match[] = {
405 { .compatible = "fsl,mpc5121-rtc", }, 405 { .compatible = "fsl,mpc5121-rtc", },
406 { .compatible = "fsl,mpc5200-rtc", }, 406 { .compatible = "fsl,mpc5200-rtc", },
407 {}, 407 {},
diff --git a/drivers/rtc/rtc-pcf85063.c b/drivers/rtc/rtc-pcf85063.c
new file mode 100644
index 000000000000..6a12bf62c504
--- /dev/null
+++ b/drivers/rtc/rtc-pcf85063.c
@@ -0,0 +1,204 @@
1/*
2 * An I2C driver for the PCF85063 RTC
3 * Copyright 2014 Rose Technology
4 *
5 * Author: Søren Andersen <san@rosetechnology.dk>
6 * Maintainers: http://www.nslu2-linux.org/
7 *
8 * based on the other drivers in this same directory.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14#include <linux/i2c.h>
15#include <linux/bcd.h>
16#include <linux/rtc.h>
17#include <linux/module.h>
18
19#define DRV_VERSION "0.0.1"
20
21#define PCF85063_REG_CTRL1 0x00 /* status */
22#define PCF85063_REG_CTRL2 0x01
23
24#define PCF85063_REG_SC 0x04 /* datetime */
25#define PCF85063_REG_MN 0x05
26#define PCF85063_REG_HR 0x06
27#define PCF85063_REG_DM 0x07
28#define PCF85063_REG_DW 0x08
29#define PCF85063_REG_MO 0x09
30#define PCF85063_REG_YR 0x0A
31
32#define PCF85063_MO_C 0x80 /* century */
33
34static struct i2c_driver pcf85063_driver;
35
36struct pcf85063 {
37 struct rtc_device *rtc;
38 int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
39 int voltage_low; /* indicates if a low_voltage was detected */
40};
41
42/*
43 * In the routines that deal directly with the pcf85063 hardware, we use
44 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
45 */
46static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
47{
48 struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
49 unsigned char buf[13] = { PCF85063_REG_CTRL1 };
50 struct i2c_msg msgs[] = {
51 {/* setup read ptr */
52 .addr = client->addr,
53 .len = 1,
54 .buf = buf
55 },
56 {/* read status + date */
57 .addr = client->addr,
58 .flags = I2C_M_RD,
59 .len = 13,
60 .buf = buf
61 },
62 };
63
64 /* read registers */
65 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
66 dev_err(&client->dev, "%s: read error\n", __func__);
67 return -EIO;
68 }
69
70 tm->tm_sec = bcd2bin(buf[PCF85063_REG_SC] & 0x7F);
71 tm->tm_min = bcd2bin(buf[PCF85063_REG_MN] & 0x7F);
72 tm->tm_hour = bcd2bin(buf[PCF85063_REG_HR] & 0x3F); /* rtc hr 0-23 */
73 tm->tm_mday = bcd2bin(buf[PCF85063_REG_DM] & 0x3F);
74 tm->tm_wday = buf[PCF85063_REG_DW] & 0x07;
75 tm->tm_mon = bcd2bin(buf[PCF85063_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
76 tm->tm_year = bcd2bin(buf[PCF85063_REG_YR]);
77 if (tm->tm_year < 70)
78 tm->tm_year += 100; /* assume we are in 1970...2069 */
79 /* detect the polarity heuristically. see note above. */
80 pcf85063->c_polarity = (buf[PCF85063_REG_MO] & PCF85063_MO_C) ?
81 (tm->tm_year >= 100) : (tm->tm_year < 100);
82
83 /* the clock can give out invalid datetime, but we cannot return
84 * -EINVAL otherwise hwclock will refuse to set the time on bootup.
85 */
86 if (rtc_valid_tm(tm) < 0)
87 dev_err(&client->dev, "retrieved date/time is not valid.\n");
88
89 return 0;
90}
91
92static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
93{
94 int i = 0, err = 0;
95 unsigned char buf[11];
96
97 /* Control & status */
98 buf[PCF85063_REG_CTRL1] = 0;
99 buf[PCF85063_REG_CTRL2] = 5;
100
101 /* hours, minutes and seconds */
102 buf[PCF85063_REG_SC] = bin2bcd(tm->tm_sec) & 0x7F;
103
104 buf[PCF85063_REG_MN] = bin2bcd(tm->tm_min);
105 buf[PCF85063_REG_HR] = bin2bcd(tm->tm_hour);
106
107 /* Day of month, 1 - 31 */
108 buf[PCF85063_REG_DM] = bin2bcd(tm->tm_mday);
109
110 /* Day, 0 - 6 */
111 buf[PCF85063_REG_DW] = tm->tm_wday & 0x07;
112
113 /* month, 1 - 12 */
114 buf[PCF85063_REG_MO] = bin2bcd(tm->tm_mon + 1);
115
116 /* year and century */
117 buf[PCF85063_REG_YR] = bin2bcd(tm->tm_year % 100);
118
119 /* write register's data */
120 for (i = 0; i < sizeof(buf); i++) {
121 unsigned char data[2] = { i, buf[i] };
122
123 err = i2c_master_send(client, data, sizeof(data));
124 if (err != sizeof(data)) {
125 dev_err(&client->dev, "%s: err=%d addr=%02x, data=%02x\n",
126 __func__, err, data[0], data[1]);
127 return -EIO;
128 }
129 }
130
131 return 0;
132}
133
134static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
135{
136 return pcf85063_get_datetime(to_i2c_client(dev), tm);
137}
138
139static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
140{
141 return pcf85063_set_datetime(to_i2c_client(dev), tm);
142}
143
144static const struct rtc_class_ops pcf85063_rtc_ops = {
145 .read_time = pcf85063_rtc_read_time,
146 .set_time = pcf85063_rtc_set_time
147};
148
149static int pcf85063_probe(struct i2c_client *client,
150 const struct i2c_device_id *id)
151{
152 struct pcf85063 *pcf85063;
153
154 dev_dbg(&client->dev, "%s\n", __func__);
155
156 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
157 return -ENODEV;
158
159 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
160 GFP_KERNEL);
161 if (!pcf85063)
162 return -ENOMEM;
163
164 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
165
166 i2c_set_clientdata(client, pcf85063);
167
168 pcf85063->rtc = devm_rtc_device_register(&client->dev,
169 pcf85063_driver.driver.name,
170 &pcf85063_rtc_ops, THIS_MODULE);
171
172 return PTR_ERR_OR_ZERO(pcf85063->rtc);
173}
174
175static const struct i2c_device_id pcf85063_id[] = {
176 { "pcf85063", 0 },
177 { }
178};
179MODULE_DEVICE_TABLE(i2c, pcf85063_id);
180
181#ifdef CONFIG_OF
182static const struct of_device_id pcf85063_of_match[] = {
183 { .compatible = "nxp,pcf85063" },
184 {}
185};
186MODULE_DEVICE_TABLE(of, pcf85063_of_match);
187#endif
188
189static struct i2c_driver pcf85063_driver = {
190 .driver = {
191 .name = "rtc-pcf85063",
192 .owner = THIS_MODULE,
193 .of_match_table = of_match_ptr(pcf85063_of_match),
194 },
195 .probe = pcf85063_probe,
196 .id_table = pcf85063_id,
197};
198
199module_i2c_driver(pcf85063_driver);
200
201MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
202MODULE_DESCRIPTION("PCF85063 RTC driver");
203MODULE_LICENSE("GPL");
204MODULE_VERSION(DRV_VERSION);
diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c
index 63b558c48196..c2ef0a22ee94 100644
--- a/drivers/rtc/rtc-pcf8563.c
+++ b/drivers/rtc/rtc-pcf8563.c
@@ -26,6 +26,8 @@
26 26
27#define PCF8563_REG_ST1 0x00 /* status */ 27#define PCF8563_REG_ST1 0x00 /* status */
28#define PCF8563_REG_ST2 0x01 28#define PCF8563_REG_ST2 0x01
29#define PCF8563_BIT_AIE (1 << 1)
30#define PCF8563_BIT_AF (1 << 3)
29 31
30#define PCF8563_REG_SC 0x02 /* datetime */ 32#define PCF8563_REG_SC 0x02 /* datetime */
31#define PCF8563_REG_MN 0x03 33#define PCF8563_REG_MN 0x03
@@ -36,9 +38,6 @@
36#define PCF8563_REG_YR 0x08 38#define PCF8563_REG_YR 0x08
37 39
38#define PCF8563_REG_AMN 0x09 /* alarm */ 40#define PCF8563_REG_AMN 0x09 /* alarm */
39#define PCF8563_REG_AHR 0x0A
40#define PCF8563_REG_ADM 0x0B
41#define PCF8563_REG_ADW 0x0C
42 41
43#define PCF8563_REG_CLKO 0x0D /* clock out */ 42#define PCF8563_REG_CLKO 0x0D /* clock out */
44#define PCF8563_REG_TMRC 0x0E /* timer control */ 43#define PCF8563_REG_TMRC 0x0E /* timer control */
@@ -67,37 +66,133 @@ struct pcf8563 {
67 */ 66 */
68 int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */ 67 int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
69 int voltage_low; /* incicates if a low_voltage was detected */ 68 int voltage_low; /* incicates if a low_voltage was detected */
69
70 struct i2c_client *client;
70}; 71};
71 72
72/* 73static int pcf8563_read_block_data(struct i2c_client *client, unsigned char reg,
73 * In the routines that deal directly with the pcf8563 hardware, we use 74 unsigned char length, unsigned char *buf)
74 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
75 */
76static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
77{ 75{
78 struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
79 unsigned char buf[13] = { PCF8563_REG_ST1 };
80
81 struct i2c_msg msgs[] = { 76 struct i2c_msg msgs[] = {
82 {/* setup read ptr */ 77 {/* setup read ptr */
83 .addr = client->addr, 78 .addr = client->addr,
84 .len = 1, 79 .len = 1,
85 .buf = buf 80 .buf = &reg,
86 }, 81 },
87 {/* read status + date */ 82 {
88 .addr = client->addr, 83 .addr = client->addr,
89 .flags = I2C_M_RD, 84 .flags = I2C_M_RD,
90 .len = 13, 85 .len = length,
91 .buf = buf 86 .buf = buf
92 }, 87 },
93 }; 88 };
94 89
95 /* read registers */
96 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { 90 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
97 dev_err(&client->dev, "%s: read error\n", __func__); 91 dev_err(&client->dev, "%s: read error\n", __func__);
98 return -EIO; 92 return -EIO;
99 } 93 }
100 94
95 return 0;
96}
97
98static int pcf8563_write_block_data(struct i2c_client *client,
99 unsigned char reg, unsigned char length,
100 unsigned char *buf)
101{
102 int i, err;
103
104 for (i = 0; i < length; i++) {
105 unsigned char data[2] = { reg + i, buf[i] };
106
107 err = i2c_master_send(client, data, sizeof(data));
108 if (err != sizeof(data)) {
109 dev_err(&client->dev,
110 "%s: err=%d addr=%02x, data=%02x\n",
111 __func__, err, data[0], data[1]);
112 return -EIO;
113 }
114 }
115
116 return 0;
117}
118
119static int pcf8563_set_alarm_mode(struct i2c_client *client, bool on)
120{
121 unsigned char buf[2];
122 int err;
123
124 err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, buf + 1);
125 if (err < 0)
126 return err;
127
128 if (on)
129 buf[1] |= PCF8563_BIT_AIE;
130 else
131 buf[1] &= ~PCF8563_BIT_AIE;
132
133 buf[1] &= ~PCF8563_BIT_AF;
134 buf[0] = PCF8563_REG_ST2;
135
136 err = pcf8563_write_block_data(client, PCF8563_REG_ST2, 1, buf + 1);
137 if (err < 0) {
138 dev_err(&client->dev, "%s: write error\n", __func__);
139 return -EIO;
140 }
141
142 return 0;
143}
144
145static int pcf8563_get_alarm_mode(struct i2c_client *client, unsigned char *en,
146 unsigned char *pen)
147{
148 unsigned char buf;
149 int err;
150
151 err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, &buf);
152 if (err)
153 return err;
154
155 if (en)
156 *en = !!(buf & PCF8563_BIT_AIE);
157 if (pen)
158 *pen = !!(buf & PCF8563_BIT_AF);
159
160 return 0;
161}
162
163static irqreturn_t pcf8563_irq(int irq, void *dev_id)
164{
165 struct pcf8563 *pcf8563 = i2c_get_clientdata(dev_id);
166 int err;
167 char pending;
168
169 err = pcf8563_get_alarm_mode(pcf8563->client, NULL, &pending);
170 if (err)
171 return IRQ_NONE;
172
173 if (pending) {
174 rtc_update_irq(pcf8563->rtc, 1, RTC_IRQF | RTC_AF);
175 pcf8563_set_alarm_mode(pcf8563->client, 1);
176 return IRQ_HANDLED;
177 }
178
179 return IRQ_NONE;
180}
181
182/*
183 * In the routines that deal directly with the pcf8563 hardware, we use
184 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
185 */
186static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
187{
188 struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
189 unsigned char buf[9];
190 int err;
191
192 err = pcf8563_read_block_data(client, PCF8563_REG_ST1, 9, buf);
193 if (err)
194 return err;
195
101 if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) { 196 if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) {
102 pcf8563->voltage_low = 1; 197 pcf8563->voltage_low = 1;
103 dev_info(&client->dev, 198 dev_info(&client->dev,
@@ -144,7 +239,7 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
144static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) 239static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
145{ 240{
146 struct pcf8563 *pcf8563 = i2c_get_clientdata(client); 241 struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
147 int i, err; 242 int err;
148 unsigned char buf[9]; 243 unsigned char buf[9];
149 244
150 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " 245 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
@@ -170,19 +265,10 @@ static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
170 265
171 buf[PCF8563_REG_DW] = tm->tm_wday & 0x07; 266 buf[PCF8563_REG_DW] = tm->tm_wday & 0x07;
172 267
173 /* write register's data */ 268 err = pcf8563_write_block_data(client, PCF8563_REG_SC,
174 for (i = 0; i < 7; i++) { 269 9 - PCF8563_REG_SC, buf + PCF8563_REG_SC);
175 unsigned char data[2] = { PCF8563_REG_SC + i, 270 if (err)
176 buf[PCF8563_REG_SC + i] }; 271 return err;
177
178 err = i2c_master_send(client, data, sizeof(data));
179 if (err != sizeof(data)) {
180 dev_err(&client->dev,
181 "%s: err=%d addr=%02x, data=%02x\n",
182 __func__, err, data[0], data[1]);
183 return -EIO;
184 }
185 }
186 272
187 return 0; 273 return 0;
188} 274}
@@ -235,16 +321,83 @@ static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
235 return pcf8563_set_datetime(to_i2c_client(dev), tm); 321 return pcf8563_set_datetime(to_i2c_client(dev), tm);
236} 322}
237 323
324static int pcf8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
325{
326 struct i2c_client *client = to_i2c_client(dev);
327 unsigned char buf[4];
328 int err;
329
330 err = pcf8563_read_block_data(client, PCF8563_REG_AMN, 4, buf);
331 if (err)
332 return err;
333
334 dev_dbg(&client->dev,
335 "%s: raw data is min=%02x, hr=%02x, mday=%02x, wday=%02x\n",
336 __func__, buf[0], buf[1], buf[2], buf[3]);
337
338 tm->time.tm_min = bcd2bin(buf[0] & 0x7F);
339 tm->time.tm_hour = bcd2bin(buf[1] & 0x7F);
340 tm->time.tm_mday = bcd2bin(buf[2] & 0x1F);
341 tm->time.tm_wday = bcd2bin(buf[3] & 0x7);
342 tm->time.tm_mon = -1;
343 tm->time.tm_year = -1;
344 tm->time.tm_yday = -1;
345 tm->time.tm_isdst = -1;
346
347 err = pcf8563_get_alarm_mode(client, &tm->enabled, &tm->pending);
348 if (err < 0)
349 return err;
350
351 dev_dbg(&client->dev, "%s: tm is mins=%d, hours=%d, mday=%d, wday=%d,"
352 " enabled=%d, pending=%d\n", __func__, tm->time.tm_min,
353 tm->time.tm_hour, tm->time.tm_mday, tm->time.tm_wday,
354 tm->enabled, tm->pending);
355
356 return 0;
357}
358
359static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
360{
361 struct i2c_client *client = to_i2c_client(dev);
362 unsigned char buf[4];
363 int err;
364
365 dev_dbg(dev, "%s, min=%d hour=%d wday=%d mday=%d "
366 "enabled=%d pending=%d\n", __func__,
367 tm->time.tm_min, tm->time.tm_hour, tm->time.tm_wday,
368 tm->time.tm_mday, tm->enabled, tm->pending);
369
370 buf[0] = bin2bcd(tm->time.tm_min);
371 buf[1] = bin2bcd(tm->time.tm_hour);
372 buf[2] = bin2bcd(tm->time.tm_mday);
373 buf[3] = tm->time.tm_wday & 0x07;
374
375 err = pcf8563_write_block_data(client, PCF8563_REG_AMN, 4, buf);
376 if (err)
377 return err;
378
379 return pcf8563_set_alarm_mode(client, 1);
380}
381
382static int pcf8563_irq_enable(struct device *dev, unsigned int enabled)
383{
384 return pcf8563_set_alarm_mode(to_i2c_client(dev), !!enabled);
385}
386
238static const struct rtc_class_ops pcf8563_rtc_ops = { 387static const struct rtc_class_ops pcf8563_rtc_ops = {
239 .ioctl = pcf8563_rtc_ioctl, 388 .ioctl = pcf8563_rtc_ioctl,
240 .read_time = pcf8563_rtc_read_time, 389 .read_time = pcf8563_rtc_read_time,
241 .set_time = pcf8563_rtc_set_time, 390 .set_time = pcf8563_rtc_set_time,
391 .read_alarm = pcf8563_rtc_read_alarm,
392 .set_alarm = pcf8563_rtc_set_alarm,
393 .alarm_irq_enable = pcf8563_irq_enable,
242}; 394};
243 395
244static int pcf8563_probe(struct i2c_client *client, 396static int pcf8563_probe(struct i2c_client *client,
245 const struct i2c_device_id *id) 397 const struct i2c_device_id *id)
246{ 398{
247 struct pcf8563 *pcf8563; 399 struct pcf8563 *pcf8563;
400 int err;
248 401
249 dev_dbg(&client->dev, "%s\n", __func__); 402 dev_dbg(&client->dev, "%s\n", __func__);
250 403
@@ -259,12 +412,30 @@ static int pcf8563_probe(struct i2c_client *client,
259 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); 412 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
260 413
261 i2c_set_clientdata(client, pcf8563); 414 i2c_set_clientdata(client, pcf8563);
415 pcf8563->client = client;
416 device_set_wakeup_capable(&client->dev, 1);
262 417
263 pcf8563->rtc = devm_rtc_device_register(&client->dev, 418 pcf8563->rtc = devm_rtc_device_register(&client->dev,
264 pcf8563_driver.driver.name, 419 pcf8563_driver.driver.name,
265 &pcf8563_rtc_ops, THIS_MODULE); 420 &pcf8563_rtc_ops, THIS_MODULE);
266 421
267 return PTR_ERR_OR_ZERO(pcf8563->rtc); 422 if (IS_ERR(pcf8563->rtc))
423 return PTR_ERR(pcf8563->rtc);
424
425 if (client->irq > 0) {
426 err = devm_request_threaded_irq(&client->dev, client->irq,
427 NULL, pcf8563_irq,
428 IRQF_SHARED|IRQF_ONESHOT|IRQF_TRIGGER_FALLING,
429 pcf8563->rtc->name, client);
430 if (err) {
431 dev_err(&client->dev, "unable to request IRQ %d\n",
432 client->irq);
433 return err;
434 }
435
436 }
437
438 return 0;
268} 439}
269 440
270static const struct i2c_device_id pcf8563_id[] = { 441static const struct i2c_device_id pcf8563_id[] = {
diff --git a/drivers/rtc/rtc-pcf8583.c b/drivers/rtc/rtc-pcf8583.c
index c2639845186b..5911a6dca291 100644
--- a/drivers/rtc/rtc-pcf8583.c
+++ b/drivers/rtc/rtc-pcf8583.c
@@ -176,7 +176,11 @@ static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
176{ 176{
177 struct i2c_client *client = to_i2c_client(dev); 177 struct i2c_client *client = to_i2c_client(dev);
178 unsigned char ctrl, year[2]; 178 unsigned char ctrl, year[2];
179 struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year }; 179 struct rtc_mem mem = {
180 .loc = CMOS_YEAR,
181 .nr = sizeof(year),
182 .data = year
183 };
180 int real_year, year_offset, err; 184 int real_year, year_offset, err;
181 185
182 /* 186 /*
@@ -222,8 +226,16 @@ static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
222{ 226{
223 struct i2c_client *client = to_i2c_client(dev); 227 struct i2c_client *client = to_i2c_client(dev);
224 unsigned char year[2], chk; 228 unsigned char year[2], chk;
225 struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year }; 229 struct rtc_mem cmos_year = {
226 struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk }; 230 .loc = CMOS_YEAR,
231 .nr = sizeof(year),
232 .data = year
233 };
234 struct rtc_mem cmos_check = {
235 .loc = CMOS_CHECKSUM,
236 .nr = 1,
237 .data = &chk
238 };
227 unsigned int proper_year = tm->tm_year + 1900; 239 unsigned int proper_year = tm->tm_year + 1900;
228 int ret; 240 int ret;
229 241
diff --git a/drivers/rtc/rtc-pm8xxx.c b/drivers/rtc/rtc-pm8xxx.c
index 197699f358c7..5adcf111fc14 100644
--- a/drivers/rtc/rtc-pm8xxx.c
+++ b/drivers/rtc/rtc-pm8xxx.c
@@ -27,21 +27,36 @@
27 27
28/* RTC_CTRL register bit fields */ 28/* RTC_CTRL register bit fields */
29#define PM8xxx_RTC_ENABLE BIT(7) 29#define PM8xxx_RTC_ENABLE BIT(7)
30#define PM8xxx_RTC_ALARM_ENABLE BIT(1)
31#define PM8xxx_RTC_ALARM_CLEAR BIT(0) 30#define PM8xxx_RTC_ALARM_CLEAR BIT(0)
32 31
33#define NUM_8_BIT_RTC_REGS 0x4 32#define NUM_8_BIT_RTC_REGS 0x4
34 33
35/** 34/**
35 * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
36 * @ctrl: base address of control register
37 * @write: base address of write register
38 * @read: base address of read register
39 * @alarm_ctrl: base address of alarm control register
40 * @alarm_ctrl2: base address of alarm control2 register
41 * @alarm_rw: base address of alarm read-write register
42 * @alarm_en: alarm enable mask
43 */
44struct pm8xxx_rtc_regs {
45 unsigned int ctrl;
46 unsigned int write;
47 unsigned int read;
48 unsigned int alarm_ctrl;
49 unsigned int alarm_ctrl2;
50 unsigned int alarm_rw;
51 unsigned int alarm_en;
52};
53
54/**
36 * struct pm8xxx_rtc - rtc driver internal structure 55 * struct pm8xxx_rtc - rtc driver internal structure
37 * @rtc: rtc device for this driver. 56 * @rtc: rtc device for this driver.
38 * @regmap: regmap used to access RTC registers 57 * @regmap: regmap used to access RTC registers
39 * @allow_set_time: indicates whether writing to the RTC is allowed 58 * @allow_set_time: indicates whether writing to the RTC is allowed
40 * @rtc_alarm_irq: rtc alarm irq number. 59 * @rtc_alarm_irq: rtc alarm irq number.
41 * @rtc_base: address of rtc control register.
42 * @rtc_read_base: base address of read registers.
43 * @rtc_write_base: base address of write registers.
44 * @alarm_rw_base: base address of alarm registers.
45 * @ctrl_reg: rtc control register. 60 * @ctrl_reg: rtc control register.
46 * @rtc_dev: device structure. 61 * @rtc_dev: device structure.
47 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg. 62 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
@@ -51,11 +66,7 @@ struct pm8xxx_rtc {
51 struct regmap *regmap; 66 struct regmap *regmap;
52 bool allow_set_time; 67 bool allow_set_time;
53 int rtc_alarm_irq; 68 int rtc_alarm_irq;
54 int rtc_base; 69 const struct pm8xxx_rtc_regs *regs;
55 int rtc_read_base;
56 int rtc_write_base;
57 int alarm_rw_base;
58 u8 ctrl_reg;
59 struct device *rtc_dev; 70 struct device *rtc_dev;
60 spinlock_t ctrl_reg_lock; 71 spinlock_t ctrl_reg_lock;
61}; 72};
@@ -71,8 +82,10 @@ static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
71{ 82{
72 int rc, i; 83 int rc, i;
73 unsigned long secs, irq_flags; 84 unsigned long secs, irq_flags;
74 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, ctrl_reg; 85 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0;
86 unsigned int ctrl_reg;
75 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); 87 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
88 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
76 89
77 if (!rtc_dd->allow_set_time) 90 if (!rtc_dd->allow_set_time)
78 return -EACCES; 91 return -EACCES;
@@ -87,30 +100,30 @@ static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
87 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); 100 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
88 101
89 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); 102 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
90 ctrl_reg = rtc_dd->ctrl_reg;
91 103
92 if (ctrl_reg & PM8xxx_RTC_ALARM_ENABLE) { 104 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
105 if (rc)
106 goto rtc_rw_fail;
107
108 if (ctrl_reg & regs->alarm_en) {
93 alarm_enabled = 1; 109 alarm_enabled = 1;
94 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; 110 ctrl_reg &= ~regs->alarm_en;
95 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); 111 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
96 if (rc) { 112 if (rc) {
97 dev_err(dev, "Write to RTC control register failed\n"); 113 dev_err(dev, "Write to RTC control register failed\n");
98 goto rtc_rw_fail; 114 goto rtc_rw_fail;
99 } 115 }
100 rtc_dd->ctrl_reg = ctrl_reg;
101 } else {
102 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
103 } 116 }
104 117
105 /* Write 0 to Byte[0] */ 118 /* Write 0 to Byte[0] */
106 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_write_base, 0); 119 rc = regmap_write(rtc_dd->regmap, regs->write, 0);
107 if (rc) { 120 if (rc) {
108 dev_err(dev, "Write to RTC write data register failed\n"); 121 dev_err(dev, "Write to RTC write data register failed\n");
109 goto rtc_rw_fail; 122 goto rtc_rw_fail;
110 } 123 }
111 124
112 /* Write Byte[1], Byte[2], Byte[3] */ 125 /* Write Byte[1], Byte[2], Byte[3] */
113 rc = regmap_bulk_write(rtc_dd->regmap, rtc_dd->rtc_write_base + 1, 126 rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
114 &value[1], sizeof(value) - 1); 127 &value[1], sizeof(value) - 1);
115 if (rc) { 128 if (rc) {
116 dev_err(dev, "Write to RTC write data register failed\n"); 129 dev_err(dev, "Write to RTC write data register failed\n");
@@ -118,25 +131,23 @@ static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
118 } 131 }
119 132
120 /* Write Byte[0] */ 133 /* Write Byte[0] */
121 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_write_base, value[0]); 134 rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
122 if (rc) { 135 if (rc) {
123 dev_err(dev, "Write to RTC write data register failed\n"); 136 dev_err(dev, "Write to RTC write data register failed\n");
124 goto rtc_rw_fail; 137 goto rtc_rw_fail;
125 } 138 }
126 139
127 if (alarm_enabled) { 140 if (alarm_enabled) {
128 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; 141 ctrl_reg |= regs->alarm_en;
129 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); 142 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
130 if (rc) { 143 if (rc) {
131 dev_err(dev, "Write to RTC control register failed\n"); 144 dev_err(dev, "Write to RTC control register failed\n");
132 goto rtc_rw_fail; 145 goto rtc_rw_fail;
133 } 146 }
134 rtc_dd->ctrl_reg = ctrl_reg;
135 } 147 }
136 148
137rtc_rw_fail: 149rtc_rw_fail:
138 if (alarm_enabled) 150 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
139 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
140 151
141 return rc; 152 return rc;
142} 153}
@@ -148,9 +159,9 @@ static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
148 unsigned long secs; 159 unsigned long secs;
149 unsigned int reg; 160 unsigned int reg;
150 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); 161 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
162 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
151 163
152 rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->rtc_read_base, 164 rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
153 value, sizeof(value));
154 if (rc) { 165 if (rc) {
155 dev_err(dev, "RTC read data register failed\n"); 166 dev_err(dev, "RTC read data register failed\n");
156 return rc; 167 return rc;
@@ -160,14 +171,14 @@ static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
160 * Read the LSB again and check if there has been a carry over. 171 * Read the LSB again and check if there has been a carry over.
161 * If there is, redo the read operation. 172 * If there is, redo the read operation.
162 */ 173 */
163 rc = regmap_read(rtc_dd->regmap, rtc_dd->rtc_read_base, &reg); 174 rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
164 if (rc < 0) { 175 if (rc < 0) {
165 dev_err(dev, "RTC read data register failed\n"); 176 dev_err(dev, "RTC read data register failed\n");
166 return rc; 177 return rc;
167 } 178 }
168 179
169 if (unlikely(reg < value[0])) { 180 if (unlikely(reg < value[0])) {
170 rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->rtc_read_base, 181 rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
171 value, sizeof(value)); 182 value, sizeof(value));
172 if (rc) { 183 if (rc) {
173 dev_err(dev, "RTC read data register failed\n"); 184 dev_err(dev, "RTC read data register failed\n");
@@ -195,9 +206,11 @@ static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
195static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 206static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
196{ 207{
197 int rc, i; 208 int rc, i;
198 u8 value[NUM_8_BIT_RTC_REGS], ctrl_reg; 209 u8 value[NUM_8_BIT_RTC_REGS];
210 unsigned int ctrl_reg;
199 unsigned long secs, irq_flags; 211 unsigned long secs, irq_flags;
200 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); 212 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
213 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
201 214
202 rtc_tm_to_time(&alarm->time, &secs); 215 rtc_tm_to_time(&alarm->time, &secs);
203 216
@@ -208,28 +221,28 @@ static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
208 221
209 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); 222 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
210 223
211 rc = regmap_bulk_write(rtc_dd->regmap, rtc_dd->alarm_rw_base, value, 224 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
212 sizeof(value)); 225 sizeof(value));
213 if (rc) { 226 if (rc) {
214 dev_err(dev, "Write to RTC ALARM register failed\n"); 227 dev_err(dev, "Write to RTC ALARM register failed\n");
215 goto rtc_rw_fail; 228 goto rtc_rw_fail;
216 } 229 }
217 230
218 ctrl_reg = rtc_dd->ctrl_reg; 231 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
232 if (rc)
233 goto rtc_rw_fail;
219 234
220 if (alarm->enabled) 235 if (alarm->enabled)
221 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; 236 ctrl_reg |= regs->alarm_en;
222 else 237 else
223 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; 238 ctrl_reg &= ~regs->alarm_en;
224 239
225 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); 240 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
226 if (rc) { 241 if (rc) {
227 dev_err(dev, "Write to RTC control register failed\n"); 242 dev_err(dev, "Write to RTC alarm control register failed\n");
228 goto rtc_rw_fail; 243 goto rtc_rw_fail;
229 } 244 }
230 245
231 rtc_dd->ctrl_reg = ctrl_reg;
232
233 dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", 246 dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
234 alarm->time.tm_hour, alarm->time.tm_min, 247 alarm->time.tm_hour, alarm->time.tm_min,
235 alarm->time.tm_sec, alarm->time.tm_mday, 248 alarm->time.tm_sec, alarm->time.tm_mday,
@@ -245,8 +258,9 @@ static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
245 u8 value[NUM_8_BIT_RTC_REGS]; 258 u8 value[NUM_8_BIT_RTC_REGS];
246 unsigned long secs; 259 unsigned long secs;
247 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); 260 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
261 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
248 262
249 rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->alarm_rw_base, value, 263 rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
250 sizeof(value)); 264 sizeof(value));
251 if (rc) { 265 if (rc) {
252 dev_err(dev, "RTC alarm time read failed\n"); 266 dev_err(dev, "RTC alarm time read failed\n");
@@ -276,25 +290,26 @@ static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
276 int rc; 290 int rc;
277 unsigned long irq_flags; 291 unsigned long irq_flags;
278 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); 292 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
279 u8 ctrl_reg; 293 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
294 unsigned int ctrl_reg;
280 295
281 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); 296 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
282 297
283 ctrl_reg = rtc_dd->ctrl_reg; 298 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
299 if (rc)
300 goto rtc_rw_fail;
284 301
285 if (enable) 302 if (enable)
286 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; 303 ctrl_reg |= regs->alarm_en;
287 else 304 else
288 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; 305 ctrl_reg &= ~regs->alarm_en;
289 306
290 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); 307 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
291 if (rc) { 308 if (rc) {
292 dev_err(dev, "Write to RTC control register failed\n"); 309 dev_err(dev, "Write to RTC control register failed\n");
293 goto rtc_rw_fail; 310 goto rtc_rw_fail;
294 } 311 }
295 312
296 rtc_dd->ctrl_reg = ctrl_reg;
297
298rtc_rw_fail: 313rtc_rw_fail:
299 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); 314 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
300 return rc; 315 return rc;
@@ -311,6 +326,7 @@ static const struct rtc_class_ops pm8xxx_rtc_ops = {
311static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id) 326static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
312{ 327{
313 struct pm8xxx_rtc *rtc_dd = dev_id; 328 struct pm8xxx_rtc *rtc_dd = dev_id;
329 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
314 unsigned int ctrl_reg; 330 unsigned int ctrl_reg;
315 int rc; 331 int rc;
316 unsigned long irq_flags; 332 unsigned long irq_flags;
@@ -320,48 +336,100 @@ static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
320 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); 336 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
321 337
322 /* Clear the alarm enable bit */ 338 /* Clear the alarm enable bit */
323 ctrl_reg = rtc_dd->ctrl_reg; 339 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
324 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; 340 if (rc) {
341 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
342 goto rtc_alarm_handled;
343 }
344
345 ctrl_reg &= ~regs->alarm_en;
325 346
326 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); 347 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
327 if (rc) { 348 if (rc) {
328 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); 349 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
329 dev_err(rtc_dd->rtc_dev, 350 dev_err(rtc_dd->rtc_dev,
330 "Write to RTC control register failed\n"); 351 "Write to alarm control register failed\n");
331 goto rtc_alarm_handled; 352 goto rtc_alarm_handled;
332 } 353 }
333 354
334 rtc_dd->ctrl_reg = ctrl_reg;
335 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); 355 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
336 356
337 /* Clear RTC alarm register */ 357 /* Clear RTC alarm register */
338 rc = regmap_read(rtc_dd->regmap, 358 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
339 rtc_dd->rtc_base + PM8XXX_ALARM_CTRL_OFFSET,
340 &ctrl_reg);
341 if (rc) { 359 if (rc) {
342 dev_err(rtc_dd->rtc_dev, 360 dev_err(rtc_dd->rtc_dev,
343 "RTC Alarm control register read failed\n"); 361 "RTC Alarm control2 register read failed\n");
344 goto rtc_alarm_handled; 362 goto rtc_alarm_handled;
345 } 363 }
346 364
347 ctrl_reg &= ~PM8xxx_RTC_ALARM_CLEAR; 365 ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
348 rc = regmap_write(rtc_dd->regmap, 366 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
349 rtc_dd->rtc_base + PM8XXX_ALARM_CTRL_OFFSET,
350 ctrl_reg);
351 if (rc) 367 if (rc)
352 dev_err(rtc_dd->rtc_dev, 368 dev_err(rtc_dd->rtc_dev,
353 "Write to RTC Alarm control register failed\n"); 369 "Write to RTC Alarm control2 register failed\n");
354 370
355rtc_alarm_handled: 371rtc_alarm_handled:
356 return IRQ_HANDLED; 372 return IRQ_HANDLED;
357} 373}
358 374
375static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
376{
377 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
378 unsigned int ctrl_reg;
379 int rc;
380
381 /* Check if the RTC is on, else turn it on */
382 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
383 if (rc)
384 return rc;
385
386 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
387 ctrl_reg |= PM8xxx_RTC_ENABLE;
388 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
389 if (rc)
390 return rc;
391 }
392
393 return 0;
394}
395
396static const struct pm8xxx_rtc_regs pm8921_regs = {
397 .ctrl = 0x11d,
398 .write = 0x11f,
399 .read = 0x123,
400 .alarm_rw = 0x127,
401 .alarm_ctrl = 0x11d,
402 .alarm_ctrl2 = 0x11e,
403 .alarm_en = BIT(1),
404};
405
406static const struct pm8xxx_rtc_regs pm8058_regs = {
407 .ctrl = 0x1e8,
408 .write = 0x1ea,
409 .read = 0x1ee,
410 .alarm_rw = 0x1f2,
411 .alarm_ctrl = 0x1e8,
412 .alarm_ctrl2 = 0x1e9,
413 .alarm_en = BIT(1),
414};
415
416static const struct pm8xxx_rtc_regs pm8941_regs = {
417 .ctrl = 0x6046,
418 .write = 0x6040,
419 .read = 0x6048,
420 .alarm_rw = 0x6140,
421 .alarm_ctrl = 0x6146,
422 .alarm_ctrl2 = 0x6148,
423 .alarm_en = BIT(7),
424};
425
359/* 426/*
360 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out 427 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
361 */ 428 */
362static const struct of_device_id pm8xxx_id_table[] = { 429static const struct of_device_id pm8xxx_id_table[] = {
363 { .compatible = "qcom,pm8921-rtc", .data = (void *) 0x11D }, 430 { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
364 { .compatible = "qcom,pm8058-rtc", .data = (void *) 0x1E8 }, 431 { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
432 { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
365 { }, 433 { },
366}; 434};
367MODULE_DEVICE_TABLE(of, pm8xxx_id_table); 435MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
@@ -369,7 +437,6 @@ MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
369static int pm8xxx_rtc_probe(struct platform_device *pdev) 437static int pm8xxx_rtc_probe(struct platform_device *pdev)
370{ 438{
371 int rc; 439 int rc;
372 unsigned int ctrl_reg;
373 struct pm8xxx_rtc *rtc_dd; 440 struct pm8xxx_rtc *rtc_dd;
374 const struct of_device_id *match; 441 const struct of_device_id *match;
375 442
@@ -399,33 +466,12 @@ static int pm8xxx_rtc_probe(struct platform_device *pdev)
399 rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node, 466 rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
400 "allow-set-time"); 467 "allow-set-time");
401 468
402 rtc_dd->rtc_base = (long) match->data; 469 rtc_dd->regs = match->data;
403
404 /* Setup RTC register addresses */
405 rtc_dd->rtc_write_base = rtc_dd->rtc_base + PM8XXX_RTC_WRITE_OFFSET;
406 rtc_dd->rtc_read_base = rtc_dd->rtc_base + PM8XXX_RTC_READ_OFFSET;
407 rtc_dd->alarm_rw_base = rtc_dd->rtc_base + PM8XXX_ALARM_RW_OFFSET;
408
409 rtc_dd->rtc_dev = &pdev->dev; 470 rtc_dd->rtc_dev = &pdev->dev;
410 471
411 /* Check if the RTC is on, else turn it on */ 472 rc = pm8xxx_rtc_enable(rtc_dd);
412 rc = regmap_read(rtc_dd->regmap, rtc_dd->rtc_base, &ctrl_reg); 473 if (rc)
413 if (rc) {
414 dev_err(&pdev->dev, "RTC control register read failed!\n");
415 return rc; 474 return rc;
416 }
417
418 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
419 ctrl_reg |= PM8xxx_RTC_ENABLE;
420 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
421 if (rc) {
422 dev_err(&pdev->dev,
423 "Write to RTC control register failed\n");
424 return rc;
425 }
426 }
427
428 rtc_dd->ctrl_reg = ctrl_reg;
429 475
430 platform_set_drvdata(pdev, rtc_dd); 476 platform_set_drvdata(pdev, rtc_dd);
431 477
diff --git a/drivers/rtc/rtc-rk808.c b/drivers/rtc/rtc-rk808.c
new file mode 100644
index 000000000000..df42257668ac
--- /dev/null
+++ b/drivers/rtc/rtc-rk808.c
@@ -0,0 +1,414 @@
1/*
2 * RTC driver for Rockchip RK808
3 *
4 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
5 *
6 * Author: Chris Zhong <zyw@rock-chips.com>
7 * Author: Zhang Qing <zhangqing@rock-chips.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/rtc.h>
22#include <linux/bcd.h>
23#include <linux/mfd/rk808.h>
24#include <linux/platform_device.h>
25#include <linux/i2c.h>
26
27/* RTC_CTRL_REG bitfields */
28#define BIT_RTC_CTRL_REG_STOP_RTC_M BIT(0)
29
30/* RK808 has a shadowed register for saving a "frozen" RTC time.
31 * When user setting "GET_TIME" to 1, the time will save in this shadowed
32 * register. If set "READSEL" to 1, user read rtc time register, actually
33 * get the time of that moment. If we need the real time, clr this bit.
34 */
35#define BIT_RTC_CTRL_REG_RTC_GET_TIME BIT(6)
36#define BIT_RTC_CTRL_REG_RTC_READSEL_M BIT(7)
37#define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M BIT(3)
38#define RTC_STATUS_MASK 0xFE
39
40#define SECONDS_REG_MSK 0x7F
41#define MINUTES_REG_MAK 0x7F
42#define HOURS_REG_MSK 0x3F
43#define DAYS_REG_MSK 0x3F
44#define MONTHS_REG_MSK 0x1F
45#define YEARS_REG_MSK 0xFF
46#define WEEKS_REG_MSK 0x7
47
48/* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
49
50#define NUM_TIME_REGS (RK808_WEEKS_REG - RK808_SECONDS_REG + 1)
51#define NUM_ALARM_REGS (RK808_ALARM_YEARS_REG - RK808_ALARM_SECONDS_REG + 1)
52
53struct rk808_rtc {
54 struct rk808 *rk808;
55 struct rtc_device *rtc;
56 int irq;
57};
58
59/* Read current time and date in RTC */
60static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm)
61{
62 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
63 struct rk808 *rk808 = rk808_rtc->rk808;
64 u8 rtc_data[NUM_TIME_REGS];
65 int ret;
66
67 /* Force an update of the shadowed registers right now */
68 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
69 BIT_RTC_CTRL_REG_RTC_GET_TIME,
70 0);
71 if (ret) {
72 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
73 return ret;
74 }
75
76 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
77 BIT_RTC_CTRL_REG_RTC_GET_TIME,
78 BIT_RTC_CTRL_REG_RTC_GET_TIME);
79 if (ret) {
80 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret);
81 return ret;
82 }
83
84 ret = regmap_bulk_read(rk808->regmap, RK808_SECONDS_REG,
85 rtc_data, NUM_TIME_REGS);
86 if (ret) {
87 dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret);
88 return ret;
89 }
90
91 tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK);
92 tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK);
93 tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK);
94 tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK);
95 tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1;
96 tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100;
97 tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK);
98 dev_dbg(dev, "RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
99 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
100 tm->tm_wday, tm->tm_hour , tm->tm_min, tm->tm_sec);
101
102 return ret;
103}
104
105/* Set current time and date in RTC */
106static int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm)
107{
108 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
109 struct rk808 *rk808 = rk808_rtc->rk808;
110 u8 rtc_data[NUM_TIME_REGS];
111 int ret;
112
113 rtc_data[0] = bin2bcd(tm->tm_sec);
114 rtc_data[1] = bin2bcd(tm->tm_min);
115 rtc_data[2] = bin2bcd(tm->tm_hour);
116 rtc_data[3] = bin2bcd(tm->tm_mday);
117 rtc_data[4] = bin2bcd(tm->tm_mon + 1);
118 rtc_data[5] = bin2bcd(tm->tm_year - 100);
119 rtc_data[6] = bin2bcd(tm->tm_wday);
120 dev_dbg(dev, "set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
121 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
122 tm->tm_wday, tm->tm_hour , tm->tm_min, tm->tm_sec);
123
124 /* Stop RTC while updating the RTC registers */
125 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
126 BIT_RTC_CTRL_REG_STOP_RTC_M,
127 BIT_RTC_CTRL_REG_STOP_RTC_M);
128 if (ret) {
129 dev_err(dev, "Failed to update RTC control: %d\n", ret);
130 return ret;
131 }
132
133 ret = regmap_bulk_write(rk808->regmap, RK808_SECONDS_REG,
134 rtc_data, NUM_TIME_REGS);
135 if (ret) {
136 dev_err(dev, "Failed to bull write rtc_data: %d\n", ret);
137 return ret;
138 }
139 /* Start RTC again */
140 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
141 BIT_RTC_CTRL_REG_STOP_RTC_M, 0);
142 if (ret) {
143 dev_err(dev, "Failed to update RTC control: %d\n", ret);
144 return ret;
145 }
146 return 0;
147}
148
149/* Read alarm time and date in RTC */
150static int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
151{
152 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
153 struct rk808 *rk808 = rk808_rtc->rk808;
154 u8 alrm_data[NUM_ALARM_REGS];
155 uint32_t int_reg;
156 int ret;
157
158 ret = regmap_bulk_read(rk808->regmap, RK808_ALARM_SECONDS_REG,
159 alrm_data, NUM_ALARM_REGS);
160
161 alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK);
162 alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK);
163 alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK);
164 alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK);
165 alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1;
166 alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100;
167
168 ret = regmap_read(rk808->regmap, RK808_RTC_INT_REG, &int_reg);
169 if (ret) {
170 dev_err(dev, "Failed to read RTC INT REG: %d\n", ret);
171 return ret;
172 }
173
174 dev_dbg(dev, "alrm read RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
175 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
176 alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
177 alrm->time.tm_min, alrm->time.tm_sec);
178
179 alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0;
180
181 return 0;
182}
183
184static int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc)
185{
186 struct rk808 *rk808 = rk808_rtc->rk808;
187 int ret;
188
189 ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
190 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0);
191
192 return ret;
193}
194
195static int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc)
196{
197 struct rk808 *rk808 = rk808_rtc->rk808;
198 int ret;
199
200 ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG,
201 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M,
202 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
203
204 return ret;
205}
206
207static int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
208{
209 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
210 struct rk808 *rk808 = rk808_rtc->rk808;
211 u8 alrm_data[NUM_ALARM_REGS];
212 int ret;
213
214 ret = rk808_rtc_stop_alarm(rk808_rtc);
215 if (ret) {
216 dev_err(dev, "Failed to stop alarm: %d\n", ret);
217 return ret;
218 }
219 dev_dbg(dev, "alrm set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n",
220 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1,
221 alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour,
222 alrm->time.tm_min, alrm->time.tm_sec);
223
224 alrm_data[0] = bin2bcd(alrm->time.tm_sec);
225 alrm_data[1] = bin2bcd(alrm->time.tm_min);
226 alrm_data[2] = bin2bcd(alrm->time.tm_hour);
227 alrm_data[3] = bin2bcd(alrm->time.tm_mday);
228 alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1);
229 alrm_data[5] = bin2bcd(alrm->time.tm_year - 100);
230
231 ret = regmap_bulk_write(rk808->regmap, RK808_ALARM_SECONDS_REG,
232 alrm_data, NUM_ALARM_REGS);
233 if (ret) {
234 dev_err(dev, "Failed to bulk write: %d\n", ret);
235 return ret;
236 }
237 if (alrm->enabled) {
238 ret = rk808_rtc_start_alarm(rk808_rtc);
239 if (ret) {
240 dev_err(dev, "Failed to start alarm: %d\n", ret);
241 return ret;
242 }
243 }
244 return 0;
245}
246
247static int rk808_rtc_alarm_irq_enable(struct device *dev,
248 unsigned int enabled)
249{
250 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev);
251
252 if (enabled)
253 return rk808_rtc_start_alarm(rk808_rtc);
254
255 return rk808_rtc_stop_alarm(rk808_rtc);
256}
257
258/*
259 * We will just handle setting the frequency and make use the framework for
260 * reading the periodic interupts.
261 *
262 * @freq: Current periodic IRQ freq:
263 * bit 0: every second
264 * bit 1: every minute
265 * bit 2: every hour
266 * bit 3: every day
267 */
268static irqreturn_t rk808_alarm_irq(int irq, void *data)
269{
270 struct rk808_rtc *rk808_rtc = data;
271 struct rk808 *rk808 = rk808_rtc->rk808;
272 struct i2c_client *client = rk808->i2c;
273 int ret;
274
275 ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
276 RTC_STATUS_MASK);
277 if (ret) {
278 dev_err(&client->dev,
279 "%s:Failed to update RTC status: %d\n", __func__, ret);
280 return ret;
281 }
282
283 rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF);
284 dev_dbg(&client->dev,
285 "%s:irq=%d\n", __func__, irq);
286 return IRQ_HANDLED;
287}
288
289static const struct rtc_class_ops rk808_rtc_ops = {
290 .read_time = rk808_rtc_readtime,
291 .set_time = rk808_rtc_set_time,
292 .read_alarm = rk808_rtc_readalarm,
293 .set_alarm = rk808_rtc_setalarm,
294 .alarm_irq_enable = rk808_rtc_alarm_irq_enable,
295};
296
297#ifdef CONFIG_PM_SLEEP
298/* Turn off the alarm if it should not be a wake source. */
299static int rk808_rtc_suspend(struct device *dev)
300{
301 struct platform_device *pdev = to_platform_device(dev);
302 struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
303
304 if (device_may_wakeup(dev))
305 enable_irq_wake(rk808_rtc->irq);
306
307 return 0;
308}
309
310/* Enable the alarm if it should be enabled (in case it was disabled to
311 * prevent use as a wake source).
312 */
313static int rk808_rtc_resume(struct device *dev)
314{
315 struct platform_device *pdev = to_platform_device(dev);
316 struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev);
317
318 if (device_may_wakeup(dev))
319 disable_irq_wake(rk808_rtc->irq);
320
321 return 0;
322}
323#endif
324
325static SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops,
326 rk808_rtc_suspend, rk808_rtc_resume);
327
328static int rk808_rtc_probe(struct platform_device *pdev)
329{
330 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
331 struct rk808_rtc *rk808_rtc;
332 struct rtc_time tm;
333 int ret;
334
335 rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL);
336 if (rk808_rtc == NULL)
337 return -ENOMEM;
338
339 platform_set_drvdata(pdev, rk808_rtc);
340 rk808_rtc->rk808 = rk808;
341
342 /* start rtc running by default, and use shadowed timer. */
343 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG,
344 BIT_RTC_CTRL_REG_STOP_RTC_M |
345 BIT_RTC_CTRL_REG_RTC_READSEL_M,
346 BIT_RTC_CTRL_REG_RTC_READSEL_M);
347 if (ret) {
348 dev_err(&pdev->dev,
349 "Failed to update RTC control: %d\n", ret);
350 return ret;
351 }
352
353 ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG,
354 RTC_STATUS_MASK);
355 if (ret) {
356 dev_err(&pdev->dev,
357 "Failed to write RTC status: %d\n", ret);
358 return ret;
359 }
360
361 /* set init time */
362 ret = rk808_rtc_readtime(&pdev->dev, &tm);
363 if (ret) {
364 dev_err(&pdev->dev, "Failed to read RTC time\n");
365 return ret;
366 }
367 ret = rtc_valid_tm(&tm);
368 if (ret)
369 dev_warn(&pdev->dev, "invalid date/time\n");
370
371 device_init_wakeup(&pdev->dev, 1);
372
373 rk808_rtc->rtc = devm_rtc_device_register(&pdev->dev, "rk808-rtc",
374 &rk808_rtc_ops, THIS_MODULE);
375 if (IS_ERR(rk808_rtc->rtc)) {
376 ret = PTR_ERR(rk808_rtc->rtc);
377 return ret;
378 }
379
380 rk808_rtc->irq = platform_get_irq(pdev, 0);
381 if (rk808_rtc->irq < 0) {
382 if (rk808_rtc->irq != -EPROBE_DEFER)
383 dev_err(&pdev->dev, "Wake up is not possible as irq = %d\n",
384 rk808_rtc->irq);
385 return rk808_rtc->irq;
386 }
387
388 /* request alarm irq of rk808 */
389 ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL,
390 rk808_alarm_irq, 0,
391 "RTC alarm", rk808_rtc);
392 if (ret) {
393 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
394 rk808_rtc->irq, ret);
395 }
396
397 return ret;
398}
399
400static struct platform_driver rk808_rtc_driver = {
401 .probe = rk808_rtc_probe,
402 .driver = {
403 .name = "rk808-rtc",
404 .pm = &rk808_rtc_pm_ops,
405 },
406};
407
408module_platform_driver(rk808_rtc_driver);
409
410MODULE_DESCRIPTION("RTC driver for the rk808 series PMICs");
411MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
412MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
413MODULE_LICENSE("GPL");
414MODULE_ALIAS("platform:rk808-rtc");
diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c
index ccf54f06396b..28871cd7e3b5 100644
--- a/drivers/rtc/rtc-rs5c372.c
+++ b/drivers/rtc/rtc-rs5c372.c
@@ -142,12 +142,11 @@ static int rs5c_get_regs(struct rs5c372 *rs5c)
142 } 142 }
143 143
144 dev_dbg(&client->dev, 144 dev_dbg(&client->dev,
145 "%02x %02x %02x (%02x) %02x %02x %02x (%02x), " 145 "%3ph (%02x) %3ph (%02x), %3ph, %3ph; %02x %02x\n",
146 "%02x %02x %02x, %02x %02x %02x; %02x %02x\n", 146 rs5c->regs + 0, rs5c->regs[3],
147 rs5c->regs[0], rs5c->regs[1], rs5c->regs[2], rs5c->regs[3], 147 rs5c->regs + 4, rs5c->regs[7],
148 rs5c->regs[4], rs5c->regs[5], rs5c->regs[6], rs5c->regs[7], 148 rs5c->regs + 8, rs5c->regs + 11,
149 rs5c->regs[8], rs5c->regs[9], rs5c->regs[10], rs5c->regs[11], 149 rs5c->regs[14], rs5c->regs[15]);
150 rs5c->regs[12], rs5c->regs[13], rs5c->regs[14], rs5c->regs[15]);
151 150
152 return 0; 151 return 0;
153} 152}
diff --git a/drivers/rtc/rtc-s3c.c b/drivers/rtc/rtc-s3c.c
index 4958a363b2c7..806072238c00 100644
--- a/drivers/rtc/rtc-s3c.c
+++ b/drivers/rtc/rtc-s3c.c
@@ -32,155 +32,150 @@
32#include <asm/irq.h> 32#include <asm/irq.h>
33#include "rtc-s3c.h" 33#include "rtc-s3c.h"
34 34
35enum s3c_cpu_type { 35struct s3c_rtc {
36 TYPE_S3C2410, 36 struct device *dev;
37 TYPE_S3C2416, 37 struct rtc_device *rtc;
38 TYPE_S3C2443,
39 TYPE_S3C64XX,
40};
41 38
42struct s3c_rtc_drv_data { 39 void __iomem *base;
43 int cpu_type; 40 struct clk *rtc_clk;
44}; 41 struct clk *rtc_src_clk;
42 bool enabled;
43
44 struct s3c_rtc_data *data;
45 45
46/* I have yet to find an S3C implementation with more than one 46 int irq_alarm;
47 * of these rtc blocks in */ 47 int irq_tick;
48 48
49static struct clk *rtc_clk; 49 spinlock_t pie_lock;
50static void __iomem *s3c_rtc_base; 50 spinlock_t alarm_clk_lock;
51static int s3c_rtc_alarmno;
52static int s3c_rtc_tickno;
53static enum s3c_cpu_type s3c_rtc_cpu_type;
54 51
55static DEFINE_SPINLOCK(s3c_rtc_pie_lock); 52 int ticnt_save, ticnt_en_save;
53 bool wake_en;
54};
55
56struct s3c_rtc_data {
57 int max_user_freq;
58 bool needs_src_clk;
59
60 void (*irq_handler) (struct s3c_rtc *info, int mask);
61 void (*set_freq) (struct s3c_rtc *info, int freq);
62 void (*enable_tick) (struct s3c_rtc *info, struct seq_file *seq);
63 void (*select_tick_clk) (struct s3c_rtc *info);
64 void (*save_tick_cnt) (struct s3c_rtc *info);
65 void (*restore_tick_cnt) (struct s3c_rtc *info);
66 void (*enable) (struct s3c_rtc *info);
67 void (*disable) (struct s3c_rtc *info);
68};
56 69
57static void s3c_rtc_alarm_clk_enable(bool enable) 70static void s3c_rtc_alarm_clk_enable(struct s3c_rtc *info, bool enable)
58{ 71{
59 static DEFINE_SPINLOCK(s3c_rtc_alarm_clk_lock);
60 static bool alarm_clk_enabled;
61 unsigned long irq_flags; 72 unsigned long irq_flags;
62 73
63 spin_lock_irqsave(&s3c_rtc_alarm_clk_lock, irq_flags); 74 spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
64 if (enable) { 75 if (enable) {
65 if (!alarm_clk_enabled) { 76 if (!info->enabled) {
66 clk_enable(rtc_clk); 77 clk_enable(info->rtc_clk);
67 alarm_clk_enabled = true; 78 if (info->data->needs_src_clk)
79 clk_enable(info->rtc_src_clk);
80 info->enabled = true;
68 } 81 }
69 } else { 82 } else {
70 if (alarm_clk_enabled) { 83 if (info->enabled) {
71 clk_disable(rtc_clk); 84 if (info->data->needs_src_clk)
72 alarm_clk_enabled = false; 85 clk_disable(info->rtc_src_clk);
86 clk_disable(info->rtc_clk);
87 info->enabled = false;
73 } 88 }
74 } 89 }
75 spin_unlock_irqrestore(&s3c_rtc_alarm_clk_lock, irq_flags); 90 spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
76} 91}
77 92
78/* IRQ Handlers */ 93/* IRQ Handlers */
79 94static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
80static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
81{ 95{
82 struct rtc_device *rdev = id; 96 struct s3c_rtc *info = (struct s3c_rtc *)id;
83
84 clk_enable(rtc_clk);
85 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
86
87 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
88 writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);
89
90 clk_disable(rtc_clk);
91 97
92 s3c_rtc_alarm_clk_enable(false); 98 if (info->data->irq_handler)
99 info->data->irq_handler(info, S3C2410_INTP_TIC);
93 100
94 return IRQ_HANDLED; 101 return IRQ_HANDLED;
95} 102}
96 103
97static irqreturn_t s3c_rtc_tickirq(int irq, void *id) 104static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
98{ 105{
99 struct rtc_device *rdev = id; 106 struct s3c_rtc *info = (struct s3c_rtc *)id;
100 107
101 clk_enable(rtc_clk); 108 if (info->data->irq_handler)
102 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF); 109 info->data->irq_handler(info, S3C2410_INTP_ALM);
103 110
104 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
105 writeb(S3C2410_INTP_TIC, s3c_rtc_base + S3C2410_INTP);
106
107 clk_disable(rtc_clk);
108 return IRQ_HANDLED; 111 return IRQ_HANDLED;
109} 112}
110 113
111/* Update control registers */ 114/* Update control registers */
112static int s3c_rtc_setaie(struct device *dev, unsigned int enabled) 115static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
113{ 116{
117 struct s3c_rtc *info = dev_get_drvdata(dev);
114 unsigned int tmp; 118 unsigned int tmp;
115 119
116 dev_dbg(dev, "%s: aie=%d\n", __func__, enabled); 120 dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
117 121
118 clk_enable(rtc_clk); 122 clk_enable(info->rtc_clk);
119 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN; 123 if (info->data->needs_src_clk)
124 clk_enable(info->rtc_src_clk);
125 tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
120 126
121 if (enabled) 127 if (enabled)
122 tmp |= S3C2410_RTCALM_ALMEN; 128 tmp |= S3C2410_RTCALM_ALMEN;
123 129
124 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM); 130 writeb(tmp, info->base + S3C2410_RTCALM);
125 clk_disable(rtc_clk); 131 if (info->data->needs_src_clk)
132 clk_disable(info->rtc_src_clk);
133 clk_disable(info->rtc_clk);
126 134
127 s3c_rtc_alarm_clk_enable(enabled); 135 s3c_rtc_alarm_clk_enable(info, enabled);
128 136
129 return 0; 137 return 0;
130} 138}
131 139
132static int s3c_rtc_setfreq(struct device *dev, int freq) 140/* Set RTC frequency */
141static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
133{ 142{
134 struct platform_device *pdev = to_platform_device(dev);
135 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
136 unsigned int tmp = 0;
137 int val;
138
139 if (!is_power_of_2(freq)) 143 if (!is_power_of_2(freq))
140 return -EINVAL; 144 return -EINVAL;
141 145
142 clk_enable(rtc_clk); 146 clk_enable(info->rtc_clk);
143 spin_lock_irq(&s3c_rtc_pie_lock); 147 if (info->data->needs_src_clk)
148 clk_enable(info->rtc_src_clk);
149 spin_lock_irq(&info->pie_lock);
144 150
145 if (s3c_rtc_cpu_type != TYPE_S3C64XX) { 151 if (info->data->set_freq)
146 tmp = readb(s3c_rtc_base + S3C2410_TICNT); 152 info->data->set_freq(info, freq);
147 tmp &= S3C2410_TICNT_ENABLE;
148 }
149 153
150 val = (rtc_dev->max_user_freq / freq) - 1; 154 spin_unlock_irq(&info->pie_lock);
151 155 if (info->data->needs_src_clk)
152 if (s3c_rtc_cpu_type == TYPE_S3C2416 || s3c_rtc_cpu_type == TYPE_S3C2443) { 156 clk_disable(info->rtc_src_clk);
153 tmp |= S3C2443_TICNT_PART(val); 157 clk_disable(info->rtc_clk);
154 writel(S3C2443_TICNT1_PART(val), s3c_rtc_base + S3C2443_TICNT1);
155
156 if (s3c_rtc_cpu_type == TYPE_S3C2416)
157 writel(S3C2416_TICNT2_PART(val), s3c_rtc_base + S3C2416_TICNT2);
158 } else {
159 tmp |= val;
160 }
161
162 writel(tmp, s3c_rtc_base + S3C2410_TICNT);
163 spin_unlock_irq(&s3c_rtc_pie_lock);
164 clk_disable(rtc_clk);
165 158
166 return 0; 159 return 0;
167} 160}
168 161
169/* Time read/write */ 162/* Time read/write */
170
171static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm) 163static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
172{ 164{
165 struct s3c_rtc *info = dev_get_drvdata(dev);
173 unsigned int have_retried = 0; 166 unsigned int have_retried = 0;
174 void __iomem *base = s3c_rtc_base;
175 167
176 clk_enable(rtc_clk); 168 clk_enable(info->rtc_clk);
169 if (info->data->needs_src_clk)
170 clk_enable(info->rtc_src_clk);
171
177 retry_get_time: 172 retry_get_time:
178 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN); 173 rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
179 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR); 174 rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
180 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE); 175 rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
181 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON); 176 rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
182 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR); 177 rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
183 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC); 178 rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
184 179
185 /* the only way to work out whether the system was mid-update 180 /* the only way to work out whether the system was mid-update
186 * when we read it is to check the second counter, and if it 181 * when we read it is to check the second counter, and if it
@@ -207,13 +202,16 @@ static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
207 202
208 rtc_tm->tm_mon -= 1; 203 rtc_tm->tm_mon -= 1;
209 204
210 clk_disable(rtc_clk); 205 if (info->data->needs_src_clk)
206 clk_disable(info->rtc_src_clk);
207 clk_disable(info->rtc_clk);
208
211 return rtc_valid_tm(rtc_tm); 209 return rtc_valid_tm(rtc_tm);
212} 210}
213 211
214static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm) 212static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
215{ 213{
216 void __iomem *base = s3c_rtc_base; 214 struct s3c_rtc *info = dev_get_drvdata(dev);
217 int year = tm->tm_year - 100; 215 int year = tm->tm_year - 100;
218 216
219 dev_dbg(dev, "set time %04d.%02d.%02d %02d:%02d:%02d\n", 217 dev_dbg(dev, "set time %04d.%02d.%02d %02d:%02d:%02d\n",
@@ -227,33 +225,42 @@ static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
227 return -EINVAL; 225 return -EINVAL;
228 } 226 }
229 227
230 clk_enable(rtc_clk); 228 clk_enable(info->rtc_clk);
231 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC); 229 if (info->data->needs_src_clk)
232 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN); 230 clk_enable(info->rtc_src_clk);
233 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR); 231
234 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE); 232 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
235 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON); 233 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
236 writeb(bin2bcd(year), base + S3C2410_RTCYEAR); 234 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
237 clk_disable(rtc_clk); 235 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
236 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
237 writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
238
239 if (info->data->needs_src_clk)
240 clk_disable(info->rtc_src_clk);
241 clk_disable(info->rtc_clk);
238 242
239 return 0; 243 return 0;
240} 244}
241 245
242static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm) 246static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
243{ 247{
248 struct s3c_rtc *info = dev_get_drvdata(dev);
244 struct rtc_time *alm_tm = &alrm->time; 249 struct rtc_time *alm_tm = &alrm->time;
245 void __iomem *base = s3c_rtc_base;
246 unsigned int alm_en; 250 unsigned int alm_en;
247 251
248 clk_enable(rtc_clk); 252 clk_enable(info->rtc_clk);
249 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC); 253 if (info->data->needs_src_clk)
250 alm_tm->tm_min = readb(base + S3C2410_ALMMIN); 254 clk_enable(info->rtc_src_clk);
251 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
252 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
253 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
254 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
255 255
256 alm_en = readb(base + S3C2410_RTCALM); 256 alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
257 alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
258 alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
259 alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
260 alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
261 alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
262
263 alm_en = readb(info->base + S3C2410_RTCALM);
257 264
258 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0; 265 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
259 266
@@ -297,65 +304,74 @@ static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
297 else 304 else
298 alm_tm->tm_year = -1; 305 alm_tm->tm_year = -1;
299 306
300 clk_disable(rtc_clk); 307 if (info->data->needs_src_clk)
308 clk_disable(info->rtc_src_clk);
309 clk_disable(info->rtc_clk);
310
301 return 0; 311 return 0;
302} 312}
303 313
304static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) 314static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
305{ 315{
316 struct s3c_rtc *info = dev_get_drvdata(dev);
306 struct rtc_time *tm = &alrm->time; 317 struct rtc_time *tm = &alrm->time;
307 void __iomem *base = s3c_rtc_base;
308 unsigned int alrm_en; 318 unsigned int alrm_en;
309 319
310 clk_enable(rtc_clk); 320 clk_enable(info->rtc_clk);
321 if (info->data->needs_src_clk)
322 clk_enable(info->rtc_src_clk);
323
311 dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n", 324 dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
312 alrm->enabled, 325 alrm->enabled,
313 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, 326 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
314 tm->tm_hour, tm->tm_min, tm->tm_sec); 327 tm->tm_hour, tm->tm_min, tm->tm_sec);
315 328
316 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN; 329 alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
317 writeb(0x00, base + S3C2410_RTCALM); 330 writeb(0x00, info->base + S3C2410_RTCALM);
318 331
319 if (tm->tm_sec < 60 && tm->tm_sec >= 0) { 332 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
320 alrm_en |= S3C2410_RTCALM_SECEN; 333 alrm_en |= S3C2410_RTCALM_SECEN;
321 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC); 334 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
322 } 335 }
323 336
324 if (tm->tm_min < 60 && tm->tm_min >= 0) { 337 if (tm->tm_min < 60 && tm->tm_min >= 0) {
325 alrm_en |= S3C2410_RTCALM_MINEN; 338 alrm_en |= S3C2410_RTCALM_MINEN;
326 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN); 339 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
327 } 340 }
328 341
329 if (tm->tm_hour < 24 && tm->tm_hour >= 0) { 342 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
330 alrm_en |= S3C2410_RTCALM_HOUREN; 343 alrm_en |= S3C2410_RTCALM_HOUREN;
331 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR); 344 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
332 } 345 }
333 346
334 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en); 347 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
335 348
336 writeb(alrm_en, base + S3C2410_RTCALM); 349 writeb(alrm_en, info->base + S3C2410_RTCALM);
337 350
338 s3c_rtc_setaie(dev, alrm->enabled); 351 s3c_rtc_setaie(dev, alrm->enabled);
339 352
340 clk_disable(rtc_clk); 353 if (info->data->needs_src_clk)
354 clk_disable(info->rtc_src_clk);
355 clk_disable(info->rtc_clk);
356
341 return 0; 357 return 0;
342} 358}
343 359
344static int s3c_rtc_proc(struct device *dev, struct seq_file *seq) 360static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
345{ 361{
346 unsigned int ticnt; 362 struct s3c_rtc *info = dev_get_drvdata(dev);
347 363
348 clk_enable(rtc_clk); 364 clk_enable(info->rtc_clk);
349 if (s3c_rtc_cpu_type == TYPE_S3C64XX) { 365 if (info->data->needs_src_clk)
350 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON); 366 clk_enable(info->rtc_src_clk);
351 ticnt &= S3C64XX_RTCCON_TICEN; 367
352 } else { 368 if (info->data->enable_tick)
353 ticnt = readb(s3c_rtc_base + S3C2410_TICNT); 369 info->data->enable_tick(info, seq);
354 ticnt &= S3C2410_TICNT_ENABLE; 370
355 } 371 if (info->data->needs_src_clk)
372 clk_disable(info->rtc_src_clk);
373 clk_disable(info->rtc_clk);
356 374
357 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
358 clk_disable(rtc_clk);
359 return 0; 375 return 0;
360} 376}
361 377
@@ -368,152 +384,201 @@ static const struct rtc_class_ops s3c_rtcops = {
368 .alarm_irq_enable = s3c_rtc_setaie, 384 .alarm_irq_enable = s3c_rtc_setaie,
369}; 385};
370 386
371static void s3c_rtc_enable(struct platform_device *pdev, int en) 387static void s3c24xx_rtc_enable(struct s3c_rtc *info)
372{ 388{
373 void __iomem *base = s3c_rtc_base; 389 unsigned int con, tmp;
374 unsigned int tmp;
375 390
376 if (s3c_rtc_base == NULL) 391 clk_enable(info->rtc_clk);
377 return; 392 if (info->data->needs_src_clk)
378 393 clk_enable(info->rtc_src_clk);
379 clk_enable(rtc_clk);
380 if (!en) {
381 tmp = readw(base + S3C2410_RTCCON);
382 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
383 tmp &= ~S3C64XX_RTCCON_TICEN;
384 tmp &= ~S3C2410_RTCCON_RTCEN;
385 writew(tmp, base + S3C2410_RTCCON);
386
387 if (s3c_rtc_cpu_type != TYPE_S3C64XX) {
388 tmp = readb(base + S3C2410_TICNT);
389 tmp &= ~S3C2410_TICNT_ENABLE;
390 writeb(tmp, base + S3C2410_TICNT);
391 }
392 } else {
393 /* re-enable the device, and check it is ok */
394 394
395 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) { 395 con = readw(info->base + S3C2410_RTCCON);
396 dev_info(&pdev->dev, "rtc disabled, re-enabling\n"); 396 /* re-enable the device, and check it is ok */
397 if ((con & S3C2410_RTCCON_RTCEN) == 0) {
398 dev_info(info->dev, "rtc disabled, re-enabling\n");
397 399
398 tmp = readw(base + S3C2410_RTCCON); 400 tmp = readw(info->base + S3C2410_RTCCON);
399 writew(tmp | S3C2410_RTCCON_RTCEN, 401 writew(tmp | S3C2410_RTCCON_RTCEN,
400 base + S3C2410_RTCCON); 402 info->base + S3C2410_RTCCON);
401 } 403 }
402 404
403 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) { 405 if (con & S3C2410_RTCCON_CNTSEL) {
404 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n"); 406 dev_info(info->dev, "removing RTCCON_CNTSEL\n");
405 407
406 tmp = readw(base + S3C2410_RTCCON); 408 tmp = readw(info->base + S3C2410_RTCCON);
407 writew(tmp & ~S3C2410_RTCCON_CNTSEL, 409 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
408 base + S3C2410_RTCCON); 410 info->base + S3C2410_RTCCON);
409 } 411 }
410 412
411 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) { 413 if (con & S3C2410_RTCCON_CLKRST) {
412 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n"); 414 dev_info(info->dev, "removing RTCCON_CLKRST\n");
413 415
414 tmp = readw(base + S3C2410_RTCCON); 416 tmp = readw(info->base + S3C2410_RTCCON);
415 writew(tmp & ~S3C2410_RTCCON_CLKRST, 417 writew(tmp & ~S3C2410_RTCCON_CLKRST,
416 base + S3C2410_RTCCON); 418 info->base + S3C2410_RTCCON);
417 }
418 } 419 }
419 clk_disable(rtc_clk); 420
421 if (info->data->needs_src_clk)
422 clk_disable(info->rtc_src_clk);
423 clk_disable(info->rtc_clk);
420} 424}
421 425
422static int s3c_rtc_remove(struct platform_device *dev) 426static void s3c24xx_rtc_disable(struct s3c_rtc *info)
423{ 427{
424 s3c_rtc_setaie(&dev->dev, 0); 428 unsigned int con;
429
430 clk_enable(info->rtc_clk);
431 if (info->data->needs_src_clk)
432 clk_enable(info->rtc_src_clk);
433
434 con = readw(info->base + S3C2410_RTCCON);
435 con &= ~S3C2410_RTCCON_RTCEN;
436 writew(con, info->base + S3C2410_RTCCON);
425 437
426 clk_unprepare(rtc_clk); 438 con = readb(info->base + S3C2410_TICNT);
427 rtc_clk = NULL; 439 con &= ~S3C2410_TICNT_ENABLE;
440 writeb(con, info->base + S3C2410_TICNT);
441
442 if (info->data->needs_src_clk)
443 clk_disable(info->rtc_src_clk);
444 clk_disable(info->rtc_clk);
445}
446
447static void s3c6410_rtc_disable(struct s3c_rtc *info)
448{
449 unsigned int con;
450
451 clk_enable(info->rtc_clk);
452 if (info->data->needs_src_clk)
453 clk_enable(info->rtc_src_clk);
454
455 con = readw(info->base + S3C2410_RTCCON);
456 con &= ~S3C64XX_RTCCON_TICEN;
457 con &= ~S3C2410_RTCCON_RTCEN;
458 writew(con, info->base + S3C2410_RTCCON);
459
460 if (info->data->needs_src_clk)
461 clk_disable(info->rtc_src_clk);
462 clk_disable(info->rtc_clk);
463}
464
465static int s3c_rtc_remove(struct platform_device *pdev)
466{
467 struct s3c_rtc *info = platform_get_drvdata(pdev);
468
469 s3c_rtc_setaie(info->dev, 0);
470
471 clk_unprepare(info->rtc_clk);
472 info->rtc_clk = NULL;
428 473
429 return 0; 474 return 0;
430} 475}
431 476
432static const struct of_device_id s3c_rtc_dt_match[]; 477static const struct of_device_id s3c_rtc_dt_match[];
433 478
434static inline int s3c_rtc_get_driver_data(struct platform_device *pdev) 479static struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
435{ 480{
436#ifdef CONFIG_OF 481 const struct of_device_id *match;
437 struct s3c_rtc_drv_data *data; 482
438 if (pdev->dev.of_node) { 483 match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
439 const struct of_device_id *match; 484 return (struct s3c_rtc_data *)match->data;
440 match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
441 data = (struct s3c_rtc_drv_data *) match->data;
442 return data->cpu_type;
443 }
444#endif
445 return platform_get_device_id(pdev)->driver_data;
446} 485}
447 486
448static int s3c_rtc_probe(struct platform_device *pdev) 487static int s3c_rtc_probe(struct platform_device *pdev)
449{ 488{
450 struct rtc_device *rtc; 489 struct s3c_rtc *info = NULL;
451 struct rtc_time rtc_tm; 490 struct rtc_time rtc_tm;
452 struct resource *res; 491 struct resource *res;
453 int ret; 492 int ret;
454 int tmp;
455 493
456 dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev); 494 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
495 if (!info)
496 return -ENOMEM;
457 497
458 /* find the IRQs */ 498 /* find the IRQs */
459 499 info->irq_tick = platform_get_irq(pdev, 1);
460 s3c_rtc_tickno = platform_get_irq(pdev, 1); 500 if (info->irq_tick < 0) {
461 if (s3c_rtc_tickno < 0) {
462 dev_err(&pdev->dev, "no irq for rtc tick\n"); 501 dev_err(&pdev->dev, "no irq for rtc tick\n");
463 return s3c_rtc_tickno; 502 return info->irq_tick;
503 }
504
505 info->dev = &pdev->dev;
506 info->data = s3c_rtc_get_data(pdev);
507 if (!info->data) {
508 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
509 return -EINVAL;
464 } 510 }
511 spin_lock_init(&info->pie_lock);
512 spin_lock_init(&info->alarm_clk_lock);
513
514 platform_set_drvdata(pdev, info);
465 515
466 s3c_rtc_alarmno = platform_get_irq(pdev, 0); 516 info->irq_alarm = platform_get_irq(pdev, 0);
467 if (s3c_rtc_alarmno < 0) { 517 if (info->irq_alarm < 0) {
468 dev_err(&pdev->dev, "no irq for alarm\n"); 518 dev_err(&pdev->dev, "no irq for alarm\n");
469 return s3c_rtc_alarmno; 519 return info->irq_alarm;
470 } 520 }
471 521
472 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n", 522 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
473 s3c_rtc_tickno, s3c_rtc_alarmno); 523 info->irq_tick, info->irq_alarm);
474 524
475 /* get the memory region */ 525 /* get the memory region */
476
477 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 526 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
478 s3c_rtc_base = devm_ioremap_resource(&pdev->dev, res); 527 info->base = devm_ioremap_resource(&pdev->dev, res);
479 if (IS_ERR(s3c_rtc_base)) 528 if (IS_ERR(info->base))
480 return PTR_ERR(s3c_rtc_base); 529 return PTR_ERR(info->base);
481 530
482 rtc_clk = devm_clk_get(&pdev->dev, "rtc"); 531 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
483 if (IS_ERR(rtc_clk)) { 532 if (IS_ERR(info->rtc_clk)) {
484 dev_err(&pdev->dev, "failed to find rtc clock source\n"); 533 dev_err(&pdev->dev, "failed to find rtc clock\n");
485 ret = PTR_ERR(rtc_clk); 534 return PTR_ERR(info->rtc_clk);
486 rtc_clk = NULL; 535 }
487 return ret; 536 clk_prepare_enable(info->rtc_clk);
537
538 if (info->data->needs_src_clk) {
539 info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
540 if (IS_ERR(info->rtc_src_clk)) {
541 dev_err(&pdev->dev,
542 "failed to find rtc source clock\n");
543 return PTR_ERR(info->rtc_src_clk);
544 }
545 clk_prepare_enable(info->rtc_src_clk);
488 } 546 }
489
490 clk_prepare_enable(rtc_clk);
491 547
492 /* check to see if everything is setup correctly */ 548 /* check to see if everything is setup correctly */
493 549 if (info->data->enable)
494 s3c_rtc_enable(pdev, 1); 550 info->data->enable(info);
495 551
496 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n", 552 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
497 readw(s3c_rtc_base + S3C2410_RTCCON)); 553 readw(info->base + S3C2410_RTCCON));
498 554
499 device_init_wakeup(&pdev->dev, 1); 555 device_init_wakeup(&pdev->dev, 1);
500 556
501 /* register RTC and exit */ 557 /* register RTC and exit */
502 558 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
503 rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
504 THIS_MODULE); 559 THIS_MODULE);
505 560 if (IS_ERR(info->rtc)) {
506 if (IS_ERR(rtc)) {
507 dev_err(&pdev->dev, "cannot attach rtc\n"); 561 dev_err(&pdev->dev, "cannot attach rtc\n");
508 ret = PTR_ERR(rtc); 562 ret = PTR_ERR(info->rtc);
509 goto err_nortc; 563 goto err_nortc;
510 } 564 }
511 565
512 s3c_rtc_cpu_type = s3c_rtc_get_driver_data(pdev); 566 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
567 0, "s3c2410-rtc alarm", info);
568 if (ret) {
569 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
570 goto err_nortc;
571 }
513 572
514 /* Check RTC Time */ 573 ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
574 0, "s3c2410-rtc tick", info);
575 if (ret) {
576 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
577 goto err_nortc;
578 }
515 579
516 s3c_rtc_gettime(NULL, &rtc_tm); 580 /* Check RTC Time */
581 s3c_rtc_gettime(&pdev->dev, &rtc_tm);
517 582
518 if (rtc_valid_tm(&rtc_tm)) { 583 if (rtc_valid_tm(&rtc_tm)) {
519 rtc_tm.tm_year = 100; 584 rtc_tm.tm_year = 100;
@@ -523,163 +588,312 @@ static int s3c_rtc_probe(struct platform_device *pdev)
523 rtc_tm.tm_min = 0; 588 rtc_tm.tm_min = 0;
524 rtc_tm.tm_sec = 0; 589 rtc_tm.tm_sec = 0;
525 590
526 s3c_rtc_settime(NULL, &rtc_tm); 591 s3c_rtc_settime(&pdev->dev, &rtc_tm);
527 592
528 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n"); 593 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
529 } 594 }
530 595
531 if (s3c_rtc_cpu_type != TYPE_S3C2410) 596 if (info->data->select_tick_clk)
532 rtc->max_user_freq = 32768; 597 info->data->select_tick_clk(info);
533 else
534 rtc->max_user_freq = 128;
535 598
536 if (s3c_rtc_cpu_type == TYPE_S3C2416 || s3c_rtc_cpu_type == TYPE_S3C2443) { 599 s3c_rtc_setfreq(info, 1);
537 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
538 tmp |= S3C2443_RTCCON_TICSEL;
539 writew(tmp, s3c_rtc_base + S3C2410_RTCCON);
540 }
541 600
542 platform_set_drvdata(pdev, rtc); 601 if (info->data->needs_src_clk)
543 602 clk_disable(info->rtc_src_clk);
544 s3c_rtc_setfreq(&pdev->dev, 1); 603 clk_disable(info->rtc_clk);
545
546 ret = devm_request_irq(&pdev->dev, s3c_rtc_alarmno, s3c_rtc_alarmirq,
547 0, "s3c2410-rtc alarm", rtc);
548 if (ret) {
549 dev_err(&pdev->dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
550 goto err_nortc;
551 }
552
553 ret = devm_request_irq(&pdev->dev, s3c_rtc_tickno, s3c_rtc_tickirq,
554 0, "s3c2410-rtc tick", rtc);
555 if (ret) {
556 dev_err(&pdev->dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
557 goto err_nortc;
558 }
559
560 clk_disable(rtc_clk);
561 604
562 return 0; 605 return 0;
563 606
564 err_nortc: 607 err_nortc:
565 s3c_rtc_enable(pdev, 0); 608 if (info->data->disable)
566 clk_disable_unprepare(rtc_clk); 609 info->data->disable(info);
610 clk_disable_unprepare(info->rtc_clk);
567 611
568 return ret; 612 return ret;
569} 613}
570 614
571#ifdef CONFIG_PM_SLEEP 615#ifdef CONFIG_PM_SLEEP
572/* RTC Power management control */
573
574static int ticnt_save, ticnt_en_save;
575static bool wake_en;
576 616
577static int s3c_rtc_suspend(struct device *dev) 617static int s3c_rtc_suspend(struct device *dev)
578{ 618{
579 struct platform_device *pdev = to_platform_device(dev); 619 struct s3c_rtc *info = dev_get_drvdata(dev);
620
621 clk_enable(info->rtc_clk);
622 if (info->data->needs_src_clk)
623 clk_enable(info->rtc_src_clk);
580 624
581 clk_enable(rtc_clk);
582 /* save TICNT for anyone using periodic interrupts */ 625 /* save TICNT for anyone using periodic interrupts */
583 if (s3c_rtc_cpu_type == TYPE_S3C64XX) { 626 if (info->data->save_tick_cnt)
584 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON); 627 info->data->save_tick_cnt(info);
585 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
586 ticnt_save = readl(s3c_rtc_base + S3C2410_TICNT);
587 } else {
588 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
589 }
590 s3c_rtc_enable(pdev, 0);
591 628
592 if (device_may_wakeup(dev) && !wake_en) { 629 if (info->data->disable)
593 if (enable_irq_wake(s3c_rtc_alarmno) == 0) 630 info->data->disable(info);
594 wake_en = true; 631
632 if (device_may_wakeup(dev) && !info->wake_en) {
633 if (enable_irq_wake(info->irq_alarm) == 0)
634 info->wake_en = true;
595 else 635 else
596 dev_err(dev, "enable_irq_wake failed\n"); 636 dev_err(dev, "enable_irq_wake failed\n");
597 } 637 }
598 clk_disable(rtc_clk); 638
639 if (info->data->needs_src_clk)
640 clk_disable(info->rtc_src_clk);
641 clk_disable(info->rtc_clk);
599 642
600 return 0; 643 return 0;
601} 644}
602 645
603static int s3c_rtc_resume(struct device *dev) 646static int s3c_rtc_resume(struct device *dev)
604{ 647{
605 struct platform_device *pdev = to_platform_device(dev); 648 struct s3c_rtc *info = dev_get_drvdata(dev);
606 unsigned int tmp;
607 649
608 clk_enable(rtc_clk); 650 clk_enable(info->rtc_clk);
609 s3c_rtc_enable(pdev, 1); 651 if (info->data->needs_src_clk)
610 if (s3c_rtc_cpu_type == TYPE_S3C64XX) { 652 clk_enable(info->rtc_src_clk);
611 writel(ticnt_save, s3c_rtc_base + S3C2410_TICNT); 653
612 if (ticnt_en_save) { 654 if (info->data->enable)
613 tmp = readw(s3c_rtc_base + S3C2410_RTCCON); 655 info->data->enable(info);
614 writew(tmp | ticnt_en_save, 656
615 s3c_rtc_base + S3C2410_RTCCON); 657 if (info->data->restore_tick_cnt)
616 } 658 info->data->restore_tick_cnt(info);
617 } else {
618 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
619 }
620 659
621 if (device_may_wakeup(dev) && wake_en) { 660 if (device_may_wakeup(dev) && info->wake_en) {
622 disable_irq_wake(s3c_rtc_alarmno); 661 disable_irq_wake(info->irq_alarm);
623 wake_en = false; 662 info->wake_en = false;
624 } 663 }
625 clk_disable(rtc_clk); 664
665 if (info->data->needs_src_clk)
666 clk_disable(info->rtc_src_clk);
667 clk_disable(info->rtc_clk);
626 668
627 return 0; 669 return 0;
628} 670}
629#endif 671#endif
630
631static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume); 672static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
632 673
633#ifdef CONFIG_OF 674static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
634static struct s3c_rtc_drv_data s3c_rtc_drv_data_array[] = { 675{
635 [TYPE_S3C2410] = { TYPE_S3C2410 }, 676 clk_enable(info->rtc_clk);
636 [TYPE_S3C2416] = { TYPE_S3C2416 }, 677 if (info->data->needs_src_clk)
637 [TYPE_S3C2443] = { TYPE_S3C2443 }, 678 clk_enable(info->rtc_src_clk);
638 [TYPE_S3C64XX] = { TYPE_S3C64XX }, 679 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
680 if (info->data->needs_src_clk)
681 clk_disable(info->rtc_src_clk);
682 clk_disable(info->rtc_clk);
683
684 s3c_rtc_alarm_clk_enable(info, false);
685}
686
687static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
688{
689 clk_enable(info->rtc_clk);
690 if (info->data->needs_src_clk)
691 clk_enable(info->rtc_src_clk);
692 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
693 writeb(mask, info->base + S3C2410_INTP);
694 if (info->data->needs_src_clk)
695 clk_disable(info->rtc_src_clk);
696 clk_disable(info->rtc_clk);
697
698 s3c_rtc_alarm_clk_enable(info, false);
699}
700
701static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
702{
703 unsigned int tmp = 0;
704 int val;
705
706 tmp = readb(info->base + S3C2410_TICNT);
707 tmp &= S3C2410_TICNT_ENABLE;
708
709 val = (info->rtc->max_user_freq / freq) - 1;
710 tmp |= val;
711
712 writel(tmp, info->base + S3C2410_TICNT);
713}
714
715static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
716{
717 unsigned int tmp = 0;
718 int val;
719
720 tmp = readb(info->base + S3C2410_TICNT);
721 tmp &= S3C2410_TICNT_ENABLE;
722
723 val = (info->rtc->max_user_freq / freq) - 1;
724
725 tmp |= S3C2443_TICNT_PART(val);
726 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
727
728 writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
729
730 writel(tmp, info->base + S3C2410_TICNT);
731}
732
733static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
734{
735 unsigned int tmp = 0;
736 int val;
737
738 tmp = readb(info->base + S3C2410_TICNT);
739 tmp &= S3C2410_TICNT_ENABLE;
740
741 val = (info->rtc->max_user_freq / freq) - 1;
742
743 tmp |= S3C2443_TICNT_PART(val);
744 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
745
746 writel(tmp, info->base + S3C2410_TICNT);
747}
748
749static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
750{
751 int val;
752
753 val = (info->rtc->max_user_freq / freq) - 1;
754 writel(val, info->base + S3C2410_TICNT);
755}
756
757static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
758{
759 unsigned int ticnt;
760
761 ticnt = readb(info->base + S3C2410_TICNT);
762 ticnt &= S3C2410_TICNT_ENABLE;
763
764 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
765}
766
767static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
768{
769 unsigned int con;
770
771 con = readw(info->base + S3C2410_RTCCON);
772 con |= S3C2443_RTCCON_TICSEL;
773 writew(con, info->base + S3C2410_RTCCON);
774}
775
776static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
777{
778 unsigned int ticnt;
779
780 ticnt = readw(info->base + S3C2410_RTCCON);
781 ticnt &= S3C64XX_RTCCON_TICEN;
782
783 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
784}
785
786static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
787{
788 info->ticnt_save = readb(info->base + S3C2410_TICNT);
789}
790
791static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
792{
793 writeb(info->ticnt_save, info->base + S3C2410_TICNT);
794}
795
796static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
797{
798 info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
799 info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
800 info->ticnt_save = readl(info->base + S3C2410_TICNT);
801}
802
803static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
804{
805 unsigned int con;
806
807 writel(info->ticnt_save, info->base + S3C2410_TICNT);
808 if (info->ticnt_en_save) {
809 con = readw(info->base + S3C2410_RTCCON);
810 writew(con | info->ticnt_en_save,
811 info->base + S3C2410_RTCCON);
812 }
813}
814
815static struct s3c_rtc_data const s3c2410_rtc_data = {
816 .max_user_freq = 128,
817 .irq_handler = s3c24xx_rtc_irq,
818 .set_freq = s3c2410_rtc_setfreq,
819 .enable_tick = s3c24xx_rtc_enable_tick,
820 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
821 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
822 .enable = s3c24xx_rtc_enable,
823 .disable = s3c24xx_rtc_disable,
824};
825
826static struct s3c_rtc_data const s3c2416_rtc_data = {
827 .max_user_freq = 32768,
828 .irq_handler = s3c24xx_rtc_irq,
829 .set_freq = s3c2416_rtc_setfreq,
830 .enable_tick = s3c24xx_rtc_enable_tick,
831 .select_tick_clk = s3c2416_rtc_select_tick_clk,
832 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
833 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
834 .enable = s3c24xx_rtc_enable,
835 .disable = s3c24xx_rtc_disable,
836};
837
838static struct s3c_rtc_data const s3c2443_rtc_data = {
839 .max_user_freq = 32768,
840 .irq_handler = s3c24xx_rtc_irq,
841 .set_freq = s3c2443_rtc_setfreq,
842 .enable_tick = s3c24xx_rtc_enable_tick,
843 .select_tick_clk = s3c2416_rtc_select_tick_clk,
844 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
845 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
846 .enable = s3c24xx_rtc_enable,
847 .disable = s3c24xx_rtc_disable,
848};
849
850static struct s3c_rtc_data const s3c6410_rtc_data = {
851 .max_user_freq = 32768,
852 .irq_handler = s3c6410_rtc_irq,
853 .set_freq = s3c6410_rtc_setfreq,
854 .enable_tick = s3c6410_rtc_enable_tick,
855 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
856 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
857 .enable = s3c24xx_rtc_enable,
858 .disable = s3c6410_rtc_disable,
859};
860
861static struct s3c_rtc_data const exynos3250_rtc_data = {
862 .max_user_freq = 32768,
863 .needs_src_clk = true,
864 .irq_handler = s3c6410_rtc_irq,
865 .set_freq = s3c6410_rtc_setfreq,
866 .enable_tick = s3c6410_rtc_enable_tick,
867 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
868 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
869 .enable = s3c24xx_rtc_enable,
870 .disable = s3c6410_rtc_disable,
639}; 871};
640 872
641static const struct of_device_id s3c_rtc_dt_match[] = { 873static const struct of_device_id s3c_rtc_dt_match[] = {
642 { 874 {
643 .compatible = "samsung,s3c2410-rtc", 875 .compatible = "samsung,s3c2410-rtc",
644 .data = &s3c_rtc_drv_data_array[TYPE_S3C2410], 876 .data = (void *)&s3c2410_rtc_data,
645 }, { 877 }, {
646 .compatible = "samsung,s3c2416-rtc", 878 .compatible = "samsung,s3c2416-rtc",
647 .data = &s3c_rtc_drv_data_array[TYPE_S3C2416], 879 .data = (void *)&s3c2416_rtc_data,
648 }, { 880 }, {
649 .compatible = "samsung,s3c2443-rtc", 881 .compatible = "samsung,s3c2443-rtc",
650 .data = &s3c_rtc_drv_data_array[TYPE_S3C2443], 882 .data = (void *)&s3c2443_rtc_data,
651 }, { 883 }, {
652 .compatible = "samsung,s3c6410-rtc", 884 .compatible = "samsung,s3c6410-rtc",
653 .data = &s3c_rtc_drv_data_array[TYPE_S3C64XX], 885 .data = (void *)&s3c6410_rtc_data,
654 },
655 {},
656};
657MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
658#endif
659
660static struct platform_device_id s3c_rtc_driver_ids[] = {
661 {
662 .name = "s3c2410-rtc",
663 .driver_data = TYPE_S3C2410,
664 }, {
665 .name = "s3c2416-rtc",
666 .driver_data = TYPE_S3C2416,
667 }, {
668 .name = "s3c2443-rtc",
669 .driver_data = TYPE_S3C2443,
670 }, { 886 }, {
671 .name = "s3c64xx-rtc", 887 .compatible = "samsung,exynos3250-rtc",
672 .driver_data = TYPE_S3C64XX, 888 .data = (void *)&exynos3250_rtc_data,
673 }, 889 },
674 { } 890 { /* sentinel */ },
675}; 891};
676 892MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
677MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
678 893
679static struct platform_driver s3c_rtc_driver = { 894static struct platform_driver s3c_rtc_driver = {
680 .probe = s3c_rtc_probe, 895 .probe = s3c_rtc_probe,
681 .remove = s3c_rtc_remove, 896 .remove = s3c_rtc_remove,
682 .id_table = s3c_rtc_driver_ids,
683 .driver = { 897 .driver = {
684 .name = "s3c-rtc", 898 .name = "s3c-rtc",
685 .owner = THIS_MODULE, 899 .owner = THIS_MODULE,
@@ -687,7 +901,6 @@ static struct platform_driver s3c_rtc_driver = {
687 .of_match_table = of_match_ptr(s3c_rtc_dt_match), 901 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
688 }, 902 },
689}; 903};
690
691module_platform_driver(s3c_rtc_driver); 904module_platform_driver(s3c_rtc_driver);
692 905
693MODULE_DESCRIPTION("Samsung S3C RTC Driver"); 906MODULE_DESCRIPTION("Samsung S3C RTC Driver");
diff --git a/drivers/rtc/rtc-s5m.c b/drivers/rtc/rtc-s5m.c
index 8f06250a0389..8754c33361e8 100644
--- a/drivers/rtc/rtc-s5m.c
+++ b/drivers/rtc/rtc-s5m.c
@@ -717,12 +717,14 @@ static int s5m_rtc_probe(struct platform_device *pdev)
717 info->device_type = s5m87xx->device_type; 717 info->device_type = s5m87xx->device_type;
718 info->wtsr_smpl = s5m87xx->wtsr_smpl; 718 info->wtsr_smpl = s5m87xx->wtsr_smpl;
719 719
720 info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq); 720 if (s5m87xx->irq_data) {
721 if (info->irq <= 0) { 721 info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq);
722 ret = -EINVAL; 722 if (info->irq <= 0) {
723 dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n", 723 ret = -EINVAL;
724 dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
724 alarm_irq); 725 alarm_irq);
725 goto err; 726 goto err;
727 }
726 } 728 }
727 729
728 platform_set_drvdata(pdev, info); 730 platform_set_drvdata(pdev, info);
@@ -744,6 +746,11 @@ static int s5m_rtc_probe(struct platform_device *pdev)
744 goto err; 746 goto err;
745 } 747 }
746 748
749 if (!info->irq) {
750 dev_info(&pdev->dev, "Alarm IRQ not available\n");
751 return 0;
752 }
753
747 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, 754 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
748 s5m_rtc_alarm_irq, 0, "rtc-alarm0", 755 s5m_rtc_alarm_irq, 0, "rtc-alarm0",
749 info); 756 info);
@@ -802,7 +809,7 @@ static int s5m_rtc_resume(struct device *dev)
802 struct s5m_rtc_info *info = dev_get_drvdata(dev); 809 struct s5m_rtc_info *info = dev_get_drvdata(dev);
803 int ret = 0; 810 int ret = 0;
804 811
805 if (device_may_wakeup(dev)) 812 if (info->irq && device_may_wakeup(dev))
806 ret = disable_irq_wake(info->irq); 813 ret = disable_irq_wake(info->irq);
807 814
808 return ret; 815 return ret;
@@ -813,7 +820,7 @@ static int s5m_rtc_suspend(struct device *dev)
813 struct s5m_rtc_info *info = dev_get_drvdata(dev); 820 struct s5m_rtc_info *info = dev_get_drvdata(dev);
814 int ret = 0; 821 int ret = 0;
815 822
816 if (device_may_wakeup(dev)) 823 if (info->irq && device_may_wakeup(dev))
817 ret = enable_irq_wake(info->irq); 824 ret = enable_irq_wake(info->irq);
818 825
819 return ret; 826 return ret;
diff --git a/drivers/rtc/rtc-sun6i.c b/drivers/rtc/rtc-sun6i.c
new file mode 100644
index 000000000000..c169a2cd4727
--- /dev/null
+++ b/drivers/rtc/rtc-sun6i.c
@@ -0,0 +1,447 @@
1/*
2 * An RTC driver for Allwinner A31/A23
3 *
4 * Copyright (c) 2014, Chen-Yu Tsai <wens@csie.org>
5 *
6 * based on rtc-sunxi.c
7 *
8 * An RTC driver for Allwinner A10/A20
9 *
10 * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 */
22
23#include <linux/delay.h>
24#include <linux/err.h>
25#include <linux/fs.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/of.h>
32#include <linux/of_address.h>
33#include <linux/of_device.h>
34#include <linux/platform_device.h>
35#include <linux/rtc.h>
36#include <linux/types.h>
37
38/* Control register */
39#define SUN6I_LOSC_CTRL 0x0000
40#define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9)
41#define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8)
42#define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7)
43#define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7)
44
45/* RTC */
46#define SUN6I_RTC_YMD 0x0010
47#define SUN6I_RTC_HMS 0x0014
48
49/* Alarm 0 (counter) */
50#define SUN6I_ALRM_COUNTER 0x0020
51#define SUN6I_ALRM_CUR_VAL 0x0024
52#define SUN6I_ALRM_EN 0x0028
53#define SUN6I_ALRM_EN_CNT_EN BIT(0)
54#define SUN6I_ALRM_IRQ_EN 0x002c
55#define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
56#define SUN6I_ALRM_IRQ_STA 0x0030
57#define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
58
59/* Alarm 1 (wall clock) */
60#define SUN6I_ALRM1_EN 0x0044
61#define SUN6I_ALRM1_IRQ_EN 0x0048
62#define SUN6I_ALRM1_IRQ_STA 0x004c
63#define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND BIT(0)
64
65/* Alarm config */
66#define SUN6I_ALARM_CONFIG 0x0050
67#define SUN6I_ALARM_CONFIG_WAKEUP BIT(0)
68
69/*
70 * Get date values
71 */
72#define SUN6I_DATE_GET_DAY_VALUE(x) ((x) & 0x0000001f)
73#define SUN6I_DATE_GET_MON_VALUE(x) (((x) & 0x00000f00) >> 8)
74#define SUN6I_DATE_GET_YEAR_VALUE(x) (((x) & 0x003f0000) >> 16)
75#define SUN6I_LEAP_GET_VALUE(x) (((x) & 0x00400000) >> 22)
76
77/*
78 * Get time values
79 */
80#define SUN6I_TIME_GET_SEC_VALUE(x) ((x) & 0x0000003f)
81#define SUN6I_TIME_GET_MIN_VALUE(x) (((x) & 0x00003f00) >> 8)
82#define SUN6I_TIME_GET_HOUR_VALUE(x) (((x) & 0x001f0000) >> 16)
83
84/*
85 * Set date values
86 */
87#define SUN6I_DATE_SET_DAY_VALUE(x) ((x) & 0x0000001f)
88#define SUN6I_DATE_SET_MON_VALUE(x) ((x) << 8 & 0x00000f00)
89#define SUN6I_DATE_SET_YEAR_VALUE(x) ((x) << 16 & 0x003f0000)
90#define SUN6I_LEAP_SET_VALUE(x) ((x) << 22 & 0x00400000)
91
92/*
93 * Set time values
94 */
95#define SUN6I_TIME_SET_SEC_VALUE(x) ((x) & 0x0000003f)
96#define SUN6I_TIME_SET_MIN_VALUE(x) ((x) << 8 & 0x00003f00)
97#define SUN6I_TIME_SET_HOUR_VALUE(x) ((x) << 16 & 0x001f0000)
98
99/*
100 * The year parameter passed to the driver is usually an offset relative to
101 * the year 1900. This macro is used to convert this offset to another one
102 * relative to the minimum year allowed by the hardware.
103 *
104 * The year range is 1970 - 2033. This range is selected to match Allwinner's
105 * driver, even though it is somewhat limited.
106 */
107#define SUN6I_YEAR_MIN 1970
108#define SUN6I_YEAR_MAX 2033
109#define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900)
110
111struct sun6i_rtc_dev {
112 struct rtc_device *rtc;
113 struct device *dev;
114 void __iomem *base;
115 int irq;
116 unsigned long alarm;
117};
118
119static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
120{
121 struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
122 u32 val;
123
124 val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
125
126 if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
127 val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
128 writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
129
130 rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
131
132 return IRQ_HANDLED;
133 }
134
135 return IRQ_NONE;
136}
137
138static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
139{
140 u32 alrm_val = 0;
141 u32 alrm_irq_val = 0;
142 u32 alrm_wake_val = 0;
143
144 if (to) {
145 alrm_val = SUN6I_ALRM_EN_CNT_EN;
146 alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
147 alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
148 } else {
149 writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
150 chip->base + SUN6I_ALRM_IRQ_STA);
151 }
152
153 writel(alrm_val, chip->base + SUN6I_ALRM_EN);
154 writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
155 writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
156}
157
158static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
159{
160 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
161 u32 date, time;
162
163 /*
164 * read again in case it changes
165 */
166 do {
167 date = readl(chip->base + SUN6I_RTC_YMD);
168 time = readl(chip->base + SUN6I_RTC_HMS);
169 } while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
170 (time != readl(chip->base + SUN6I_RTC_HMS)));
171
172 rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time);
173 rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time);
174 rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
175
176 rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
177 rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date);
178 rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
179
180 rtc_tm->tm_mon -= 1;
181
182 /*
183 * switch from (data_year->min)-relative offset to
184 * a (1900)-relative one
185 */
186 rtc_tm->tm_year += SUN6I_YEAR_OFF;
187
188 return rtc_valid_tm(rtc_tm);
189}
190
191static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
192{
193 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
194 u32 alrm_st;
195 u32 alrm_en;
196
197 alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
198 alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
199 wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
200 wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
201 rtc_time_to_tm(chip->alarm, &wkalrm->time);
202
203 return 0;
204}
205
206static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
207{
208 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
209 struct rtc_time *alrm_tm = &wkalrm->time;
210 struct rtc_time tm_now;
211 unsigned long time_now = 0;
212 unsigned long time_set = 0;
213 unsigned long time_gap = 0;
214 int ret = 0;
215
216 ret = sun6i_rtc_gettime(dev, &tm_now);
217 if (ret < 0) {
218 dev_err(dev, "Error in getting time\n");
219 return -EINVAL;
220 }
221
222 rtc_tm_to_time(alrm_tm, &time_set);
223 rtc_tm_to_time(&tm_now, &time_now);
224 if (time_set <= time_now) {
225 dev_err(dev, "Date to set in the past\n");
226 return -EINVAL;
227 }
228
229 time_gap = time_set - time_now;
230
231 if (time_gap > U32_MAX) {
232 dev_err(dev, "Date too far in the future\n");
233 return -EINVAL;
234 }
235
236 sun6i_rtc_setaie(0, chip);
237 writel(0, chip->base + SUN6I_ALRM_COUNTER);
238 usleep_range(100, 300);
239
240 writel(time_gap, chip->base + SUN6I_ALRM_COUNTER);
241 chip->alarm = time_set;
242
243 sun6i_rtc_setaie(wkalrm->enabled, chip);
244
245 return 0;
246}
247
248static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
249 unsigned int mask, unsigned int ms_timeout)
250{
251 const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
252 u32 reg;
253
254 do {
255 reg = readl(chip->base + offset);
256 reg &= mask;
257
258 if (!reg)
259 return 0;
260
261 } while (time_before(jiffies, timeout));
262
263 return -ETIMEDOUT;
264}
265
266static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
267{
268 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
269 u32 date = 0;
270 u32 time = 0;
271 int year;
272
273 year = rtc_tm->tm_year + 1900;
274 if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) {
275 dev_err(dev, "rtc only supports year in range %d - %d\n",
276 SUN6I_YEAR_MIN, SUN6I_YEAR_MAX);
277 return -EINVAL;
278 }
279
280 rtc_tm->tm_year -= SUN6I_YEAR_OFF;
281 rtc_tm->tm_mon += 1;
282
283 date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
284 SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
285 SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
286
287 if (is_leap_year(year))
288 date |= SUN6I_LEAP_SET_VALUE(1);
289
290 time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
291 SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
292 SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
293
294 /* Check whether registers are writable */
295 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
296 SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
297 dev_err(dev, "rtc is still busy.\n");
298 return -EBUSY;
299 }
300
301 writel(time, chip->base + SUN6I_RTC_HMS);
302
303 /*
304 * After writing the RTC HH-MM-SS register, the
305 * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
306 * be cleared until the real writing operation is finished
307 */
308
309 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
310 SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
311 dev_err(dev, "Failed to set rtc time.\n");
312 return -ETIMEDOUT;
313 }
314
315 writel(date, chip->base + SUN6I_RTC_YMD);
316
317 /*
318 * After writing the RTC YY-MM-DD register, the
319 * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
320 * be cleared until the real writing operation is finished
321 */
322
323 if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
324 SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
325 dev_err(dev, "Failed to set rtc time.\n");
326 return -ETIMEDOUT;
327 }
328
329 return 0;
330}
331
332static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
333{
334 struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
335
336 if (!enabled)
337 sun6i_rtc_setaie(enabled, chip);
338
339 return 0;
340}
341
342static const struct rtc_class_ops sun6i_rtc_ops = {
343 .read_time = sun6i_rtc_gettime,
344 .set_time = sun6i_rtc_settime,
345 .read_alarm = sun6i_rtc_getalarm,
346 .set_alarm = sun6i_rtc_setalarm,
347 .alarm_irq_enable = sun6i_rtc_alarm_irq_enable
348};
349
350static int sun6i_rtc_probe(struct platform_device *pdev)
351{
352 struct sun6i_rtc_dev *chip;
353 struct resource *res;
354 int ret;
355
356 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
357 if (!chip)
358 return -ENOMEM;
359
360 platform_set_drvdata(pdev, chip);
361 chip->dev = &pdev->dev;
362
363 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
364 chip->base = devm_ioremap_resource(&pdev->dev, res);
365 if (IS_ERR(chip->base))
366 return PTR_ERR(chip->base);
367
368 chip->irq = platform_get_irq(pdev, 0);
369 if (chip->irq < 0) {
370 dev_err(&pdev->dev, "No IRQ resource\n");
371 return chip->irq;
372 }
373
374 ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
375 0, dev_name(&pdev->dev), chip);
376 if (ret) {
377 dev_err(&pdev->dev, "Could not request IRQ\n");
378 return ret;
379 }
380
381 /* clear the alarm counter value */
382 writel(0, chip->base + SUN6I_ALRM_COUNTER);
383
384 /* disable counter alarm */
385 writel(0, chip->base + SUN6I_ALRM_EN);
386
387 /* disable counter alarm interrupt */
388 writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
389
390 /* disable week alarm */
391 writel(0, chip->base + SUN6I_ALRM1_EN);
392
393 /* disable week alarm interrupt */
394 writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
395
396 /* clear counter alarm pending interrupts */
397 writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
398 chip->base + SUN6I_ALRM_IRQ_STA);
399
400 /* clear week alarm pending interrupts */
401 writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
402 chip->base + SUN6I_ALRM1_IRQ_STA);
403
404 /* disable alarm wakeup */
405 writel(0, chip->base + SUN6I_ALARM_CONFIG);
406
407 chip->rtc = rtc_device_register("rtc-sun6i", &pdev->dev,
408 &sun6i_rtc_ops, THIS_MODULE);
409 if (IS_ERR(chip->rtc)) {
410 dev_err(&pdev->dev, "unable to register device\n");
411 return PTR_ERR(chip->rtc);
412 }
413
414 dev_info(&pdev->dev, "RTC enabled\n");
415
416 return 0;
417}
418
419static int sun6i_rtc_remove(struct platform_device *pdev)
420{
421 struct sun6i_rtc_dev *chip = platform_get_drvdata(pdev);
422
423 rtc_device_unregister(chip->rtc);
424
425 return 0;
426}
427
428static const struct of_device_id sun6i_rtc_dt_ids[] = {
429 { .compatible = "allwinner,sun6i-a31-rtc" },
430 { /* sentinel */ },
431};
432MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
433
434static struct platform_driver sun6i_rtc_driver = {
435 .probe = sun6i_rtc_probe,
436 .remove = sun6i_rtc_remove,
437 .driver = {
438 .name = "sun6i-rtc",
439 .of_match_table = sun6i_rtc_dt_ids,
440 },
441};
442
443module_platform_driver(sun6i_rtc_driver);
444
445MODULE_DESCRIPTION("sun6i RTC driver");
446MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
447MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-tps65910.c b/drivers/rtc/rtc-tps65910.c
index 7af00208d637..2583349fbde5 100644
--- a/drivers/rtc/rtc-tps65910.c
+++ b/drivers/rtc/rtc-tps65910.c
@@ -258,6 +258,8 @@ static int tps65910_rtc_probe(struct platform_device *pdev)
258 if (ret < 0) 258 if (ret < 0)
259 return ret; 259 return ret;
260 260
261 platform_set_drvdata(pdev, tps_rtc);
262
261 irq = platform_get_irq(pdev, 0); 263 irq = platform_get_irq(pdev, 0);
262 if (irq <= 0) { 264 if (irq <= 0) {
263 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n", 265 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
@@ -283,8 +285,6 @@ static int tps65910_rtc_probe(struct platform_device *pdev)
283 return ret; 285 return ret;
284 } 286 }
285 287
286 platform_set_drvdata(pdev, tps_rtc);
287
288 return 0; 288 return 0;
289} 289}
290 290