aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig122
-rw-r--r--drivers/rtc/Makefile11
-rw-r--r--drivers/rtc/interface.c2
-rw-r--r--drivers/rtc/rtc-at91rm9200.c46
-rw-r--r--drivers/rtc/rtc-bq4802.c230
-rw-r--r--drivers/rtc/rtc-cmos.c218
-rw-r--r--drivers/rtc/rtc-dev.c15
-rw-r--r--drivers/rtc/rtc-ds1216.c26
-rw-r--r--drivers/rtc/rtc-ds1286.c410
-rw-r--r--drivers/rtc/rtc-ds1302.c30
-rw-r--r--drivers/rtc/rtc-ds1305.c39
-rw-r--r--drivers/rtc/rtc-ds1307.c349
-rw-r--r--drivers/rtc/rtc-ds1374.c21
-rw-r--r--drivers/rtc/rtc-ds1390.c220
-rw-r--r--drivers/rtc/rtc-ds1511.c56
-rw-r--r--drivers/rtc/rtc-ds1553.c50
-rw-r--r--drivers/rtc/rtc-ds1672.c114
-rw-r--r--drivers/rtc/rtc-ds1742.c30
-rw-r--r--drivers/rtc/rtc-ds3234.c290
-rw-r--r--drivers/rtc/rtc-fm3130.c56
-rw-r--r--drivers/rtc/rtc-isl1208.c42
-rw-r--r--drivers/rtc/rtc-m41t80.c87
-rw-r--r--drivers/rtc/rtc-m41t94.c28
-rw-r--r--drivers/rtc/rtc-m48t35.c235
-rw-r--r--drivers/rtc/rtc-m48t59.c147
-rw-r--r--drivers/rtc/rtc-m48t86.c28
-rw-r--r--drivers/rtc/rtc-max6900.c253
-rw-r--r--drivers/rtc/rtc-max6902.c32
-rw-r--r--drivers/rtc/rtc-omap.c24
-rw-r--r--drivers/rtc/rtc-parisc.c111
-rw-r--r--drivers/rtc/rtc-pcf8563.c82
-rw-r--r--drivers/rtc/rtc-pcf8583.c20
-rw-r--r--drivers/rtc/rtc-pl030.c11
-rw-r--r--drivers/rtc/rtc-pl031.c14
-rw-r--r--drivers/rtc/rtc-r9701.c24
-rw-r--r--drivers/rtc/rtc-rs5c313.c28
-rw-r--r--drivers/rtc/rtc-rs5c348.c30
-rw-r--r--drivers/rtc/rtc-rs5c372.c256
-rw-r--r--drivers/rtc/rtc-rx8581.c281
-rw-r--r--drivers/rtc/rtc-s35390a.c34
-rw-r--r--drivers/rtc/rtc-s3c.c50
-rw-r--r--drivers/rtc/rtc-sh.c68
-rw-r--r--drivers/rtc/rtc-starfire.c120
-rw-r--r--drivers/rtc/rtc-stk17ta8.c51
-rw-r--r--drivers/rtc/rtc-sun4v.c153
-rw-r--r--drivers/rtc/rtc-twl4030.c564
-rw-r--r--drivers/rtc/rtc-v3020.c28
-rw-r--r--drivers/rtc/rtc-vr41xx.c4
-rw-r--r--drivers/rtc/rtc-wm8350.c514
-rw-r--r--drivers/rtc/rtc-x1205.c30
50 files changed, 4635 insertions, 1049 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 9a9755c92fad..123092d8a984 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -171,10 +171,10 @@ config RTC_DRV_MAX6900
171 will be called rtc-max6900. 171 will be called rtc-max6900.
172 172
173config RTC_DRV_RS5C372 173config RTC_DRV_RS5C372
174 tristate "Ricoh RS5C372A/B, RV5C386, RV5C387A" 174 tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A"
175 help 175 help
176 If you say yes here you get support for the 176 If you say yes here you get support for the
177 Ricoh RS5C372A, RS5C372B, RV5C386, and RV5C387A RTC chips. 177 Ricoh R2025S/D, RS5C372A, RS5C372B, RV5C386, and RV5C387A RTC chips.
178 178
179 This driver can also be built as a module. If so, the module 179 This driver can also be built as a module. If so, the module
180 will be called rtc-rs5c372. 180 will be called rtc-rs5c372.
@@ -220,22 +220,22 @@ config RTC_DRV_PCF8583
220 will be called rtc-pcf8583. 220 will be called rtc-pcf8583.
221 221
222config RTC_DRV_M41T80 222config RTC_DRV_M41T80
223 tristate "ST M41T80/81/82/83/84/85/87" 223 tristate "ST M41T65/M41T80/81/82/83/84/85/87"
224 help 224 help
225 If you say Y here you will get support for the 225 If you say Y here you will get support for the ST M41T60
226 ST M41T80 RTC chips series. Currently following chips are 226 and M41T80 RTC chips series. Currently, the following chips are
227 supported: M41T80, M41T81, M41T82, M41T83, M41ST84, M41ST85 227 supported: M41T65, M41T80, M41T81, M41T82, M41T83, M41ST84,
228 and M41ST87. 228 M41ST85, and M41ST87.
229 229
230 This driver can also be built as a module. If so, the module 230 This driver can also be built as a module. If so, the module
231 will be called rtc-m41t80. 231 will be called rtc-m41t80.
232 232
233config RTC_DRV_M41T80_WDT 233config RTC_DRV_M41T80_WDT
234 bool "ST M41T80 series RTC watchdog timer" 234 bool "ST M41T65/M41T80 series RTC watchdog timer"
235 depends on RTC_DRV_M41T80 235 depends on RTC_DRV_M41T80
236 help 236 help
237 If you say Y here you will get support for the 237 If you say Y here you will get support for the
238 watchdog timer in ST M41T80 RTC chips series. 238 watchdog timer in the ST M41T60 and M41T80 RTC chips series.
239 239
240config RTC_DRV_TWL92330 240config RTC_DRV_TWL92330
241 boolean "TI TWL92330/Menelaus" 241 boolean "TI TWL92330/Menelaus"
@@ -246,6 +246,16 @@ config RTC_DRV_TWL92330
246 platforms. The support is integrated with the rest of 246 platforms. The support is integrated with the rest of
247 the Menelaus driver; it's not separate module. 247 the Menelaus driver; it's not separate module.
248 248
249config RTC_DRV_TWL4030
250 tristate "TI TWL4030/TWL5030/TPS659x0"
251 depends on RTC_CLASS && TWL4030_CORE
252 help
253 If you say yes here you get support for the RTC on the
254 TWL4030 family chips, used mostly with OMAP3 platforms.
255
256 This driver can also be built as a module. If so, the module
257 will be called rtc-twl4030.
258
249config RTC_DRV_S35390A 259config RTC_DRV_S35390A
250 tristate "Seiko Instruments S-35390A" 260 tristate "Seiko Instruments S-35390A"
251 select BITREVERSE 261 select BITREVERSE
@@ -267,6 +277,14 @@ config RTC_DRV_FM3130
267 This driver can also be built as a module. If so the module 277 This driver can also be built as a module. If so the module
268 will be called rtc-fm3130. 278 will be called rtc-fm3130.
269 279
280config RTC_DRV_RX8581
281 tristate "Epson RX-8581"
282 help
283 If you say yes here you will get support for the Epson RX-8581.
284
285 This driver can also be built as a module. If so the module
286 will be called rtc-rx8581.
287
270endif # I2C 288endif # I2C
271 289
272comment "SPI RTC drivers" 290comment "SPI RTC drivers"
@@ -292,6 +310,17 @@ config RTC_DRV_DS1305
292 This driver can also be built as a module. If so, the module 310 This driver can also be built as a module. If so, the module
293 will be called rtc-ds1305. 311 will be called rtc-ds1305.
294 312
313config RTC_DRV_DS1390
314 tristate "Dallas/Maxim DS1390/93/94"
315 help
316 If you say yes here you get support for the DS1390/93/94 chips.
317
318 This driver only supports the RTC feature, and not other chip
319 features such as alarms and trickle charging.
320
321 This driver can also be built as a module. If so, the module
322 will be called rtc-ds1390.
323
295config RTC_DRV_MAX6902 324config RTC_DRV_MAX6902
296 tristate "Maxim MAX6902" 325 tristate "Maxim MAX6902"
297 help 326 help
@@ -319,6 +348,15 @@ config RTC_DRV_RS5C348
319 This driver can also be built as a module. If so, the module 348 This driver can also be built as a module. If so, the module
320 will be called rtc-rs5c348. 349 will be called rtc-rs5c348.
321 350
351config RTC_DRV_DS3234
352 tristate "Maxim/Dallas DS3234"
353 help
354 If you say yes here you get support for the
355 Maxim/Dallas DS3234 SPI RTC chip.
356
357 This driver can also be built as a module. If so, the module
358 will be called rtc-ds3234.
359
322endif # SPI_MASTER 360endif # SPI_MASTER
323 361
324comment "Platform RTC drivers" 362comment "Platform RTC drivers"
@@ -329,7 +367,7 @@ comment "Platform RTC drivers"
329 367
330config RTC_DRV_CMOS 368config RTC_DRV_CMOS
331 tristate "PC-style 'CMOS'" 369 tristate "PC-style 'CMOS'"
332 depends on X86 || ALPHA || ARM || M32R || ATARI || PPC || MIPS 370 depends on X86 || ALPHA || ARM || M32R || ATARI || PPC || MIPS || SPARC64
333 default y if X86 371 default y if X86
334 help 372 help
335 Say "yes" here to get direct support for the real time clock 373 Say "yes" here to get direct support for the real time clock
@@ -352,6 +390,11 @@ config RTC_DRV_DS1216
352 help 390 help
353 If you say yes here you get support for the Dallas DS1216 RTC chips. 391 If you say yes here you get support for the Dallas DS1216 RTC chips.
354 392
393config RTC_DRV_DS1286
394 tristate "Dallas DS1286"
395 help
396 If you say yes here you get support for the Dallas DS1286 RTC chips.
397
355config RTC_DRV_DS1302 398config RTC_DRV_DS1302
356 tristate "Dallas DS1302" 399 tristate "Dallas DS1302"
357 depends on SH_SECUREEDGE5410 400 depends on SH_SECUREEDGE5410
@@ -405,15 +448,36 @@ config RTC_DRV_M48T86
405 This driver can also be built as a module. If so, the module 448 This driver can also be built as a module. If so, the module
406 will be called rtc-m48t86. 449 will be called rtc-m48t86.
407 450
451config RTC_DRV_M48T35
452 tristate "ST M48T35"
453 help
454 If you say Y here you will get support for the
455 ST M48T35 RTC chip.
456
457 This driver can also be built as a module, if so, the module
458 will be called "rtc-m48t35".
459
408config RTC_DRV_M48T59 460config RTC_DRV_M48T59
409 tristate "ST M48T59" 461 tristate "ST M48T59/M48T08/M48T02"
410 help 462 help
411 If you say Y here you will get support for the 463 If you say Y here you will get support for the
412 ST M48T59 RTC chip. 464 ST M48T59 RTC chip and compatible ST M48T08 and M48T02.
465
466 These chips are usually found in Sun SPARC and UltraSPARC
467 workstations.
413 468
414 This driver can also be built as a module, if so, the module 469 This driver can also be built as a module, if so, the module
415 will be called "rtc-m48t59". 470 will be called "rtc-m48t59".
416 471
472config RTC_DRV_BQ4802
473 tristate "TI BQ4802"
474 help
475 If you say Y here you will get support for the TI
476 BQ4802 RTC chip.
477
478 This driver can also be built as a module. If so, the module
479 will be called rtc-bq4802.
480
417config RTC_DRV_V3020 481config RTC_DRV_V3020
418 tristate "EM Microelectronic V3020" 482 tristate "EM Microelectronic V3020"
419 help 483 help
@@ -423,6 +487,16 @@ config RTC_DRV_V3020
423 This driver can also be built as a module. If so, the module 487 This driver can also be built as a module. If so, the module
424 will be called rtc-v3020. 488 will be called rtc-v3020.
425 489
490config RTC_DRV_WM8350
491 tristate "Wolfson Microelectronics WM8350 RTC"
492 depends on MFD_WM8350
493 help
494 If you say yes here you will get support for the RTC subsystem
495 of the Wolfson Microelectronics WM8350.
496
497 This driver can also be built as a module. If so, the module
498 will be called "rtc-wm8350".
499
426comment "on-CPU RTC drivers" 500comment "on-CPU RTC drivers"
427 501
428config RTC_DRV_OMAP 502config RTC_DRV_OMAP
@@ -575,12 +649,34 @@ config RTC_DRV_RS5C313
575 help 649 help
576 If you say yes here you get support for the Ricoh RS5C313 RTC chips. 650 If you say yes here you get support for the Ricoh RS5C313 RTC chips.
577 651
652config RTC_DRV_PARISC
653 tristate "PA-RISC firmware RTC support"
654 depends on PARISC
655 help
656 Say Y or M here to enable RTC support on PA-RISC systems using
657 firmware calls. If you do not know what you are doing, you should
658 just say Y.
659
578config RTC_DRV_PPC 660config RTC_DRV_PPC
579 tristate "PowerPC machine dependent RTC support" 661 tristate "PowerPC machine dependent RTC support"
580 depends on PPC_MERGE 662 depends on PPC
581 help 663 help
582 The PowerPC kernel has machine-specific functions for accessing 664 The PowerPC kernel has machine-specific functions for accessing
583 the RTC. This exposes that functionality through the generic RTC 665 the RTC. This exposes that functionality through the generic RTC
584 class. 666 class.
585 667
668config RTC_DRV_SUN4V
669 bool "SUN4V Hypervisor RTC"
670 depends on SPARC64
671 help
672 If you say Y here you will get support for the Hypervisor
673 based RTC on SUN4V systems.
674
675config RTC_DRV_STARFIRE
676 bool "Starfire RTC"
677 depends on SPARC64
678 help
679 If you say Y here you will get support for the RTC found on
680 Starfire systems.
681
586endif # RTC_CLASS 682endif # RTC_CLASS
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 18622ef84cab..6e79c912bf9e 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -23,21 +23,28 @@ obj-$(CONFIG_RTC_DRV_AT91SAM9) += rtc-at91sam9.o
23obj-$(CONFIG_RTC_DRV_BFIN) += rtc-bfin.o 23obj-$(CONFIG_RTC_DRV_BFIN) += rtc-bfin.o
24obj-$(CONFIG_RTC_DRV_CMOS) += rtc-cmos.o 24obj-$(CONFIG_RTC_DRV_CMOS) += rtc-cmos.o
25obj-$(CONFIG_RTC_DRV_DS1216) += rtc-ds1216.o 25obj-$(CONFIG_RTC_DRV_DS1216) += rtc-ds1216.o
26obj-$(CONFIG_RTC_DRV_DS1286) += rtc-ds1286.o
26obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o 27obj-$(CONFIG_RTC_DRV_DS1302) += rtc-ds1302.o
27obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o 28obj-$(CONFIG_RTC_DRV_DS1305) += rtc-ds1305.o
28obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o 29obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o
29obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o 30obj-$(CONFIG_RTC_DRV_DS1374) += rtc-ds1374.o
31obj-$(CONFIG_RTC_DRV_DS1390) += rtc-ds1390.o
30obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o 32obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o
31obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o 33obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o
32obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o 34obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o
33obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o 35obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o
36obj-$(CONFIG_RTC_DRV_DS3234) += rtc-ds3234.o
34obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o 37obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o
35obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o 38obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o
36obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o 39obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
37obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o 40obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o
38obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o 41obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o
42obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
39obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o 43obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o
40obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o 44obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o
45obj-$(CONFIG_RTC_DRV_BQ4802) += rtc-bq4802.o
46obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o
47obj-$(CONFIG_RTC_DRV_STARFIRE) += rtc-starfire.o
41obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o 48obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o
42obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o 49obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o
43obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o 50obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o
@@ -45,17 +52,21 @@ obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o
45obj-$(CONFIG_RTC_DRV_PCF8583) += rtc-pcf8583.o 52obj-$(CONFIG_RTC_DRV_PCF8583) += rtc-pcf8583.o
46obj-$(CONFIG_RTC_DRV_PL030) += rtc-pl030.o 53obj-$(CONFIG_RTC_DRV_PL030) += rtc-pl030.o
47obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o 54obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o
55obj-$(CONFIG_RTC_DRV_PARISC) += rtc-parisc.o
48obj-$(CONFIG_RTC_DRV_PPC) += rtc-ppc.o 56obj-$(CONFIG_RTC_DRV_PPC) += rtc-ppc.o
49obj-$(CONFIG_RTC_DRV_R9701) += rtc-r9701.o 57obj-$(CONFIG_RTC_DRV_R9701) += rtc-r9701.o
50obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o 58obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o
51obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o 59obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o
52obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o 60obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o
61obj-$(CONFIG_RTC_DRV_RX8581) += rtc-rx8581.o
53obj-$(CONFIG_RTC_DRV_S35390A) += rtc-s35390a.o 62obj-$(CONFIG_RTC_DRV_S35390A) += rtc-s35390a.o
54obj-$(CONFIG_RTC_DRV_S3C) += rtc-s3c.o 63obj-$(CONFIG_RTC_DRV_S3C) += rtc-s3c.o
55obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o 64obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o
56obj-$(CONFIG_RTC_DRV_SH) += rtc-sh.o 65obj-$(CONFIG_RTC_DRV_SH) += rtc-sh.o
57obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o 66obj-$(CONFIG_RTC_DRV_STK17TA8) += rtc-stk17ta8.o
58obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o 67obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
68obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl4030.o
59obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o 69obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o
60obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o 70obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o
71obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o
61obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o 72obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 7af60b98d8a4..a04c1b6b1575 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -271,7 +271,7 @@ int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
271 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); 271 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
272 do { 272 do {
273 alarm->time.tm_year++; 273 alarm->time.tm_year++;
274 } while (!rtc_valid_tm(&alarm->time)); 274 } while (rtc_valid_tm(&alarm->time) != 0);
275 break; 275 break;
276 276
277 default: 277 default:
diff --git a/drivers/rtc/rtc-at91rm9200.c b/drivers/rtc/rtc-at91rm9200.c
index 4e888cc8be5b..b5bf93706913 100644
--- a/drivers/rtc/rtc-at91rm9200.c
+++ b/drivers/rtc/rtc-at91rm9200.c
@@ -29,10 +29,10 @@
29#include <linux/completion.h> 29#include <linux/completion.h>
30 30
31#include <asm/uaccess.h> 31#include <asm/uaccess.h>
32
32#include <mach/at91_rtc.h> 33#include <mach/at91_rtc.h>
33 34
34 35
35#define AT91_RTC_FREQ 1
36#define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */ 36#define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */
37 37
38static DECLARE_COMPLETION(at91_rtc_updated); 38static DECLARE_COMPLETION(at91_rtc_updated);
@@ -53,21 +53,21 @@ static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
53 } while ((time != at91_sys_read(timereg)) || 53 } while ((time != at91_sys_read(timereg)) ||
54 (date != at91_sys_read(calreg))); 54 (date != at91_sys_read(calreg)));
55 55
56 tm->tm_sec = BCD2BIN((time & AT91_RTC_SEC) >> 0); 56 tm->tm_sec = bcd2bin((time & AT91_RTC_SEC) >> 0);
57 tm->tm_min = BCD2BIN((time & AT91_RTC_MIN) >> 8); 57 tm->tm_min = bcd2bin((time & AT91_RTC_MIN) >> 8);
58 tm->tm_hour = BCD2BIN((time & AT91_RTC_HOUR) >> 16); 58 tm->tm_hour = bcd2bin((time & AT91_RTC_HOUR) >> 16);
59 59
60 /* 60 /*
61 * The Calendar Alarm register does not have a field for 61 * The Calendar Alarm register does not have a field for
62 * the year - so these will return an invalid value. When an 62 * the year - so these will return an invalid value. When an
63 * alarm is set, at91_alarm_year wille store the current year. 63 * alarm is set, at91_alarm_year wille store the current year.
64 */ 64 */
65 tm->tm_year = BCD2BIN(date & AT91_RTC_CENT) * 100; /* century */ 65 tm->tm_year = bcd2bin(date & AT91_RTC_CENT) * 100; /* century */
66 tm->tm_year += BCD2BIN((date & AT91_RTC_YEAR) >> 8); /* year */ 66 tm->tm_year += bcd2bin((date & AT91_RTC_YEAR) >> 8); /* year */
67 67
68 tm->tm_wday = BCD2BIN((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */ 68 tm->tm_wday = bcd2bin((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */
69 tm->tm_mon = BCD2BIN((date & AT91_RTC_MONTH) >> 16) - 1; 69 tm->tm_mon = bcd2bin((date & AT91_RTC_MONTH) >> 16) - 1;
70 tm->tm_mday = BCD2BIN((date & AT91_RTC_DATE) >> 24); 70 tm->tm_mday = bcd2bin((date & AT91_RTC_DATE) >> 24);
71} 71}
72 72
73/* 73/*
@@ -106,16 +106,16 @@ static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
106 at91_sys_write(AT91_RTC_IDR, AT91_RTC_ACKUPD); 106 at91_sys_write(AT91_RTC_IDR, AT91_RTC_ACKUPD);
107 107
108 at91_sys_write(AT91_RTC_TIMR, 108 at91_sys_write(AT91_RTC_TIMR,
109 BIN2BCD(tm->tm_sec) << 0 109 bin2bcd(tm->tm_sec) << 0
110 | BIN2BCD(tm->tm_min) << 8 110 | bin2bcd(tm->tm_min) << 8
111 | BIN2BCD(tm->tm_hour) << 16); 111 | bin2bcd(tm->tm_hour) << 16);
112 112
113 at91_sys_write(AT91_RTC_CALR, 113 at91_sys_write(AT91_RTC_CALR,
114 BIN2BCD((tm->tm_year + 1900) / 100) /* century */ 114 bin2bcd((tm->tm_year + 1900) / 100) /* century */
115 | BIN2BCD(tm->tm_year % 100) << 8 /* year */ 115 | bin2bcd(tm->tm_year % 100) << 8 /* year */
116 | BIN2BCD(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */ 116 | bin2bcd(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */
117 | BIN2BCD(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */ 117 | bin2bcd(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */
118 | BIN2BCD(tm->tm_mday) << 24); 118 | bin2bcd(tm->tm_mday) << 24);
119 119
120 /* Restart Time/Calendar */ 120 /* Restart Time/Calendar */
121 cr = at91_sys_read(AT91_RTC_CR); 121 cr = at91_sys_read(AT91_RTC_CR);
@@ -162,13 +162,13 @@ static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
162 162
163 at91_sys_write(AT91_RTC_IDR, AT91_RTC_ALARM); 163 at91_sys_write(AT91_RTC_IDR, AT91_RTC_ALARM);
164 at91_sys_write(AT91_RTC_TIMALR, 164 at91_sys_write(AT91_RTC_TIMALR,
165 BIN2BCD(tm.tm_sec) << 0 165 bin2bcd(tm.tm_sec) << 0
166 | BIN2BCD(tm.tm_min) << 8 166 | bin2bcd(tm.tm_min) << 8
167 | BIN2BCD(tm.tm_hour) << 16 167 | bin2bcd(tm.tm_hour) << 16
168 | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN); 168 | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
169 at91_sys_write(AT91_RTC_CALALR, 169 at91_sys_write(AT91_RTC_CALALR,
170 BIN2BCD(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */ 170 bin2bcd(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */
171 | BIN2BCD(tm.tm_mday) << 24 171 | bin2bcd(tm.tm_mday) << 24
172 | AT91_RTC_DATEEN | AT91_RTC_MTHEN); 172 | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
173 173
174 if (alrm->enabled) { 174 if (alrm->enabled) {
@@ -228,8 +228,6 @@ static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
228 (imr & AT91_RTC_ACKUPD) ? "yes" : "no"); 228 (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
229 seq_printf(seq, "periodic_IRQ\t: %s\n", 229 seq_printf(seq, "periodic_IRQ\t: %s\n",
230 (imr & AT91_RTC_SECEV) ? "yes" : "no"); 230 (imr & AT91_RTC_SECEV) ? "yes" : "no");
231 seq_printf(seq, "periodic_freq\t: %ld\n",
232 (unsigned long) AT91_RTC_FREQ);
233 231
234 return 0; 232 return 0;
235} 233}
diff --git a/drivers/rtc/rtc-bq4802.c b/drivers/rtc/rtc-bq4802.c
new file mode 100644
index 000000000000..d00a274df8fc
--- /dev/null
+++ b/drivers/rtc/rtc-bq4802.c
@@ -0,0 +1,230 @@
1/* rtc-bq4802.c: TI BQ4802 RTC driver.
2 *
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/io.h>
10#include <linux/platform_device.h>
11#include <linux/rtc.h>
12#include <linux/bcd.h>
13
14MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
15MODULE_DESCRIPTION("TI BQ4802 RTC driver");
16MODULE_LICENSE("GPL");
17
18struct bq4802 {
19 void __iomem *regs;
20 unsigned long ioport;
21 struct rtc_device *rtc;
22 spinlock_t lock;
23 struct resource *r;
24 u8 (*read)(struct bq4802 *, int);
25 void (*write)(struct bq4802 *, int, u8);
26};
27
28static u8 bq4802_read_io(struct bq4802 *p, int off)
29{
30 return inb(p->ioport + off);
31}
32
33static void bq4802_write_io(struct bq4802 *p, int off, u8 val)
34{
35 outb(val, p->ioport + off);
36}
37
38static u8 bq4802_read_mem(struct bq4802 *p, int off)
39{
40 return readb(p->regs + off);
41}
42
43static void bq4802_write_mem(struct bq4802 *p, int off, u8 val)
44{
45 writeb(val, p->regs + off);
46}
47
48static int bq4802_read_time(struct device *dev, struct rtc_time *tm)
49{
50 struct platform_device *pdev = to_platform_device(dev);
51 struct bq4802 *p = platform_get_drvdata(pdev);
52 unsigned long flags;
53 unsigned int century;
54 u8 val;
55
56 spin_lock_irqsave(&p->lock, flags);
57
58 val = p->read(p, 0x0e);
59 p->write(p, 0xe, val | 0x08);
60
61 tm->tm_sec = p->read(p, 0x00);
62 tm->tm_min = p->read(p, 0x02);
63 tm->tm_hour = p->read(p, 0x04);
64 tm->tm_mday = p->read(p, 0x06);
65 tm->tm_mon = p->read(p, 0x09);
66 tm->tm_year = p->read(p, 0x0a);
67 tm->tm_wday = p->read(p, 0x08);
68 century = p->read(p, 0x0f);
69
70 p->write(p, 0x0e, val);
71
72 spin_unlock_irqrestore(&p->lock, flags);
73
74 tm->tm_sec = bcd2bin(tm->tm_sec);
75 tm->tm_min = bcd2bin(tm->tm_min);
76 tm->tm_hour = bcd2bin(tm->tm_hour);
77 tm->tm_mday = bcd2bin(tm->tm_mday);
78 tm->tm_mon = bcd2bin(tm->tm_mon);
79 tm->tm_year = bcd2bin(tm->tm_year);
80 tm->tm_wday = bcd2bin(tm->tm_wday);
81 century = bcd2bin(century);
82
83 tm->tm_year += (century * 100);
84 tm->tm_year -= 1900;
85
86 tm->tm_mon--;
87
88 return 0;
89}
90
91static int bq4802_set_time(struct device *dev, struct rtc_time *tm)
92{
93 struct platform_device *pdev = to_platform_device(dev);
94 struct bq4802 *p = platform_get_drvdata(pdev);
95 u8 sec, min, hrs, day, mon, yrs, century, val;
96 unsigned long flags;
97 unsigned int year;
98
99 year = tm->tm_year + 1900;
100 century = year / 100;
101 yrs = year % 100;
102
103 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
104 day = tm->tm_mday;
105 hrs = tm->tm_hour;
106 min = tm->tm_min;
107 sec = tm->tm_sec;
108
109 sec = bin2bcd(sec);
110 min = bin2bcd(min);
111 hrs = bin2bcd(hrs);
112 day = bin2bcd(day);
113 mon = bin2bcd(mon);
114 yrs = bin2bcd(yrs);
115 century = bin2bcd(century);
116
117 spin_lock_irqsave(&p->lock, flags);
118
119 val = p->read(p, 0x0e);
120 p->write(p, 0x0e, val | 0x08);
121
122 p->write(p, 0x00, sec);
123 p->write(p, 0x02, min);
124 p->write(p, 0x04, hrs);
125 p->write(p, 0x06, day);
126 p->write(p, 0x09, mon);
127 p->write(p, 0x0a, yrs);
128 p->write(p, 0x0f, century);
129
130 p->write(p, 0x0e, val);
131
132 spin_unlock_irqrestore(&p->lock, flags);
133
134 return 0;
135}
136
137static const struct rtc_class_ops bq4802_ops = {
138 .read_time = bq4802_read_time,
139 .set_time = bq4802_set_time,
140};
141
142static int __devinit bq4802_probe(struct platform_device *pdev)
143{
144 struct bq4802 *p = kzalloc(sizeof(*p), GFP_KERNEL);
145 int err = -ENOMEM;
146
147 if (!p)
148 goto out;
149
150 spin_lock_init(&p->lock);
151
152 p->r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 if (!p->r) {
154 p->r = platform_get_resource(pdev, IORESOURCE_IO, 0);
155 err = -EINVAL;
156 if (!p->r)
157 goto out_free;
158 }
159 if (p->r->flags & IORESOURCE_IO) {
160 p->ioport = p->r->start;
161 p->read = bq4802_read_io;
162 p->write = bq4802_write_io;
163 } else if (p->r->flags & IORESOURCE_MEM) {
164 p->regs = ioremap(p->r->start, resource_size(p->r));
165 p->read = bq4802_read_mem;
166 p->write = bq4802_write_mem;
167 } else {
168 err = -EINVAL;
169 goto out_free;
170 }
171
172 p->rtc = rtc_device_register("bq4802", &pdev->dev,
173 &bq4802_ops, THIS_MODULE);
174 if (IS_ERR(p->rtc)) {
175 err = PTR_ERR(p->rtc);
176 goto out_iounmap;
177 }
178
179 platform_set_drvdata(pdev, p);
180 err = 0;
181out:
182 return err;
183
184out_iounmap:
185 if (p->r->flags & IORESOURCE_MEM)
186 iounmap(p->regs);
187out_free:
188 kfree(p);
189 goto out;
190}
191
192static int __devexit bq4802_remove(struct platform_device *pdev)
193{
194 struct bq4802 *p = platform_get_drvdata(pdev);
195
196 rtc_device_unregister(p->rtc);
197 if (p->r->flags & IORESOURCE_MEM)
198 iounmap(p->regs);
199
200 platform_set_drvdata(pdev, NULL);
201
202 kfree(p);
203
204 return 0;
205}
206
207/* work with hotplug and coldplug */
208MODULE_ALIAS("platform:rtc-bq4802");
209
210static struct platform_driver bq4802_driver = {
211 .driver = {
212 .name = "rtc-bq4802",
213 .owner = THIS_MODULE,
214 },
215 .probe = bq4802_probe,
216 .remove = __devexit_p(bq4802_remove),
217};
218
219static int __init bq4802_init(void)
220{
221 return platform_driver_register(&bq4802_driver);
222}
223
224static void __exit bq4802_exit(void)
225{
226 platform_driver_unregister(&bq4802_driver);
227}
228
229module_init(bq4802_init);
230module_exit(bq4802_exit);
diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index b184367637d0..6cf8e282338f 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -143,6 +143,43 @@ static inline int hpet_unregister_irq_handler(irq_handler_t handler)
143 143
144/*----------------------------------------------------------------*/ 144/*----------------------------------------------------------------*/
145 145
146#ifdef RTC_PORT
147
148/* Most newer x86 systems have two register banks, the first used
149 * for RTC and NVRAM and the second only for NVRAM. Caller must
150 * own rtc_lock ... and we won't worry about access during NMI.
151 */
152#define can_bank2 true
153
154static inline unsigned char cmos_read_bank2(unsigned char addr)
155{
156 outb(addr, RTC_PORT(2));
157 return inb(RTC_PORT(3));
158}
159
160static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
161{
162 outb(addr, RTC_PORT(2));
163 outb(val, RTC_PORT(2));
164}
165
166#else
167
168#define can_bank2 false
169
170static inline unsigned char cmos_read_bank2(unsigned char addr)
171{
172 return 0;
173}
174
175static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
176{
177}
178
179#endif
180
181/*----------------------------------------------------------------*/
182
146static int cmos_read_time(struct device *dev, struct rtc_time *t) 183static int cmos_read_time(struct device *dev, struct rtc_time *t)
147{ 184{
148 /* REVISIT: if the clock has a "century" register, use 185 /* REVISIT: if the clock has a "century" register, use
@@ -203,26 +240,26 @@ static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
203 /* REVISIT this assumes PC style usage: always BCD */ 240 /* REVISIT this assumes PC style usage: always BCD */
204 241
205 if (((unsigned)t->time.tm_sec) < 0x60) 242 if (((unsigned)t->time.tm_sec) < 0x60)
206 t->time.tm_sec = BCD2BIN(t->time.tm_sec); 243 t->time.tm_sec = bcd2bin(t->time.tm_sec);
207 else 244 else
208 t->time.tm_sec = -1; 245 t->time.tm_sec = -1;
209 if (((unsigned)t->time.tm_min) < 0x60) 246 if (((unsigned)t->time.tm_min) < 0x60)
210 t->time.tm_min = BCD2BIN(t->time.tm_min); 247 t->time.tm_min = bcd2bin(t->time.tm_min);
211 else 248 else
212 t->time.tm_min = -1; 249 t->time.tm_min = -1;
213 if (((unsigned)t->time.tm_hour) < 0x24) 250 if (((unsigned)t->time.tm_hour) < 0x24)
214 t->time.tm_hour = BCD2BIN(t->time.tm_hour); 251 t->time.tm_hour = bcd2bin(t->time.tm_hour);
215 else 252 else
216 t->time.tm_hour = -1; 253 t->time.tm_hour = -1;
217 254
218 if (cmos->day_alrm) { 255 if (cmos->day_alrm) {
219 if (((unsigned)t->time.tm_mday) <= 0x31) 256 if (((unsigned)t->time.tm_mday) <= 0x31)
220 t->time.tm_mday = BCD2BIN(t->time.tm_mday); 257 t->time.tm_mday = bcd2bin(t->time.tm_mday);
221 else 258 else
222 t->time.tm_mday = -1; 259 t->time.tm_mday = -1;
223 if (cmos->mon_alrm) { 260 if (cmos->mon_alrm) {
224 if (((unsigned)t->time.tm_mon) <= 0x12) 261 if (((unsigned)t->time.tm_mon) <= 0x12)
225 t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1; 262 t->time.tm_mon = bcd2bin(t->time.tm_mon) - 1;
226 else 263 else
227 t->time.tm_mon = -1; 264 t->time.tm_mon = -1;
228 } 265 }
@@ -294,19 +331,19 @@ static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
294 /* Writing 0xff means "don't care" or "match all". */ 331 /* Writing 0xff means "don't care" or "match all". */
295 332
296 mon = t->time.tm_mon + 1; 333 mon = t->time.tm_mon + 1;
297 mon = (mon <= 12) ? BIN2BCD(mon) : 0xff; 334 mon = (mon <= 12) ? bin2bcd(mon) : 0xff;
298 335
299 mday = t->time.tm_mday; 336 mday = t->time.tm_mday;
300 mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff; 337 mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
301 338
302 hrs = t->time.tm_hour; 339 hrs = t->time.tm_hour;
303 hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff; 340 hrs = (hrs < 24) ? bin2bcd(hrs) : 0xff;
304 341
305 min = t->time.tm_min; 342 min = t->time.tm_min;
306 min = (min < 60) ? BIN2BCD(min) : 0xff; 343 min = (min < 60) ? bin2bcd(min) : 0xff;
307 344
308 sec = t->time.tm_sec; 345 sec = t->time.tm_sec;
309 sec = (sec < 60) ? BIN2BCD(sec) : 0xff; 346 sec = (sec < 60) ? bin2bcd(sec) : 0xff;
310 347
311 spin_lock_irq(&rtc_lock); 348 spin_lock_irq(&rtc_lock);
312 349
@@ -491,12 +528,21 @@ cmos_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
491 528
492 if (unlikely(off >= attr->size)) 529 if (unlikely(off >= attr->size))
493 return 0; 530 return 0;
531 if (unlikely(off < 0))
532 return -EINVAL;
494 if ((off + count) > attr->size) 533 if ((off + count) > attr->size)
495 count = attr->size - off; 534 count = attr->size - off;
496 535
536 off += NVRAM_OFFSET;
497 spin_lock_irq(&rtc_lock); 537 spin_lock_irq(&rtc_lock);
498 for (retval = 0, off += NVRAM_OFFSET; count--; retval++, off++) 538 for (retval = 0; count; count--, off++, retval++) {
499 *buf++ = CMOS_READ(off); 539 if (off < 128)
540 *buf++ = CMOS_READ(off);
541 else if (can_bank2)
542 *buf++ = cmos_read_bank2(off);
543 else
544 break;
545 }
500 spin_unlock_irq(&rtc_lock); 546 spin_unlock_irq(&rtc_lock);
501 547
502 return retval; 548 return retval;
@@ -512,6 +558,8 @@ cmos_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
512 cmos = dev_get_drvdata(container_of(kobj, struct device, kobj)); 558 cmos = dev_get_drvdata(container_of(kobj, struct device, kobj));
513 if (unlikely(off >= attr->size)) 559 if (unlikely(off >= attr->size))
514 return -EFBIG; 560 return -EFBIG;
561 if (unlikely(off < 0))
562 return -EINVAL;
515 if ((off + count) > attr->size) 563 if ((off + count) > attr->size)
516 count = attr->size - off; 564 count = attr->size - off;
517 565
@@ -520,15 +568,20 @@ cmos_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
520 * here. If userspace is smart enough to know what fields of 568 * here. If userspace is smart enough to know what fields of
521 * NVRAM to update, updating checksums is also part of its job. 569 * NVRAM to update, updating checksums is also part of its job.
522 */ 570 */
571 off += NVRAM_OFFSET;
523 spin_lock_irq(&rtc_lock); 572 spin_lock_irq(&rtc_lock);
524 for (retval = 0, off += NVRAM_OFFSET; count--; retval++, off++) { 573 for (retval = 0; count; count--, off++, retval++) {
525 /* don't trash RTC registers */ 574 /* don't trash RTC registers */
526 if (off == cmos->day_alrm 575 if (off == cmos->day_alrm
527 || off == cmos->mon_alrm 576 || off == cmos->mon_alrm
528 || off == cmos->century) 577 || off == cmos->century)
529 buf++; 578 buf++;
530 else 579 else if (off < 128)
531 CMOS_WRITE(*buf++, off); 580 CMOS_WRITE(*buf++, off);
581 else if (can_bank2)
582 cmos_write_bank2(*buf++, off);
583 else
584 break;
532 } 585 }
533 spin_unlock_irq(&rtc_lock); 586 spin_unlock_irq(&rtc_lock);
534 587
@@ -539,7 +592,6 @@ static struct bin_attribute nvram = {
539 .attr = { 592 .attr = {
540 .name = "nvram", 593 .name = "nvram",
541 .mode = S_IRUGO | S_IWUSR, 594 .mode = S_IRUGO | S_IWUSR,
542 .owner = THIS_MODULE,
543 }, 595 },
544 596
545 .read = cmos_nvram_read, 597 .read = cmos_nvram_read,
@@ -631,17 +683,19 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
631 683
632 /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM 684 /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
633 * driver did, but don't reject unknown configs. Old hardware 685 * driver did, but don't reject unknown configs. Old hardware
634 * won't address 128 bytes, and for now we ignore the way newer 686 * won't address 128 bytes. Newer chips have multiple banks,
635 * chips can address 256 bytes (using two more i/o ports). 687 * though they may not be listed in one I/O resource.
636 */ 688 */
637#if defined(CONFIG_ATARI) 689#if defined(CONFIG_ATARI)
638 address_space = 64; 690 address_space = 64;
639#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) 691#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__sparc__)
640 address_space = 128; 692 address_space = 128;
641#else 693#else
642#warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes. 694#warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
643 address_space = 128; 695 address_space = 128;
644#endif 696#endif
697 if (can_bank2 && ports->end > (ports->start + 1))
698 address_space = 256;
645 699
646 /* For ACPI systems extension info comes from the FADT. On others, 700 /* For ACPI systems extension info comes from the FADT. On others,
647 * board specific setup provides it as appropriate. Systems where 701 * board specific setup provides it as appropriate. Systems where
@@ -699,7 +753,8 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
699 /* FIXME teach the alarm code how to handle binary mode; 753 /* FIXME teach the alarm code how to handle binary mode;
700 * <asm-generic/rtc.h> doesn't know 12-hour mode either. 754 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
701 */ 755 */
702 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) { 756 if (is_valid_irq(rtc_irq) &&
757 (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))) {
703 dev_dbg(dev, "only 24-hr BCD mode supported\n"); 758 dev_dbg(dev, "only 24-hr BCD mode supported\n");
704 retval = -ENXIO; 759 retval = -ENXIO;
705 goto cleanup1; 760 goto cleanup1;
@@ -739,7 +794,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
739 goto cleanup2; 794 goto cleanup2;
740 } 795 }
741 796
742 pr_info("%s: alarms up to one %s%s%s\n", 797 pr_info("%s: alarms up to one %s%s, %zd bytes nvram%s\n",
743 cmos_rtc.rtc->dev.bus_id, 798 cmos_rtc.rtc->dev.bus_id,
744 is_valid_irq(rtc_irq) 799 is_valid_irq(rtc_irq)
745 ? (cmos_rtc.mon_alrm 800 ? (cmos_rtc.mon_alrm
@@ -748,6 +803,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
748 ? "month" : "day")) 803 ? "month" : "day"))
749 : "no", 804 : "no",
750 cmos_rtc.century ? ", y3k" : "", 805 cmos_rtc.century ? ", y3k" : "",
806 nvram.size,
751 is_hpet_enabled() ? ", hpet irqs" : ""); 807 is_hpet_enabled() ? ", hpet irqs" : "");
752 808
753 return 0; 809 return 0;
@@ -912,6 +968,92 @@ static inline int cmos_poweroff(struct device *dev)
912 * predate even PNPBIOS should set up platform_bus devices. 968 * predate even PNPBIOS should set up platform_bus devices.
913 */ 969 */
914 970
971#ifdef CONFIG_ACPI
972
973#include <linux/acpi.h>
974
975#ifdef CONFIG_PM
976static u32 rtc_handler(void *context)
977{
978 acpi_clear_event(ACPI_EVENT_RTC);
979 acpi_disable_event(ACPI_EVENT_RTC, 0);
980 return ACPI_INTERRUPT_HANDLED;
981}
982
983static inline void rtc_wake_setup(void)
984{
985 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
986 /*
987 * After the RTC handler is installed, the Fixed_RTC event should
988 * be disabled. Only when the RTC alarm is set will it be enabled.
989 */
990 acpi_clear_event(ACPI_EVENT_RTC);
991 acpi_disable_event(ACPI_EVENT_RTC, 0);
992}
993
994static void rtc_wake_on(struct device *dev)
995{
996 acpi_clear_event(ACPI_EVENT_RTC);
997 acpi_enable_event(ACPI_EVENT_RTC, 0);
998}
999
1000static void rtc_wake_off(struct device *dev)
1001{
1002 acpi_disable_event(ACPI_EVENT_RTC, 0);
1003}
1004#else
1005#define rtc_wake_setup() do{}while(0)
1006#define rtc_wake_on NULL
1007#define rtc_wake_off NULL
1008#endif
1009
1010/* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
1011 * its device node and pass extra config data. This helps its driver use
1012 * capabilities that the now-obsolete mc146818 didn't have, and informs it
1013 * that this board's RTC is wakeup-capable (per ACPI spec).
1014 */
1015static struct cmos_rtc_board_info acpi_rtc_info;
1016
1017static void __devinit
1018cmos_wake_setup(struct device *dev)
1019{
1020 if (acpi_disabled)
1021 return;
1022
1023 rtc_wake_setup();
1024 acpi_rtc_info.wake_on = rtc_wake_on;
1025 acpi_rtc_info.wake_off = rtc_wake_off;
1026
1027 /* workaround bug in some ACPI tables */
1028 if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
1029 dev_dbg(dev, "bogus FADT month_alarm (%d)\n",
1030 acpi_gbl_FADT.month_alarm);
1031 acpi_gbl_FADT.month_alarm = 0;
1032 }
1033
1034 acpi_rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
1035 acpi_rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
1036 acpi_rtc_info.rtc_century = acpi_gbl_FADT.century;
1037
1038 /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
1039 if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
1040 dev_info(dev, "RTC can wake from S4\n");
1041
1042 dev->platform_data = &acpi_rtc_info;
1043
1044 /* RTC always wakes from S1/S2/S3, and often S4/STD */
1045 device_init_wakeup(dev, 1);
1046}
1047
1048#else
1049
1050static void __devinit
1051cmos_wake_setup(struct device *dev)
1052{
1053}
1054
1055#endif
1056
915#ifdef CONFIG_PNP 1057#ifdef CONFIG_PNP
916 1058
917#include <linux/pnp.h> 1059#include <linux/pnp.h>
@@ -919,6 +1061,8 @@ static inline int cmos_poweroff(struct device *dev)
919static int __devinit 1061static int __devinit
920cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) 1062cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
921{ 1063{
1064 cmos_wake_setup(&pnp->dev);
1065
922 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0)) 1066 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
923 /* Some machines contain a PNP entry for the RTC, but 1067 /* Some machines contain a PNP entry for the RTC, but
924 * don't define the IRQ. It should always be safe to 1068 * don't define the IRQ. It should always be safe to
@@ -996,6 +1140,7 @@ static struct pnp_driver cmos_pnp_driver = {
996 1140
997static int __init cmos_platform_probe(struct platform_device *pdev) 1141static int __init cmos_platform_probe(struct platform_device *pdev)
998{ 1142{
1143 cmos_wake_setup(&pdev->dev);
999 return cmos_do_probe(&pdev->dev, 1144 return cmos_do_probe(&pdev->dev,
1000 platform_get_resource(pdev, IORESOURCE_IO, 0), 1145 platform_get_resource(pdev, IORESOURCE_IO, 0),
1001 platform_get_irq(pdev, 0)); 1146 platform_get_irq(pdev, 0));
@@ -1030,29 +1175,32 @@ static struct platform_driver cmos_platform_driver = {
1030 1175
1031static int __init cmos_init(void) 1176static int __init cmos_init(void)
1032{ 1177{
1178 int retval = 0;
1179
1033#ifdef CONFIG_PNP 1180#ifdef CONFIG_PNP
1034 if (pnp_platform_devices) 1181 pnp_register_driver(&cmos_pnp_driver);
1035 return pnp_register_driver(&cmos_pnp_driver); 1182#endif
1036 else 1183
1037 return platform_driver_probe(&cmos_platform_driver, 1184 if (!cmos_rtc.dev)
1038 cmos_platform_probe); 1185 retval = platform_driver_probe(&cmos_platform_driver,
1039#else 1186 cmos_platform_probe);
1040 return platform_driver_probe(&cmos_platform_driver, 1187
1041 cmos_platform_probe); 1188 if (retval == 0)
1042#endif /* CONFIG_PNP */ 1189 return 0;
1190
1191#ifdef CONFIG_PNP
1192 pnp_unregister_driver(&cmos_pnp_driver);
1193#endif
1194 return retval;
1043} 1195}
1044module_init(cmos_init); 1196module_init(cmos_init);
1045 1197
1046static void __exit cmos_exit(void) 1198static void __exit cmos_exit(void)
1047{ 1199{
1048#ifdef CONFIG_PNP 1200#ifdef CONFIG_PNP
1049 if (pnp_platform_devices) 1201 pnp_unregister_driver(&cmos_pnp_driver);
1050 pnp_unregister_driver(&cmos_pnp_driver); 1202#endif
1051 else
1052 platform_driver_unregister(&cmos_platform_driver);
1053#else
1054 platform_driver_unregister(&cmos_platform_driver); 1203 platform_driver_unregister(&cmos_platform_driver);
1055#endif /* CONFIG_PNP */
1056} 1204}
1057module_exit(cmos_exit); 1205module_exit(cmos_exit);
1058 1206
diff --git a/drivers/rtc/rtc-dev.c b/drivers/rtc/rtc-dev.c
index 52e2743b04ec..ecdea44ae4e5 100644
--- a/drivers/rtc/rtc-dev.c
+++ b/drivers/rtc/rtc-dev.c
@@ -432,17 +432,20 @@ static int rtc_dev_release(struct inode *inode, struct file *file)
432{ 432{
433 struct rtc_device *rtc = file->private_data; 433 struct rtc_device *rtc = file->private_data;
434 434
435#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 435 /* We shut down the repeating IRQs that userspace enabled,
436 clear_uie(rtc); 436 * since nothing is listening to them.
437#endif 437 * - Update (UIE) ... currently only managed through ioctls
438 * - Periodic (PIE) ... also used through rtc_*() interface calls
439 *
440 * Leave the alarm alone; it may be set to trigger a system wakeup
441 * later, or be used by kernel code, and is a one-shot event anyway.
442 */
443 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
438 rtc_irq_set_state(rtc, NULL, 0); 444 rtc_irq_set_state(rtc, NULL, 0);
439 445
440 if (rtc->ops->release) 446 if (rtc->ops->release)
441 rtc->ops->release(rtc->dev.parent); 447 rtc->ops->release(rtc->dev.parent);
442 448
443 if (file->f_flags & FASYNC)
444 rtc_dev_fasync(-1, file, 0);
445
446 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); 449 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
447 return 0; 450 return 0;
448} 451}
diff --git a/drivers/rtc/rtc-ds1216.c b/drivers/rtc/rtc-ds1216.c
index 0b17770b032b..9a234a4ec06d 100644
--- a/drivers/rtc/rtc-ds1216.c
+++ b/drivers/rtc/rtc-ds1216.c
@@ -86,19 +86,19 @@ static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm)
86 ds1216_switch_ds_to_clock(priv->ioaddr); 86 ds1216_switch_ds_to_clock(priv->ioaddr);
87 ds1216_read(priv->ioaddr, (u8 *)&regs); 87 ds1216_read(priv->ioaddr, (u8 *)&regs);
88 88
89 tm->tm_sec = BCD2BIN(regs.sec); 89 tm->tm_sec = bcd2bin(regs.sec);
90 tm->tm_min = BCD2BIN(regs.min); 90 tm->tm_min = bcd2bin(regs.min);
91 if (regs.hour & DS1216_HOUR_1224) { 91 if (regs.hour & DS1216_HOUR_1224) {
92 /* AM/PM mode */ 92 /* AM/PM mode */
93 tm->tm_hour = BCD2BIN(regs.hour & 0x1f); 93 tm->tm_hour = bcd2bin(regs.hour & 0x1f);
94 if (regs.hour & DS1216_HOUR_AMPM) 94 if (regs.hour & DS1216_HOUR_AMPM)
95 tm->tm_hour += 12; 95 tm->tm_hour += 12;
96 } else 96 } else
97 tm->tm_hour = BCD2BIN(regs.hour & 0x3f); 97 tm->tm_hour = bcd2bin(regs.hour & 0x3f);
98 tm->tm_wday = (regs.wday & 7) - 1; 98 tm->tm_wday = (regs.wday & 7) - 1;
99 tm->tm_mday = BCD2BIN(regs.mday & 0x3f); 99 tm->tm_mday = bcd2bin(regs.mday & 0x3f);
100 tm->tm_mon = BCD2BIN(regs.month & 0x1f); 100 tm->tm_mon = bcd2bin(regs.month & 0x1f);
101 tm->tm_year = BCD2BIN(regs.year); 101 tm->tm_year = bcd2bin(regs.year);
102 if (tm->tm_year < 70) 102 if (tm->tm_year < 70)
103 tm->tm_year += 100; 103 tm->tm_year += 100;
104 return 0; 104 return 0;
@@ -114,19 +114,19 @@ static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
114 ds1216_read(priv->ioaddr, (u8 *)&regs); 114 ds1216_read(priv->ioaddr, (u8 *)&regs);
115 115
116 regs.tsec = 0; /* clear 0.1 and 0.01 seconds */ 116 regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
117 regs.sec = BIN2BCD(tm->tm_sec); 117 regs.sec = bin2bcd(tm->tm_sec);
118 regs.min = BIN2BCD(tm->tm_min); 118 regs.min = bin2bcd(tm->tm_min);
119 regs.hour &= DS1216_HOUR_1224; 119 regs.hour &= DS1216_HOUR_1224;
120 if (regs.hour && tm->tm_hour > 12) { 120 if (regs.hour && tm->tm_hour > 12) {
121 regs.hour |= DS1216_HOUR_AMPM; 121 regs.hour |= DS1216_HOUR_AMPM;
122 tm->tm_hour -= 12; 122 tm->tm_hour -= 12;
123 } 123 }
124 regs.hour |= BIN2BCD(tm->tm_hour); 124 regs.hour |= bin2bcd(tm->tm_hour);
125 regs.wday &= ~7; 125 regs.wday &= ~7;
126 regs.wday |= tm->tm_wday; 126 regs.wday |= tm->tm_wday;
127 regs.mday = BIN2BCD(tm->tm_mday); 127 regs.mday = bin2bcd(tm->tm_mday);
128 regs.month = BIN2BCD(tm->tm_mon); 128 regs.month = bin2bcd(tm->tm_mon);
129 regs.year = BIN2BCD(tm->tm_year % 100); 129 regs.year = bin2bcd(tm->tm_year % 100);
130 130
131 ds1216_switch_ds_to_clock(priv->ioaddr); 131 ds1216_switch_ds_to_clock(priv->ioaddr);
132 ds1216_write(priv->ioaddr, (u8 *)&regs); 132 ds1216_write(priv->ioaddr, (u8 *)&regs);
diff --git a/drivers/rtc/rtc-ds1286.c b/drivers/rtc/rtc-ds1286.c
new file mode 100644
index 000000000000..4fcb16bbff4a
--- /dev/null
+++ b/drivers/rtc/rtc-ds1286.c
@@ -0,0 +1,410 @@
1/*
2 * DS1286 Real Time Clock interface for Linux
3 *
4 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
5 * Copyright (C) 2008 Thomas Bogendoerfer
6 *
7 * Based on code written by Paul Gortmaker.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14
15#include <linux/module.h>
16#include <linux/rtc.h>
17#include <linux/platform_device.h>
18#include <linux/bcd.h>
19#include <linux/ds1286.h>
20#include <linux/io.h>
21
22#define DRV_VERSION "1.0"
23
24struct ds1286_priv {
25 struct rtc_device *rtc;
26 u32 __iomem *rtcregs;
27 size_t size;
28 unsigned long baseaddr;
29 spinlock_t lock;
30};
31
32static inline u8 ds1286_rtc_read(struct ds1286_priv *priv, int reg)
33{
34 return __raw_readl(&priv->rtcregs[reg]) & 0xff;
35}
36
37static inline void ds1286_rtc_write(struct ds1286_priv *priv, u8 data, int reg)
38{
39 __raw_writel(data, &priv->rtcregs[reg]);
40}
41
42#ifdef CONFIG_RTC_INTF_DEV
43
44static int ds1286_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
45{
46 struct ds1286_priv *priv = dev_get_drvdata(dev);
47 unsigned long flags;
48 unsigned char val;
49
50 switch (cmd) {
51 case RTC_AIE_OFF:
52 /* Mask alarm int. enab. bit */
53 spin_lock_irqsave(&priv->lock, flags);
54 val = ds1286_rtc_read(priv, RTC_CMD);
55 val |= RTC_TDM;
56 ds1286_rtc_write(priv, val, RTC_CMD);
57 spin_unlock_irqrestore(&priv->lock, flags);
58 break;
59 case RTC_AIE_ON:
60 /* Allow alarm interrupts. */
61 spin_lock_irqsave(&priv->lock, flags);
62 val = ds1286_rtc_read(priv, RTC_CMD);
63 val &= ~RTC_TDM;
64 ds1286_rtc_write(priv, val, RTC_CMD);
65 spin_unlock_irqrestore(&priv->lock, flags);
66 break;
67 case RTC_WIE_OFF:
68 /* Mask watchdog int. enab. bit */
69 spin_lock_irqsave(&priv->lock, flags);
70 val = ds1286_rtc_read(priv, RTC_CMD);
71 val |= RTC_WAM;
72 ds1286_rtc_write(priv, val, RTC_CMD);
73 spin_unlock_irqrestore(&priv->lock, flags);
74 break;
75 case RTC_WIE_ON:
76 /* Allow watchdog interrupts. */
77 spin_lock_irqsave(&priv->lock, flags);
78 val = ds1286_rtc_read(priv, RTC_CMD);
79 val &= ~RTC_WAM;
80 ds1286_rtc_write(priv, val, RTC_CMD);
81 spin_unlock_irqrestore(&priv->lock, flags);
82 break;
83 default:
84 return -ENOIOCTLCMD;
85 }
86 return 0;
87}
88
89#else
90#define ds1286_ioctl NULL
91#endif
92
93#ifdef CONFIG_PROC_FS
94
95static int ds1286_proc(struct device *dev, struct seq_file *seq)
96{
97 struct ds1286_priv *priv = dev_get_drvdata(dev);
98 unsigned char month, cmd, amode;
99 const char *s;
100
101 month = ds1286_rtc_read(priv, RTC_MONTH);
102 seq_printf(seq,
103 "oscillator\t: %s\n"
104 "square_wave\t: %s\n",
105 (month & RTC_EOSC) ? "disabled" : "enabled",
106 (month & RTC_ESQW) ? "disabled" : "enabled");
107
108 amode = ((ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x80) >> 5) |
109 ((ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x80) >> 6) |
110 ((ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x80) >> 7);
111 switch (amode) {
112 case 7:
113 s = "each minute";
114 break;
115 case 3:
116 s = "minutes match";
117 break;
118 case 1:
119 s = "hours and minutes match";
120 break;
121 case 0:
122 s = "days, hours and minutes match";
123 break;
124 default:
125 s = "invalid";
126 break;
127 }
128 seq_printf(seq, "alarm_mode\t: %s\n", s);
129
130 cmd = ds1286_rtc_read(priv, RTC_CMD);
131 seq_printf(seq,
132 "alarm_enable\t: %s\n"
133 "wdog_alarm\t: %s\n"
134 "alarm_mask\t: %s\n"
135 "wdog_alarm_mask\t: %s\n"
136 "interrupt_mode\t: %s\n"
137 "INTB_mode\t: %s_active\n"
138 "interrupt_pins\t: %s\n",
139 (cmd & RTC_TDF) ? "yes" : "no",
140 (cmd & RTC_WAF) ? "yes" : "no",
141 (cmd & RTC_TDM) ? "disabled" : "enabled",
142 (cmd & RTC_WAM) ? "disabled" : "enabled",
143 (cmd & RTC_PU_LVL) ? "pulse" : "level",
144 (cmd & RTC_IBH_LO) ? "low" : "high",
145 (cmd & RTC_IPSW) ? "unswapped" : "swapped");
146 return 0;
147}
148
149#else
150#define ds1286_proc NULL
151#endif
152
153static int ds1286_read_time(struct device *dev, struct rtc_time *tm)
154{
155 struct ds1286_priv *priv = dev_get_drvdata(dev);
156 unsigned char save_control;
157 unsigned long flags;
158 unsigned long uip_watchdog = jiffies;
159
160 /*
161 * read RTC once any update in progress is done. The update
162 * can take just over 2ms. We wait 10 to 20ms. There is no need to
163 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
164 * If you need to know *exactly* when a second has started, enable
165 * periodic update complete interrupts, (via ioctl) and then
166 * immediately read /dev/rtc which will block until you get the IRQ.
167 * Once the read clears, read the RTC time (again via ioctl). Easy.
168 */
169
170 if (ds1286_rtc_read(priv, RTC_CMD) & RTC_TE)
171 while (time_before(jiffies, uip_watchdog + 2*HZ/100))
172 barrier();
173
174 /*
175 * Only the values that we read from the RTC are set. We leave
176 * tm_wday, tm_yday and tm_isdst untouched. Even though the
177 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
178 * by the RTC when initially set to a non-zero value.
179 */
180 spin_lock_irqsave(&priv->lock, flags);
181 save_control = ds1286_rtc_read(priv, RTC_CMD);
182 ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
183
184 tm->tm_sec = ds1286_rtc_read(priv, RTC_SECONDS);
185 tm->tm_min = ds1286_rtc_read(priv, RTC_MINUTES);
186 tm->tm_hour = ds1286_rtc_read(priv, RTC_HOURS) & 0x3f;
187 tm->tm_mday = ds1286_rtc_read(priv, RTC_DATE);
188 tm->tm_mon = ds1286_rtc_read(priv, RTC_MONTH) & 0x1f;
189 tm->tm_year = ds1286_rtc_read(priv, RTC_YEAR);
190
191 ds1286_rtc_write(priv, save_control, RTC_CMD);
192 spin_unlock_irqrestore(&priv->lock, flags);
193
194 tm->tm_sec = bcd2bin(tm->tm_sec);
195 tm->tm_min = bcd2bin(tm->tm_min);
196 tm->tm_hour = bcd2bin(tm->tm_hour);
197 tm->tm_mday = bcd2bin(tm->tm_mday);
198 tm->tm_mon = bcd2bin(tm->tm_mon);
199 tm->tm_year = bcd2bin(tm->tm_year);
200
201 /*
202 * Account for differences between how the RTC uses the values
203 * and how they are defined in a struct rtc_time;
204 */
205 if (tm->tm_year < 45)
206 tm->tm_year += 30;
207 tm->tm_year += 40;
208 if (tm->tm_year < 70)
209 tm->tm_year += 100;
210
211 tm->tm_mon--;
212
213 return rtc_valid_tm(tm);
214}
215
216static int ds1286_set_time(struct device *dev, struct rtc_time *tm)
217{
218 struct ds1286_priv *priv = dev_get_drvdata(dev);
219 unsigned char mon, day, hrs, min, sec;
220 unsigned char save_control;
221 unsigned int yrs;
222 unsigned long flags;
223
224 yrs = tm->tm_year + 1900;
225 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
226 day = tm->tm_mday;
227 hrs = tm->tm_hour;
228 min = tm->tm_min;
229 sec = tm->tm_sec;
230
231 if (yrs < 1970)
232 return -EINVAL;
233
234 yrs -= 1940;
235 if (yrs > 255) /* They are unsigned */
236 return -EINVAL;
237
238 if (yrs >= 100)
239 yrs -= 100;
240
241 sec = bin2bcd(sec);
242 min = bin2bcd(min);
243 hrs = bin2bcd(hrs);
244 day = bin2bcd(day);
245 mon = bin2bcd(mon);
246 yrs = bin2bcd(yrs);
247
248 spin_lock_irqsave(&priv->lock, flags);
249 save_control = ds1286_rtc_read(priv, RTC_CMD);
250 ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
251
252 ds1286_rtc_write(priv, yrs, RTC_YEAR);
253 ds1286_rtc_write(priv, mon, RTC_MONTH);
254 ds1286_rtc_write(priv, day, RTC_DATE);
255 ds1286_rtc_write(priv, hrs, RTC_HOURS);
256 ds1286_rtc_write(priv, min, RTC_MINUTES);
257 ds1286_rtc_write(priv, sec, RTC_SECONDS);
258 ds1286_rtc_write(priv, 0, RTC_HUNDREDTH_SECOND);
259
260 ds1286_rtc_write(priv, save_control, RTC_CMD);
261 spin_unlock_irqrestore(&priv->lock, flags);
262 return 0;
263}
264
265static int ds1286_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
266{
267 struct ds1286_priv *priv = dev_get_drvdata(dev);
268 unsigned char cmd;
269 unsigned long flags;
270
271 /*
272 * Only the values that we read from the RTC are set. That
273 * means only tm_wday, tm_hour, tm_min.
274 */
275 spin_lock_irqsave(&priv->lock, flags);
276 alm->time.tm_min = ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x7f;
277 alm->time.tm_hour = ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x1f;
278 alm->time.tm_wday = ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x07;
279 cmd = ds1286_rtc_read(priv, RTC_CMD);
280 spin_unlock_irqrestore(&priv->lock, flags);
281
282 alm->time.tm_min = bcd2bin(alm->time.tm_min);
283 alm->time.tm_hour = bcd2bin(alm->time.tm_hour);
284 alm->time.tm_sec = 0;
285 return 0;
286}
287
288static int ds1286_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
289{
290 struct ds1286_priv *priv = dev_get_drvdata(dev);
291 unsigned char hrs, min, sec;
292
293 hrs = alm->time.tm_hour;
294 min = alm->time.tm_min;
295 sec = alm->time.tm_sec;
296
297 if (hrs >= 24)
298 hrs = 0xff;
299
300 if (min >= 60)
301 min = 0xff;
302
303 if (sec != 0)
304 return -EINVAL;
305
306 min = bin2bcd(min);
307 hrs = bin2bcd(hrs);
308
309 spin_lock(&priv->lock);
310 ds1286_rtc_write(priv, hrs, RTC_HOURS_ALARM);
311 ds1286_rtc_write(priv, min, RTC_MINUTES_ALARM);
312 spin_unlock(&priv->lock);
313
314 return 0;
315}
316
317static const struct rtc_class_ops ds1286_ops = {
318 .ioctl = ds1286_ioctl,
319 .proc = ds1286_proc,
320 .read_time = ds1286_read_time,
321 .set_time = ds1286_set_time,
322 .read_alarm = ds1286_read_alarm,
323 .set_alarm = ds1286_set_alarm,
324};
325
326static int __devinit ds1286_probe(struct platform_device *pdev)
327{
328 struct rtc_device *rtc;
329 struct resource *res;
330 struct ds1286_priv *priv;
331 int ret = 0;
332
333 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
334 if (!res)
335 return -ENODEV;
336 priv = kzalloc(sizeof(struct ds1286_priv), GFP_KERNEL);
337 if (!priv)
338 return -ENOMEM;
339
340 priv->size = res->end - res->start + 1;
341 if (!request_mem_region(res->start, priv->size, pdev->name)) {
342 ret = -EBUSY;
343 goto out;
344 }
345 priv->baseaddr = res->start;
346 priv->rtcregs = ioremap(priv->baseaddr, priv->size);
347 if (!priv->rtcregs) {
348 ret = -ENOMEM;
349 goto out;
350 }
351 spin_lock_init(&priv->lock);
352 rtc = rtc_device_register("ds1286", &pdev->dev,
353 &ds1286_ops, THIS_MODULE);
354 if (IS_ERR(rtc)) {
355 ret = PTR_ERR(rtc);
356 goto out;
357 }
358 priv->rtc = rtc;
359 platform_set_drvdata(pdev, priv);
360 return 0;
361
362out:
363 if (priv->rtc)
364 rtc_device_unregister(priv->rtc);
365 if (priv->rtcregs)
366 iounmap(priv->rtcregs);
367 if (priv->baseaddr)
368 release_mem_region(priv->baseaddr, priv->size);
369 kfree(priv);
370 return ret;
371}
372
373static int __devexit ds1286_remove(struct platform_device *pdev)
374{
375 struct ds1286_priv *priv = platform_get_drvdata(pdev);
376
377 rtc_device_unregister(priv->rtc);
378 iounmap(priv->rtcregs);
379 release_mem_region(priv->baseaddr, priv->size);
380 kfree(priv);
381 return 0;
382}
383
384static struct platform_driver ds1286_platform_driver = {
385 .driver = {
386 .name = "rtc-ds1286",
387 .owner = THIS_MODULE,
388 },
389 .probe = ds1286_probe,
390 .remove = __devexit_p(ds1286_remove),
391};
392
393static int __init ds1286_init(void)
394{
395 return platform_driver_register(&ds1286_platform_driver);
396}
397
398static void __exit ds1286_exit(void)
399{
400 platform_driver_unregister(&ds1286_platform_driver);
401}
402
403MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
404MODULE_DESCRIPTION("DS1286 RTC driver");
405MODULE_LICENSE("GPL");
406MODULE_VERSION(DRV_VERSION);
407MODULE_ALIAS("platform:rtc-ds1286");
408
409module_init(ds1286_init);
410module_exit(ds1286_exit);
diff --git a/drivers/rtc/rtc-ds1302.c b/drivers/rtc/rtc-ds1302.c
index b9397818f73a..184556620778 100644
--- a/drivers/rtc/rtc-ds1302.c
+++ b/drivers/rtc/rtc-ds1302.c
@@ -40,7 +40,7 @@
40#define RTC_SCLK 0x0400 40#define RTC_SCLK 0x0400
41 41
42#ifdef CONFIG_SH_SECUREEDGE5410 42#ifdef CONFIG_SH_SECUREEDGE5410
43#include <asm/snapgear.h> 43#include <mach/snapgear.h>
44#define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00) 44#define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
45#define get_dp() SECUREEDGE_READ_IOPORT() 45#define get_dp() SECUREEDGE_READ_IOPORT()
46#else 46#else
@@ -107,13 +107,13 @@ static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
107 107
108 spin_lock_irq(&rtc->lock); 108 spin_lock_irq(&rtc->lock);
109 109
110 tm->tm_sec = BCD2BIN(ds1302_readbyte(RTC_ADDR_SEC)); 110 tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
111 tm->tm_min = BCD2BIN(ds1302_readbyte(RTC_ADDR_MIN)); 111 tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
112 tm->tm_hour = BCD2BIN(ds1302_readbyte(RTC_ADDR_HOUR)); 112 tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
113 tm->tm_wday = BCD2BIN(ds1302_readbyte(RTC_ADDR_DAY)); 113 tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY));
114 tm->tm_mday = BCD2BIN(ds1302_readbyte(RTC_ADDR_DATE)); 114 tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE));
115 tm->tm_mon = BCD2BIN(ds1302_readbyte(RTC_ADDR_MON)) - 1; 115 tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1;
116 tm->tm_year = BCD2BIN(ds1302_readbyte(RTC_ADDR_YEAR)); 116 tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR));
117 117
118 if (tm->tm_year < 70) 118 if (tm->tm_year < 70)
119 tm->tm_year += 100; 119 tm->tm_year += 100;
@@ -141,13 +141,13 @@ static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
141 /* Stop RTC */ 141 /* Stop RTC */
142 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80); 142 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
143 143
144 ds1302_writebyte(RTC_ADDR_SEC, BIN2BCD(tm->tm_sec)); 144 ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec));
145 ds1302_writebyte(RTC_ADDR_MIN, BIN2BCD(tm->tm_min)); 145 ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min));
146 ds1302_writebyte(RTC_ADDR_HOUR, BIN2BCD(tm->tm_hour)); 146 ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour));
147 ds1302_writebyte(RTC_ADDR_DAY, BIN2BCD(tm->tm_wday)); 147 ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday));
148 ds1302_writebyte(RTC_ADDR_DATE, BIN2BCD(tm->tm_mday)); 148 ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday));
149 ds1302_writebyte(RTC_ADDR_MON, BIN2BCD(tm->tm_mon + 1)); 149 ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1));
150 ds1302_writebyte(RTC_ADDR_YEAR, BIN2BCD(tm->tm_year % 100)); 150 ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100));
151 151
152 /* Start RTC */ 152 /* Start RTC */
153 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80); 153 ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
diff --git a/drivers/rtc/rtc-ds1305.c b/drivers/rtc/rtc-ds1305.c
index b91d02a3ace9..fc372df6534b 100644
--- a/drivers/rtc/rtc-ds1305.c
+++ b/drivers/rtc/rtc-ds1305.c
@@ -114,10 +114,10 @@ static unsigned bcd2hour(u8 bcd)
114 hour = 12; 114 hour = 12;
115 bcd &= ~DS1305_HR_PM; 115 bcd &= ~DS1305_HR_PM;
116 } 116 }
117 hour += BCD2BIN(bcd); 117 hour += bcd2bin(bcd);
118 return hour - 1; 118 return hour - 1;
119 } 119 }
120 return BCD2BIN(bcd); 120 return bcd2bin(bcd);
121} 121}
122 122
123static u8 hour2bcd(bool hr12, int hour) 123static u8 hour2bcd(bool hr12, int hour)
@@ -125,11 +125,11 @@ static u8 hour2bcd(bool hr12, int hour)
125 if (hr12) { 125 if (hr12) {
126 hour++; 126 hour++;
127 if (hour <= 12) 127 if (hour <= 12)
128 return DS1305_HR_12 | BIN2BCD(hour); 128 return DS1305_HR_12 | bin2bcd(hour);
129 hour -= 12; 129 hour -= 12;
130 return DS1305_HR_12 | DS1305_HR_PM | BIN2BCD(hour); 130 return DS1305_HR_12 | DS1305_HR_PM | bin2bcd(hour);
131 } 131 }
132 return BIN2BCD(hour); 132 return bin2bcd(hour);
133} 133}
134 134
135/*----------------------------------------------------------------------*/ 135/*----------------------------------------------------------------------*/
@@ -206,13 +206,13 @@ static int ds1305_get_time(struct device *dev, struct rtc_time *time)
206 buf[4], buf[5], buf[6]); 206 buf[4], buf[5], buf[6]);
207 207
208 /* Decode the registers */ 208 /* Decode the registers */
209 time->tm_sec = BCD2BIN(buf[DS1305_SEC]); 209 time->tm_sec = bcd2bin(buf[DS1305_SEC]);
210 time->tm_min = BCD2BIN(buf[DS1305_MIN]); 210 time->tm_min = bcd2bin(buf[DS1305_MIN]);
211 time->tm_hour = bcd2hour(buf[DS1305_HOUR]); 211 time->tm_hour = bcd2hour(buf[DS1305_HOUR]);
212 time->tm_wday = buf[DS1305_WDAY] - 1; 212 time->tm_wday = buf[DS1305_WDAY] - 1;
213 time->tm_mday = BCD2BIN(buf[DS1305_MDAY]); 213 time->tm_mday = bcd2bin(buf[DS1305_MDAY]);
214 time->tm_mon = BCD2BIN(buf[DS1305_MON]) - 1; 214 time->tm_mon = bcd2bin(buf[DS1305_MON]) - 1;
215 time->tm_year = BCD2BIN(buf[DS1305_YEAR]) + 100; 215 time->tm_year = bcd2bin(buf[DS1305_YEAR]) + 100;
216 216
217 dev_vdbg(dev, "%s secs=%d, mins=%d, " 217 dev_vdbg(dev, "%s secs=%d, mins=%d, "
218 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", 218 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -239,13 +239,13 @@ static int ds1305_set_time(struct device *dev, struct rtc_time *time)
239 /* Write registers starting at the first time/date address. */ 239 /* Write registers starting at the first time/date address. */
240 *bp++ = DS1305_WRITE | DS1305_SEC; 240 *bp++ = DS1305_WRITE | DS1305_SEC;
241 241
242 *bp++ = BIN2BCD(time->tm_sec); 242 *bp++ = bin2bcd(time->tm_sec);
243 *bp++ = BIN2BCD(time->tm_min); 243 *bp++ = bin2bcd(time->tm_min);
244 *bp++ = hour2bcd(ds1305->hr12, time->tm_hour); 244 *bp++ = hour2bcd(ds1305->hr12, time->tm_hour);
245 *bp++ = (time->tm_wday < 7) ? (time->tm_wday + 1) : 1; 245 *bp++ = (time->tm_wday < 7) ? (time->tm_wday + 1) : 1;
246 *bp++ = BIN2BCD(time->tm_mday); 246 *bp++ = bin2bcd(time->tm_mday);
247 *bp++ = BIN2BCD(time->tm_mon + 1); 247 *bp++ = bin2bcd(time->tm_mon + 1);
248 *bp++ = BIN2BCD(time->tm_year - 100); 248 *bp++ = bin2bcd(time->tm_year - 100);
249 249
250 dev_dbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n", 250 dev_dbg(dev, "%s: %02x %02x %02x, %02x %02x %02x %02x\n",
251 "write", buf[1], buf[2], buf[3], 251 "write", buf[1], buf[2], buf[3],
@@ -329,8 +329,8 @@ static int ds1305_get_alarm(struct device *dev, struct rtc_wkalrm *alm)
329 * fill in the rest ... and also handle rollover to tomorrow when 329 * fill in the rest ... and also handle rollover to tomorrow when
330 * that's needed. 330 * that's needed.
331 */ 331 */
332 alm->time.tm_sec = BCD2BIN(buf[DS1305_SEC]); 332 alm->time.tm_sec = bcd2bin(buf[DS1305_SEC]);
333 alm->time.tm_min = BCD2BIN(buf[DS1305_MIN]); 333 alm->time.tm_min = bcd2bin(buf[DS1305_MIN]);
334 alm->time.tm_hour = bcd2hour(buf[DS1305_HOUR]); 334 alm->time.tm_hour = bcd2hour(buf[DS1305_HOUR]);
335 alm->time.tm_mday = -1; 335 alm->time.tm_mday = -1;
336 alm->time.tm_mon = -1; 336 alm->time.tm_mon = -1;
@@ -387,8 +387,8 @@ static int ds1305_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
387 387
388 /* write alarm */ 388 /* write alarm */
389 buf[0] = DS1305_WRITE | DS1305_ALM0(DS1305_SEC); 389 buf[0] = DS1305_WRITE | DS1305_ALM0(DS1305_SEC);
390 buf[1 + DS1305_SEC] = BIN2BCD(alm->time.tm_sec); 390 buf[1 + DS1305_SEC] = bin2bcd(alm->time.tm_sec);
391 buf[1 + DS1305_MIN] = BIN2BCD(alm->time.tm_min); 391 buf[1 + DS1305_MIN] = bin2bcd(alm->time.tm_min);
392 buf[1 + DS1305_HOUR] = hour2bcd(ds1305->hr12, alm->time.tm_hour); 392 buf[1 + DS1305_HOUR] = hour2bcd(ds1305->hr12, alm->time.tm_hour);
393 buf[1 + DS1305_WDAY] = DS1305_ALM_DISABLE; 393 buf[1 + DS1305_WDAY] = DS1305_ALM_DISABLE;
394 394
@@ -606,7 +606,6 @@ ds1305_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
606static struct bin_attribute nvram = { 606static struct bin_attribute nvram = {
607 .attr.name = "nvram", 607 .attr.name = "nvram",
608 .attr.mode = S_IRUGO | S_IWUSR, 608 .attr.mode = S_IRUGO | S_IWUSR,
609 .attr.owner = THIS_MODULE,
610 .read = ds1305_nvram_read, 609 .read = ds1305_nvram_read,
611 .write = ds1305_nvram_write, 610 .write = ds1305_nvram_write,
612 .size = DS1305_NVRAM_LEN, 611 .size = DS1305_NVRAM_LEN,
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index bbf97e65202a..162330b9d1dc 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -23,10 +23,6 @@
23 * to have set the chip up as a clock (turning on the oscillator and 23 * to have set the chip up as a clock (turning on the oscillator and
24 * setting the date and time), Linux can ignore the non-clock features. 24 * setting the date and time), Linux can ignore the non-clock features.
25 * That's a natural job for a factory or repair bench. 25 * That's a natural job for a factory or repair bench.
26 *
27 * This is currently a simple no-alarms driver. If your board has the
28 * alarm irq wired up on a ds1337 or ds1339, and you want to use that,
29 * then look at the rtc-rs5c372 driver for code to steal...
30 */ 26 */
31enum ds_type { 27enum ds_type {
32 ds_1307, 28 ds_1307,
@@ -67,6 +63,7 @@ enum ds_type {
67# define DS1307_BIT_RS0 0x01 63# define DS1307_BIT_RS0 0x01
68#define DS1337_REG_CONTROL 0x0e 64#define DS1337_REG_CONTROL 0x0e
69# define DS1337_BIT_nEOSC 0x80 65# define DS1337_BIT_nEOSC 0x80
66# define DS1339_BIT_BBSQI 0x20
70# define DS1337_BIT_RS2 0x10 67# define DS1337_BIT_RS2 0x10
71# define DS1337_BIT_RS1 0x08 68# define DS1337_BIT_RS1 0x08
72# define DS1337_BIT_INTCN 0x04 69# define DS1337_BIT_INTCN 0x04
@@ -83,19 +80,22 @@ enum ds_type {
83# define DS1337_BIT_OSF 0x80 80# define DS1337_BIT_OSF 0x80
84# define DS1337_BIT_A2I 0x02 81# define DS1337_BIT_A2I 0x02
85# define DS1337_BIT_A1I 0x01 82# define DS1337_BIT_A1I 0x01
83#define DS1339_REG_ALARM1_SECS 0x07
86#define DS1339_REG_TRICKLE 0x10 84#define DS1339_REG_TRICKLE 0x10
87 85
88 86
89 87
90struct ds1307 { 88struct ds1307 {
91 u8 reg_addr; 89 u8 reg_addr;
92 bool has_nvram; 90 u8 regs[11];
93 u8 regs[8];
94 enum ds_type type; 91 enum ds_type type;
92 unsigned long flags;
93#define HAS_NVRAM 0 /* bit 0 == sysfs file active */
94#define HAS_ALARM 1 /* bit 1 == irq claimed */
95 struct i2c_msg msg[2]; 95 struct i2c_msg msg[2];
96 struct i2c_client *client; 96 struct i2c_client *client;
97 struct i2c_client dev;
98 struct rtc_device *rtc; 97 struct rtc_device *rtc;
98 struct work_struct work;
99}; 99};
100 100
101struct chip_desc { 101struct chip_desc {
@@ -132,12 +132,79 @@ static const struct i2c_device_id ds1307_id[] = {
132}; 132};
133MODULE_DEVICE_TABLE(i2c, ds1307_id); 133MODULE_DEVICE_TABLE(i2c, ds1307_id);
134 134
135/*----------------------------------------------------------------------*/
136
137/*
138 * The IRQ logic includes a "real" handler running in IRQ context just
139 * long enough to schedule this workqueue entry. We need a task context
140 * to talk to the RTC, since I2C I/O calls require that; and disable the
141 * IRQ until we clear its status on the chip, so that this handler can
142 * work with any type of triggering (not just falling edge).
143 *
144 * The ds1337 and ds1339 both have two alarms, but we only use the first
145 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
146 * signal; ds1339 chips have only one alarm signal.
147 */
148static void ds1307_work(struct work_struct *work)
149{
150 struct ds1307 *ds1307;
151 struct i2c_client *client;
152 struct mutex *lock;
153 int stat, control;
154
155 ds1307 = container_of(work, struct ds1307, work);
156 client = ds1307->client;
157 lock = &ds1307->rtc->ops_lock;
158
159 mutex_lock(lock);
160 stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
161 if (stat < 0)
162 goto out;
163
164 if (stat & DS1337_BIT_A1I) {
165 stat &= ~DS1337_BIT_A1I;
166 i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
167
168 control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
169 if (control < 0)
170 goto out;
171
172 control &= ~DS1337_BIT_A1IE;
173 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
174
175 /* rtc_update_irq() assumes that it is called
176 * from IRQ-disabled context.
177 */
178 local_irq_disable();
179 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
180 local_irq_enable();
181 }
182
183out:
184 if (test_bit(HAS_ALARM, &ds1307->flags))
185 enable_irq(client->irq);
186 mutex_unlock(lock);
187}
188
189static irqreturn_t ds1307_irq(int irq, void *dev_id)
190{
191 struct i2c_client *client = dev_id;
192 struct ds1307 *ds1307 = i2c_get_clientdata(client);
193
194 disable_irq_nosync(irq);
195 schedule_work(&ds1307->work);
196 return IRQ_HANDLED;
197}
198
199/*----------------------------------------------------------------------*/
200
135static int ds1307_get_time(struct device *dev, struct rtc_time *t) 201static int ds1307_get_time(struct device *dev, struct rtc_time *t)
136{ 202{
137 struct ds1307 *ds1307 = dev_get_drvdata(dev); 203 struct ds1307 *ds1307 = dev_get_drvdata(dev);
138 int tmp; 204 int tmp;
139 205
140 /* read the RTC date and time registers all at once */ 206 /* read the RTC date and time registers all at once */
207 ds1307->reg_addr = 0;
141 ds1307->msg[1].flags = I2C_M_RD; 208 ds1307->msg[1].flags = I2C_M_RD;
142 ds1307->msg[1].len = 7; 209 ds1307->msg[1].len = 7;
143 210
@@ -155,17 +222,17 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
155 ds1307->regs[4], ds1307->regs[5], 222 ds1307->regs[4], ds1307->regs[5],
156 ds1307->regs[6]); 223 ds1307->regs[6]);
157 224
158 t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f); 225 t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
159 t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f); 226 t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
160 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f; 227 tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
161 t->tm_hour = BCD2BIN(tmp); 228 t->tm_hour = bcd2bin(tmp);
162 t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1; 229 t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
163 t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f); 230 t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
164 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f; 231 tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
165 t->tm_mon = BCD2BIN(tmp) - 1; 232 t->tm_mon = bcd2bin(tmp) - 1;
166 233
167 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */ 234 /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
168 t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100; 235 t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
169 236
170 dev_dbg(dev, "%s secs=%d, mins=%d, " 237 dev_dbg(dev, "%s secs=%d, mins=%d, "
171 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", 238 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -191,16 +258,16 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
191 t->tm_mon, t->tm_year, t->tm_wday); 258 t->tm_mon, t->tm_year, t->tm_wday);
192 259
193 *buf++ = 0; /* first register addr */ 260 *buf++ = 0; /* first register addr */
194 buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec); 261 buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
195 buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min); 262 buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
196 buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour); 263 buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
197 buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1); 264 buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
198 buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday); 265 buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
199 buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1); 266 buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
200 267
201 /* assume 20YY not 19YY */ 268 /* assume 20YY not 19YY */
202 tmp = t->tm_year - 100; 269 tmp = t->tm_year - 100;
203 buf[DS1307_REG_YEAR] = BIN2BCD(tmp); 270 buf[DS1307_REG_YEAR] = bin2bcd(tmp);
204 271
205 switch (ds1307->type) { 272 switch (ds1307->type) {
206 case ds_1337: 273 case ds_1337:
@@ -231,9 +298,186 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
231 return 0; 298 return 0;
232} 299}
233 300
301static int ds1307_read_alarm(struct device *dev, struct rtc_wkalrm *t)
302{
303 struct i2c_client *client = to_i2c_client(dev);
304 struct ds1307 *ds1307 = i2c_get_clientdata(client);
305 int ret;
306
307 if (!test_bit(HAS_ALARM, &ds1307->flags))
308 return -EINVAL;
309
310 /* read all ALARM1, ALARM2, and status registers at once */
311 ds1307->reg_addr = DS1339_REG_ALARM1_SECS;
312 ds1307->msg[1].flags = I2C_M_RD;
313 ds1307->msg[1].len = 9;
314
315 ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
316 ds1307->msg, 2);
317 if (ret != 2) {
318 dev_err(dev, "%s error %d\n", "alarm read", ret);
319 return -EIO;
320 }
321
322 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
323 "alarm read",
324 ds1307->regs[0], ds1307->regs[1],
325 ds1307->regs[2], ds1307->regs[3],
326 ds1307->regs[4], ds1307->regs[5],
327 ds1307->regs[6], ds1307->regs[7],
328 ds1307->regs[8]);
329
330 /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
331 * and that all four fields are checked matches
332 */
333 t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
334 t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
335 t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
336 t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
337 t->time.tm_mon = -1;
338 t->time.tm_year = -1;
339 t->time.tm_wday = -1;
340 t->time.tm_yday = -1;
341 t->time.tm_isdst = -1;
342
343 /* ... and status */
344 t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
345 t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
346
347 dev_dbg(dev, "%s secs=%d, mins=%d, "
348 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
349 "alarm read", t->time.tm_sec, t->time.tm_min,
350 t->time.tm_hour, t->time.tm_mday,
351 t->enabled, t->pending);
352
353 return 0;
354}
355
356static int ds1307_set_alarm(struct device *dev, struct rtc_wkalrm *t)
357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct ds1307 *ds1307 = i2c_get_clientdata(client);
360 unsigned char *buf = ds1307->regs;
361 u8 control, status;
362 int ret;
363
364 if (!test_bit(HAS_ALARM, &ds1307->flags))
365 return -EINVAL;
366
367 dev_dbg(dev, "%s secs=%d, mins=%d, "
368 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
369 "alarm set", t->time.tm_sec, t->time.tm_min,
370 t->time.tm_hour, t->time.tm_mday,
371 t->enabled, t->pending);
372
373 /* read current status of both alarms and the chip */
374 ds1307->reg_addr = DS1339_REG_ALARM1_SECS;
375 ds1307->msg[1].flags = I2C_M_RD;
376 ds1307->msg[1].len = 9;
377
378 ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
379 ds1307->msg, 2);
380 if (ret != 2) {
381 dev_err(dev, "%s error %d\n", "alarm write", ret);
382 return -EIO;
383 }
384 control = ds1307->regs[7];
385 status = ds1307->regs[8];
386
387 dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
388 "alarm set (old status)",
389 ds1307->regs[0], ds1307->regs[1],
390 ds1307->regs[2], ds1307->regs[3],
391 ds1307->regs[4], ds1307->regs[5],
392 ds1307->regs[6], control, status);
393
394 /* set ALARM1, using 24 hour and day-of-month modes */
395 *buf++ = DS1339_REG_ALARM1_SECS; /* first register addr */
396 buf[0] = bin2bcd(t->time.tm_sec);
397 buf[1] = bin2bcd(t->time.tm_min);
398 buf[2] = bin2bcd(t->time.tm_hour);
399 buf[3] = bin2bcd(t->time.tm_mday);
400
401 /* set ALARM2 to non-garbage */
402 buf[4] = 0;
403 buf[5] = 0;
404 buf[6] = 0;
405
406 /* optionally enable ALARM1 */
407 buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
408 if (t->enabled) {
409 dev_dbg(dev, "alarm IRQ armed\n");
410 buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
411 }
412 buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
413
414 ds1307->msg[1].flags = 0;
415 ds1307->msg[1].len = 10;
416
417 ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
418 &ds1307->msg[1], 1);
419 if (ret != 1) {
420 dev_err(dev, "can't set alarm time\n");
421 return -EIO;
422 }
423
424 return 0;
425}
426
427static int ds1307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
428{
429 struct i2c_client *client = to_i2c_client(dev);
430 struct ds1307 *ds1307 = i2c_get_clientdata(client);
431 int ret;
432
433 switch (cmd) {
434 case RTC_AIE_OFF:
435 if (!test_bit(HAS_ALARM, &ds1307->flags))
436 return -ENOTTY;
437
438 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
439 if (ret < 0)
440 return ret;
441
442 ret &= ~DS1337_BIT_A1IE;
443
444 ret = i2c_smbus_write_byte_data(client,
445 DS1337_REG_CONTROL, ret);
446 if (ret < 0)
447 return ret;
448
449 break;
450
451 case RTC_AIE_ON:
452 if (!test_bit(HAS_ALARM, &ds1307->flags))
453 return -ENOTTY;
454
455 ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
456 if (ret < 0)
457 return ret;
458
459 ret |= DS1337_BIT_A1IE;
460
461 ret = i2c_smbus_write_byte_data(client,
462 DS1337_REG_CONTROL, ret);
463 if (ret < 0)
464 return ret;
465
466 break;
467
468 default:
469 return -ENOIOCTLCMD;
470 }
471
472 return 0;
473}
474
234static const struct rtc_class_ops ds13xx_rtc_ops = { 475static const struct rtc_class_ops ds13xx_rtc_ops = {
235 .read_time = ds1307_get_time, 476 .read_time = ds1307_get_time,
236 .set_time = ds1307_set_time, 477 .set_time = ds1307_set_time,
478 .read_alarm = ds1307_read_alarm,
479 .set_alarm = ds1307_set_alarm,
480 .ioctl = ds1307_ioctl,
237}; 481};
238 482
239/*----------------------------------------------------------------------*/ 483/*----------------------------------------------------------------------*/
@@ -307,7 +551,6 @@ static struct bin_attribute nvram = {
307 .attr = { 551 .attr = {
308 .name = "nvram", 552 .name = "nvram",
309 .mode = S_IRUGO | S_IWUSR, 553 .mode = S_IRUGO | S_IWUSR,
310 .owner = THIS_MODULE,
311 }, 554 },
312 555
313 .read = ds1307_nvram_read, 556 .read = ds1307_nvram_read,
@@ -327,6 +570,7 @@ static int __devinit ds1307_probe(struct i2c_client *client,
327 int tmp; 570 int tmp;
328 const struct chip_desc *chip = &chips[id->driver_data]; 571 const struct chip_desc *chip = &chips[id->driver_data];
329 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 572 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
573 int want_irq = false;
330 574
331 if (!i2c_check_functionality(adapter, 575 if (!i2c_check_functionality(adapter,
332 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) 576 I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
@@ -353,6 +597,12 @@ static int __devinit ds1307_probe(struct i2c_client *client,
353 switch (ds1307->type) { 597 switch (ds1307->type) {
354 case ds_1337: 598 case ds_1337:
355 case ds_1339: 599 case ds_1339:
600 /* has IRQ? */
601 if (ds1307->client->irq > 0 && chip->alarm) {
602 INIT_WORK(&ds1307->work, ds1307_work);
603 want_irq = true;
604 }
605
356 ds1307->reg_addr = DS1337_REG_CONTROL; 606 ds1307->reg_addr = DS1337_REG_CONTROL;
357 ds1307->msg[1].len = 2; 607 ds1307->msg[1].len = 2;
358 608
@@ -369,8 +619,20 @@ static int __devinit ds1307_probe(struct i2c_client *client,
369 619
370 /* oscillator off? turn it on, so clock can tick. */ 620 /* oscillator off? turn it on, so clock can tick. */
371 if (ds1307->regs[0] & DS1337_BIT_nEOSC) 621 if (ds1307->regs[0] & DS1337_BIT_nEOSC)
372 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, 622 ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
373 ds1307->regs[0] & ~DS1337_BIT_nEOSC); 623
624 /* Using IRQ? Disable the square wave and both alarms.
625 * For ds1339, be sure alarms can trigger when we're
626 * running on Vbackup (BBSQI); we assume ds1337 will
627 * ignore that bit
628 */
629 if (want_irq) {
630 ds1307->regs[0] |= DS1337_BIT_INTCN | DS1339_BIT_BBSQI;
631 ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
632 }
633
634 i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
635 ds1307->regs[0]);
374 636
375 /* oscillator fault? clear flag, and warn */ 637 /* oscillator fault? clear flag, and warn */
376 if (ds1307->regs[1] & DS1337_BIT_OSF) { 638 if (ds1307->regs[1] & DS1337_BIT_OSF) {
@@ -446,18 +708,18 @@ read_rtc:
446 } 708 }
447 709
448 tmp = ds1307->regs[DS1307_REG_SECS]; 710 tmp = ds1307->regs[DS1307_REG_SECS];
449 tmp = BCD2BIN(tmp & 0x7f); 711 tmp = bcd2bin(tmp & 0x7f);
450 if (tmp > 60) 712 if (tmp > 60)
451 goto exit_bad; 713 goto exit_bad;
452 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f); 714 tmp = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
453 if (tmp > 60) 715 if (tmp > 60)
454 goto exit_bad; 716 goto exit_bad;
455 717
456 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f); 718 tmp = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
457 if (tmp == 0 || tmp > 31) 719 if (tmp == 0 || tmp > 31)
458 goto exit_bad; 720 goto exit_bad;
459 721
460 tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f); 722 tmp = bcd2bin(ds1307->regs[DS1307_REG_MONTH] & 0x1f);
461 if (tmp == 0 || tmp > 12) 723 if (tmp == 0 || tmp > 12)
462 goto exit_bad; 724 goto exit_bad;
463 725
@@ -476,14 +738,14 @@ read_rtc:
476 /* Be sure we're in 24 hour mode. Multi-master systems 738 /* Be sure we're in 24 hour mode. Multi-master systems
477 * take note... 739 * take note...
478 */ 740 */
479 tmp = BCD2BIN(tmp & 0x1f); 741 tmp = bcd2bin(tmp & 0x1f);
480 if (tmp == 12) 742 if (tmp == 12)
481 tmp = 0; 743 tmp = 0;
482 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM) 744 if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
483 tmp += 12; 745 tmp += 12;
484 i2c_smbus_write_byte_data(client, 746 i2c_smbus_write_byte_data(client,
485 DS1307_REG_HOUR, 747 DS1307_REG_HOUR,
486 BIN2BCD(tmp)); 748 bin2bcd(tmp));
487 } 749 }
488 750
489 ds1307->rtc = rtc_device_register(client->name, &client->dev, 751 ds1307->rtc = rtc_device_register(client->name, &client->dev,
@@ -495,10 +757,22 @@ read_rtc:
495 goto exit_free; 757 goto exit_free;
496 } 758 }
497 759
760 if (want_irq) {
761 err = request_irq(client->irq, ds1307_irq, 0,
762 ds1307->rtc->name, client);
763 if (err) {
764 dev_err(&client->dev,
765 "unable to request IRQ!\n");
766 goto exit_irq;
767 }
768 set_bit(HAS_ALARM, &ds1307->flags);
769 dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
770 }
771
498 if (chip->nvram56) { 772 if (chip->nvram56) {
499 err = sysfs_create_bin_file(&client->dev.kobj, &nvram); 773 err = sysfs_create_bin_file(&client->dev.kobj, &nvram);
500 if (err == 0) { 774 if (err == 0) {
501 ds1307->has_nvram = true; 775 set_bit(HAS_NVRAM, &ds1307->flags);
502 dev_info(&client->dev, "56 bytes nvram\n"); 776 dev_info(&client->dev, "56 bytes nvram\n");
503 } 777 }
504 } 778 }
@@ -512,7 +786,9 @@ exit_bad:
512 ds1307->regs[2], ds1307->regs[3], 786 ds1307->regs[2], ds1307->regs[3],
513 ds1307->regs[4], ds1307->regs[5], 787 ds1307->regs[4], ds1307->regs[5],
514 ds1307->regs[6]); 788 ds1307->regs[6]);
515 789exit_irq:
790 if (ds1307->rtc)
791 rtc_device_unregister(ds1307->rtc);
516exit_free: 792exit_free:
517 kfree(ds1307); 793 kfree(ds1307);
518 return err; 794 return err;
@@ -520,9 +796,14 @@ exit_free:
520 796
521static int __devexit ds1307_remove(struct i2c_client *client) 797static int __devexit ds1307_remove(struct i2c_client *client)
522{ 798{
523 struct ds1307 *ds1307 = i2c_get_clientdata(client); 799 struct ds1307 *ds1307 = i2c_get_clientdata(client);
800
801 if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
802 free_irq(client->irq, client);
803 cancel_work_sync(&ds1307->work);
804 }
524 805
525 if (ds1307->has_nvram) 806 if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
526 sysfs_remove_bin_file(&client->dev.kobj, &nvram); 807 sysfs_remove_bin_file(&client->dev.kobj, &nvram);
527 808
528 rtc_device_unregister(ds1307->rtc); 809 rtc_device_unregister(ds1307->rtc);
diff --git a/drivers/rtc/rtc-ds1374.c b/drivers/rtc/rtc-ds1374.c
index a150418fba76..a5b0fc09f0c6 100644
--- a/drivers/rtc/rtc-ds1374.c
+++ b/drivers/rtc/rtc-ds1374.c
@@ -429,12 +429,33 @@ static int __devexit ds1374_remove(struct i2c_client *client)
429 return 0; 429 return 0;
430} 430}
431 431
432#ifdef CONFIG_PM
433static int ds1374_suspend(struct i2c_client *client, pm_message_t state)
434{
435 if (client->irq >= 0 && device_may_wakeup(&client->dev))
436 enable_irq_wake(client->irq);
437 return 0;
438}
439
440static int ds1374_resume(struct i2c_client *client)
441{
442 if (client->irq >= 0 && device_may_wakeup(&client->dev))
443 disable_irq_wake(client->irq);
444 return 0;
445}
446#else
447#define ds1374_suspend NULL
448#define ds1374_resume NULL
449#endif
450
432static struct i2c_driver ds1374_driver = { 451static struct i2c_driver ds1374_driver = {
433 .driver = { 452 .driver = {
434 .name = "rtc-ds1374", 453 .name = "rtc-ds1374",
435 .owner = THIS_MODULE, 454 .owner = THIS_MODULE,
436 }, 455 },
437 .probe = ds1374_probe, 456 .probe = ds1374_probe,
457 .suspend = ds1374_suspend,
458 .resume = ds1374_resume,
438 .remove = __devexit_p(ds1374_remove), 459 .remove = __devexit_p(ds1374_remove),
439 .id_table = ds1374_id, 460 .id_table = ds1374_id,
440}; 461};
diff --git a/drivers/rtc/rtc-ds1390.c b/drivers/rtc/rtc-ds1390.c
new file mode 100644
index 000000000000..599e976bf014
--- /dev/null
+++ b/drivers/rtc/rtc-ds1390.c
@@ -0,0 +1,220 @@
1/*
2 * rtc-ds1390.c -- driver for DS1390/93/94
3 *
4 * Copyright (C) 2008 Mercury IMC Ltd
5 * Written by Mark Jackson <mpfj@mimc.co.uk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * NOTE : Currently this driver only supports the bare minimum for read
12 * and write the RTC. The extra features provided by the chip family
13 * (alarms, trickle charger, different control registers) are unavailable.
14 */
15
16#include <linux/platform_device.h>
17#include <linux/rtc.h>
18#include <linux/spi/spi.h>
19#include <linux/bcd.h>
20
21#define DS1390_REG_100THS 0x00
22#define DS1390_REG_SECONDS 0x01
23#define DS1390_REG_MINUTES 0x02
24#define DS1390_REG_HOURS 0x03
25#define DS1390_REG_DAY 0x04
26#define DS1390_REG_DATE 0x05
27#define DS1390_REG_MONTH_CENT 0x06
28#define DS1390_REG_YEAR 0x07
29
30#define DS1390_REG_ALARM_100THS 0x08
31#define DS1390_REG_ALARM_SECONDS 0x09
32#define DS1390_REG_ALARM_MINUTES 0x0A
33#define DS1390_REG_ALARM_HOURS 0x0B
34#define DS1390_REG_ALARM_DAY_DATE 0x0C
35
36#define DS1390_REG_CONTROL 0x0D
37#define DS1390_REG_STATUS 0x0E
38#define DS1390_REG_TRICKLE 0x0F
39
40struct ds1390 {
41 struct rtc_device *rtc;
42 u8 txrx_buf[9]; /* cmd + 8 registers */
43};
44
45static void ds1390_set_reg(struct device *dev, unsigned char address,
46 unsigned char data)
47{
48 struct spi_device *spi = to_spi_device(dev);
49 struct ds1390 *chip = dev_get_drvdata(dev);
50
51 /* Set MSB to indicate write */
52 chip->txrx_buf[0] = address | 0x80;
53 chip->txrx_buf[1] = data;
54
55 /* do the i/o */
56 spi_write_then_read(spi, chip->txrx_buf, 2, NULL, 0);
57}
58
59static int ds1390_get_reg(struct device *dev, unsigned char address,
60 unsigned char *data)
61{
62 struct spi_device *spi = to_spi_device(dev);
63 struct ds1390 *chip = dev_get_drvdata(dev);
64 int status;
65
66 if (!data)
67 return -EINVAL;
68
69 /* Clear MSB to indicate read */
70 chip->txrx_buf[0] = address & 0x7f;
71 /* do the i/o */
72 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
73 if (status != 0)
74 return status;
75
76 *data = chip->txrx_buf[1];
77
78 return 0;
79}
80
81static int ds1390_get_datetime(struct device *dev, struct rtc_time *dt)
82{
83 struct spi_device *spi = to_spi_device(dev);
84 struct ds1390 *chip = dev_get_drvdata(dev);
85 int status;
86
87 /* build the message */
88 chip->txrx_buf[0] = DS1390_REG_SECONDS;
89
90 /* do the i/o */
91 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
92 if (status != 0)
93 return status;
94
95 /* The chip sends data in this order:
96 * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */
97 dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
98 dt->tm_min = bcd2bin(chip->txrx_buf[1]);
99 dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
100 dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
101 dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
102 /* mask off century bit */
103 dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
104 /* adjust for century bit */
105 dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
106
107 return rtc_valid_tm(dt);
108}
109
110static int ds1390_set_datetime(struct device *dev, struct rtc_time *dt)
111{
112 struct spi_device *spi = to_spi_device(dev);
113 struct ds1390 *chip = dev_get_drvdata(dev);
114
115 /* build the message */
116 chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
117 chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
118 chip->txrx_buf[2] = bin2bcd(dt->tm_min);
119 chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
120 chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
121 chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
122 chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
123 ((dt->tm_year > 99) ? 0x80 : 0x00);
124 chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
125
126 /* do the i/o */
127 return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
128}
129
130static int ds1390_read_time(struct device *dev, struct rtc_time *tm)
131{
132 return ds1390_get_datetime(dev, tm);
133}
134
135static int ds1390_set_time(struct device *dev, struct rtc_time *tm)
136{
137 return ds1390_set_datetime(dev, tm);
138}
139
140static const struct rtc_class_ops ds1390_rtc_ops = {
141 .read_time = ds1390_read_time,
142 .set_time = ds1390_set_time,
143};
144
145static int __devinit ds1390_probe(struct spi_device *spi)
146{
147 struct rtc_device *rtc;
148 unsigned char tmp;
149 struct ds1390 *chip;
150 int res;
151
152 printk(KERN_DEBUG "DS1390 SPI RTC driver\n");
153
154 rtc = rtc_device_register("ds1390",
155 &spi->dev, &ds1390_rtc_ops, THIS_MODULE);
156 if (IS_ERR(rtc)) {
157 printk(KERN_ALERT "RTC : unable to register device\n");
158 return PTR_ERR(rtc);
159 }
160
161 spi->mode = SPI_MODE_3;
162 spi->bits_per_word = 8;
163 spi_setup(spi);
164
165 chip = kzalloc(sizeof *chip, GFP_KERNEL);
166 if (!chip) {
167 printk(KERN_ALERT "RTC : unable to allocate device memory\n");
168 rtc_device_unregister(rtc);
169 return -ENOMEM;
170 }
171 chip->rtc = rtc;
172 dev_set_drvdata(&spi->dev, chip);
173
174 res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
175 if (res) {
176 printk(KERN_ALERT "RTC : unable to read device\n");
177 rtc_device_unregister(rtc);
178 return res;
179 }
180
181 return 0;
182}
183
184static int __devexit ds1390_remove(struct spi_device *spi)
185{
186 struct ds1390 *chip = platform_get_drvdata(spi);
187 struct rtc_device *rtc = chip->rtc;
188
189 if (rtc)
190 rtc_device_unregister(rtc);
191
192 kfree(chip);
193
194 return 0;
195}
196
197static struct spi_driver ds1390_driver = {
198 .driver = {
199 .name = "rtc-ds1390",
200 .owner = THIS_MODULE,
201 },
202 .probe = ds1390_probe,
203 .remove = __devexit_p(ds1390_remove),
204};
205
206static __init int ds1390_init(void)
207{
208 return spi_register_driver(&ds1390_driver);
209}
210module_init(ds1390_init);
211
212static __exit void ds1390_exit(void)
213{
214 spi_unregister_driver(&ds1390_driver);
215}
216module_exit(ds1390_exit);
217
218MODULE_DESCRIPTION("DS1390/93/94 SPI RTC driver");
219MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
220MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-ds1511.c b/drivers/rtc/rtc-ds1511.c
index 0f0d27d1c4ca..25caada78398 100644
--- a/drivers/rtc/rtc-ds1511.c
+++ b/drivers/rtc/rtc-ds1511.c
@@ -153,8 +153,8 @@ ds1511_wdog_set(unsigned long deciseconds)
153 /* 153 /*
154 * set the wdog values in the wdog registers 154 * set the wdog values in the wdog registers
155 */ 155 */
156 rtc_write(BIN2BCD(deciseconds % 100), DS1511_WD_MSEC); 156 rtc_write(bin2bcd(deciseconds % 100), DS1511_WD_MSEC);
157 rtc_write(BIN2BCD(deciseconds / 100), DS1511_WD_SEC); 157 rtc_write(bin2bcd(deciseconds / 100), DS1511_WD_SEC);
158 /* 158 /*
159 * set wdog enable and wdog 'steering' bit to issue a reset 159 * set wdog enable and wdog 'steering' bit to issue a reset
160 */ 160 */
@@ -220,13 +220,13 @@ static int ds1511_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
220 /* 220 /*
221 * each register is a different number of valid bits 221 * each register is a different number of valid bits
222 */ 222 */
223 sec = BIN2BCD(sec) & 0x7f; 223 sec = bin2bcd(sec) & 0x7f;
224 min = BIN2BCD(min) & 0x7f; 224 min = bin2bcd(min) & 0x7f;
225 hrs = BIN2BCD(hrs) & 0x3f; 225 hrs = bin2bcd(hrs) & 0x3f;
226 day = BIN2BCD(day) & 0x3f; 226 day = bin2bcd(day) & 0x3f;
227 mon = BIN2BCD(mon) & 0x1f; 227 mon = bin2bcd(mon) & 0x1f;
228 yrs = BIN2BCD(yrs) & 0xff; 228 yrs = bin2bcd(yrs) & 0xff;
229 cen = BIN2BCD(cen) & 0xff; 229 cen = bin2bcd(cen) & 0xff;
230 230
231 spin_lock_irqsave(&ds1511_lock, flags); 231 spin_lock_irqsave(&ds1511_lock, flags);
232 rtc_disable_update(); 232 rtc_disable_update();
@@ -264,14 +264,14 @@ static int ds1511_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
264 rtc_enable_update(); 264 rtc_enable_update();
265 spin_unlock_irqrestore(&ds1511_lock, flags); 265 spin_unlock_irqrestore(&ds1511_lock, flags);
266 266
267 rtc_tm->tm_sec = BCD2BIN(rtc_tm->tm_sec); 267 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
268 rtc_tm->tm_min = BCD2BIN(rtc_tm->tm_min); 268 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
269 rtc_tm->tm_hour = BCD2BIN(rtc_tm->tm_hour); 269 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
270 rtc_tm->tm_mday = BCD2BIN(rtc_tm->tm_mday); 270 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
271 rtc_tm->tm_wday = BCD2BIN(rtc_tm->tm_wday); 271 rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
272 rtc_tm->tm_mon = BCD2BIN(rtc_tm->tm_mon); 272 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
273 rtc_tm->tm_year = BCD2BIN(rtc_tm->tm_year); 273 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
274 century = BCD2BIN(century) * 100; 274 century = bcd2bin(century) * 100;
275 275
276 /* 276 /*
277 * Account for differences between how the RTC uses the values 277 * Account for differences between how the RTC uses the values
@@ -304,16 +304,16 @@ ds1511_rtc_update_alarm(struct rtc_plat_data *pdata)
304 304
305 spin_lock_irqsave(&pdata->rtc->irq_lock, flags); 305 spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
306 rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 306 rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
307 0x80 : BIN2BCD(pdata->alrm_mday) & 0x3f, 307 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f,
308 RTC_ALARM_DATE); 308 RTC_ALARM_DATE);
309 rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? 309 rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
310 0x80 : BIN2BCD(pdata->alrm_hour) & 0x3f, 310 0x80 : bin2bcd(pdata->alrm_hour) & 0x3f,
311 RTC_ALARM_HOUR); 311 RTC_ALARM_HOUR);
312 rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? 312 rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
313 0x80 : BIN2BCD(pdata->alrm_min) & 0x7f, 313 0x80 : bin2bcd(pdata->alrm_min) & 0x7f,
314 RTC_ALARM_MIN); 314 RTC_ALARM_MIN);
315 rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? 315 rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
316 0x80 : BIN2BCD(pdata->alrm_sec) & 0x7f, 316 0x80 : bin2bcd(pdata->alrm_sec) & 0x7f,
317 RTC_ALARM_SEC); 317 RTC_ALARM_SEC);
318 rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD); 318 rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD);
319 rtc_read(RTC_CMD1); /* clear interrupts */ 319 rtc_read(RTC_CMD1); /* clear interrupts */
@@ -379,18 +379,6 @@ ds1511_interrupt(int irq, void *dev_id)
379 return IRQ_HANDLED; 379 return IRQ_HANDLED;
380} 380}
381 381
382 static void
383ds1511_rtc_release(struct device *dev)
384{
385 struct platform_device *pdev = to_platform_device(dev);
386 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
387
388 if (pdata->irq >= 0) {
389 pdata->irqen = 0;
390 ds1511_rtc_update_alarm(pdata);
391 }
392}
393
394 static int 382 static int
395ds1511_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 383ds1511_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
396{ 384{
@@ -428,7 +416,6 @@ static const struct rtc_class_ops ds1511_rtc_ops = {
428 .set_time = ds1511_rtc_set_time, 416 .set_time = ds1511_rtc_set_time,
429 .read_alarm = ds1511_rtc_read_alarm, 417 .read_alarm = ds1511_rtc_read_alarm,
430 .set_alarm = ds1511_rtc_set_alarm, 418 .set_alarm = ds1511_rtc_set_alarm,
431 .release = ds1511_rtc_release,
432 .ioctl = ds1511_rtc_ioctl, 419 .ioctl = ds1511_rtc_ioctl,
433}; 420};
434 421
@@ -494,7 +481,6 @@ static struct bin_attribute ds1511_nvram_attr = {
494 .attr = { 481 .attr = {
495 .name = "nvram", 482 .name = "nvram",
496 .mode = S_IRUGO | S_IWUGO, 483 .mode = S_IRUGO | S_IWUGO,
497 .owner = THIS_MODULE,
498 }, 484 },
499 .size = DS1511_RAM_MAX, 485 .size = DS1511_RAM_MAX,
500 .read = ds1511_nvram_read, 486 .read = ds1511_nvram_read,
diff --git a/drivers/rtc/rtc-ds1553.c b/drivers/rtc/rtc-ds1553.c
index a19f11415540..b9475cd20210 100644
--- a/drivers/rtc/rtc-ds1553.c
+++ b/drivers/rtc/rtc-ds1553.c
@@ -78,17 +78,17 @@ static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
78 void __iomem *ioaddr = pdata->ioaddr; 78 void __iomem *ioaddr = pdata->ioaddr;
79 u8 century; 79 u8 century;
80 80
81 century = BIN2BCD((tm->tm_year + 1900) / 100); 81 century = bin2bcd((tm->tm_year + 1900) / 100);
82 82
83 writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL); 83 writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL);
84 84
85 writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR); 85 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
86 writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH); 86 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
87 writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY); 87 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
88 writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE); 88 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
89 writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS); 89 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
90 writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES); 90 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
91 writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS); 91 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
92 92
93 /* RTC_CENTURY and RTC_CONTROL share same register */ 93 /* RTC_CENTURY and RTC_CONTROL share same register */
94 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY); 94 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
@@ -118,14 +118,14 @@ static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm)
118 year = readb(ioaddr + RTC_YEAR); 118 year = readb(ioaddr + RTC_YEAR);
119 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; 119 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
120 writeb(0, ioaddr + RTC_CONTROL); 120 writeb(0, ioaddr + RTC_CONTROL);
121 tm->tm_sec = BCD2BIN(second); 121 tm->tm_sec = bcd2bin(second);
122 tm->tm_min = BCD2BIN(minute); 122 tm->tm_min = bcd2bin(minute);
123 tm->tm_hour = BCD2BIN(hour); 123 tm->tm_hour = bcd2bin(hour);
124 tm->tm_mday = BCD2BIN(day); 124 tm->tm_mday = bcd2bin(day);
125 tm->tm_wday = BCD2BIN(week); 125 tm->tm_wday = bcd2bin(week);
126 tm->tm_mon = BCD2BIN(month) - 1; 126 tm->tm_mon = bcd2bin(month) - 1;
127 /* year is 1900 + tm->tm_year */ 127 /* year is 1900 + tm->tm_year */
128 tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900; 128 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
129 129
130 if (rtc_valid_tm(tm) < 0) { 130 if (rtc_valid_tm(tm) < 0) {
131 dev_err(dev, "retrieved date/time is not valid.\n"); 131 dev_err(dev, "retrieved date/time is not valid.\n");
@@ -141,16 +141,16 @@ static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
141 141
142 spin_lock_irqsave(&pdata->rtc->irq_lock, flags); 142 spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
143 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 143 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
144 0x80 : BIN2BCD(pdata->alrm_mday), 144 0x80 : bin2bcd(pdata->alrm_mday),
145 ioaddr + RTC_DATE_ALARM); 145 ioaddr + RTC_DATE_ALARM);
146 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? 146 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
147 0x80 : BIN2BCD(pdata->alrm_hour), 147 0x80 : bin2bcd(pdata->alrm_hour),
148 ioaddr + RTC_HOURS_ALARM); 148 ioaddr + RTC_HOURS_ALARM);
149 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? 149 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
150 0x80 : BIN2BCD(pdata->alrm_min), 150 0x80 : bin2bcd(pdata->alrm_min),
151 ioaddr + RTC_MINUTES_ALARM); 151 ioaddr + RTC_MINUTES_ALARM);
152 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? 152 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
153 0x80 : BIN2BCD(pdata->alrm_sec), 153 0x80 : bin2bcd(pdata->alrm_sec),
154 ioaddr + RTC_SECONDS_ALARM); 154 ioaddr + RTC_SECONDS_ALARM);
155 writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS); 155 writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS);
156 readb(ioaddr + RTC_FLAGS); /* clear interrupts */ 156 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
@@ -207,17 +207,6 @@ static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
207 return IRQ_HANDLED; 207 return IRQ_HANDLED;
208} 208}
209 209
210static void ds1553_rtc_release(struct device *dev)
211{
212 struct platform_device *pdev = to_platform_device(dev);
213 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
214
215 if (pdata->irq >= 0) {
216 pdata->irqen = 0;
217 ds1553_rtc_update_alarm(pdata);
218 }
219}
220
221static int ds1553_rtc_ioctl(struct device *dev, unsigned int cmd, 210static int ds1553_rtc_ioctl(struct device *dev, unsigned int cmd,
222 unsigned long arg) 211 unsigned long arg)
223{ 212{
@@ -254,7 +243,6 @@ static const struct rtc_class_ops ds1553_rtc_ops = {
254 .set_time = ds1553_rtc_set_time, 243 .set_time = ds1553_rtc_set_time,
255 .read_alarm = ds1553_rtc_read_alarm, 244 .read_alarm = ds1553_rtc_read_alarm,
256 .set_alarm = ds1553_rtc_set_alarm, 245 .set_alarm = ds1553_rtc_set_alarm,
257 .release = ds1553_rtc_release,
258 .ioctl = ds1553_rtc_ioctl, 246 .ioctl = ds1553_rtc_ioctl,
259}; 247};
260 248
diff --git a/drivers/rtc/rtc-ds1672.c b/drivers/rtc/rtc-ds1672.c
index 6fa4556f5f5c..341d7a5b45a2 100644
--- a/drivers/rtc/rtc-ds1672.c
+++ b/drivers/rtc/rtc-ds1672.c
@@ -9,17 +9,10 @@
9 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
10 */ 10 */
11 11
12#include <linux/module.h>
13#include <linux/i2c.h> 12#include <linux/i2c.h>
14#include <linux/rtc.h> 13#include <linux/rtc.h>
15 14
16#define DRV_VERSION "0.3" 15#define DRV_VERSION "0.4"
17
18/* Addresses to scan: none. This chip cannot be detected. */
19static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
20
21/* Insmod parameters */
22I2C_CLIENT_INSMOD;
23 16
24/* Registers */ 17/* Registers */
25 18
@@ -29,8 +22,7 @@ I2C_CLIENT_INSMOD;
29 22
30#define DS1672_REG_CONTROL_EOSC 0x80 23#define DS1672_REG_CONTROL_EOSC 0x80
31 24
32/* Prototypes */ 25static struct i2c_driver ds1672_driver;
33static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind);
34 26
35/* 27/*
36 * In the routines that deal directly with the ds1672 hardware, we use 28 * In the routines that deal directly with the ds1672 hardware, we use
@@ -44,8 +36,8 @@ static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
44 unsigned char buf[4]; 36 unsigned char buf[4];
45 37
46 struct i2c_msg msgs[] = { 38 struct i2c_msg msgs[] = {
47 { client->addr, 0, 1, &addr }, /* setup read ptr */ 39 {client->addr, 0, 1, &addr}, /* setup read ptr */
48 { client->addr, I2C_M_RD, 4, buf }, /* read date */ 40 {client->addr, I2C_M_RD, 4, buf}, /* read date */
49 }; 41 };
50 42
51 /* read date registers */ 43 /* read date registers */
@@ -80,7 +72,7 @@ static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
80 buf[2] = (secs & 0x0000FF00) >> 8; 72 buf[2] = (secs & 0x0000FF00) >> 8;
81 buf[3] = (secs & 0x00FF0000) >> 16; 73 buf[3] = (secs & 0x00FF0000) >> 16;
82 buf[4] = (secs & 0xFF000000) >> 24; 74 buf[4] = (secs & 0xFF000000) >> 24;
83 buf[5] = 0; /* set control reg to enable counting */ 75 buf[5] = 0; /* set control reg to enable counting */
84 76
85 xfer = i2c_master_send(client, buf, 6); 77 xfer = i2c_master_send(client, buf, 6);
86 if (xfer != 6) { 78 if (xfer != 6) {
@@ -127,8 +119,8 @@ static int ds1672_get_control(struct i2c_client *client, u8 *status)
127 unsigned char addr = DS1672_REG_CONTROL; 119 unsigned char addr = DS1672_REG_CONTROL;
128 120
129 struct i2c_msg msgs[] = { 121 struct i2c_msg msgs[] = {
130 { client->addr, 0, 1, &addr }, /* setup read ptr */ 122 {client->addr, 0, 1, &addr}, /* setup read ptr */
131 { client->addr, I2C_M_RD, 1, status }, /* read control */ 123 {client->addr, I2C_M_RD, 1, status}, /* read control */
132 }; 124 };
133 125
134 /* read control register */ 126 /* read control register */
@@ -141,7 +133,8 @@ static int ds1672_get_control(struct i2c_client *client, u8 *status)
141} 133}
142 134
143/* following are the sysfs callback functions */ 135/* following are the sysfs callback functions */
144static ssize_t show_control(struct device *dev, struct device_attribute *attr, char *buf) 136static ssize_t show_control(struct device *dev, struct device_attribute *attr,
137 char *buf)
145{ 138{
146 struct i2c_client *client = to_i2c_client(dev); 139 struct i2c_client *client = to_i2c_client(dev);
147 u8 control; 140 u8 control;
@@ -152,85 +145,46 @@ static ssize_t show_control(struct device *dev, struct device_attribute *attr, c
152 return err; 145 return err;
153 146
154 return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC) 147 return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
155 ? "disabled" : "enabled"); 148 ? "disabled" : "enabled");
156} 149}
150
157static DEVICE_ATTR(control, S_IRUGO, show_control, NULL); 151static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
158 152
159static const struct rtc_class_ops ds1672_rtc_ops = { 153static const struct rtc_class_ops ds1672_rtc_ops = {
160 .read_time = ds1672_rtc_read_time, 154 .read_time = ds1672_rtc_read_time,
161 .set_time = ds1672_rtc_set_time, 155 .set_time = ds1672_rtc_set_time,
162 .set_mmss = ds1672_rtc_set_mmss, 156 .set_mmss = ds1672_rtc_set_mmss,
163}; 157};
164 158
165static int ds1672_attach(struct i2c_adapter *adapter) 159static int ds1672_remove(struct i2c_client *client)
166{ 160{
167 return i2c_probe(adapter, &addr_data, ds1672_probe);
168}
169
170static int ds1672_detach(struct i2c_client *client)
171{
172 int err;
173 struct rtc_device *rtc = i2c_get_clientdata(client); 161 struct rtc_device *rtc = i2c_get_clientdata(client);
174 162
175 if (rtc) 163 if (rtc)
176 rtc_device_unregister(rtc); 164 rtc_device_unregister(rtc);
177 165
178 if ((err = i2c_detach_client(client)))
179 return err;
180
181 kfree(client);
182
183 return 0; 166 return 0;
184} 167}
185 168
186static struct i2c_driver ds1672_driver = { 169static int ds1672_probe(struct i2c_client *client,
187 .driver = { 170 const struct i2c_device_id *id)
188 .name = "ds1672",
189 },
190 .id = I2C_DRIVERID_DS1672,
191 .attach_adapter = &ds1672_attach,
192 .detach_client = &ds1672_detach,
193};
194
195static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
196{ 171{
197 int err = 0; 172 int err = 0;
198 u8 control; 173 u8 control;
199 struct i2c_client *client;
200 struct rtc_device *rtc; 174 struct rtc_device *rtc;
201 175
202 dev_dbg(&adapter->dev, "%s\n", __func__); 176 dev_dbg(&client->dev, "%s\n", __func__);
203 177
204 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { 178 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
205 err = -ENODEV; 179 return -ENODEV;
206 goto exit;
207 }
208
209 if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
210 err = -ENOMEM;
211 goto exit;
212 }
213
214 /* I2C client */
215 client->addr = address;
216 client->driver = &ds1672_driver;
217 client->adapter = adapter;
218
219 strlcpy(client->name, ds1672_driver.driver.name, I2C_NAME_SIZE);
220
221 /* Inform the i2c layer */
222 if ((err = i2c_attach_client(client)))
223 goto exit_kfree;
224 180
225 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); 181 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
226 182
227 rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev, 183 rtc = rtc_device_register(ds1672_driver.driver.name, &client->dev,
228 &ds1672_rtc_ops, THIS_MODULE); 184 &ds1672_rtc_ops, THIS_MODULE);
229 185
230 if (IS_ERR(rtc)) { 186 if (IS_ERR(rtc))
231 err = PTR_ERR(rtc); 187 return PTR_ERR(rtc);
232 goto exit_detach;
233 }
234 188
235 i2c_set_clientdata(client, rtc); 189 i2c_set_clientdata(client, rtc);
236 190
@@ -241,7 +195,7 @@ static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
241 195
242 if (control & DS1672_REG_CONTROL_EOSC) 196 if (control & DS1672_REG_CONTROL_EOSC)
243 dev_warn(&client->dev, "Oscillator not enabled. " 197 dev_warn(&client->dev, "Oscillator not enabled. "
244 "Set time to enable.\n"); 198 "Set time to enable.\n");
245 199
246 /* Register sysfs hooks */ 200 /* Register sysfs hooks */
247 err = device_create_file(&client->dev, &dev_attr_control); 201 err = device_create_file(&client->dev, &dev_attr_control);
@@ -250,19 +204,19 @@ static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
250 204
251 return 0; 205 return 0;
252 206
253exit_devreg: 207 exit_devreg:
254 rtc_device_unregister(rtc); 208 rtc_device_unregister(rtc);
255
256exit_detach:
257 i2c_detach_client(client);
258
259exit_kfree:
260 kfree(client);
261
262exit:
263 return err; 209 return err;
264} 210}
265 211
212static struct i2c_driver ds1672_driver = {
213 .driver = {
214 .name = "rtc-ds1672",
215 },
216 .probe = &ds1672_probe,
217 .remove = &ds1672_remove,
218};
219
266static int __init ds1672_init(void) 220static int __init ds1672_init(void)
267{ 221{
268 return i2c_add_driver(&ds1672_driver); 222 return i2c_add_driver(&ds1672_driver);
diff --git a/drivers/rtc/rtc-ds1742.c b/drivers/rtc/rtc-ds1742.c
index 24d35ede2dbf..8bc8501bffc8 100644
--- a/drivers/rtc/rtc-ds1742.c
+++ b/drivers/rtc/rtc-ds1742.c
@@ -66,17 +66,17 @@ static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
66 void __iomem *ioaddr = pdata->ioaddr_rtc; 66 void __iomem *ioaddr = pdata->ioaddr_rtc;
67 u8 century; 67 u8 century;
68 68
69 century = BIN2BCD((tm->tm_year + 1900) / 100); 69 century = bin2bcd((tm->tm_year + 1900) / 100);
70 70
71 writeb(RTC_WRITE, ioaddr + RTC_CONTROL); 71 writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
72 72
73 writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR); 73 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
74 writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH); 74 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
75 writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY); 75 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
76 writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE); 76 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
77 writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS); 77 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
78 writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES); 78 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
79 writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS); 79 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
80 80
81 /* RTC_CENTURY and RTC_CONTROL share same register */ 81 /* RTC_CENTURY and RTC_CONTROL share same register */
82 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY); 82 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
@@ -106,14 +106,14 @@ static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
106 year = readb(ioaddr + RTC_YEAR); 106 year = readb(ioaddr + RTC_YEAR);
107 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK; 107 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
108 writeb(0, ioaddr + RTC_CONTROL); 108 writeb(0, ioaddr + RTC_CONTROL);
109 tm->tm_sec = BCD2BIN(second); 109 tm->tm_sec = bcd2bin(second);
110 tm->tm_min = BCD2BIN(minute); 110 tm->tm_min = bcd2bin(minute);
111 tm->tm_hour = BCD2BIN(hour); 111 tm->tm_hour = bcd2bin(hour);
112 tm->tm_mday = BCD2BIN(day); 112 tm->tm_mday = bcd2bin(day);
113 tm->tm_wday = BCD2BIN(week); 113 tm->tm_wday = bcd2bin(week);
114 tm->tm_mon = BCD2BIN(month) - 1; 114 tm->tm_mon = bcd2bin(month) - 1;
115 /* year is 1900 + tm->tm_year */ 115 /* year is 1900 + tm->tm_year */
116 tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900; 116 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
117 117
118 if (rtc_valid_tm(tm) < 0) { 118 if (rtc_valid_tm(tm) < 0) {
119 dev_err(dev, "retrieved date/time is not valid.\n"); 119 dev_err(dev, "retrieved date/time is not valid.\n");
diff --git a/drivers/rtc/rtc-ds3234.c b/drivers/rtc/rtc-ds3234.c
new file mode 100644
index 000000000000..45e5b106af73
--- /dev/null
+++ b/drivers/rtc/rtc-ds3234.c
@@ -0,0 +1,290 @@
1/* drivers/rtc/rtc-ds3234.c
2 *
3 * Driver for Dallas Semiconductor (DS3234) SPI RTC with Integrated Crystal
4 * and SRAM.
5 *
6 * Copyright (C) 2008 MIMOMax Wireless Ltd.
7 *
8 * 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 * published by the Free Software Foundation.
11 *
12 * Changelog:
13 *
14 * 07-May-2008: Dennis Aberilla <denzzzhome@yahoo.com>
15 * - Created based on the max6902 code. Only implements the
16 * date/time keeping functions; no SRAM yet.
17 */
18
19#include <linux/device.h>
20#include <linux/platform_device.h>
21#include <linux/rtc.h>
22#include <linux/spi/spi.h>
23#include <linux/bcd.h>
24
25#define DS3234_REG_SECONDS 0x00
26#define DS3234_REG_MINUTES 0x01
27#define DS3234_REG_HOURS 0x02
28#define DS3234_REG_DAY 0x03
29#define DS3234_REG_DATE 0x04
30#define DS3234_REG_MONTH 0x05
31#define DS3234_REG_YEAR 0x06
32#define DS3234_REG_CENTURY (1 << 7) /* Bit 7 of the Month register */
33
34#define DS3234_REG_CONTROL 0x0E
35#define DS3234_REG_CONT_STAT 0x0F
36
37#undef DS3234_DEBUG
38
39struct ds3234 {
40 struct rtc_device *rtc;
41 u8 buf[8]; /* Burst read: addr + 7 regs */
42 u8 tx_buf[2];
43 u8 rx_buf[2];
44};
45
46static void ds3234_set_reg(struct device *dev, unsigned char address,
47 unsigned char data)
48{
49 struct spi_device *spi = to_spi_device(dev);
50 unsigned char buf[2];
51
52 /* MSB must be '1' to indicate write */
53 buf[0] = address | 0x80;
54 buf[1] = data;
55
56 spi_write(spi, buf, 2);
57}
58
59static int ds3234_get_reg(struct device *dev, unsigned char address,
60 unsigned char *data)
61{
62 struct spi_device *spi = to_spi_device(dev);
63 struct ds3234 *chip = dev_get_drvdata(dev);
64 struct spi_message message;
65 struct spi_transfer xfer;
66 int status;
67
68 if (!data)
69 return -EINVAL;
70
71 /* Build our spi message */
72 spi_message_init(&message);
73 memset(&xfer, 0, sizeof(xfer));
74
75 /* Address + dummy tx byte */
76 xfer.len = 2;
77 xfer.tx_buf = chip->tx_buf;
78 xfer.rx_buf = chip->rx_buf;
79
80 chip->tx_buf[0] = address;
81 chip->tx_buf[1] = 0xff;
82
83 spi_message_add_tail(&xfer, &message);
84
85 /* do the i/o */
86 status = spi_sync(spi, &message);
87 if (status == 0)
88 status = message.status;
89 else
90 return status;
91
92 *data = chip->rx_buf[1];
93
94 return status;
95}
96
97static int ds3234_get_datetime(struct device *dev, struct rtc_time *dt)
98{
99 struct spi_device *spi = to_spi_device(dev);
100 struct ds3234 *chip = dev_get_drvdata(dev);
101 struct spi_message message;
102 struct spi_transfer xfer;
103 int status;
104
105 /* build the message */
106 spi_message_init(&message);
107 memset(&xfer, 0, sizeof(xfer));
108 xfer.len = 1 + 7; /* Addr + 7 registers */
109 xfer.tx_buf = chip->buf;
110 xfer.rx_buf = chip->buf;
111 chip->buf[0] = 0x00; /* Start address */
112 spi_message_add_tail(&xfer, &message);
113
114 /* do the i/o */
115 status = spi_sync(spi, &message);
116 if (status == 0)
117 status = message.status;
118 else
119 return status;
120
121 /* Seconds, Minutes, Hours, Day, Date, Month, Year */
122 dt->tm_sec = bcd2bin(chip->buf[1]);
123 dt->tm_min = bcd2bin(chip->buf[2]);
124 dt->tm_hour = bcd2bin(chip->buf[3] & 0x3f);
125 dt->tm_wday = bcd2bin(chip->buf[4]) - 1; /* 0 = Sun */
126 dt->tm_mday = bcd2bin(chip->buf[5]);
127 dt->tm_mon = bcd2bin(chip->buf[6] & 0x1f) - 1; /* 0 = Jan */
128 dt->tm_year = bcd2bin(chip->buf[7] & 0xff) + 100; /* Assume 20YY */
129
130#ifdef DS3234_DEBUG
131 dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
132 dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
133 dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
134 dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
135 dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
136 dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
137 dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
138 dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
139#endif
140
141 return 0;
142}
143
144static int ds3234_set_datetime(struct device *dev, struct rtc_time *dt)
145{
146#ifdef DS3234_DEBUG
147 dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
148 dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
149 dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
150 dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
151 dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
152 dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
153 dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
154 dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
155#endif
156
157 ds3234_set_reg(dev, DS3234_REG_SECONDS, bin2bcd(dt->tm_sec));
158 ds3234_set_reg(dev, DS3234_REG_MINUTES, bin2bcd(dt->tm_min));
159 ds3234_set_reg(dev, DS3234_REG_HOURS, bin2bcd(dt->tm_hour) & 0x3f);
160
161 /* 0 = Sun */
162 ds3234_set_reg(dev, DS3234_REG_DAY, bin2bcd(dt->tm_wday + 1));
163 ds3234_set_reg(dev, DS3234_REG_DATE, bin2bcd(dt->tm_mday));
164
165 /* 0 = Jan */
166 ds3234_set_reg(dev, DS3234_REG_MONTH, bin2bcd(dt->tm_mon + 1));
167
168 /* Assume 20YY although we just want to make sure not to go negative. */
169 if (dt->tm_year > 100)
170 dt->tm_year -= 100;
171
172 ds3234_set_reg(dev, DS3234_REG_YEAR, bin2bcd(dt->tm_year));
173
174 return 0;
175}
176
177static int ds3234_read_time(struct device *dev, struct rtc_time *tm)
178{
179 return ds3234_get_datetime(dev, tm);
180}
181
182static int ds3234_set_time(struct device *dev, struct rtc_time *tm)
183{
184 return ds3234_set_datetime(dev, tm);
185}
186
187static const struct rtc_class_ops ds3234_rtc_ops = {
188 .read_time = ds3234_read_time,
189 .set_time = ds3234_set_time,
190};
191
192static int __devinit ds3234_probe(struct spi_device *spi)
193{
194 struct rtc_device *rtc;
195 unsigned char tmp;
196 struct ds3234 *chip;
197 int res;
198
199 rtc = rtc_device_register("ds3234",
200 &spi->dev, &ds3234_rtc_ops, THIS_MODULE);
201 if (IS_ERR(rtc))
202 return PTR_ERR(rtc);
203
204 spi->mode = SPI_MODE_3;
205 spi->bits_per_word = 8;
206 spi_setup(spi);
207
208 chip = kzalloc(sizeof(struct ds3234), GFP_KERNEL);
209 if (!chip) {
210 rtc_device_unregister(rtc);
211 return -ENOMEM;
212 }
213 chip->rtc = rtc;
214 dev_set_drvdata(&spi->dev, chip);
215
216 res = ds3234_get_reg(&spi->dev, DS3234_REG_SECONDS, &tmp);
217 if (res) {
218 rtc_device_unregister(rtc);
219 return res;
220 }
221
222 /* Control settings
223 *
224 * CONTROL_REG
225 * BIT 7 6 5 4 3 2 1 0
226 * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
227 *
228 * 0 0 0 1 1 1 0 0
229 *
230 * CONTROL_STAT_REG
231 * BIT 7 6 5 4 3 2 1 0
232 * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
233 *
234 * 1 0 0 0 1 0 0 0
235 */
236 ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
237 ds3234_set_reg(&spi->dev, DS3234_REG_CONTROL, tmp & 0x1c);
238
239 ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
240 ds3234_set_reg(&spi->dev, DS3234_REG_CONT_STAT, tmp & 0x88);
241
242 /* Print our settings */
243 ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
244 dev_info(&spi->dev, "Control Reg: 0x%02x\n", tmp);
245
246 ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
247 dev_info(&spi->dev, "Ctrl/Stat Reg: 0x%02x\n", tmp);
248
249 return 0;
250}
251
252static int __devexit ds3234_remove(struct spi_device *spi)
253{
254 struct ds3234 *chip = platform_get_drvdata(spi);
255 struct rtc_device *rtc = chip->rtc;
256
257 if (rtc)
258 rtc_device_unregister(rtc);
259
260 kfree(chip);
261
262 return 0;
263}
264
265static struct spi_driver ds3234_driver = {
266 .driver = {
267 .name = "ds3234",
268 .bus = &spi_bus_type,
269 .owner = THIS_MODULE,
270 },
271 .probe = ds3234_probe,
272 .remove = __devexit_p(ds3234_remove),
273};
274
275static __init int ds3234_init(void)
276{
277 printk(KERN_INFO "DS3234 SPI RTC Driver\n");
278 return spi_register_driver(&ds3234_driver);
279}
280module_init(ds3234_init);
281
282static __exit void ds3234_exit(void)
283{
284 spi_unregister_driver(&ds3234_driver);
285}
286module_exit(ds3234_exit);
287
288MODULE_DESCRIPTION("DS3234 SPI RTC driver");
289MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
290MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-fm3130.c b/drivers/rtc/rtc-fm3130.c
index abfdfcbaa059..3a7be11cc6b9 100644
--- a/drivers/rtc/rtc-fm3130.c
+++ b/drivers/rtc/rtc-fm3130.c
@@ -131,17 +131,17 @@ static int fm3130_get_time(struct device *dev, struct rtc_time *t)
131 fm3130->regs[0xc], fm3130->regs[0xd], 131 fm3130->regs[0xc], fm3130->regs[0xd],
132 fm3130->regs[0xe]); 132 fm3130->regs[0xe]);
133 133
134 t->tm_sec = BCD2BIN(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f); 134 t->tm_sec = bcd2bin(fm3130->regs[FM3130_RTC_SECONDS] & 0x7f);
135 t->tm_min = BCD2BIN(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f); 135 t->tm_min = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f);
136 tmp = fm3130->regs[FM3130_RTC_HOURS] & 0x3f; 136 tmp = fm3130->regs[FM3130_RTC_HOURS] & 0x3f;
137 t->tm_hour = BCD2BIN(tmp); 137 t->tm_hour = bcd2bin(tmp);
138 t->tm_wday = BCD2BIN(fm3130->regs[FM3130_RTC_DAY] & 0x07) - 1; 138 t->tm_wday = bcd2bin(fm3130->regs[FM3130_RTC_DAY] & 0x07) - 1;
139 t->tm_mday = BCD2BIN(fm3130->regs[FM3130_RTC_DATE] & 0x3f); 139 t->tm_mday = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f);
140 tmp = fm3130->regs[FM3130_RTC_MONTHS] & 0x1f; 140 tmp = fm3130->regs[FM3130_RTC_MONTHS] & 0x1f;
141 t->tm_mon = BCD2BIN(tmp) - 1; 141 t->tm_mon = bcd2bin(tmp) - 1;
142 142
143 /* assume 20YY not 19YY, and ignore CF bit */ 143 /* assume 20YY not 19YY, and ignore CF bit */
144 t->tm_year = BCD2BIN(fm3130->regs[FM3130_RTC_YEARS]) + 100; 144 t->tm_year = bcd2bin(fm3130->regs[FM3130_RTC_YEARS]) + 100;
145 145
146 dev_dbg(dev, "%s secs=%d, mins=%d, " 146 dev_dbg(dev, "%s secs=%d, mins=%d, "
147 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", 147 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -167,16 +167,16 @@ static int fm3130_set_time(struct device *dev, struct rtc_time *t)
167 t->tm_mon, t->tm_year, t->tm_wday); 167 t->tm_mon, t->tm_year, t->tm_wday);
168 168
169 /* first register addr */ 169 /* first register addr */
170 buf[FM3130_RTC_SECONDS] = BIN2BCD(t->tm_sec); 170 buf[FM3130_RTC_SECONDS] = bin2bcd(t->tm_sec);
171 buf[FM3130_RTC_MINUTES] = BIN2BCD(t->tm_min); 171 buf[FM3130_RTC_MINUTES] = bin2bcd(t->tm_min);
172 buf[FM3130_RTC_HOURS] = BIN2BCD(t->tm_hour); 172 buf[FM3130_RTC_HOURS] = bin2bcd(t->tm_hour);
173 buf[FM3130_RTC_DAY] = BIN2BCD(t->tm_wday + 1); 173 buf[FM3130_RTC_DAY] = bin2bcd(t->tm_wday + 1);
174 buf[FM3130_RTC_DATE] = BIN2BCD(t->tm_mday); 174 buf[FM3130_RTC_DATE] = bin2bcd(t->tm_mday);
175 buf[FM3130_RTC_MONTHS] = BIN2BCD(t->tm_mon + 1); 175 buf[FM3130_RTC_MONTHS] = bin2bcd(t->tm_mon + 1);
176 176
177 /* assume 20YY not 19YY */ 177 /* assume 20YY not 19YY */
178 tmp = t->tm_year - 100; 178 tmp = t->tm_year - 100;
179 buf[FM3130_RTC_YEARS] = BIN2BCD(tmp); 179 buf[FM3130_RTC_YEARS] = bin2bcd(tmp);
180 180
181 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x" 181 dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x"
182 "%02x %02x %02x %02x %02x %02x %02x %02x\n", 182 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
@@ -222,11 +222,11 @@ static int fm3130_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
222 fm3130->regs[FM3130_ALARM_MONTHS]); 222 fm3130->regs[FM3130_ALARM_MONTHS]);
223 223
224 224
225 tm->tm_sec = BCD2BIN(fm3130->regs[FM3130_ALARM_SECONDS] & 0x7F); 225 tm->tm_sec = bcd2bin(fm3130->regs[FM3130_ALARM_SECONDS] & 0x7F);
226 tm->tm_min = BCD2BIN(fm3130->regs[FM3130_ALARM_MINUTES] & 0x7F); 226 tm->tm_min = bcd2bin(fm3130->regs[FM3130_ALARM_MINUTES] & 0x7F);
227 tm->tm_hour = BCD2BIN(fm3130->regs[FM3130_ALARM_HOURS] & 0x3F); 227 tm->tm_hour = bcd2bin(fm3130->regs[FM3130_ALARM_HOURS] & 0x3F);
228 tm->tm_mday = BCD2BIN(fm3130->regs[FM3130_ALARM_DATE] & 0x3F); 228 tm->tm_mday = bcd2bin(fm3130->regs[FM3130_ALARM_DATE] & 0x3F);
229 tm->tm_mon = BCD2BIN(fm3130->regs[FM3130_ALARM_MONTHS] & 0x1F); 229 tm->tm_mon = bcd2bin(fm3130->regs[FM3130_ALARM_MONTHS] & 0x1F);
230 if (tm->tm_mon > 0) 230 if (tm->tm_mon > 0)
231 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ 231 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
232 dev_dbg(dev, "%s secs=%d, mins=%d, " 232 dev_dbg(dev, "%s secs=%d, mins=%d, "
@@ -252,23 +252,23 @@ static int fm3130_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
252 252
253 if (tm->tm_sec != -1) 253 if (tm->tm_sec != -1)
254 fm3130->regs[FM3130_ALARM_SECONDS] = 254 fm3130->regs[FM3130_ALARM_SECONDS] =
255 BIN2BCD(tm->tm_sec) | 0x80; 255 bin2bcd(tm->tm_sec) | 0x80;
256 256
257 if (tm->tm_min != -1) 257 if (tm->tm_min != -1)
258 fm3130->regs[FM3130_ALARM_MINUTES] = 258 fm3130->regs[FM3130_ALARM_MINUTES] =
259 BIN2BCD(tm->tm_min) | 0x80; 259 bin2bcd(tm->tm_min) | 0x80;
260 260
261 if (tm->tm_hour != -1) 261 if (tm->tm_hour != -1)
262 fm3130->regs[FM3130_ALARM_HOURS] = 262 fm3130->regs[FM3130_ALARM_HOURS] =
263 BIN2BCD(tm->tm_hour) | 0x80; 263 bin2bcd(tm->tm_hour) | 0x80;
264 264
265 if (tm->tm_mday != -1) 265 if (tm->tm_mday != -1)
266 fm3130->regs[FM3130_ALARM_DATE] = 266 fm3130->regs[FM3130_ALARM_DATE] =
267 BIN2BCD(tm->tm_mday) | 0x80; 267 bin2bcd(tm->tm_mday) | 0x80;
268 268
269 if (tm->tm_mon != -1) 269 if (tm->tm_mon != -1)
270 fm3130->regs[FM3130_ALARM_MONTHS] = 270 fm3130->regs[FM3130_ALARM_MONTHS] =
271 BIN2BCD(tm->tm_mon + 1) | 0x80; 271 bin2bcd(tm->tm_mon + 1) | 0x80;
272 272
273 dev_dbg(dev, "alarm write %02x %02x %02x %02x %02x\n", 273 dev_dbg(dev, "alarm write %02x %02x %02x %02x %02x\n",
274 fm3130->regs[FM3130_ALARM_SECONDS], 274 fm3130->regs[FM3130_ALARM_SECONDS],
@@ -414,18 +414,18 @@ static int __devinit fm3130_probe(struct i2c_client *client,
414 /* TODO */ 414 /* TODO */
415 /* TODO need to sanity check alarm */ 415 /* TODO need to sanity check alarm */
416 tmp = fm3130->regs[FM3130_RTC_SECONDS]; 416 tmp = fm3130->regs[FM3130_RTC_SECONDS];
417 tmp = BCD2BIN(tmp & 0x7f); 417 tmp = bcd2bin(tmp & 0x7f);
418 if (tmp > 60) 418 if (tmp > 60)
419 goto exit_bad; 419 goto exit_bad;
420 tmp = BCD2BIN(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f); 420 tmp = bcd2bin(fm3130->regs[FM3130_RTC_MINUTES] & 0x7f);
421 if (tmp > 60) 421 if (tmp > 60)
422 goto exit_bad; 422 goto exit_bad;
423 423
424 tmp = BCD2BIN(fm3130->regs[FM3130_RTC_DATE] & 0x3f); 424 tmp = bcd2bin(fm3130->regs[FM3130_RTC_DATE] & 0x3f);
425 if (tmp == 0 || tmp > 31) 425 if (tmp == 0 || tmp > 31)
426 goto exit_bad; 426 goto exit_bad;
427 427
428 tmp = BCD2BIN(fm3130->regs[FM3130_RTC_MONTHS] & 0x1f); 428 tmp = bcd2bin(fm3130->regs[FM3130_RTC_MONTHS] & 0x1f);
429 if (tmp == 0 || tmp > 12) 429 if (tmp == 0 || tmp > 12)
430 goto exit_bad; 430 goto exit_bad;
431 431
diff --git a/drivers/rtc/rtc-isl1208.c b/drivers/rtc/rtc-isl1208.c
index a81adab6e515..2cd77ab8fc66 100644
--- a/drivers/rtc/rtc-isl1208.c
+++ b/drivers/rtc/rtc-isl1208.c
@@ -259,26 +259,26 @@ isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
259 return sr; 259 return sr;
260 } 260 }
261 261
262 tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SC]); 262 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
263 tm->tm_min = BCD2BIN(regs[ISL1208_REG_MN]); 263 tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
264 264
265 /* HR field has a more complex interpretation */ 265 /* HR field has a more complex interpretation */
266 { 266 {
267 const u8 _hr = regs[ISL1208_REG_HR]; 267 const u8 _hr = regs[ISL1208_REG_HR];
268 if (_hr & ISL1208_REG_HR_MIL) /* 24h format */ 268 if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
269 tm->tm_hour = BCD2BIN(_hr & 0x3f); 269 tm->tm_hour = bcd2bin(_hr & 0x3f);
270 else { 270 else {
271 /* 12h format */ 271 /* 12h format */
272 tm->tm_hour = BCD2BIN(_hr & 0x1f); 272 tm->tm_hour = bcd2bin(_hr & 0x1f);
273 if (_hr & ISL1208_REG_HR_PM) /* PM flag set */ 273 if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
274 tm->tm_hour += 12; 274 tm->tm_hour += 12;
275 } 275 }
276 } 276 }
277 277
278 tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DT]); 278 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
279 tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */ 279 tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
280 tm->tm_year = BCD2BIN(regs[ISL1208_REG_YR]) + 100; 280 tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
281 tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DW]); 281 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
282 282
283 return 0; 283 return 0;
284} 284}
@@ -305,13 +305,13 @@ isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
305 } 305 }
306 306
307 /* MSB of each alarm register is an enable bit */ 307 /* MSB of each alarm register is an enable bit */
308 tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f); 308 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
309 tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f); 309 tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
310 tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f); 310 tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
311 tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f); 311 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
312 tm->tm_mon = 312 tm->tm_mon =
313 BCD2BIN(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1; 313 bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
314 tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03); 314 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
315 315
316 return 0; 316 return 0;
317} 317}
@@ -328,15 +328,15 @@ isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
328 int sr; 328 int sr;
329 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; 329 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
330 330
331 regs[ISL1208_REG_SC] = BIN2BCD(tm->tm_sec); 331 regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
332 regs[ISL1208_REG_MN] = BIN2BCD(tm->tm_min); 332 regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
333 regs[ISL1208_REG_HR] = BIN2BCD(tm->tm_hour) | ISL1208_REG_HR_MIL; 333 regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
334 334
335 regs[ISL1208_REG_DT] = BIN2BCD(tm->tm_mday); 335 regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
336 regs[ISL1208_REG_MO] = BIN2BCD(tm->tm_mon + 1); 336 regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
337 regs[ISL1208_REG_YR] = BIN2BCD(tm->tm_year - 100); 337 regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
338 338
339 regs[ISL1208_REG_DW] = BIN2BCD(tm->tm_wday & 7); 339 regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
340 340
341 sr = isl1208_i2c_get_sr(client); 341 sr = isl1208_i2c_get_sr(client);
342 if (sr < 0) { 342 if (sr < 0) {
diff --git a/drivers/rtc/rtc-m41t80.c b/drivers/rtc/rtc-m41t80.c
index 24bc1689fc74..893f7dece239 100644
--- a/drivers/rtc/rtc-m41t80.c
+++ b/drivers/rtc/rtc-m41t80.c
@@ -56,21 +56,27 @@
56#define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */ 56#define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */
57#define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */ 57#define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */
58#define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */ 58#define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */
59#define M41T80_WATCHDOG_RB2 (1 << 7) /* RB: Watchdog resolution */
60#define M41T80_WATCHDOG_RB1 (1 << 1) /* RB: Watchdog resolution */
61#define M41T80_WATCHDOG_RB0 (1 << 0) /* RB: Watchdog resolution */
59 62
60#define M41T80_FEATURE_HT (1 << 0) 63#define M41T80_FEATURE_HT (1 << 0) /* Halt feature */
61#define M41T80_FEATURE_BL (1 << 1) 64#define M41T80_FEATURE_BL (1 << 1) /* Battery low indicator */
65#define M41T80_FEATURE_SQ (1 << 2) /* Squarewave feature */
66#define M41T80_FEATURE_WD (1 << 3) /* Extra watchdog resolution */
62 67
63#define DRV_VERSION "0.05" 68#define DRV_VERSION "0.05"
64 69
65static const struct i2c_device_id m41t80_id[] = { 70static const struct i2c_device_id m41t80_id[] = {
66 { "m41t80", 0 }, 71 { "m41t65", M41T80_FEATURE_HT | M41T80_FEATURE_WD },
67 { "m41t81", M41T80_FEATURE_HT }, 72 { "m41t80", M41T80_FEATURE_SQ },
68 { "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL }, 73 { "m41t81", M41T80_FEATURE_HT | M41T80_FEATURE_SQ},
69 { "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL }, 74 { "m41t81s", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
70 { "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL }, 75 { "m41t82", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
71 { "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL }, 76 { "m41t83", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
72 { "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL }, 77 { "m41st84", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
73 { "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL }, 78 { "m41st85", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
79 { "m41st87", M41T80_FEATURE_HT | M41T80_FEATURE_BL | M41T80_FEATURE_SQ },
74 { } 80 { }
75}; 81};
76MODULE_DEVICE_TABLE(i2c, m41t80_id); 82MODULE_DEVICE_TABLE(i2c, m41t80_id);
@@ -104,15 +110,15 @@ static int m41t80_get_datetime(struct i2c_client *client,
104 return -EIO; 110 return -EIO;
105 } 111 }
106 112
107 tm->tm_sec = BCD2BIN(buf[M41T80_REG_SEC] & 0x7f); 113 tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f);
108 tm->tm_min = BCD2BIN(buf[M41T80_REG_MIN] & 0x7f); 114 tm->tm_min = bcd2bin(buf[M41T80_REG_MIN] & 0x7f);
109 tm->tm_hour = BCD2BIN(buf[M41T80_REG_HOUR] & 0x3f); 115 tm->tm_hour = bcd2bin(buf[M41T80_REG_HOUR] & 0x3f);
110 tm->tm_mday = BCD2BIN(buf[M41T80_REG_DAY] & 0x3f); 116 tm->tm_mday = bcd2bin(buf[M41T80_REG_DAY] & 0x3f);
111 tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07; 117 tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
112 tm->tm_mon = BCD2BIN(buf[M41T80_REG_MON] & 0x1f) - 1; 118 tm->tm_mon = bcd2bin(buf[M41T80_REG_MON] & 0x1f) - 1;
113 119
114 /* assume 20YY not 19YY, and ignore the Century Bit */ 120 /* assume 20YY not 19YY, and ignore the Century Bit */
115 tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100; 121 tm->tm_year = bcd2bin(buf[M41T80_REG_YEAR]) + 100;
116 return 0; 122 return 0;
117} 123}
118 124
@@ -155,19 +161,19 @@ static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
155 /* Merge time-data and register flags into buf[0..7] */ 161 /* Merge time-data and register flags into buf[0..7] */
156 buf[M41T80_REG_SSEC] = 0; 162 buf[M41T80_REG_SSEC] = 0;
157 buf[M41T80_REG_SEC] = 163 buf[M41T80_REG_SEC] =
158 BIN2BCD(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f); 164 bin2bcd(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f);
159 buf[M41T80_REG_MIN] = 165 buf[M41T80_REG_MIN] =
160 BIN2BCD(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f); 166 bin2bcd(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f);
161 buf[M41T80_REG_HOUR] = 167 buf[M41T80_REG_HOUR] =
162 BIN2BCD(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ; 168 bin2bcd(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ;
163 buf[M41T80_REG_WDAY] = 169 buf[M41T80_REG_WDAY] =
164 (tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07); 170 (tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07);
165 buf[M41T80_REG_DAY] = 171 buf[M41T80_REG_DAY] =
166 BIN2BCD(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f); 172 bin2bcd(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f);
167 buf[M41T80_REG_MON] = 173 buf[M41T80_REG_MON] =
168 BIN2BCD(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f); 174 bin2bcd(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f);
169 /* assume 20YY not 19YY */ 175 /* assume 20YY not 19YY */
170 buf[M41T80_REG_YEAR] = BIN2BCD(tm->tm_year % 100); 176 buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year % 100);
171 177
172 if (i2c_transfer(client->adapter, msgs, 1) != 1) { 178 if (i2c_transfer(client->adapter, msgs, 1) != 1) {
173 dev_err(&client->dev, "write error\n"); 179 dev_err(&client->dev, "write error\n");
@@ -282,15 +288,15 @@ static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
282 288
283 wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */ 289 wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */
284 reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ? 290 reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ?
285 BIN2BCD(t->time.tm_sec) : 0x80; 291 bin2bcd(t->time.tm_sec) : 0x80;
286 reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ? 292 reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ?
287 BIN2BCD(t->time.tm_min) : 0x80; 293 bin2bcd(t->time.tm_min) : 0x80;
288 reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ? 294 reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ?
289 BIN2BCD(t->time.tm_hour) : 0x80; 295 bin2bcd(t->time.tm_hour) : 0x80;
290 reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ? 296 reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ?
291 BIN2BCD(t->time.tm_mday) : 0x80; 297 bin2bcd(t->time.tm_mday) : 0x80;
292 if (t->time.tm_mon >= 0) 298 if (t->time.tm_mon >= 0)
293 reg[M41T80_REG_ALARM_MON] |= BIN2BCD(t->time.tm_mon + 1); 299 reg[M41T80_REG_ALARM_MON] |= bin2bcd(t->time.tm_mon + 1);
294 else 300 else
295 reg[M41T80_REG_ALARM_DAY] |= 0x40; 301 reg[M41T80_REG_ALARM_DAY] |= 0x40;
296 302
@@ -341,15 +347,15 @@ static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
341 t->time.tm_mday = -1; 347 t->time.tm_mday = -1;
342 t->time.tm_mon = -1; 348 t->time.tm_mon = -1;
343 if (!(reg[M41T80_REG_ALARM_SEC] & 0x80)) 349 if (!(reg[M41T80_REG_ALARM_SEC] & 0x80))
344 t->time.tm_sec = BCD2BIN(reg[M41T80_REG_ALARM_SEC] & 0x7f); 350 t->time.tm_sec = bcd2bin(reg[M41T80_REG_ALARM_SEC] & 0x7f);
345 if (!(reg[M41T80_REG_ALARM_MIN] & 0x80)) 351 if (!(reg[M41T80_REG_ALARM_MIN] & 0x80))
346 t->time.tm_min = BCD2BIN(reg[M41T80_REG_ALARM_MIN] & 0x7f); 352 t->time.tm_min = bcd2bin(reg[M41T80_REG_ALARM_MIN] & 0x7f);
347 if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80)) 353 if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80))
348 t->time.tm_hour = BCD2BIN(reg[M41T80_REG_ALARM_HOUR] & 0x3f); 354 t->time.tm_hour = bcd2bin(reg[M41T80_REG_ALARM_HOUR] & 0x3f);
349 if (!(reg[M41T80_REG_ALARM_DAY] & 0x80)) 355 if (!(reg[M41T80_REG_ALARM_DAY] & 0x80))
350 t->time.tm_mday = BCD2BIN(reg[M41T80_REG_ALARM_DAY] & 0x3f); 356 t->time.tm_mday = bcd2bin(reg[M41T80_REG_ALARM_DAY] & 0x3f);
351 if (!(reg[M41T80_REG_ALARM_DAY] & 0x40)) 357 if (!(reg[M41T80_REG_ALARM_DAY] & 0x40))
352 t->time.tm_mon = BCD2BIN(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1; 358 t->time.tm_mon = bcd2bin(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1;
353 t->time.tm_year = -1; 359 t->time.tm_year = -1;
354 t->time.tm_wday = -1; 360 t->time.tm_wday = -1;
355 t->time.tm_yday = -1; 361 t->time.tm_yday = -1;
@@ -386,8 +392,12 @@ static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
386 struct device_attribute *attr, char *buf) 392 struct device_attribute *attr, char *buf)
387{ 393{
388 struct i2c_client *client = to_i2c_client(dev); 394 struct i2c_client *client = to_i2c_client(dev);
395 struct m41t80_data *clientdata = i2c_get_clientdata(client);
389 int val; 396 int val;
390 397
398 if (!(clientdata->features & M41T80_FEATURE_SQ))
399 return -EINVAL;
400
391 val = i2c_smbus_read_byte_data(client, M41T80_REG_SQW); 401 val = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
392 if (val < 0) 402 if (val < 0)
393 return -EIO; 403 return -EIO;
@@ -408,9 +418,13 @@ static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
408 const char *buf, size_t count) 418 const char *buf, size_t count)
409{ 419{
410 struct i2c_client *client = to_i2c_client(dev); 420 struct i2c_client *client = to_i2c_client(dev);
421 struct m41t80_data *clientdata = i2c_get_clientdata(client);
411 int almon, sqw; 422 int almon, sqw;
412 int val = simple_strtoul(buf, NULL, 0); 423 int val = simple_strtoul(buf, NULL, 0);
413 424
425 if (!(clientdata->features & M41T80_FEATURE_SQ))
426 return -EINVAL;
427
414 if (val) { 428 if (val) {
415 if (!is_power_of_2(val)) 429 if (!is_power_of_2(val))
416 return -EINVAL; 430 return -EINVAL;
@@ -499,6 +513,8 @@ static void wdt_ping(void)
499 .buf = i2c_data, 513 .buf = i2c_data,
500 }, 514 },
501 }; 515 };
516 struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
517
502 i2c_data[0] = 0x09; /* watchdog register */ 518 i2c_data[0] = 0x09; /* watchdog register */
503 519
504 if (wdt_margin > 31) 520 if (wdt_margin > 31)
@@ -509,6 +525,13 @@ static void wdt_ping(void)
509 */ 525 */
510 i2c_data[1] = wdt_margin<<2 | 0x82; 526 i2c_data[1] = wdt_margin<<2 | 0x82;
511 527
528 /*
529 * M41T65 has three bits for watchdog resolution. Don't set bit 7, as
530 * that would be an invalid resolution.
531 */
532 if (clientdata->features & M41T80_FEATURE_WD)
533 i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
534
512 i2c_transfer(save_client->adapter, msgs1, 1); 535 i2c_transfer(save_client->adapter, msgs1, 1);
513} 536}
514 537
diff --git a/drivers/rtc/rtc-m41t94.c b/drivers/rtc/rtc-m41t94.c
index 9b19499c829e..c3a18c58daf6 100644
--- a/drivers/rtc/rtc-m41t94.c
+++ b/drivers/rtc/rtc-m41t94.c
@@ -41,17 +41,17 @@ static int m41t94_set_time(struct device *dev, struct rtc_time *tm)
41 tm->tm_mon, tm->tm_year, tm->tm_wday); 41 tm->tm_mon, tm->tm_year, tm->tm_wday);
42 42
43 buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */ 43 buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */
44 buf[M41T94_REG_SECONDS] = BIN2BCD(tm->tm_sec); 44 buf[M41T94_REG_SECONDS] = bin2bcd(tm->tm_sec);
45 buf[M41T94_REG_MINUTES] = BIN2BCD(tm->tm_min); 45 buf[M41T94_REG_MINUTES] = bin2bcd(tm->tm_min);
46 buf[M41T94_REG_HOURS] = BIN2BCD(tm->tm_hour); 46 buf[M41T94_REG_HOURS] = bin2bcd(tm->tm_hour);
47 buf[M41T94_REG_WDAY] = BIN2BCD(tm->tm_wday + 1); 47 buf[M41T94_REG_WDAY] = bin2bcd(tm->tm_wday + 1);
48 buf[M41T94_REG_DAY] = BIN2BCD(tm->tm_mday); 48 buf[M41T94_REG_DAY] = bin2bcd(tm->tm_mday);
49 buf[M41T94_REG_MONTH] = BIN2BCD(tm->tm_mon + 1); 49 buf[M41T94_REG_MONTH] = bin2bcd(tm->tm_mon + 1);
50 50
51 buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB; 51 buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB;
52 if (tm->tm_year >= 100) 52 if (tm->tm_year >= 100)
53 buf[M41T94_REG_HOURS] |= M41T94_BIT_CB; 53 buf[M41T94_REG_HOURS] |= M41T94_BIT_CB;
54 buf[M41T94_REG_YEAR] = BIN2BCD(tm->tm_year % 100); 54 buf[M41T94_REG_YEAR] = bin2bcd(tm->tm_year % 100);
55 55
56 return spi_write(spi, buf, 8); 56 return spi_write(spi, buf, 8);
57} 57}
@@ -82,14 +82,14 @@ static int m41t94_read_time(struct device *dev, struct rtc_time *tm)
82 spi_write(spi, buf, 2); 82 spi_write(spi, buf, 2);
83 } 83 }
84 84
85 tm->tm_sec = BCD2BIN(spi_w8r8(spi, M41T94_REG_SECONDS)); 85 tm->tm_sec = bcd2bin(spi_w8r8(spi, M41T94_REG_SECONDS));
86 tm->tm_min = BCD2BIN(spi_w8r8(spi, M41T94_REG_MINUTES)); 86 tm->tm_min = bcd2bin(spi_w8r8(spi, M41T94_REG_MINUTES));
87 hour = spi_w8r8(spi, M41T94_REG_HOURS); 87 hour = spi_w8r8(spi, M41T94_REG_HOURS);
88 tm->tm_hour = BCD2BIN(hour & 0x3f); 88 tm->tm_hour = bcd2bin(hour & 0x3f);
89 tm->tm_wday = BCD2BIN(spi_w8r8(spi, M41T94_REG_WDAY)) - 1; 89 tm->tm_wday = bcd2bin(spi_w8r8(spi, M41T94_REG_WDAY)) - 1;
90 tm->tm_mday = BCD2BIN(spi_w8r8(spi, M41T94_REG_DAY)); 90 tm->tm_mday = bcd2bin(spi_w8r8(spi, M41T94_REG_DAY));
91 tm->tm_mon = BCD2BIN(spi_w8r8(spi, M41T94_REG_MONTH)) - 1; 91 tm->tm_mon = bcd2bin(spi_w8r8(spi, M41T94_REG_MONTH)) - 1;
92 tm->tm_year = BCD2BIN(spi_w8r8(spi, M41T94_REG_YEAR)); 92 tm->tm_year = bcd2bin(spi_w8r8(spi, M41T94_REG_YEAR));
93 if ((hour & M41T94_BIT_CB) || !(hour & M41T94_BIT_CEB)) 93 if ((hour & M41T94_BIT_CB) || !(hour & M41T94_BIT_CEB))
94 tm->tm_year += 100; 94 tm->tm_year += 100;
95 95
diff --git a/drivers/rtc/rtc-m48t35.c b/drivers/rtc/rtc-m48t35.c
new file mode 100644
index 000000000000..0b2197559940
--- /dev/null
+++ b/drivers/rtc/rtc-m48t35.c
@@ -0,0 +1,235 @@
1/*
2 * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
3 *
4 * Copyright (C) 2000 Silicon Graphics, Inc.
5 * Written by Ulf Carlsson (ulfc@engr.sgi.com)
6 *
7 * Copyright (C) 2008 Thomas Bogendoerfer
8 *
9 * Based on code written by Paul Gortmaker.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/rtc.h>
19#include <linux/platform_device.h>
20#include <linux/bcd.h>
21#include <linux/io.h>
22
23#define DRV_VERSION "1.0"
24
25struct m48t35_rtc {
26 u8 pad[0x7ff8]; /* starts at 0x7ff8 */
27 u8 control;
28 u8 sec;
29 u8 min;
30 u8 hour;
31 u8 day;
32 u8 date;
33 u8 month;
34 u8 year;
35};
36
37#define M48T35_RTC_SET 0x80
38#define M48T35_RTC_READ 0x40
39
40struct m48t35_priv {
41 struct rtc_device *rtc;
42 struct m48t35_rtc __iomem *reg;
43 size_t size;
44 unsigned long baseaddr;
45 spinlock_t lock;
46};
47
48static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
49{
50 struct m48t35_priv *priv = dev_get_drvdata(dev);
51 u8 control;
52
53 /*
54 * Only the values that we read from the RTC are set. We leave
55 * tm_wday, tm_yday and tm_isdst untouched. Even though the
56 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
57 * by the RTC when initially set to a non-zero value.
58 */
59 spin_lock_irq(&priv->lock);
60 control = readb(&priv->reg->control);
61 writeb(control | M48T35_RTC_READ, &priv->reg->control);
62 tm->tm_sec = readb(&priv->reg->sec);
63 tm->tm_min = readb(&priv->reg->min);
64 tm->tm_hour = readb(&priv->reg->hour);
65 tm->tm_mday = readb(&priv->reg->date);
66 tm->tm_mon = readb(&priv->reg->month);
67 tm->tm_year = readb(&priv->reg->year);
68 writeb(control, &priv->reg->control);
69 spin_unlock_irq(&priv->lock);
70
71 tm->tm_sec = bcd2bin(tm->tm_sec);
72 tm->tm_min = bcd2bin(tm->tm_min);
73 tm->tm_hour = bcd2bin(tm->tm_hour);
74 tm->tm_mday = bcd2bin(tm->tm_mday);
75 tm->tm_mon = bcd2bin(tm->tm_mon);
76 tm->tm_year = bcd2bin(tm->tm_year);
77
78 /*
79 * Account for differences between how the RTC uses the values
80 * and how they are defined in a struct rtc_time;
81 */
82 tm->tm_year += 70;
83 if (tm->tm_year <= 69)
84 tm->tm_year += 100;
85
86 tm->tm_mon--;
87 return rtc_valid_tm(tm);
88}
89
90static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
91{
92 struct m48t35_priv *priv = dev_get_drvdata(dev);
93 unsigned char mon, day, hrs, min, sec;
94 unsigned int yrs;
95 u8 control;
96
97 yrs = tm->tm_year + 1900;
98 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
99 day = tm->tm_mday;
100 hrs = tm->tm_hour;
101 min = tm->tm_min;
102 sec = tm->tm_sec;
103
104 if (yrs < 1970)
105 return -EINVAL;
106
107 yrs -= 1970;
108 if (yrs > 255) /* They are unsigned */
109 return -EINVAL;
110
111 if (yrs > 169)
112 return -EINVAL;
113
114 if (yrs >= 100)
115 yrs -= 100;
116
117 sec = bin2bcd(sec);
118 min = bin2bcd(min);
119 hrs = bin2bcd(hrs);
120 day = bin2bcd(day);
121 mon = bin2bcd(mon);
122 yrs = bin2bcd(yrs);
123
124 spin_lock_irq(&priv->lock);
125 control = readb(&priv->reg->control);
126 writeb(control | M48T35_RTC_SET, &priv->reg->control);
127 writeb(yrs, &priv->reg->year);
128 writeb(mon, &priv->reg->month);
129 writeb(day, &priv->reg->date);
130 writeb(hrs, &priv->reg->hour);
131 writeb(min, &priv->reg->min);
132 writeb(sec, &priv->reg->sec);
133 writeb(control, &priv->reg->control);
134 spin_unlock_irq(&priv->lock);
135 return 0;
136}
137
138static const struct rtc_class_ops m48t35_ops = {
139 .read_time = m48t35_read_time,
140 .set_time = m48t35_set_time,
141};
142
143static int __devinit m48t35_probe(struct platform_device *pdev)
144{
145 struct rtc_device *rtc;
146 struct resource *res;
147 struct m48t35_priv *priv;
148 int ret = 0;
149
150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 if (!res)
152 return -ENODEV;
153 priv = kzalloc(sizeof(struct m48t35_priv), GFP_KERNEL);
154 if (!priv)
155 return -ENOMEM;
156
157 priv->size = res->end - res->start + 1;
158 /*
159 * kludge: remove the #ifndef after ioc3 resource
160 * conflicts are resolved
161 */
162#ifndef CONFIG_SGI_IP27
163 if (!request_mem_region(res->start, priv->size, pdev->name)) {
164 ret = -EBUSY;
165 goto out;
166 }
167#endif
168 priv->baseaddr = res->start;
169 priv->reg = ioremap(priv->baseaddr, priv->size);
170 if (!priv->reg) {
171 ret = -ENOMEM;
172 goto out;
173 }
174 spin_lock_init(&priv->lock);
175 rtc = rtc_device_register("m48t35", &pdev->dev,
176 &m48t35_ops, THIS_MODULE);
177 if (IS_ERR(rtc)) {
178 ret = PTR_ERR(rtc);
179 goto out;
180 }
181 priv->rtc = rtc;
182 platform_set_drvdata(pdev, priv);
183 return 0;
184
185out:
186 if (priv->rtc)
187 rtc_device_unregister(priv->rtc);
188 if (priv->reg)
189 iounmap(priv->reg);
190 if (priv->baseaddr)
191 release_mem_region(priv->baseaddr, priv->size);
192 kfree(priv);
193 return ret;
194}
195
196static int __devexit m48t35_remove(struct platform_device *pdev)
197{
198 struct m48t35_priv *priv = platform_get_drvdata(pdev);
199
200 rtc_device_unregister(priv->rtc);
201 iounmap(priv->reg);
202#ifndef CONFIG_SGI_IP27
203 release_mem_region(priv->baseaddr, priv->size);
204#endif
205 kfree(priv);
206 return 0;
207}
208
209static struct platform_driver m48t35_platform_driver = {
210 .driver = {
211 .name = "rtc-m48t35",
212 .owner = THIS_MODULE,
213 },
214 .probe = m48t35_probe,
215 .remove = __devexit_p(m48t35_remove),
216};
217
218static int __init m48t35_init(void)
219{
220 return platform_driver_register(&m48t35_platform_driver);
221}
222
223static void __exit m48t35_exit(void)
224{
225 platform_driver_unregister(&m48t35_platform_driver);
226}
227
228MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
229MODULE_DESCRIPTION("M48T35 RTC driver");
230MODULE_LICENSE("GPL");
231MODULE_VERSION(DRV_VERSION);
232MODULE_ALIAS("platform:rtc-m48t35");
233
234module_init(m48t35_init);
235module_exit(m48t35_exit);
diff --git a/drivers/rtc/rtc-m48t59.c b/drivers/rtc/rtc-m48t59.c
index 013e6c103b9c..43afb7ab5289 100644
--- a/drivers/rtc/rtc-m48t59.c
+++ b/drivers/rtc/rtc-m48t59.c
@@ -24,8 +24,9 @@
24#define NO_IRQ (-1) 24#define NO_IRQ (-1)
25#endif 25#endif
26 26
27#define M48T59_READ(reg) pdata->read_byte(dev, reg) 27#define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
28#define M48T59_WRITE(val, reg) pdata->write_byte(dev, reg, val) 28#define M48T59_WRITE(val, reg) \
29 (pdata->write_byte(dev, pdata->offset + reg, val))
29 30
30#define M48T59_SET_BITS(mask, reg) \ 31#define M48T59_SET_BITS(mask, reg) \
31 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg)) 32 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
@@ -34,7 +35,6 @@
34 35
35struct m48t59_private { 36struct m48t59_private {
36 void __iomem *ioaddr; 37 void __iomem *ioaddr;
37 unsigned int size; /* iomem size */
38 int irq; 38 int irq;
39 struct rtc_device *rtc; 39 struct rtc_device *rtc;
40 spinlock_t lock; /* serialize the NVRAM and RTC access */ 40 spinlock_t lock; /* serialize the NVRAM and RTC access */
@@ -76,21 +76,26 @@ static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
76 /* Issue the READ command */ 76 /* Issue the READ command */
77 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL); 77 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
78 78
79 tm->tm_year = BCD2BIN(M48T59_READ(M48T59_YEAR)); 79 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
80 /* tm_mon is 0-11 */ 80 /* tm_mon is 0-11 */
81 tm->tm_mon = BCD2BIN(M48T59_READ(M48T59_MONTH)) - 1; 81 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
82 tm->tm_mday = BCD2BIN(M48T59_READ(M48T59_MDAY)); 82 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
83 83
84 val = M48T59_READ(M48T59_WDAY); 84 val = M48T59_READ(M48T59_WDAY);
85 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) { 85 if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
86 (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
86 dev_dbg(dev, "Century bit is enabled\n"); 87 dev_dbg(dev, "Century bit is enabled\n");
87 tm->tm_year += 100; /* one century */ 88 tm->tm_year += 100; /* one century */
88 } 89 }
90#ifdef CONFIG_SPARC
91 /* Sun SPARC machines count years since 1968 */
92 tm->tm_year += 68;
93#endif
89 94
90 tm->tm_wday = BCD2BIN(val & 0x07); 95 tm->tm_wday = bcd2bin(val & 0x07);
91 tm->tm_hour = BCD2BIN(M48T59_READ(M48T59_HOUR) & 0x3F); 96 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
92 tm->tm_min = BCD2BIN(M48T59_READ(M48T59_MIN) & 0x7F); 97 tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
93 tm->tm_sec = BCD2BIN(M48T59_READ(M48T59_SEC) & 0x7F); 98 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
94 99
95 /* Clear the READ bit */ 100 /* Clear the READ bit */
96 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL); 101 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
@@ -109,26 +114,35 @@ static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
109 struct m48t59_private *m48t59 = platform_get_drvdata(pdev); 114 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
110 unsigned long flags; 115 unsigned long flags;
111 u8 val = 0; 116 u8 val = 0;
117 int year = tm->tm_year;
118
119#ifdef CONFIG_SPARC
120 /* Sun SPARC machines count years since 1968 */
121 year -= 68;
122#endif
112 123
113 dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n", 124 dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
114 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, 125 year + 1900, tm->tm_mon, tm->tm_mday,
115 tm->tm_hour, tm->tm_min, tm->tm_sec); 126 tm->tm_hour, tm->tm_min, tm->tm_sec);
116 127
128 if (year < 0)
129 return -EINVAL;
130
117 spin_lock_irqsave(&m48t59->lock, flags); 131 spin_lock_irqsave(&m48t59->lock, flags);
118 /* Issue the WRITE command */ 132 /* Issue the WRITE command */
119 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL); 133 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
120 134
121 M48T59_WRITE((BIN2BCD(tm->tm_sec) & 0x7F), M48T59_SEC); 135 M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
122 M48T59_WRITE((BIN2BCD(tm->tm_min) & 0x7F), M48T59_MIN); 136 M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
123 M48T59_WRITE((BIN2BCD(tm->tm_hour) & 0x3F), M48T59_HOUR); 137 M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
124 M48T59_WRITE((BIN2BCD(tm->tm_mday) & 0x3F), M48T59_MDAY); 138 M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
125 /* tm_mon is 0-11 */ 139 /* tm_mon is 0-11 */
126 M48T59_WRITE((BIN2BCD(tm->tm_mon + 1) & 0x1F), M48T59_MONTH); 140 M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
127 M48T59_WRITE(BIN2BCD(tm->tm_year % 100), M48T59_YEAR); 141 M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
128 142
129 if (tm->tm_year/100) 143 if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
130 val = (M48T59_WDAY_CEB | M48T59_WDAY_CB); 144 val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
131 val |= (BIN2BCD(tm->tm_wday) & 0x07); 145 val |= (bin2bcd(tm->tm_wday) & 0x07);
132 M48T59_WRITE(val, M48T59_WDAY); 146 M48T59_WRITE(val, M48T59_WDAY);
133 147
134 /* Clear the WRITE bit */ 148 /* Clear the WRITE bit */
@@ -157,18 +171,22 @@ static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
157 /* Issue the READ command */ 171 /* Issue the READ command */
158 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL); 172 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
159 173
160 tm->tm_year = BCD2BIN(M48T59_READ(M48T59_YEAR)); 174 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
175#ifdef CONFIG_SPARC
176 /* Sun SPARC machines count years since 1968 */
177 tm->tm_year += 68;
178#endif
161 /* tm_mon is 0-11 */ 179 /* tm_mon is 0-11 */
162 tm->tm_mon = BCD2BIN(M48T59_READ(M48T59_MONTH)) - 1; 180 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
163 181
164 val = M48T59_READ(M48T59_WDAY); 182 val = M48T59_READ(M48T59_WDAY);
165 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) 183 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
166 tm->tm_year += 100; /* one century */ 184 tm->tm_year += 100; /* one century */
167 185
168 tm->tm_mday = BCD2BIN(M48T59_READ(M48T59_ALARM_DATE)); 186 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE));
169 tm->tm_hour = BCD2BIN(M48T59_READ(M48T59_ALARM_HOUR)); 187 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR));
170 tm->tm_min = BCD2BIN(M48T59_READ(M48T59_ALARM_MIN)); 188 tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN));
171 tm->tm_sec = BCD2BIN(M48T59_READ(M48T59_ALARM_SEC)); 189 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC));
172 190
173 /* Clear the READ bit */ 191 /* Clear the READ bit */
174 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL); 192 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
@@ -191,27 +209,36 @@ static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
191 struct rtc_time *tm = &alrm->time; 209 struct rtc_time *tm = &alrm->time;
192 u8 mday, hour, min, sec; 210 u8 mday, hour, min, sec;
193 unsigned long flags; 211 unsigned long flags;
212 int year = tm->tm_year;
213
214#ifdef CONFIG_SPARC
215 /* Sun SPARC machines count years since 1968 */
216 year -= 68;
217#endif
194 218
195 /* If no irq, we don't support ALARM */ 219 /* If no irq, we don't support ALARM */
196 if (m48t59->irq == NO_IRQ) 220 if (m48t59->irq == NO_IRQ)
197 return -EIO; 221 return -EIO;
198 222
223 if (year < 0)
224 return -EINVAL;
225
199 /* 226 /*
200 * 0xff means "always match" 227 * 0xff means "always match"
201 */ 228 */
202 mday = tm->tm_mday; 229 mday = tm->tm_mday;
203 mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff; 230 mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
204 if (mday == 0xff) 231 if (mday == 0xff)
205 mday = M48T59_READ(M48T59_MDAY); 232 mday = M48T59_READ(M48T59_MDAY);
206 233
207 hour = tm->tm_hour; 234 hour = tm->tm_hour;
208 hour = (hour < 24) ? BIN2BCD(hour) : 0x00; 235 hour = (hour < 24) ? bin2bcd(hour) : 0x00;
209 236
210 min = tm->tm_min; 237 min = tm->tm_min;
211 min = (min < 60) ? BIN2BCD(min) : 0x00; 238 min = (min < 60) ? bin2bcd(min) : 0x00;
212 239
213 sec = tm->tm_sec; 240 sec = tm->tm_sec;
214 sec = (sec < 60) ? BIN2BCD(sec) : 0x00; 241 sec = (sec < 60) ? bin2bcd(sec) : 0x00;
215 242
216 spin_lock_irqsave(&m48t59->lock, flags); 243 spin_lock_irqsave(&m48t59->lock, flags);
217 /* Issue the WRITE command */ 244 /* Issue the WRITE command */
@@ -227,7 +254,7 @@ static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
227 spin_unlock_irqrestore(&m48t59->lock, flags); 254 spin_unlock_irqrestore(&m48t59->lock, flags);
228 255
229 dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n", 256 dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
230 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, 257 year + 1900, tm->tm_mon, tm->tm_mday,
231 tm->tm_hour, tm->tm_min, tm->tm_sec); 258 tm->tm_hour, tm->tm_min, tm->tm_sec);
232 return 0; 259 return 0;
233} 260}
@@ -310,6 +337,11 @@ static const struct rtc_class_ops m48t59_rtc_ops = {
310 .proc = m48t59_rtc_proc, 337 .proc = m48t59_rtc_proc,
311}; 338};
312 339
340static const struct rtc_class_ops m48t02_rtc_ops = {
341 .read_time = m48t59_rtc_read_time,
342 .set_time = m48t59_rtc_set_time,
343};
344
313static ssize_t m48t59_nvram_read(struct kobject *kobj, 345static ssize_t m48t59_nvram_read(struct kobject *kobj,
314 struct bin_attribute *bin_attr, 346 struct bin_attribute *bin_attr,
315 char *buf, loff_t pos, size_t size) 347 char *buf, loff_t pos, size_t size)
@@ -321,7 +353,7 @@ static ssize_t m48t59_nvram_read(struct kobject *kobj,
321 ssize_t cnt = 0; 353 ssize_t cnt = 0;
322 unsigned long flags; 354 unsigned long flags;
323 355
324 for (; size > 0 && pos < M48T59_NVRAM_SIZE; cnt++, size--) { 356 for (; size > 0 && pos < pdata->offset; cnt++, size--) {
325 spin_lock_irqsave(&m48t59->lock, flags); 357 spin_lock_irqsave(&m48t59->lock, flags);
326 *buf++ = M48T59_READ(cnt); 358 *buf++ = M48T59_READ(cnt);
327 spin_unlock_irqrestore(&m48t59->lock, flags); 359 spin_unlock_irqrestore(&m48t59->lock, flags);
@@ -341,7 +373,7 @@ static ssize_t m48t59_nvram_write(struct kobject *kobj,
341 ssize_t cnt = 0; 373 ssize_t cnt = 0;
342 unsigned long flags; 374 unsigned long flags;
343 375
344 for (; size > 0 && pos < M48T59_NVRAM_SIZE; cnt++, size--) { 376 for (; size > 0 && pos < pdata->offset; cnt++, size--) {
345 spin_lock_irqsave(&m48t59->lock, flags); 377 spin_lock_irqsave(&m48t59->lock, flags);
346 M48T59_WRITE(*buf++, cnt); 378 M48T59_WRITE(*buf++, cnt);
347 spin_unlock_irqrestore(&m48t59->lock, flags); 379 spin_unlock_irqrestore(&m48t59->lock, flags);
@@ -354,11 +386,9 @@ static struct bin_attribute m48t59_nvram_attr = {
354 .attr = { 386 .attr = {
355 .name = "nvram", 387 .name = "nvram",
356 .mode = S_IRUGO | S_IWUSR, 388 .mode = S_IRUGO | S_IWUSR,
357 .owner = THIS_MODULE,
358 }, 389 },
359 .read = m48t59_nvram_read, 390 .read = m48t59_nvram_read,
360 .write = m48t59_nvram_write, 391 .write = m48t59_nvram_write,
361 .size = M48T59_NVRAM_SIZE,
362}; 392};
363 393
364static int __devinit m48t59_rtc_probe(struct platform_device *pdev) 394static int __devinit m48t59_rtc_probe(struct platform_device *pdev)
@@ -367,6 +397,8 @@ static int __devinit m48t59_rtc_probe(struct platform_device *pdev)
367 struct m48t59_private *m48t59 = NULL; 397 struct m48t59_private *m48t59 = NULL;
368 struct resource *res; 398 struct resource *res;
369 int ret = -ENOMEM; 399 int ret = -ENOMEM;
400 char *name;
401 const struct rtc_class_ops *ops;
370 402
371 /* This chip could be memory-mapped or I/O-mapped */ 403 /* This chip could be memory-mapped or I/O-mapped */
372 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 404 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -391,6 +423,8 @@ static int __devinit m48t59_rtc_probe(struct platform_device *pdev)
391 /* Ensure we only kmalloc platform data once */ 423 /* Ensure we only kmalloc platform data once */
392 pdev->dev.platform_data = pdata; 424 pdev->dev.platform_data = pdata;
393 } 425 }
426 if (!pdata->type)
427 pdata->type = M48T59RTC_TYPE_M48T59;
394 428
395 /* Try to use the generic memory read/write ops */ 429 /* Try to use the generic memory read/write ops */
396 if (!pdata->write_byte) 430 if (!pdata->write_byte)
@@ -403,10 +437,14 @@ static int __devinit m48t59_rtc_probe(struct platform_device *pdev)
403 if (!m48t59) 437 if (!m48t59)
404 return -ENOMEM; 438 return -ENOMEM;
405 439
406 m48t59->size = res->end - res->start + 1; 440 m48t59->ioaddr = pdata->ioaddr;
407 m48t59->ioaddr = ioremap(res->start, m48t59->size); 441
408 if (!m48t59->ioaddr) 442 if (!m48t59->ioaddr) {
409 goto out; 443 /* ioaddr not mapped externally */
444 m48t59->ioaddr = ioremap(res->start, res->end - res->start + 1);
445 if (!m48t59->ioaddr)
446 goto out;
447 }
410 448
411 /* Try to get irq number. We also can work in 449 /* Try to get irq number. We also can work in
412 * the mode without IRQ. 450 * the mode without IRQ.
@@ -421,14 +459,36 @@ static int __devinit m48t59_rtc_probe(struct platform_device *pdev)
421 if (ret) 459 if (ret)
422 goto out; 460 goto out;
423 } 461 }
462 switch (pdata->type) {
463 case M48T59RTC_TYPE_M48T59:
464 name = "m48t59";
465 ops = &m48t59_rtc_ops;
466 pdata->offset = 0x1ff0;
467 break;
468 case M48T59RTC_TYPE_M48T02:
469 name = "m48t02";
470 ops = &m48t02_rtc_ops;
471 pdata->offset = 0x7f0;
472 break;
473 case M48T59RTC_TYPE_M48T08:
474 name = "m48t08";
475 ops = &m48t02_rtc_ops;
476 pdata->offset = 0x1ff0;
477 break;
478 default:
479 dev_err(&pdev->dev, "Unknown RTC type\n");
480 ret = -ENODEV;
481 goto out;
482 }
424 483
425 m48t59->rtc = rtc_device_register("m48t59", &pdev->dev, 484 m48t59->rtc = rtc_device_register(name, &pdev->dev, ops, THIS_MODULE);
426 &m48t59_rtc_ops, THIS_MODULE);
427 if (IS_ERR(m48t59->rtc)) { 485 if (IS_ERR(m48t59->rtc)) {
428 ret = PTR_ERR(m48t59->rtc); 486 ret = PTR_ERR(m48t59->rtc);
429 goto out; 487 goto out;
430 } 488 }
431 489
490 m48t59_nvram_attr.size = pdata->offset;
491
432 ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr); 492 ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
433 if (ret) 493 if (ret)
434 goto out; 494 goto out;
@@ -452,11 +512,12 @@ out:
452static int __devexit m48t59_rtc_remove(struct platform_device *pdev) 512static int __devexit m48t59_rtc_remove(struct platform_device *pdev)
453{ 513{
454 struct m48t59_private *m48t59 = platform_get_drvdata(pdev); 514 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
515 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
455 516
456 sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr); 517 sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
457 if (!IS_ERR(m48t59->rtc)) 518 if (!IS_ERR(m48t59->rtc))
458 rtc_device_unregister(m48t59->rtc); 519 rtc_device_unregister(m48t59->rtc);
459 if (m48t59->ioaddr) 520 if (m48t59->ioaddr && !pdata->ioaddr)
460 iounmap(m48t59->ioaddr); 521 iounmap(m48t59->ioaddr);
461 if (m48t59->irq != NO_IRQ) 522 if (m48t59->irq != NO_IRQ)
462 free_irq(m48t59->irq, &pdev->dev); 523 free_irq(m48t59->irq, &pdev->dev);
@@ -491,5 +552,5 @@ module_init(m48t59_rtc_init);
491module_exit(m48t59_rtc_exit); 552module_exit(m48t59_rtc_exit);
492 553
493MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>"); 554MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
494MODULE_DESCRIPTION("M48T59 RTC driver"); 555MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
495MODULE_LICENSE("GPL"); 556MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-m48t86.c b/drivers/rtc/rtc-m48t86.c
index 3f7f99a5d96a..7c045cffa9ff 100644
--- a/drivers/rtc/rtc-m48t86.c
+++ b/drivers/rtc/rtc-m48t86.c
@@ -62,14 +62,14 @@ static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
62 tm->tm_wday = ops->readbyte(M48T86_REG_DOW); 62 tm->tm_wday = ops->readbyte(M48T86_REG_DOW);
63 } else { 63 } else {
64 /* bcd mode */ 64 /* bcd mode */
65 tm->tm_sec = BCD2BIN(ops->readbyte(M48T86_REG_SEC)); 65 tm->tm_sec = bcd2bin(ops->readbyte(M48T86_REG_SEC));
66 tm->tm_min = BCD2BIN(ops->readbyte(M48T86_REG_MIN)); 66 tm->tm_min = bcd2bin(ops->readbyte(M48T86_REG_MIN));
67 tm->tm_hour = BCD2BIN(ops->readbyte(M48T86_REG_HOUR) & 0x3F); 67 tm->tm_hour = bcd2bin(ops->readbyte(M48T86_REG_HOUR) & 0x3F);
68 tm->tm_mday = BCD2BIN(ops->readbyte(M48T86_REG_DOM)); 68 tm->tm_mday = bcd2bin(ops->readbyte(M48T86_REG_DOM));
69 /* tm_mon is 0-11 */ 69 /* tm_mon is 0-11 */
70 tm->tm_mon = BCD2BIN(ops->readbyte(M48T86_REG_MONTH)) - 1; 70 tm->tm_mon = bcd2bin(ops->readbyte(M48T86_REG_MONTH)) - 1;
71 tm->tm_year = BCD2BIN(ops->readbyte(M48T86_REG_YEAR)) + 100; 71 tm->tm_year = bcd2bin(ops->readbyte(M48T86_REG_YEAR)) + 100;
72 tm->tm_wday = BCD2BIN(ops->readbyte(M48T86_REG_DOW)); 72 tm->tm_wday = bcd2bin(ops->readbyte(M48T86_REG_DOW));
73 } 73 }
74 74
75 /* correct the hour if the clock is in 12h mode */ 75 /* correct the hour if the clock is in 12h mode */
@@ -103,13 +103,13 @@ static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
103 ops->writebyte(tm->tm_wday, M48T86_REG_DOW); 103 ops->writebyte(tm->tm_wday, M48T86_REG_DOW);
104 } else { 104 } else {
105 /* bcd mode */ 105 /* bcd mode */
106 ops->writebyte(BIN2BCD(tm->tm_sec), M48T86_REG_SEC); 106 ops->writebyte(bin2bcd(tm->tm_sec), M48T86_REG_SEC);
107 ops->writebyte(BIN2BCD(tm->tm_min), M48T86_REG_MIN); 107 ops->writebyte(bin2bcd(tm->tm_min), M48T86_REG_MIN);
108 ops->writebyte(BIN2BCD(tm->tm_hour), M48T86_REG_HOUR); 108 ops->writebyte(bin2bcd(tm->tm_hour), M48T86_REG_HOUR);
109 ops->writebyte(BIN2BCD(tm->tm_mday), M48T86_REG_DOM); 109 ops->writebyte(bin2bcd(tm->tm_mday), M48T86_REG_DOM);
110 ops->writebyte(BIN2BCD(tm->tm_mon + 1), M48T86_REG_MONTH); 110 ops->writebyte(bin2bcd(tm->tm_mon + 1), M48T86_REG_MONTH);
111 ops->writebyte(BIN2BCD(tm->tm_year % 100), M48T86_REG_YEAR); 111 ops->writebyte(bin2bcd(tm->tm_year % 100), M48T86_REG_YEAR);
112 ops->writebyte(BIN2BCD(tm->tm_wday), M48T86_REG_DOW); 112 ops->writebyte(bin2bcd(tm->tm_wday), M48T86_REG_DOW);
113 } 113 }
114 114
115 /* update ended */ 115 /* update ended */
diff --git a/drivers/rtc/rtc-max6900.c b/drivers/rtc/rtc-max6900.c
index ded3c0abad83..80782798763f 100644
--- a/drivers/rtc/rtc-max6900.c
+++ b/drivers/rtc/rtc-max6900.c
@@ -17,19 +17,18 @@
17#include <linux/rtc.h> 17#include <linux/rtc.h>
18#include <linux/delay.h> 18#include <linux/delay.h>
19 19
20#define DRV_NAME "max6900" 20#define DRV_VERSION "0.2"
21#define DRV_VERSION "0.1"
22 21
23/* 22/*
24 * register indices 23 * register indices
25 */ 24 */
26#define MAX6900_REG_SC 0 /* seconds 00-59 */ 25#define MAX6900_REG_SC 0 /* seconds 00-59 */
27#define MAX6900_REG_MN 1 /* minutes 00-59 */ 26#define MAX6900_REG_MN 1 /* minutes 00-59 */
28#define MAX6900_REG_HR 2 /* hours 00-23 */ 27#define MAX6900_REG_HR 2 /* hours 00-23 */
29#define MAX6900_REG_DT 3 /* day of month 00-31 */ 28#define MAX6900_REG_DT 3 /* day of month 00-31 */
30#define MAX6900_REG_MO 4 /* month 01-12 */ 29#define MAX6900_REG_MO 4 /* month 01-12 */
31#define MAX6900_REG_DW 5 /* day of week 1-7 */ 30#define MAX6900_REG_DW 5 /* day of week 1-7 */
32#define MAX6900_REG_YR 6 /* year 00-99 */ 31#define MAX6900_REG_YR 6 /* year 00-99 */
33#define MAX6900_REG_CT 7 /* control */ 32#define MAX6900_REG_CT 7 /* control */
34 /* register 8 is undocumented */ 33 /* register 8 is undocumented */
35#define MAX6900_REG_CENTURY 9 /* century */ 34#define MAX6900_REG_CENTURY 9 /* century */
@@ -39,7 +38,6 @@
39 38
40#define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */ 39#define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
41 40
42
43/* 41/*
44 * register read/write commands 42 * register read/write commands
45 */ 43 */
@@ -52,16 +50,7 @@
52 50
53#define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */ 51#define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
54 52
55#define MAX6900_I2C_ADDR 0xa0 53static struct i2c_driver max6900_driver;
56
57static const unsigned short normal_i2c[] = {
58 MAX6900_I2C_ADDR >> 1,
59 I2C_CLIENT_END
60};
61
62I2C_CLIENT_INSMOD; /* defines addr_data */
63
64static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind);
65 54
66static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf) 55static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
67{ 56{
@@ -69,36 +58,35 @@ static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
69 u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ }; 58 u8 reg_century_read[1] = { MAX6900_REG_CENTURY_READ };
70 struct i2c_msg msgs[4] = { 59 struct i2c_msg msgs[4] = {
71 { 60 {
72 .addr = client->addr, 61 .addr = client->addr,
73 .flags = 0, /* write */ 62 .flags = 0, /* write */
74 .len = sizeof(reg_burst_read), 63 .len = sizeof(reg_burst_read),
75 .buf = reg_burst_read 64 .buf = reg_burst_read}
76 }, 65 ,
77 { 66 {
78 .addr = client->addr, 67 .addr = client->addr,
79 .flags = I2C_M_RD, 68 .flags = I2C_M_RD,
80 .len = MAX6900_BURST_LEN, 69 .len = MAX6900_BURST_LEN,
81 .buf = buf 70 .buf = buf}
82 }, 71 ,
83 { 72 {
84 .addr = client->addr, 73 .addr = client->addr,
85 .flags = 0, /* write */ 74 .flags = 0, /* write */
86 .len = sizeof(reg_century_read), 75 .len = sizeof(reg_century_read),
87 .buf = reg_century_read 76 .buf = reg_century_read}
88 }, 77 ,
89 { 78 {
90 .addr = client->addr, 79 .addr = client->addr,
91 .flags = I2C_M_RD, 80 .flags = I2C_M_RD,
92 .len = sizeof(buf[MAX6900_REG_CENTURY]), 81 .len = sizeof(buf[MAX6900_REG_CENTURY]),
93 .buf = &buf[MAX6900_REG_CENTURY] 82 .buf = &buf[MAX6900_REG_CENTURY]
94 } 83 }
95 }; 84 };
96 int rc; 85 int rc;
97 86
98 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 87 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
99 if (rc != ARRAY_SIZE(msgs)) { 88 if (rc != ARRAY_SIZE(msgs)) {
100 dev_err(&client->dev, "%s: register read failed\n", 89 dev_err(&client->dev, "%s: register read failed\n", __func__);
101 __func__);
102 return -EIO; 90 return -EIO;
103 } 91 }
104 return 0; 92 return 0;
@@ -109,20 +97,18 @@ static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
109 u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE }; 97 u8 i2c_century_buf[1 + 1] = { MAX6900_REG_CENTURY_WRITE };
110 struct i2c_msg century_msgs[1] = { 98 struct i2c_msg century_msgs[1] = {
111 { 99 {
112 .addr = client->addr, 100 .addr = client->addr,
113 .flags = 0, /* write */ 101 .flags = 0, /* write */
114 .len = sizeof(i2c_century_buf), 102 .len = sizeof(i2c_century_buf),
115 .buf = i2c_century_buf 103 .buf = i2c_century_buf}
116 }
117 }; 104 };
118 u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE }; 105 u8 i2c_burst_buf[MAX6900_BURST_LEN + 1] = { MAX6900_REG_BURST_WRITE };
119 struct i2c_msg burst_msgs[1] = { 106 struct i2c_msg burst_msgs[1] = {
120 { 107 {
121 .addr = client->addr, 108 .addr = client->addr,
122 .flags = 0, /* write */ 109 .flags = 0, /* write */
123 .len = sizeof(i2c_burst_buf), 110 .len = sizeof(i2c_burst_buf),
124 .buf = i2c_burst_buf 111 .buf = i2c_burst_buf}
125 }
126 }; 112 };
127 int rc; 113 int rc;
128 114
@@ -133,10 +119,12 @@ static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
133 * bit as part of the burst write. 119 * bit as part of the burst write.
134 */ 120 */
135 i2c_century_buf[1] = buf[MAX6900_REG_CENTURY]; 121 i2c_century_buf[1] = buf[MAX6900_REG_CENTURY];
122
136 rc = i2c_transfer(client->adapter, century_msgs, 123 rc = i2c_transfer(client->adapter, century_msgs,
137 ARRAY_SIZE(century_msgs)); 124 ARRAY_SIZE(century_msgs));
138 if (rc != ARRAY_SIZE(century_msgs)) 125 if (rc != ARRAY_SIZE(century_msgs))
139 goto write_failed; 126 goto write_failed;
127
140 msleep(MAX6900_IDLE_TIME_AFTER_WRITE); 128 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
141 129
142 memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN); 130 memcpy(&i2c_burst_buf[1], buf, MAX6900_BURST_LEN);
@@ -148,45 +136,11 @@ static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
148 136
149 return 0; 137 return 0;
150 138
151write_failed: 139 write_failed:
152 dev_err(&client->dev, "%s: register write failed\n", 140 dev_err(&client->dev, "%s: register write failed\n", __func__);
153 __func__);
154 return -EIO; 141 return -EIO;
155} 142}
156 143
157static int max6900_i2c_validate_client(struct i2c_client *client)
158{
159 u8 regs[MAX6900_REG_LEN];
160 u8 zero_mask[] = {
161 0x80, /* seconds */
162 0x80, /* minutes */
163 0x40, /* hours */
164 0xc0, /* day of month */
165 0xe0, /* month */
166 0xf8, /* day of week */
167 0x00, /* year */
168 0x7f, /* control */
169 };
170 int i;
171 int rc;
172 int reserved;
173
174 reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
175 if (reserved != 0x07)
176 return -ENODEV;
177
178 rc = max6900_i2c_read_regs(client, regs);
179 if (rc < 0)
180 return rc;
181
182 for (i = 0; i < ARRAY_SIZE(zero_mask); ++i) {
183 if (regs[i] & zero_mask[i])
184 return -ENODEV;
185 }
186
187 return 0;
188}
189
190static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm) 144static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
191{ 145{
192 int rc; 146 int rc;
@@ -196,14 +150,14 @@ static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
196 if (rc < 0) 150 if (rc < 0)
197 return rc; 151 return rc;
198 152
199 tm->tm_sec = BCD2BIN(regs[MAX6900_REG_SC]); 153 tm->tm_sec = bcd2bin(regs[MAX6900_REG_SC]);
200 tm->tm_min = BCD2BIN(regs[MAX6900_REG_MN]); 154 tm->tm_min = bcd2bin(regs[MAX6900_REG_MN]);
201 tm->tm_hour = BCD2BIN(regs[MAX6900_REG_HR] & 0x3f); 155 tm->tm_hour = bcd2bin(regs[MAX6900_REG_HR] & 0x3f);
202 tm->tm_mday = BCD2BIN(regs[MAX6900_REG_DT]); 156 tm->tm_mday = bcd2bin(regs[MAX6900_REG_DT]);
203 tm->tm_mon = BCD2BIN(regs[MAX6900_REG_MO]) - 1; 157 tm->tm_mon = bcd2bin(regs[MAX6900_REG_MO]) - 1;
204 tm->tm_year = BCD2BIN(regs[MAX6900_REG_YR]) + 158 tm->tm_year = bcd2bin(regs[MAX6900_REG_YR]) +
205 BCD2BIN(regs[MAX6900_REG_CENTURY]) * 100 - 1900; 159 bcd2bin(regs[MAX6900_REG_CENTURY]) * 100 - 1900;
206 tm->tm_wday = BCD2BIN(regs[MAX6900_REG_DW]); 160 tm->tm_wday = bcd2bin(regs[MAX6900_REG_DW]);
207 161
208 return 0; 162 return 0;
209} 163}
@@ -211,7 +165,7 @@ static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
211static int max6900_i2c_clear_write_protect(struct i2c_client *client) 165static int max6900_i2c_clear_write_protect(struct i2c_client *client)
212{ 166{
213 int rc; 167 int rc;
214 rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0); 168 rc = i2c_smbus_write_byte_data(client, MAX6900_REG_CONTROL_WRITE, 0);
215 if (rc < 0) { 169 if (rc < 0) {
216 dev_err(&client->dev, "%s: control register write failed\n", 170 dev_err(&client->dev, "%s: control register write failed\n",
217 __func__); 171 __func__);
@@ -220,8 +174,8 @@ static int max6900_i2c_clear_write_protect(struct i2c_client *client)
220 return 0; 174 return 0;
221} 175}
222 176
223static int max6900_i2c_set_time(struct i2c_client *client, 177static int
224 struct rtc_time const *tm) 178max6900_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
225{ 179{
226 u8 regs[MAX6900_REG_LEN]; 180 u8 regs[MAX6900_REG_LEN];
227 int rc; 181 int rc;
@@ -230,14 +184,14 @@ static int max6900_i2c_set_time(struct i2c_client *client,
230 if (rc < 0) 184 if (rc < 0)
231 return rc; 185 return rc;
232 186
233 regs[MAX6900_REG_SC] = BIN2BCD(tm->tm_sec); 187 regs[MAX6900_REG_SC] = bin2bcd(tm->tm_sec);
234 regs[MAX6900_REG_MN] = BIN2BCD(tm->tm_min); 188 regs[MAX6900_REG_MN] = bin2bcd(tm->tm_min);
235 regs[MAX6900_REG_HR] = BIN2BCD(tm->tm_hour); 189 regs[MAX6900_REG_HR] = bin2bcd(tm->tm_hour);
236 regs[MAX6900_REG_DT] = BIN2BCD(tm->tm_mday); 190 regs[MAX6900_REG_DT] = bin2bcd(tm->tm_mday);
237 regs[MAX6900_REG_MO] = BIN2BCD(tm->tm_mon + 1); 191 regs[MAX6900_REG_MO] = bin2bcd(tm->tm_mon + 1);
238 regs[MAX6900_REG_DW] = BIN2BCD(tm->tm_wday); 192 regs[MAX6900_REG_DW] = bin2bcd(tm->tm_wday);
239 regs[MAX6900_REG_YR] = BIN2BCD(tm->tm_year % 100); 193 regs[MAX6900_REG_YR] = bin2bcd(tm->tm_year % 100);
240 regs[MAX6900_REG_CENTURY] = BIN2BCD((tm->tm_year + 1900) / 100); 194 regs[MAX6900_REG_CENTURY] = bin2bcd((tm->tm_year + 1900) / 100);
241 /* set write protect */ 195 /* set write protect */
242 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP; 196 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP;
243 197
@@ -258,89 +212,49 @@ static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
258 return max6900_i2c_set_time(to_i2c_client(dev), tm); 212 return max6900_i2c_set_time(to_i2c_client(dev), tm);
259} 213}
260 214
261static int max6900_attach_adapter(struct i2c_adapter *adapter) 215static int max6900_remove(struct i2c_client *client)
262{
263 return i2c_probe(adapter, &addr_data, max6900_probe);
264}
265
266static int max6900_detach_client(struct i2c_client *client)
267{ 216{
268 struct rtc_device *const rtc = i2c_get_clientdata(client); 217 struct rtc_device *rtc = i2c_get_clientdata(client);
269 218
270 if (rtc) 219 if (rtc)
271 rtc_device_unregister(rtc); 220 rtc_device_unregister(rtc);
272 221
273 return i2c_detach_client(client); 222 return 0;
274} 223}
275 224
276static struct i2c_driver max6900_driver = {
277 .driver = {
278 .name = DRV_NAME,
279 },
280 .id = I2C_DRIVERID_MAX6900,
281 .attach_adapter = max6900_attach_adapter,
282 .detach_client = max6900_detach_client,
283};
284
285static const struct rtc_class_ops max6900_rtc_ops = { 225static const struct rtc_class_ops max6900_rtc_ops = {
286 .read_time = max6900_rtc_read_time, 226 .read_time = max6900_rtc_read_time,
287 .set_time = max6900_rtc_set_time, 227 .set_time = max6900_rtc_set_time,
288}; 228};
289 229
290static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind) 230static int
231max6900_probe(struct i2c_client *client, const struct i2c_device_id *id)
291{ 232{
292 int rc = 0; 233 struct rtc_device *rtc;
293 struct i2c_client *client = NULL;
294 struct rtc_device *rtc = NULL;
295
296 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
297 rc = -ENODEV;
298 goto failout;
299 }
300
301 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
302 if (client == NULL) {
303 rc = -ENOMEM;
304 goto failout;
305 }
306
307 client->addr = addr;
308 client->adapter = adapter;
309 client->driver = &max6900_driver;
310 strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
311
312 if (kind < 0) {
313 rc = max6900_i2c_validate_client(client);
314 if (rc < 0)
315 goto failout;
316 }
317 234
318 rc = i2c_attach_client(client); 235 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
319 if (rc < 0) 236 return -ENODEV;
320 goto failout;
321 237
322 dev_info(&client->dev, 238 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
323 "chip found, driver version " DRV_VERSION "\n");
324 239
325 rtc = rtc_device_register(max6900_driver.driver.name, 240 rtc = rtc_device_register(max6900_driver.driver.name,
326 &client->dev, 241 &client->dev, &max6900_rtc_ops, THIS_MODULE);
327 &max6900_rtc_ops, THIS_MODULE); 242 if (IS_ERR(rtc))
328 if (IS_ERR(rtc)) { 243 return PTR_ERR(rtc);
329 rc = PTR_ERR(rtc);
330 goto failout_detach;
331 }
332 244
333 i2c_set_clientdata(client, rtc); 245 i2c_set_clientdata(client, rtc);
334 246
335 return 0; 247 return 0;
336
337failout_detach:
338 i2c_detach_client(client);
339failout:
340 kfree(client);
341 return rc;
342} 248}
343 249
250static struct i2c_driver max6900_driver = {
251 .driver = {
252 .name = "rtc-max6900",
253 },
254 .probe = max6900_probe,
255 .remove = max6900_remove,
256};
257
344static int __init max6900_init(void) 258static int __init max6900_init(void)
345{ 259{
346 return i2c_add_driver(&max6900_driver); 260 return i2c_add_driver(&max6900_driver);
@@ -352,6 +266,7 @@ static void __exit max6900_exit(void)
352} 266}
353 267
354MODULE_DESCRIPTION("Maxim MAX6900 RTC driver"); 268MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
269MODULE_AUTHOR("Dale Farnsworth <dale@farnsworth.org>");
355MODULE_LICENSE("GPL"); 270MODULE_LICENSE("GPL");
356MODULE_VERSION(DRV_VERSION); 271MODULE_VERSION(DRV_VERSION);
357 272
diff --git a/drivers/rtc/rtc-max6902.c b/drivers/rtc/rtc-max6902.c
index 78b2551fb19d..2f6507df7b49 100644
--- a/drivers/rtc/rtc-max6902.c
+++ b/drivers/rtc/rtc-max6902.c
@@ -124,15 +124,15 @@ static int max6902_get_datetime(struct device *dev, struct rtc_time *dt)
124 124
125 /* The chip sends data in this order: 125 /* The chip sends data in this order:
126 * Seconds, Minutes, Hours, Date, Month, Day, Year */ 126 * Seconds, Minutes, Hours, Date, Month, Day, Year */
127 dt->tm_sec = BCD2BIN(chip->buf[1]); 127 dt->tm_sec = bcd2bin(chip->buf[1]);
128 dt->tm_min = BCD2BIN(chip->buf[2]); 128 dt->tm_min = bcd2bin(chip->buf[2]);
129 dt->tm_hour = BCD2BIN(chip->buf[3]); 129 dt->tm_hour = bcd2bin(chip->buf[3]);
130 dt->tm_mday = BCD2BIN(chip->buf[4]); 130 dt->tm_mday = bcd2bin(chip->buf[4]);
131 dt->tm_mon = BCD2BIN(chip->buf[5]) - 1; 131 dt->tm_mon = bcd2bin(chip->buf[5]) - 1;
132 dt->tm_wday = BCD2BIN(chip->buf[6]); 132 dt->tm_wday = bcd2bin(chip->buf[6]);
133 dt->tm_year = BCD2BIN(chip->buf[7]); 133 dt->tm_year = bcd2bin(chip->buf[7]);
134 134
135 century = BCD2BIN(tmp) * 100; 135 century = bcd2bin(tmp) * 100;
136 136
137 dt->tm_year += century; 137 dt->tm_year += century;
138 dt->tm_year -= 1900; 138 dt->tm_year -= 1900;
@@ -168,15 +168,15 @@ static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
168 /* Remove write protection */ 168 /* Remove write protection */
169 max6902_set_reg(dev, 0xF, 0); 169 max6902_set_reg(dev, 0xF, 0);
170 170
171 max6902_set_reg(dev, 0x01, BIN2BCD(dt->tm_sec)); 171 max6902_set_reg(dev, 0x01, bin2bcd(dt->tm_sec));
172 max6902_set_reg(dev, 0x03, BIN2BCD(dt->tm_min)); 172 max6902_set_reg(dev, 0x03, bin2bcd(dt->tm_min));
173 max6902_set_reg(dev, 0x05, BIN2BCD(dt->tm_hour)); 173 max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour));
174 174
175 max6902_set_reg(dev, 0x07, BIN2BCD(dt->tm_mday)); 175 max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday));
176 max6902_set_reg(dev, 0x09, BIN2BCD(dt->tm_mon+1)); 176 max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon+1));
177 max6902_set_reg(dev, 0x0B, BIN2BCD(dt->tm_wday)); 177 max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday));
178 max6902_set_reg(dev, 0x0D, BIN2BCD(dt->tm_year%100)); 178 max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year%100));
179 max6902_set_reg(dev, 0x13, BIN2BCD(dt->tm_year/100)); 179 max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year/100));
180 180
181 /* Compulab used a delay here. However, the datasheet 181 /* Compulab used a delay here. However, the datasheet
182 * does not mention a delay being required anywhere... */ 182 * does not mention a delay being required anywhere... */
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
index 8876605d4d4b..2cbeb0794f14 100644
--- a/drivers/rtc/rtc-omap.c
+++ b/drivers/rtc/rtc-omap.c
@@ -186,30 +186,30 @@ static int tm2bcd(struct rtc_time *tm)
186 if (rtc_valid_tm(tm) != 0) 186 if (rtc_valid_tm(tm) != 0)
187 return -EINVAL; 187 return -EINVAL;
188 188
189 tm->tm_sec = BIN2BCD(tm->tm_sec); 189 tm->tm_sec = bin2bcd(tm->tm_sec);
190 tm->tm_min = BIN2BCD(tm->tm_min); 190 tm->tm_min = bin2bcd(tm->tm_min);
191 tm->tm_hour = BIN2BCD(tm->tm_hour); 191 tm->tm_hour = bin2bcd(tm->tm_hour);
192 tm->tm_mday = BIN2BCD(tm->tm_mday); 192 tm->tm_mday = bin2bcd(tm->tm_mday);
193 193
194 tm->tm_mon = BIN2BCD(tm->tm_mon + 1); 194 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
195 195
196 /* epoch == 1900 */ 196 /* epoch == 1900 */
197 if (tm->tm_year < 100 || tm->tm_year > 199) 197 if (tm->tm_year < 100 || tm->tm_year > 199)
198 return -EINVAL; 198 return -EINVAL;
199 tm->tm_year = BIN2BCD(tm->tm_year - 100); 199 tm->tm_year = bin2bcd(tm->tm_year - 100);
200 200
201 return 0; 201 return 0;
202} 202}
203 203
204static void bcd2tm(struct rtc_time *tm) 204static void bcd2tm(struct rtc_time *tm)
205{ 205{
206 tm->tm_sec = BCD2BIN(tm->tm_sec); 206 tm->tm_sec = bcd2bin(tm->tm_sec);
207 tm->tm_min = BCD2BIN(tm->tm_min); 207 tm->tm_min = bcd2bin(tm->tm_min);
208 tm->tm_hour = BCD2BIN(tm->tm_hour); 208 tm->tm_hour = bcd2bin(tm->tm_hour);
209 tm->tm_mday = BCD2BIN(tm->tm_mday); 209 tm->tm_mday = bcd2bin(tm->tm_mday);
210 tm->tm_mon = BCD2BIN(tm->tm_mon) - 1; 210 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
211 /* epoch == 1900 */ 211 /* epoch == 1900 */
212 tm->tm_year = BCD2BIN(tm->tm_year) + 100; 212 tm->tm_year = bcd2bin(tm->tm_year) + 100;
213} 213}
214 214
215 215
diff --git a/drivers/rtc/rtc-parisc.c b/drivers/rtc/rtc-parisc.c
new file mode 100644
index 000000000000..346d633655e7
--- /dev/null
+++ b/drivers/rtc/rtc-parisc.c
@@ -0,0 +1,111 @@
1/* rtc-parisc: RTC for HP PA-RISC firmware
2 *
3 * Copyright (C) 2008 Kyle McMartin <kyle@mcmartin.ca>
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/time.h>
9#include <linux/platform_device.h>
10
11#include <asm/rtc.h>
12
13/* as simple as can be, and no simpler. */
14struct parisc_rtc {
15 struct rtc_device *rtc;
16 spinlock_t lock;
17};
18
19static int parisc_get_time(struct device *dev, struct rtc_time *tm)
20{
21 struct parisc_rtc *p = dev_get_drvdata(dev);
22 unsigned long flags, ret;
23
24 spin_lock_irqsave(&p->lock, flags);
25 ret = get_rtc_time(tm);
26 spin_unlock_irqrestore(&p->lock, flags);
27
28 if (ret & RTC_BATT_BAD)
29 return -EOPNOTSUPP;
30
31 return 0;
32}
33
34static int parisc_set_time(struct device *dev, struct rtc_time *tm)
35{
36 struct parisc_rtc *p = dev_get_drvdata(dev);
37 unsigned long flags, ret;
38
39 spin_lock_irqsave(&p->lock, flags);
40 ret = set_rtc_time(tm);
41 spin_unlock_irqrestore(&p->lock, flags);
42
43 if (ret < 0)
44 return -EOPNOTSUPP;
45
46 return 0;
47}
48
49static const struct rtc_class_ops parisc_rtc_ops = {
50 .read_time = parisc_get_time,
51 .set_time = parisc_set_time,
52};
53
54static int __devinit parisc_rtc_probe(struct platform_device *dev)
55{
56 struct parisc_rtc *p;
57
58 p = kzalloc(sizeof (*p), GFP_KERNEL);
59 if (!p)
60 return -ENOMEM;
61
62 spin_lock_init(&p->lock);
63
64 p->rtc = rtc_device_register("rtc-parisc", &dev->dev, &parisc_rtc_ops,
65 THIS_MODULE);
66 if (IS_ERR(p->rtc)) {
67 int err = PTR_ERR(p->rtc);
68 kfree(p);
69 return err;
70 }
71
72 platform_set_drvdata(dev, p);
73
74 return 0;
75}
76
77static int __devexit parisc_rtc_remove(struct platform_device *dev)
78{
79 struct parisc_rtc *p = platform_get_drvdata(dev);
80
81 rtc_device_unregister(p->rtc);
82 kfree(p);
83
84 return 0;
85}
86
87static struct platform_driver parisc_rtc_driver = {
88 .driver = {
89 .name = "rtc-parisc",
90 .owner = THIS_MODULE,
91 },
92 .probe = parisc_rtc_probe,
93 .remove = __devexit_p(parisc_rtc_remove),
94};
95
96static int __init parisc_rtc_init(void)
97{
98 return platform_driver_register(&parisc_rtc_driver);
99}
100
101static void __exit parisc_rtc_fini(void)
102{
103 platform_driver_unregister(&parisc_rtc_driver);
104}
105
106module_init(parisc_rtc_init);
107module_exit(parisc_rtc_fini);
108
109MODULE_AUTHOR("Kyle McMartin <kyle@mcmartin.ca>");
110MODULE_LICENSE("GPL");
111MODULE_DESCRIPTION("HP PA-RISC RTC driver");
diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c
index 748a502a6355..b725913ccbe8 100644
--- a/drivers/rtc/rtc-pcf8563.c
+++ b/drivers/rtc/rtc-pcf8563.c
@@ -97,13 +97,13 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
97 buf[8]); 97 buf[8]);
98 98
99 99
100 tm->tm_sec = BCD2BIN(buf[PCF8563_REG_SC] & 0x7F); 100 tm->tm_sec = bcd2bin(buf[PCF8563_REG_SC] & 0x7F);
101 tm->tm_min = BCD2BIN(buf[PCF8563_REG_MN] & 0x7F); 101 tm->tm_min = bcd2bin(buf[PCF8563_REG_MN] & 0x7F);
102 tm->tm_hour = BCD2BIN(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */ 102 tm->tm_hour = bcd2bin(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */
103 tm->tm_mday = BCD2BIN(buf[PCF8563_REG_DM] & 0x3F); 103 tm->tm_mday = bcd2bin(buf[PCF8563_REG_DM] & 0x3F);
104 tm->tm_wday = buf[PCF8563_REG_DW] & 0x07; 104 tm->tm_wday = buf[PCF8563_REG_DW] & 0x07;
105 tm->tm_mon = BCD2BIN(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ 105 tm->tm_mon = bcd2bin(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
106 tm->tm_year = BCD2BIN(buf[PCF8563_REG_YR]); 106 tm->tm_year = bcd2bin(buf[PCF8563_REG_YR]);
107 if (tm->tm_year < 70) 107 if (tm->tm_year < 70)
108 tm->tm_year += 100; /* assume we are in 1970...2069 */ 108 tm->tm_year += 100; /* assume we are in 1970...2069 */
109 /* detect the polarity heuristically. see note above. */ 109 /* detect the polarity heuristically. see note above. */
@@ -138,17 +138,17 @@ static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
138 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 138 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
139 139
140 /* hours, minutes and seconds */ 140 /* hours, minutes and seconds */
141 buf[PCF8563_REG_SC] = BIN2BCD(tm->tm_sec); 141 buf[PCF8563_REG_SC] = bin2bcd(tm->tm_sec);
142 buf[PCF8563_REG_MN] = BIN2BCD(tm->tm_min); 142 buf[PCF8563_REG_MN] = bin2bcd(tm->tm_min);
143 buf[PCF8563_REG_HR] = BIN2BCD(tm->tm_hour); 143 buf[PCF8563_REG_HR] = bin2bcd(tm->tm_hour);
144 144
145 buf[PCF8563_REG_DM] = BIN2BCD(tm->tm_mday); 145 buf[PCF8563_REG_DM] = bin2bcd(tm->tm_mday);
146 146
147 /* month, 1 - 12 */ 147 /* month, 1 - 12 */
148 buf[PCF8563_REG_MO] = BIN2BCD(tm->tm_mon + 1); 148 buf[PCF8563_REG_MO] = bin2bcd(tm->tm_mon + 1);
149 149
150 /* year and century */ 150 /* year and century */
151 buf[PCF8563_REG_YR] = BIN2BCD(tm->tm_year % 100); 151 buf[PCF8563_REG_YR] = bin2bcd(tm->tm_year % 100);
152 if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100)) 152 if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100))
153 buf[PCF8563_REG_MO] |= PCF8563_MO_C; 153 buf[PCF8563_REG_MO] |= PCF8563_MO_C;
154 154
@@ -179,58 +179,6 @@ struct pcf8563_limit
179 unsigned char max; 179 unsigned char max;
180}; 180};
181 181
182static int pcf8563_validate_client(struct i2c_client *client)
183{
184 int i;
185
186 static const struct pcf8563_limit pattern[] = {
187 /* register, mask, min, max */
188 { PCF8563_REG_SC, 0x7F, 0, 59 },
189 { PCF8563_REG_MN, 0x7F, 0, 59 },
190 { PCF8563_REG_HR, 0x3F, 0, 23 },
191 { PCF8563_REG_DM, 0x3F, 0, 31 },
192 { PCF8563_REG_MO, 0x1F, 0, 12 },
193 };
194
195 /* check limits (only registers with bcd values) */
196 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
197 int xfer;
198 unsigned char value;
199 unsigned char buf = pattern[i].reg;
200
201 struct i2c_msg msgs[] = {
202 { client->addr, 0, 1, &buf },
203 { client->addr, I2C_M_RD, 1, &buf },
204 };
205
206 xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
207
208 if (xfer != ARRAY_SIZE(msgs)) {
209 dev_err(&client->dev,
210 "%s: could not read register 0x%02X\n",
211 __func__, pattern[i].reg);
212
213 return -EIO;
214 }
215
216 value = BCD2BIN(buf & pattern[i].mask);
217
218 if (value > pattern[i].max ||
219 value < pattern[i].min) {
220 dev_dbg(&client->dev,
221 "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, "
222 "max=%d, value=%d, raw=0x%02X\n",
223 __func__, i, pattern[i].reg, pattern[i].mask,
224 pattern[i].min, pattern[i].max,
225 value, buf);
226
227 return -ENODEV;
228 }
229 }
230
231 return 0;
232}
233
234static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm) 182static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
235{ 183{
236 return pcf8563_get_datetime(to_i2c_client(dev), tm); 184 return pcf8563_get_datetime(to_i2c_client(dev), tm);
@@ -262,12 +210,6 @@ static int pcf8563_probe(struct i2c_client *client,
262 if (!pcf8563) 210 if (!pcf8563)
263 return -ENOMEM; 211 return -ENOMEM;
264 212
265 /* Verify the chip is really an PCF8563 */
266 if (pcf8563_validate_client(client) < 0) {
267 err = -ENODEV;
268 goto exit_kfree;
269 }
270
271 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); 213 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
272 214
273 pcf8563->rtc = rtc_device_register(pcf8563_driver.driver.name, 215 pcf8563->rtc = rtc_device_register(pcf8563_driver.driver.name,
diff --git a/drivers/rtc/rtc-pcf8583.c b/drivers/rtc/rtc-pcf8583.c
index d388c662bf4b..7d33cda3f8f6 100644
--- a/drivers/rtc/rtc-pcf8583.c
+++ b/drivers/rtc/rtc-pcf8583.c
@@ -76,11 +76,11 @@ static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
76 buf[4] &= 0x3f; 76 buf[4] &= 0x3f;
77 buf[5] &= 0x1f; 77 buf[5] &= 0x1f;
78 78
79 dt->tm_sec = BCD2BIN(buf[1]); 79 dt->tm_sec = bcd2bin(buf[1]);
80 dt->tm_min = BCD2BIN(buf[2]); 80 dt->tm_min = bcd2bin(buf[2]);
81 dt->tm_hour = BCD2BIN(buf[3]); 81 dt->tm_hour = bcd2bin(buf[3]);
82 dt->tm_mday = BCD2BIN(buf[4]); 82 dt->tm_mday = bcd2bin(buf[4]);
83 dt->tm_mon = BCD2BIN(buf[5]) - 1; 83 dt->tm_mon = bcd2bin(buf[5]) - 1;
84 } 84 }
85 85
86 return ret == 2 ? 0 : -EIO; 86 return ret == 2 ? 0 : -EIO;
@@ -94,14 +94,14 @@ static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt,
94 buf[0] = 0; 94 buf[0] = 0;
95 buf[1] = get_ctrl(client) | 0x80; 95 buf[1] = get_ctrl(client) | 0x80;
96 buf[2] = 0; 96 buf[2] = 0;
97 buf[3] = BIN2BCD(dt->tm_sec); 97 buf[3] = bin2bcd(dt->tm_sec);
98 buf[4] = BIN2BCD(dt->tm_min); 98 buf[4] = bin2bcd(dt->tm_min);
99 buf[5] = BIN2BCD(dt->tm_hour); 99 buf[5] = bin2bcd(dt->tm_hour);
100 100
101 if (datetoo) { 101 if (datetoo) {
102 len = 8; 102 len = 8;
103 buf[6] = BIN2BCD(dt->tm_mday) | (dt->tm_year << 6); 103 buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
104 buf[7] = BIN2BCD(dt->tm_mon + 1) | (dt->tm_wday << 5); 104 buf[7] = bin2bcd(dt->tm_mon + 1) | (dt->tm_wday << 5);
105 } 105 }
106 106
107 ret = i2c_master_send(client, (char *)buf, len); 107 ret = i2c_master_send(client, (char *)buf, len);
diff --git a/drivers/rtc/rtc-pl030.c b/drivers/rtc/rtc-pl030.c
index 8448eeb9d675..826153552157 100644
--- a/drivers/rtc/rtc-pl030.c
+++ b/drivers/rtc/rtc-pl030.c
@@ -34,15 +34,6 @@ static irqreturn_t pl030_interrupt(int irq, void *dev_id)
34 return IRQ_HANDLED; 34 return IRQ_HANDLED;
35} 35}
36 36
37static int pl030_open(struct device *dev)
38{
39 return 0;
40}
41
42static void pl030_release(struct device *dev)
43{
44}
45
46static int pl030_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 37static int pl030_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
47{ 38{
48 return -ENOIOCTLCMD; 39 return -ENOIOCTLCMD;
@@ -104,8 +95,6 @@ static int pl030_set_time(struct device *dev, struct rtc_time *tm)
104} 95}
105 96
106static const struct rtc_class_ops pl030_ops = { 97static const struct rtc_class_ops pl030_ops = {
107 .open = pl030_open,
108 .release = pl030_release,
109 .ioctl = pl030_ioctl, 98 .ioctl = pl030_ioctl,
110 .read_time = pl030_read_time, 99 .read_time = pl030_read_time,
111 .set_time = pl030_set_time, 100 .set_time = pl030_set_time,
diff --git a/drivers/rtc/rtc-pl031.c b/drivers/rtc/rtc-pl031.c
index 08b4610ec5a6..333eec689d2f 100644
--- a/drivers/rtc/rtc-pl031.c
+++ b/drivers/rtc/rtc-pl031.c
@@ -45,18 +45,6 @@ static irqreturn_t pl031_interrupt(int irq, void *dev_id)
45 return IRQ_HANDLED; 45 return IRQ_HANDLED;
46} 46}
47 47
48static int pl031_open(struct device *dev)
49{
50 /*
51 * We request IRQ in pl031_probe, so nothing to do here...
52 */
53 return 0;
54}
55
56static void pl031_release(struct device *dev)
57{
58}
59
60static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 48static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
61{ 49{
62 struct pl031_local *ldata = dev_get_drvdata(dev); 50 struct pl031_local *ldata = dev_get_drvdata(dev);
@@ -118,8 +106,6 @@ static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
118} 106}
119 107
120static const struct rtc_class_ops pl031_ops = { 108static const struct rtc_class_ops pl031_ops = {
121 .open = pl031_open,
122 .release = pl031_release,
123 .ioctl = pl031_ioctl, 109 .ioctl = pl031_ioctl,
124 .read_time = pl031_read_time, 110 .read_time = pl031_read_time,
125 .set_time = pl031_set_time, 111 .set_time = pl031_set_time,
diff --git a/drivers/rtc/rtc-r9701.c b/drivers/rtc/rtc-r9701.c
index 395985b339c9..42028f233bef 100644
--- a/drivers/rtc/rtc-r9701.c
+++ b/drivers/rtc/rtc-r9701.c
@@ -80,13 +80,13 @@ static int r9701_get_datetime(struct device *dev, struct rtc_time *dt)
80 80
81 memset(dt, 0, sizeof(*dt)); 81 memset(dt, 0, sizeof(*dt));
82 82
83 dt->tm_sec = BCD2BIN(buf[0]); /* RSECCNT */ 83 dt->tm_sec = bcd2bin(buf[0]); /* RSECCNT */
84 dt->tm_min = BCD2BIN(buf[1]); /* RMINCNT */ 84 dt->tm_min = bcd2bin(buf[1]); /* RMINCNT */
85 dt->tm_hour = BCD2BIN(buf[2]); /* RHRCNT */ 85 dt->tm_hour = bcd2bin(buf[2]); /* RHRCNT */
86 86
87 dt->tm_mday = BCD2BIN(buf[3]); /* RDAYCNT */ 87 dt->tm_mday = bcd2bin(buf[3]); /* RDAYCNT */
88 dt->tm_mon = BCD2BIN(buf[4]) - 1; /* RMONCNT */ 88 dt->tm_mon = bcd2bin(buf[4]) - 1; /* RMONCNT */
89 dt->tm_year = BCD2BIN(buf[5]) + 100; /* RYRCNT */ 89 dt->tm_year = bcd2bin(buf[5]) + 100; /* RYRCNT */
90 90
91 /* the rtc device may contain illegal values on power up 91 /* the rtc device may contain illegal values on power up
92 * according to the data sheet. make sure they are valid. 92 * according to the data sheet. make sure they are valid.
@@ -103,12 +103,12 @@ static int r9701_set_datetime(struct device *dev, struct rtc_time *dt)
103 if (year >= 2100 || year < 2000) 103 if (year >= 2100 || year < 2000)
104 return -EINVAL; 104 return -EINVAL;
105 105
106 ret = write_reg(dev, RHRCNT, BIN2BCD(dt->tm_hour)); 106 ret = write_reg(dev, RHRCNT, bin2bcd(dt->tm_hour));
107 ret = ret ? ret : write_reg(dev, RMINCNT, BIN2BCD(dt->tm_min)); 107 ret = ret ? ret : write_reg(dev, RMINCNT, bin2bcd(dt->tm_min));
108 ret = ret ? ret : write_reg(dev, RSECCNT, BIN2BCD(dt->tm_sec)); 108 ret = ret ? ret : write_reg(dev, RSECCNT, bin2bcd(dt->tm_sec));
109 ret = ret ? ret : write_reg(dev, RDAYCNT, BIN2BCD(dt->tm_mday)); 109 ret = ret ? ret : write_reg(dev, RDAYCNT, bin2bcd(dt->tm_mday));
110 ret = ret ? ret : write_reg(dev, RMONCNT, BIN2BCD(dt->tm_mon + 1)); 110 ret = ret ? ret : write_reg(dev, RMONCNT, bin2bcd(dt->tm_mon + 1));
111 ret = ret ? ret : write_reg(dev, RYRCNT, BIN2BCD(dt->tm_year - 100)); 111 ret = ret ? ret : write_reg(dev, RYRCNT, bin2bcd(dt->tm_year - 100));
112 ret = ret ? ret : write_reg(dev, RWKCNT, 1 << dt->tm_wday); 112 ret = ret ? ret : write_reg(dev, RWKCNT, 1 << dt->tm_wday);
113 113
114 return ret; 114 return ret;
diff --git a/drivers/rtc/rtc-rs5c313.c b/drivers/rtc/rtc-rs5c313.c
index 1c14d4497c4d..e6ea3f5ee1eb 100644
--- a/drivers/rtc/rtc-rs5c313.c
+++ b/drivers/rtc/rtc-rs5c313.c
@@ -235,33 +235,33 @@ static int rs5c313_rtc_read_time(struct device *dev, struct rtc_time *tm)
235 235
236 data = rs5c313_read_reg(RS5C313_ADDR_SEC); 236 data = rs5c313_read_reg(RS5C313_ADDR_SEC);
237 data |= (rs5c313_read_reg(RS5C313_ADDR_SEC10) << 4); 237 data |= (rs5c313_read_reg(RS5C313_ADDR_SEC10) << 4);
238 tm->tm_sec = BCD2BIN(data); 238 tm->tm_sec = bcd2bin(data);
239 239
240 data = rs5c313_read_reg(RS5C313_ADDR_MIN); 240 data = rs5c313_read_reg(RS5C313_ADDR_MIN);
241 data |= (rs5c313_read_reg(RS5C313_ADDR_MIN10) << 4); 241 data |= (rs5c313_read_reg(RS5C313_ADDR_MIN10) << 4);
242 tm->tm_min = BCD2BIN(data); 242 tm->tm_min = bcd2bin(data);
243 243
244 data = rs5c313_read_reg(RS5C313_ADDR_HOUR); 244 data = rs5c313_read_reg(RS5C313_ADDR_HOUR);
245 data |= (rs5c313_read_reg(RS5C313_ADDR_HOUR10) << 4); 245 data |= (rs5c313_read_reg(RS5C313_ADDR_HOUR10) << 4);
246 tm->tm_hour = BCD2BIN(data); 246 tm->tm_hour = bcd2bin(data);
247 247
248 data = rs5c313_read_reg(RS5C313_ADDR_DAY); 248 data = rs5c313_read_reg(RS5C313_ADDR_DAY);
249 data |= (rs5c313_read_reg(RS5C313_ADDR_DAY10) << 4); 249 data |= (rs5c313_read_reg(RS5C313_ADDR_DAY10) << 4);
250 tm->tm_mday = BCD2BIN(data); 250 tm->tm_mday = bcd2bin(data);
251 251
252 data = rs5c313_read_reg(RS5C313_ADDR_MON); 252 data = rs5c313_read_reg(RS5C313_ADDR_MON);
253 data |= (rs5c313_read_reg(RS5C313_ADDR_MON10) << 4); 253 data |= (rs5c313_read_reg(RS5C313_ADDR_MON10) << 4);
254 tm->tm_mon = BCD2BIN(data) - 1; 254 tm->tm_mon = bcd2bin(data) - 1;
255 255
256 data = rs5c313_read_reg(RS5C313_ADDR_YEAR); 256 data = rs5c313_read_reg(RS5C313_ADDR_YEAR);
257 data |= (rs5c313_read_reg(RS5C313_ADDR_YEAR10) << 4); 257 data |= (rs5c313_read_reg(RS5C313_ADDR_YEAR10) << 4);
258 tm->tm_year = BCD2BIN(data); 258 tm->tm_year = bcd2bin(data);
259 259
260 if (tm->tm_year < 70) 260 if (tm->tm_year < 70)
261 tm->tm_year += 100; 261 tm->tm_year += 100;
262 262
263 data = rs5c313_read_reg(RS5C313_ADDR_WEEK); 263 data = rs5c313_read_reg(RS5C313_ADDR_WEEK);
264 tm->tm_wday = BCD2BIN(data); 264 tm->tm_wday = bcd2bin(data);
265 265
266 RS5C313_CEDISABLE; 266 RS5C313_CEDISABLE;
267 ndelay(700); /* CE:L */ 267 ndelay(700); /* CE:L */
@@ -294,31 +294,31 @@ static int rs5c313_rtc_set_time(struct device *dev, struct rtc_time *tm)
294 } 294 }
295 } 295 }
296 296
297 data = BIN2BCD(tm->tm_sec); 297 data = bin2bcd(tm->tm_sec);
298 rs5c313_write_reg(RS5C313_ADDR_SEC, data); 298 rs5c313_write_reg(RS5C313_ADDR_SEC, data);
299 rs5c313_write_reg(RS5C313_ADDR_SEC10, (data >> 4)); 299 rs5c313_write_reg(RS5C313_ADDR_SEC10, (data >> 4));
300 300
301 data = BIN2BCD(tm->tm_min); 301 data = bin2bcd(tm->tm_min);
302 rs5c313_write_reg(RS5C313_ADDR_MIN, data ); 302 rs5c313_write_reg(RS5C313_ADDR_MIN, data );
303 rs5c313_write_reg(RS5C313_ADDR_MIN10, (data >> 4)); 303 rs5c313_write_reg(RS5C313_ADDR_MIN10, (data >> 4));
304 304
305 data = BIN2BCD(tm->tm_hour); 305 data = bin2bcd(tm->tm_hour);
306 rs5c313_write_reg(RS5C313_ADDR_HOUR, data); 306 rs5c313_write_reg(RS5C313_ADDR_HOUR, data);
307 rs5c313_write_reg(RS5C313_ADDR_HOUR10, (data >> 4)); 307 rs5c313_write_reg(RS5C313_ADDR_HOUR10, (data >> 4));
308 308
309 data = BIN2BCD(tm->tm_mday); 309 data = bin2bcd(tm->tm_mday);
310 rs5c313_write_reg(RS5C313_ADDR_DAY, data); 310 rs5c313_write_reg(RS5C313_ADDR_DAY, data);
311 rs5c313_write_reg(RS5C313_ADDR_DAY10, (data>> 4)); 311 rs5c313_write_reg(RS5C313_ADDR_DAY10, (data>> 4));
312 312
313 data = BIN2BCD(tm->tm_mon + 1); 313 data = bin2bcd(tm->tm_mon + 1);
314 rs5c313_write_reg(RS5C313_ADDR_MON, data); 314 rs5c313_write_reg(RS5C313_ADDR_MON, data);
315 rs5c313_write_reg(RS5C313_ADDR_MON10, (data >> 4)); 315 rs5c313_write_reg(RS5C313_ADDR_MON10, (data >> 4));
316 316
317 data = BIN2BCD(tm->tm_year % 100); 317 data = bin2bcd(tm->tm_year % 100);
318 rs5c313_write_reg(RS5C313_ADDR_YEAR, data); 318 rs5c313_write_reg(RS5C313_ADDR_YEAR, data);
319 rs5c313_write_reg(RS5C313_ADDR_YEAR10, (data >> 4)); 319 rs5c313_write_reg(RS5C313_ADDR_YEAR10, (data >> 4));
320 320
321 data = BIN2BCD(tm->tm_wday); 321 data = bin2bcd(tm->tm_wday);
322 rs5c313_write_reg(RS5C313_ADDR_WEEK, data); 322 rs5c313_write_reg(RS5C313_ADDR_WEEK, data);
323 323
324 RS5C313_CEDISABLE; /* CE:H */ 324 RS5C313_CEDISABLE; /* CE:H */
diff --git a/drivers/rtc/rtc-rs5c348.c b/drivers/rtc/rtc-rs5c348.c
index 839462659afa..dd1e2bc7a472 100644
--- a/drivers/rtc/rtc-rs5c348.c
+++ b/drivers/rtc/rtc-rs5c348.c
@@ -74,20 +74,20 @@ rs5c348_rtc_set_time(struct device *dev, struct rtc_time *tm)
74 txbuf[3] = 0; /* dummy */ 74 txbuf[3] = 0; /* dummy */
75 txbuf[4] = RS5C348_CMD_MW(RS5C348_REG_SECS); /* cmd, sec, ... */ 75 txbuf[4] = RS5C348_CMD_MW(RS5C348_REG_SECS); /* cmd, sec, ... */
76 txp = &txbuf[5]; 76 txp = &txbuf[5];
77 txp[RS5C348_REG_SECS] = BIN2BCD(tm->tm_sec); 77 txp[RS5C348_REG_SECS] = bin2bcd(tm->tm_sec);
78 txp[RS5C348_REG_MINS] = BIN2BCD(tm->tm_min); 78 txp[RS5C348_REG_MINS] = bin2bcd(tm->tm_min);
79 if (pdata->rtc_24h) { 79 if (pdata->rtc_24h) {
80 txp[RS5C348_REG_HOURS] = BIN2BCD(tm->tm_hour); 80 txp[RS5C348_REG_HOURS] = bin2bcd(tm->tm_hour);
81 } else { 81 } else {
82 /* hour 0 is AM12, noon is PM12 */ 82 /* hour 0 is AM12, noon is PM12 */
83 txp[RS5C348_REG_HOURS] = BIN2BCD((tm->tm_hour + 11) % 12 + 1) | 83 txp[RS5C348_REG_HOURS] = bin2bcd((tm->tm_hour + 11) % 12 + 1) |
84 (tm->tm_hour >= 12 ? RS5C348_BIT_PM : 0); 84 (tm->tm_hour >= 12 ? RS5C348_BIT_PM : 0);
85 } 85 }
86 txp[RS5C348_REG_WDAY] = BIN2BCD(tm->tm_wday); 86 txp[RS5C348_REG_WDAY] = bin2bcd(tm->tm_wday);
87 txp[RS5C348_REG_DAY] = BIN2BCD(tm->tm_mday); 87 txp[RS5C348_REG_DAY] = bin2bcd(tm->tm_mday);
88 txp[RS5C348_REG_MONTH] = BIN2BCD(tm->tm_mon + 1) | 88 txp[RS5C348_REG_MONTH] = bin2bcd(tm->tm_mon + 1) |
89 (tm->tm_year >= 100 ? RS5C348_BIT_Y2K : 0); 89 (tm->tm_year >= 100 ? RS5C348_BIT_Y2K : 0);
90 txp[RS5C348_REG_YEAR] = BIN2BCD(tm->tm_year % 100); 90 txp[RS5C348_REG_YEAR] = bin2bcd(tm->tm_year % 100);
91 /* write in one transfer to avoid data inconsistency */ 91 /* write in one transfer to avoid data inconsistency */
92 ret = spi_write_then_read(spi, txbuf, sizeof(txbuf), NULL, 0); 92 ret = spi_write_then_read(spi, txbuf, sizeof(txbuf), NULL, 0);
93 udelay(62); /* Tcsr 62us */ 93 udelay(62); /* Tcsr 62us */
@@ -116,20 +116,20 @@ rs5c348_rtc_read_time(struct device *dev, struct rtc_time *tm)
116 if (ret < 0) 116 if (ret < 0)
117 return ret; 117 return ret;
118 118
119 tm->tm_sec = BCD2BIN(rxbuf[RS5C348_REG_SECS] & RS5C348_SECS_MASK); 119 tm->tm_sec = bcd2bin(rxbuf[RS5C348_REG_SECS] & RS5C348_SECS_MASK);
120 tm->tm_min = BCD2BIN(rxbuf[RS5C348_REG_MINS] & RS5C348_MINS_MASK); 120 tm->tm_min = bcd2bin(rxbuf[RS5C348_REG_MINS] & RS5C348_MINS_MASK);
121 tm->tm_hour = BCD2BIN(rxbuf[RS5C348_REG_HOURS] & RS5C348_HOURS_MASK); 121 tm->tm_hour = bcd2bin(rxbuf[RS5C348_REG_HOURS] & RS5C348_HOURS_MASK);
122 if (!pdata->rtc_24h) { 122 if (!pdata->rtc_24h) {
123 tm->tm_hour %= 12; 123 tm->tm_hour %= 12;
124 if (rxbuf[RS5C348_REG_HOURS] & RS5C348_BIT_PM) 124 if (rxbuf[RS5C348_REG_HOURS] & RS5C348_BIT_PM)
125 tm->tm_hour += 12; 125 tm->tm_hour += 12;
126 } 126 }
127 tm->tm_wday = BCD2BIN(rxbuf[RS5C348_REG_WDAY] & RS5C348_WDAY_MASK); 127 tm->tm_wday = bcd2bin(rxbuf[RS5C348_REG_WDAY] & RS5C348_WDAY_MASK);
128 tm->tm_mday = BCD2BIN(rxbuf[RS5C348_REG_DAY] & RS5C348_DAY_MASK); 128 tm->tm_mday = bcd2bin(rxbuf[RS5C348_REG_DAY] & RS5C348_DAY_MASK);
129 tm->tm_mon = 129 tm->tm_mon =
130 BCD2BIN(rxbuf[RS5C348_REG_MONTH] & RS5C348_MONTH_MASK) - 1; 130 bcd2bin(rxbuf[RS5C348_REG_MONTH] & RS5C348_MONTH_MASK) - 1;
131 /* year is 1900 + tm->tm_year */ 131 /* year is 1900 + tm->tm_year */
132 tm->tm_year = BCD2BIN(rxbuf[RS5C348_REG_YEAR]) + 132 tm->tm_year = bcd2bin(rxbuf[RS5C348_REG_YEAR]) +
133 ((rxbuf[RS5C348_REG_MONTH] & RS5C348_BIT_Y2K) ? 100 : 0); 133 ((rxbuf[RS5C348_REG_MONTH] & RS5C348_BIT_Y2K) ? 100 : 0);
134 134
135 if (rtc_valid_tm(tm) < 0) { 135 if (rtc_valid_tm(tm) < 0) {
diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c
index 56caf6b2c3e5..2f2c68d476da 100644
--- a/drivers/rtc/rtc-rs5c372.c
+++ b/drivers/rtc/rtc-rs5c372.c
@@ -1,8 +1,9 @@
1/* 1/*
2 * An I2C driver for Ricoh RS5C372 and RV5C38[67] RTCs 2 * An I2C driver for Ricoh RS5C372, R2025S/D and RV5C38[67] RTCs
3 * 3 *
4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net> 4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5 * Copyright (C) 2006 Tower Technologies 5 * Copyright (C) 2006 Tower Technologies
6 * Copyright (C) 2008 Paul Mundt
6 * 7 *
7 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as 9 * it under the terms of the GNU General Public License version 2 as
@@ -13,7 +14,7 @@
13#include <linux/rtc.h> 14#include <linux/rtc.h>
14#include <linux/bcd.h> 15#include <linux/bcd.h>
15 16
16#define DRV_VERSION "0.5" 17#define DRV_VERSION "0.6"
17 18
18 19
19/* 20/*
@@ -51,7 +52,8 @@
51# define RS5C_CTRL1_CT4 (4 << 0) /* 1 Hz level irq */ 52# define RS5C_CTRL1_CT4 (4 << 0) /* 1 Hz level irq */
52#define RS5C_REG_CTRL2 15 53#define RS5C_REG_CTRL2 15
53# define RS5C372_CTRL2_24 (1 << 5) 54# define RS5C372_CTRL2_24 (1 << 5)
54# define RS5C_CTRL2_XSTP (1 << 4) 55# define R2025_CTRL2_XST (1 << 5)
56# define RS5C_CTRL2_XSTP (1 << 4) /* only if !R2025S/D */
55# define RS5C_CTRL2_CTFG (1 << 2) 57# define RS5C_CTRL2_CTFG (1 << 2)
56# define RS5C_CTRL2_AAFG (1 << 1) /* or WAFG */ 58# define RS5C_CTRL2_AAFG (1 << 1) /* or WAFG */
57# define RS5C_CTRL2_BAFG (1 << 0) /* or DAFG */ 59# define RS5C_CTRL2_BAFG (1 << 0) /* or DAFG */
@@ -63,6 +65,7 @@
63 65
64enum rtc_type { 66enum rtc_type {
65 rtc_undef = 0, 67 rtc_undef = 0,
68 rtc_r2025sd,
66 rtc_rs5c372a, 69 rtc_rs5c372a,
67 rtc_rs5c372b, 70 rtc_rs5c372b,
68 rtc_rv5c386, 71 rtc_rv5c386,
@@ -70,6 +73,7 @@ enum rtc_type {
70}; 73};
71 74
72static const struct i2c_device_id rs5c372_id[] = { 75static const struct i2c_device_id rs5c372_id[] = {
76 { "r2025sd", rtc_r2025sd },
73 { "rs5c372a", rtc_rs5c372a }, 77 { "rs5c372a", rtc_rs5c372a },
74 { "rs5c372b", rtc_rs5c372b }, 78 { "rs5c372b", rtc_rs5c372b },
75 { "rv5c386", rtc_rv5c386 }, 79 { "rv5c386", rtc_rv5c386 },
@@ -89,6 +93,7 @@ struct rs5c372 {
89 enum rtc_type type; 93 enum rtc_type type;
90 unsigned time24:1; 94 unsigned time24:1;
91 unsigned has_irq:1; 95 unsigned has_irq:1;
96 unsigned smbus:1;
92 char buf[17]; 97 char buf[17];
93 char *regs; 98 char *regs;
94}; 99};
@@ -106,10 +111,25 @@ static int rs5c_get_regs(struct rs5c372 *rs5c)
106 * 111 *
107 * The first method doesn't work with the iop3xx adapter driver, on at 112 * The first method doesn't work with the iop3xx adapter driver, on at
108 * least 80219 chips; this works around that bug. 113 * least 80219 chips; this works around that bug.
114 *
115 * The third method on the other hand doesn't work for the SMBus-only
116 * configurations, so we use the the first method there, stripping off
117 * the extra register in the process.
109 */ 118 */
110 if ((i2c_transfer(client->adapter, msgs, 1)) != 1) { 119 if (rs5c->smbus) {
111 dev_warn(&client->dev, "can't read registers\n"); 120 int addr = RS5C_ADDR(RS5C372_REG_SECS);
112 return -EIO; 121 int size = sizeof(rs5c->buf) - 1;
122
123 if (i2c_smbus_read_i2c_block_data(client, addr, size,
124 rs5c->buf + 1) != size) {
125 dev_warn(&client->dev, "can't read registers\n");
126 return -EIO;
127 }
128 } else {
129 if ((i2c_transfer(client->adapter, msgs, 1)) != 1) {
130 dev_warn(&client->dev, "can't read registers\n");
131 return -EIO;
132 }
113 } 133 }
114 134
115 dev_dbg(&client->dev, 135 dev_dbg(&client->dev,
@@ -128,9 +148,9 @@ static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg)
128 unsigned hour; 148 unsigned hour;
129 149
130 if (rs5c->time24) 150 if (rs5c->time24)
131 return BCD2BIN(reg & 0x3f); 151 return bcd2bin(reg & 0x3f);
132 152
133 hour = BCD2BIN(reg & 0x1f); 153 hour = bcd2bin(reg & 0x1f);
134 if (hour == 12) 154 if (hour == 12)
135 hour = 0; 155 hour = 0;
136 if (reg & 0x20) 156 if (reg & 0x20)
@@ -141,15 +161,15 @@ static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg)
141static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour) 161static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour)
142{ 162{
143 if (rs5c->time24) 163 if (rs5c->time24)
144 return BIN2BCD(hour); 164 return bin2bcd(hour);
145 165
146 if (hour > 12) 166 if (hour > 12)
147 return 0x20 | BIN2BCD(hour - 12); 167 return 0x20 | bin2bcd(hour - 12);
148 if (hour == 12) 168 if (hour == 12)
149 return 0x20 | BIN2BCD(12); 169 return 0x20 | bin2bcd(12);
150 if (hour == 0) 170 if (hour == 0)
151 return BIN2BCD(12); 171 return bin2bcd(12);
152 return BIN2BCD(hour); 172 return bin2bcd(hour);
153} 173}
154 174
155static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm) 175static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
@@ -160,18 +180,18 @@ static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
160 if (status < 0) 180 if (status < 0)
161 return status; 181 return status;
162 182
163 tm->tm_sec = BCD2BIN(rs5c->regs[RS5C372_REG_SECS] & 0x7f); 183 tm->tm_sec = bcd2bin(rs5c->regs[RS5C372_REG_SECS] & 0x7f);
164 tm->tm_min = BCD2BIN(rs5c->regs[RS5C372_REG_MINS] & 0x7f); 184 tm->tm_min = bcd2bin(rs5c->regs[RS5C372_REG_MINS] & 0x7f);
165 tm->tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C372_REG_HOURS]); 185 tm->tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C372_REG_HOURS]);
166 186
167 tm->tm_wday = BCD2BIN(rs5c->regs[RS5C372_REG_WDAY] & 0x07); 187 tm->tm_wday = bcd2bin(rs5c->regs[RS5C372_REG_WDAY] & 0x07);
168 tm->tm_mday = BCD2BIN(rs5c->regs[RS5C372_REG_DAY] & 0x3f); 188 tm->tm_mday = bcd2bin(rs5c->regs[RS5C372_REG_DAY] & 0x3f);
169 189
170 /* tm->tm_mon is zero-based */ 190 /* tm->tm_mon is zero-based */
171 tm->tm_mon = BCD2BIN(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1; 191 tm->tm_mon = bcd2bin(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1;
172 192
173 /* year is 1900 + tm->tm_year */ 193 /* year is 1900 + tm->tm_year */
174 tm->tm_year = BCD2BIN(rs5c->regs[RS5C372_REG_YEAR]) + 100; 194 tm->tm_year = bcd2bin(rs5c->regs[RS5C372_REG_YEAR]) + 100;
175 195
176 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 196 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
177 "mday=%d, mon=%d, year=%d, wday=%d\n", 197 "mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -187,6 +207,7 @@ static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
187{ 207{
188 struct rs5c372 *rs5c = i2c_get_clientdata(client); 208 struct rs5c372 *rs5c = i2c_get_clientdata(client);
189 unsigned char buf[8]; 209 unsigned char buf[8];
210 int addr;
190 211
191 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d " 212 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
192 "mday=%d, mon=%d, year=%d, wday=%d\n", 213 "mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -194,16 +215,16 @@ static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
194 tm->tm_sec, tm->tm_min, tm->tm_hour, 215 tm->tm_sec, tm->tm_min, tm->tm_hour,
195 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 216 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
196 217
197 buf[0] = RS5C_ADDR(RS5C372_REG_SECS); 218 addr = RS5C_ADDR(RS5C372_REG_SECS);
198 buf[1] = BIN2BCD(tm->tm_sec); 219 buf[0] = bin2bcd(tm->tm_sec);
199 buf[2] = BIN2BCD(tm->tm_min); 220 buf[1] = bin2bcd(tm->tm_min);
200 buf[3] = rs5c_hr2reg(rs5c, tm->tm_hour); 221 buf[2] = rs5c_hr2reg(rs5c, tm->tm_hour);
201 buf[4] = BIN2BCD(tm->tm_wday); 222 buf[3] = bin2bcd(tm->tm_wday);
202 buf[5] = BIN2BCD(tm->tm_mday); 223 buf[4] = bin2bcd(tm->tm_mday);
203 buf[6] = BIN2BCD(tm->tm_mon + 1); 224 buf[5] = bin2bcd(tm->tm_mon + 1);
204 buf[7] = BIN2BCD(tm->tm_year - 100); 225 buf[6] = bin2bcd(tm->tm_year - 100);
205 226
206 if ((i2c_master_send(client, buf, 8)) != 8) { 227 if (i2c_smbus_write_i2c_block_data(client, addr, sizeof(buf), buf) < 0) {
207 dev_err(&client->dev, "%s: write error\n", __func__); 228 dev_err(&client->dev, "%s: write error\n", __func__);
208 return -EIO; 229 return -EIO;
209 } 230 }
@@ -266,16 +287,16 @@ rs5c_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
266{ 287{
267 struct i2c_client *client = to_i2c_client(dev); 288 struct i2c_client *client = to_i2c_client(dev);
268 struct rs5c372 *rs5c = i2c_get_clientdata(client); 289 struct rs5c372 *rs5c = i2c_get_clientdata(client);
269 unsigned char buf[2]; 290 unsigned char buf;
270 int status; 291 int status, addr;
271 292
272 buf[1] = rs5c->regs[RS5C_REG_CTRL1]; 293 buf = rs5c->regs[RS5C_REG_CTRL1];
273 switch (cmd) { 294 switch (cmd) {
274 case RTC_UIE_OFF: 295 case RTC_UIE_OFF:
275 case RTC_UIE_ON: 296 case RTC_UIE_ON:
276 /* some 327a modes use a different IRQ pin for 1Hz irqs */ 297 /* some 327a modes use a different IRQ pin for 1Hz irqs */
277 if (rs5c->type == rtc_rs5c372a 298 if (rs5c->type == rtc_rs5c372a
278 && (buf[1] & RS5C372A_CTRL1_SL1)) 299 && (buf & RS5C372A_CTRL1_SL1))
279 return -ENOIOCTLCMD; 300 return -ENOIOCTLCMD;
280 case RTC_AIE_OFF: 301 case RTC_AIE_OFF:
281 case RTC_AIE_ON: 302 case RTC_AIE_ON:
@@ -293,28 +314,30 @@ rs5c_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
293 if (status < 0) 314 if (status < 0)
294 return status; 315 return status;
295 316
296 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); 317 addr = RS5C_ADDR(RS5C_REG_CTRL1);
297 switch (cmd) { 318 switch (cmd) {
298 case RTC_AIE_OFF: /* alarm off */ 319 case RTC_AIE_OFF: /* alarm off */
299 buf[1] &= ~RS5C_CTRL1_AALE; 320 buf &= ~RS5C_CTRL1_AALE;
300 break; 321 break;
301 case RTC_AIE_ON: /* alarm on */ 322 case RTC_AIE_ON: /* alarm on */
302 buf[1] |= RS5C_CTRL1_AALE; 323 buf |= RS5C_CTRL1_AALE;
303 break; 324 break;
304 case RTC_UIE_OFF: /* update off */ 325 case RTC_UIE_OFF: /* update off */
305 buf[1] &= ~RS5C_CTRL1_CT_MASK; 326 buf &= ~RS5C_CTRL1_CT_MASK;
306 break; 327 break;
307 case RTC_UIE_ON: /* update on */ 328 case RTC_UIE_ON: /* update on */
308 buf[1] &= ~RS5C_CTRL1_CT_MASK; 329 buf &= ~RS5C_CTRL1_CT_MASK;
309 buf[1] |= RS5C_CTRL1_CT4; 330 buf |= RS5C_CTRL1_CT4;
310 break; 331 break;
311 } 332 }
312 if ((i2c_master_send(client, buf, 2)) != 2) { 333
334 if (i2c_smbus_write_byte_data(client, addr, buf) < 0) {
313 printk(KERN_WARNING "%s: can't update alarm\n", 335 printk(KERN_WARNING "%s: can't update alarm\n",
314 rs5c->rtc->name); 336 rs5c->rtc->name);
315 status = -EIO; 337 status = -EIO;
316 } else 338 } else
317 rs5c->regs[RS5C_REG_CTRL1] = buf[1]; 339 rs5c->regs[RS5C_REG_CTRL1] = buf;
340
318 return status; 341 return status;
319} 342}
320 343
@@ -344,7 +367,7 @@ static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t)
344 367
345 /* report alarm time */ 368 /* report alarm time */
346 t->time.tm_sec = 0; 369 t->time.tm_sec = 0;
347 t->time.tm_min = BCD2BIN(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f); 370 t->time.tm_min = bcd2bin(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f);
348 t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]); 371 t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]);
349 t->time.tm_mday = -1; 372 t->time.tm_mday = -1;
350 t->time.tm_mon = -1; 373 t->time.tm_mon = -1;
@@ -364,8 +387,8 @@ static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
364{ 387{
365 struct i2c_client *client = to_i2c_client(dev); 388 struct i2c_client *client = to_i2c_client(dev);
366 struct rs5c372 *rs5c = i2c_get_clientdata(client); 389 struct rs5c372 *rs5c = i2c_get_clientdata(client);
367 int status; 390 int status, addr, i;
368 unsigned char buf[4]; 391 unsigned char buf[3];
369 392
370 /* only handle up to 24 hours in the future, like RTC_ALM_SET */ 393 /* only handle up to 24 hours in the future, like RTC_ALM_SET */
371 if (t->time.tm_mday != -1 394 if (t->time.tm_mday != -1
@@ -380,33 +403,36 @@ static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
380 if (status < 0) 403 if (status < 0)
381 return status; 404 return status;
382 if (rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE) { 405 if (rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE) {
383 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); 406 addr = RS5C_ADDR(RS5C_REG_CTRL1);
384 buf[1] = rs5c->regs[RS5C_REG_CTRL1] & ~RS5C_CTRL1_AALE; 407 buf[0] = rs5c->regs[RS5C_REG_CTRL1] & ~RS5C_CTRL1_AALE;
385 if (i2c_master_send(client, buf, 2) != 2) { 408 if (i2c_smbus_write_byte_data(client, addr, buf[0]) < 0) {
386 pr_debug("%s: can't disable alarm\n", rs5c->rtc->name); 409 pr_debug("%s: can't disable alarm\n", rs5c->rtc->name);
387 return -EIO; 410 return -EIO;
388 } 411 }
389 rs5c->regs[RS5C_REG_CTRL1] = buf[1]; 412 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
390 } 413 }
391 414
392 /* set alarm */ 415 /* set alarm */
393 buf[0] = RS5C_ADDR(RS5C_REG_ALARM_A_MIN); 416 buf[0] = bin2bcd(t->time.tm_min);
394 buf[1] = BIN2BCD(t->time.tm_min); 417 buf[1] = rs5c_hr2reg(rs5c, t->time.tm_hour);
395 buf[2] = rs5c_hr2reg(rs5c, t->time.tm_hour); 418 buf[2] = 0x7f; /* any/all days */
396 buf[3] = 0x7f; /* any/all days */ 419
397 if ((i2c_master_send(client, buf, 4)) != 4) { 420 for (i = 0; i < sizeof(buf); i++) {
398 pr_debug("%s: can't set alarm time\n", rs5c->rtc->name); 421 addr = RS5C_ADDR(RS5C_REG_ALARM_A_MIN + i);
399 return -EIO; 422 if (i2c_smbus_write_byte_data(client, addr, buf[i]) < 0) {
423 pr_debug("%s: can't set alarm time\n", rs5c->rtc->name);
424 return -EIO;
425 }
400 } 426 }
401 427
402 /* ... and maybe enable its irq */ 428 /* ... and maybe enable its irq */
403 if (t->enabled) { 429 if (t->enabled) {
404 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); 430 addr = RS5C_ADDR(RS5C_REG_CTRL1);
405 buf[1] = rs5c->regs[RS5C_REG_CTRL1] | RS5C_CTRL1_AALE; 431 buf[0] = rs5c->regs[RS5C_REG_CTRL1] | RS5C_CTRL1_AALE;
406 if ((i2c_master_send(client, buf, 2)) != 2) 432 if (i2c_smbus_write_byte_data(client, addr, buf[0]) < 0)
407 printk(KERN_WARNING "%s: can't enable alarm\n", 433 printk(KERN_WARNING "%s: can't enable alarm\n",
408 rs5c->rtc->name); 434 rs5c->rtc->name);
409 rs5c->regs[RS5C_REG_CTRL1] = buf[1]; 435 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
410 } 436 }
411 437
412 return 0; 438 return 0;
@@ -503,18 +529,81 @@ static void rs5c_sysfs_unregister(struct device *dev)
503 529
504static struct i2c_driver rs5c372_driver; 530static struct i2c_driver rs5c372_driver;
505 531
532static int rs5c_oscillator_setup(struct rs5c372 *rs5c372)
533{
534 unsigned char buf[2];
535 int addr, i, ret = 0;
536
537 if (rs5c372->type == rtc_r2025sd) {
538 if (!(rs5c372->regs[RS5C_REG_CTRL2] & R2025_CTRL2_XST))
539 return ret;
540 rs5c372->regs[RS5C_REG_CTRL2] &= ~R2025_CTRL2_XST;
541 } else {
542 if (!(rs5c372->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_XSTP))
543 return ret;
544 rs5c372->regs[RS5C_REG_CTRL2] &= ~RS5C_CTRL2_XSTP;
545 }
546
547 addr = RS5C_ADDR(RS5C_REG_CTRL1);
548 buf[0] = rs5c372->regs[RS5C_REG_CTRL1];
549 buf[1] = rs5c372->regs[RS5C_REG_CTRL2];
550
551 /* use 24hr mode */
552 switch (rs5c372->type) {
553 case rtc_rs5c372a:
554 case rtc_rs5c372b:
555 buf[1] |= RS5C372_CTRL2_24;
556 rs5c372->time24 = 1;
557 break;
558 case rtc_r2025sd:
559 case rtc_rv5c386:
560 case rtc_rv5c387a:
561 buf[0] |= RV5C387_CTRL1_24;
562 rs5c372->time24 = 1;
563 break;
564 default:
565 /* impossible */
566 break;
567 }
568
569 for (i = 0; i < sizeof(buf); i++) {
570 addr = RS5C_ADDR(RS5C_REG_CTRL1 + i);
571 ret = i2c_smbus_write_byte_data(rs5c372->client, addr, buf[i]);
572 if (unlikely(ret < 0))
573 return ret;
574 }
575
576 rs5c372->regs[RS5C_REG_CTRL1] = buf[0];
577 rs5c372->regs[RS5C_REG_CTRL2] = buf[1];
578
579 return 0;
580}
581
506static int rs5c372_probe(struct i2c_client *client, 582static int rs5c372_probe(struct i2c_client *client,
507 const struct i2c_device_id *id) 583 const struct i2c_device_id *id)
508{ 584{
509 int err = 0; 585 int err = 0;
586 int smbus_mode = 0;
510 struct rs5c372 *rs5c372; 587 struct rs5c372 *rs5c372;
511 struct rtc_time tm; 588 struct rtc_time tm;
512 589
513 dev_dbg(&client->dev, "%s\n", __func__); 590 dev_dbg(&client->dev, "%s\n", __func__);
514 591
515 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 592 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
516 err = -ENODEV; 593 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK)) {
517 goto exit; 594 /*
595 * If we don't have any master mode adapter, try breaking
596 * it down in to the barest of capabilities.
597 */
598 if (i2c_check_functionality(client->adapter,
599 I2C_FUNC_SMBUS_BYTE_DATA |
600 I2C_FUNC_SMBUS_I2C_BLOCK))
601 smbus_mode = 1;
602 else {
603 /* Still no good, give up */
604 err = -ENODEV;
605 goto exit;
606 }
518 } 607 }
519 608
520 if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) { 609 if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) {
@@ -528,6 +617,7 @@ static int rs5c372_probe(struct i2c_client *client,
528 617
529 /* we read registers 0x0f then 0x00-0x0f; skip the first one */ 618 /* we read registers 0x0f then 0x00-0x0f; skip the first one */
530 rs5c372->regs = &rs5c372->buf[1]; 619 rs5c372->regs = &rs5c372->buf[1];
620 rs5c372->smbus = smbus_mode;
531 621
532 err = rs5c_get_regs(rs5c372); 622 err = rs5c_get_regs(rs5c372);
533 if (err < 0) 623 if (err < 0)
@@ -543,6 +633,7 @@ static int rs5c372_probe(struct i2c_client *client,
543 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C372_CTRL2_24) 633 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C372_CTRL2_24)
544 rs5c372->time24 = 1; 634 rs5c372->time24 = 1;
545 break; 635 break;
636 case rtc_r2025sd:
546 case rtc_rv5c386: 637 case rtc_rv5c386:
547 case rtc_rv5c387a: 638 case rtc_rv5c387a:
548 if (rs5c372->regs[RS5C_REG_CTRL1] & RV5C387_CTRL1_24) 639 if (rs5c372->regs[RS5C_REG_CTRL1] & RV5C387_CTRL1_24)
@@ -558,39 +649,14 @@ static int rs5c372_probe(struct i2c_client *client,
558 649
559 /* if the oscillator lost power and no other software (like 650 /* if the oscillator lost power and no other software (like
560 * the bootloader) set it up, do it here. 651 * the bootloader) set it up, do it here.
652 *
653 * The R2025S/D does this a little differently than the other
654 * parts, so we special case that..
561 */ 655 */
562 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_XSTP) { 656 err = rs5c_oscillator_setup(rs5c372);
563 unsigned char buf[3]; 657 if (unlikely(err < 0)) {
564 658 dev_err(&client->dev, "setup error\n");
565 rs5c372->regs[RS5C_REG_CTRL2] &= ~RS5C_CTRL2_XSTP; 659 goto exit_kfree;
566
567 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1);
568 buf[1] = rs5c372->regs[RS5C_REG_CTRL1];
569 buf[2] = rs5c372->regs[RS5C_REG_CTRL2];
570
571 /* use 24hr mode */
572 switch (rs5c372->type) {
573 case rtc_rs5c372a:
574 case rtc_rs5c372b:
575 buf[2] |= RS5C372_CTRL2_24;
576 rs5c372->time24 = 1;
577 break;
578 case rtc_rv5c386:
579 case rtc_rv5c387a:
580 buf[1] |= RV5C387_CTRL1_24;
581 rs5c372->time24 = 1;
582 break;
583 default:
584 /* impossible */
585 break;
586 }
587
588 if ((i2c_master_send(client, buf, 3)) != 3) {
589 dev_err(&client->dev, "setup error\n");
590 goto exit_kfree;
591 }
592 rs5c372->regs[RS5C_REG_CTRL1] = buf[1];
593 rs5c372->regs[RS5C_REG_CTRL2] = buf[2];
594 } 660 }
595 661
596 if (rs5c372_get_datetime(client, &tm) < 0) 662 if (rs5c372_get_datetime(client, &tm) < 0)
@@ -598,6 +664,7 @@ static int rs5c372_probe(struct i2c_client *client,
598 664
599 dev_info(&client->dev, "%s found, %s, driver version " DRV_VERSION "\n", 665 dev_info(&client->dev, "%s found, %s, driver version " DRV_VERSION "\n",
600 ({ char *s; switch (rs5c372->type) { 666 ({ char *s; switch (rs5c372->type) {
667 case rtc_r2025sd: s = "r2025sd"; break;
601 case rtc_rs5c372a: s = "rs5c372a"; break; 668 case rtc_rs5c372a: s = "rs5c372a"; break;
602 case rtc_rs5c372b: s = "rs5c372b"; break; 669 case rtc_rs5c372b: s = "rs5c372b"; break;
603 case rtc_rv5c386: s = "rv5c386"; break; 670 case rtc_rv5c386: s = "rv5c386"; break;
@@ -667,7 +734,8 @@ module_exit(rs5c372_exit);
667 734
668MODULE_AUTHOR( 735MODULE_AUTHOR(
669 "Pavel Mironchik <pmironchik@optifacio.net>, " 736 "Pavel Mironchik <pmironchik@optifacio.net>, "
670 "Alessandro Zummo <a.zummo@towertech.it>"); 737 "Alessandro Zummo <a.zummo@towertech.it>, "
738 "Paul Mundt <lethal@linux-sh.org>");
671MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver"); 739MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver");
672MODULE_LICENSE("GPL"); 740MODULE_LICENSE("GPL");
673MODULE_VERSION(DRV_VERSION); 741MODULE_VERSION(DRV_VERSION);
diff --git a/drivers/rtc/rtc-rx8581.c b/drivers/rtc/rtc-rx8581.c
new file mode 100644
index 000000000000..c9522f3bc21c
--- /dev/null
+++ b/drivers/rtc/rtc-rx8581.c
@@ -0,0 +1,281 @@
1/*
2 * An I2C driver for the Epson RX8581 RTC
3 *
4 * Author: Martyn Welch <martyn.welch@gefanuc.com>
5 * Copyright 2008 GE Fanuc Intelligent Platforms Embedded Systems, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
12 * Copyright 2005-06 Tower Technologies
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/bcd.h>
18#include <linux/rtc.h>
19#include <linux/log2.h>
20
21#define DRV_VERSION "0.1"
22
23#define RX8581_REG_SC 0x00 /* Second in BCD */
24#define RX8581_REG_MN 0x01 /* Minute in BCD */
25#define RX8581_REG_HR 0x02 /* Hour in BCD */
26#define RX8581_REG_DW 0x03 /* Day of Week */
27#define RX8581_REG_DM 0x04 /* Day of Month in BCD */
28#define RX8581_REG_MO 0x05 /* Month in BCD */
29#define RX8581_REG_YR 0x06 /* Year in BCD */
30#define RX8581_REG_RAM 0x07 /* RAM */
31#define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/
32#define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */
33#define RX8581_REG_ADM 0x0A
34#define RX8581_REG_ADW 0x0A
35#define RX8581_REG_TMR0 0x0B
36#define RX8581_REG_TMR1 0x0C
37#define RX8581_REG_EXT 0x0D /* Extension Register */
38#define RX8581_REG_FLAG 0x0E /* Flag Register */
39#define RX8581_REG_CTRL 0x0F /* Control Register */
40
41
42/* Flag Register bit definitions */
43#define RX8581_FLAG_UF 0x20 /* Update */
44#define RX8581_FLAG_TF 0x10 /* Timer */
45#define RX8581_FLAG_AF 0x08 /* Alarm */
46#define RX8581_FLAG_VLF 0x02 /* Voltage Low */
47
48/* Control Register bit definitions */
49#define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */
50#define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */
51#define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */
52#define RX8581_CTRL_STOP 0x02 /* STOP bit */
53#define RX8581_CTRL_RESET 0x01 /* RESET bit */
54
55static struct i2c_driver rx8581_driver;
56
57/*
58 * In the routines that deal directly with the rx8581 hardware, we use
59 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
60 */
61static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm)
62{
63 unsigned char date[7];
64 int data, err;
65
66 /* First we ensure that the "update flag" is not set, we read the
67 * time and date then re-read the "update flag". If the update flag
68 * has been set, we know that the time has changed during the read so
69 * we repeat the whole process again.
70 */
71 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
72 if (data < 0) {
73 dev_err(&client->dev, "Unable to read device flags\n");
74 return -EIO;
75 }
76
77 do {
78 /* If update flag set, clear it */
79 if (data & RX8581_FLAG_UF) {
80 err = i2c_smbus_write_byte_data(client,
81 RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
82 if (err != 0) {
83 dev_err(&client->dev, "Unable to write device "
84 "flags\n");
85 return -EIO;
86 }
87 }
88
89 /* Now read time and date */
90 err = i2c_smbus_read_i2c_block_data(client, RX8581_REG_SC,
91 7, date);
92 if (err < 0) {
93 dev_err(&client->dev, "Unable to read date\n");
94 return -EIO;
95 }
96
97 /* Check flag register */
98 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
99 if (data < 0) {
100 dev_err(&client->dev, "Unable to read device flags\n");
101 return -EIO;
102 }
103 } while (data & RX8581_FLAG_UF);
104
105 if (data & RX8581_FLAG_VLF)
106 dev_info(&client->dev,
107 "low voltage detected, date/time is not reliable.\n");
108
109 dev_dbg(&client->dev,
110 "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
111 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
112 __func__,
113 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
114
115 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
116 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
117 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
118 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
119 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
120 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
121 tm->tm_year = bcd2bin(date[RX8581_REG_YR]);
122 if (tm->tm_year < 70)
123 tm->tm_year += 100; /* assume we are in 1970...2069 */
124
125
126 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
127 "mday=%d, mon=%d, year=%d, wday=%d\n",
128 __func__,
129 tm->tm_sec, tm->tm_min, tm->tm_hour,
130 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
131
132 err = rtc_valid_tm(tm);
133 if (err < 0)
134 dev_err(&client->dev, "retrieved date/time is not valid.\n");
135
136 return err;
137}
138
139static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
140{
141 int data, err;
142 unsigned char buf[7];
143
144 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
145 "mday=%d, mon=%d, year=%d, wday=%d\n",
146 __func__,
147 tm->tm_sec, tm->tm_min, tm->tm_hour,
148 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
149
150 /* hours, minutes and seconds */
151 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
152 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
153 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
154
155 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
156
157 /* month, 1 - 12 */
158 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
159
160 /* year and century */
161 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
162 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
163
164 /* Stop the clock */
165 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
166 if (data < 0) {
167 dev_err(&client->dev, "Unable to read control register\n");
168 return -EIO;
169 }
170
171 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
172 (data | RX8581_CTRL_STOP));
173 if (err < 0) {
174 dev_err(&client->dev, "Unable to write control register\n");
175 return -EIO;
176 }
177
178 /* write register's data */
179 err = i2c_smbus_write_i2c_block_data(client, RX8581_REG_SC, 7, buf);
180 if (err < 0) {
181 dev_err(&client->dev, "Unable to write to date registers\n");
182 return -EIO;
183 }
184
185 /* Restart the clock */
186 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
187 if (data < 0) {
188 dev_err(&client->dev, "Unable to read control register\n");
189 return -EIO;
190 }
191
192 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
193 (data | ~(RX8581_CTRL_STOP)));
194 if (err != 0) {
195 dev_err(&client->dev, "Unable to write control register\n");
196 return -EIO;
197 }
198
199 return 0;
200}
201
202static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
203{
204 return rx8581_get_datetime(to_i2c_client(dev), tm);
205}
206
207static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
208{
209 return rx8581_set_datetime(to_i2c_client(dev), tm);
210}
211
212static const struct rtc_class_ops rx8581_rtc_ops = {
213 .read_time = rx8581_rtc_read_time,
214 .set_time = rx8581_rtc_set_time,
215};
216
217static int __devinit rx8581_probe(struct i2c_client *client,
218 const struct i2c_device_id *id)
219{
220 struct rtc_device *rtc;
221
222 dev_dbg(&client->dev, "%s\n", __func__);
223
224 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
225 return -ENODEV;
226
227 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
228
229 rtc = rtc_device_register(rx8581_driver.driver.name,
230 &client->dev, &rx8581_rtc_ops, THIS_MODULE);
231
232 if (IS_ERR(rtc))
233 return PTR_ERR(rtc);
234
235 i2c_set_clientdata(client, rtc);
236
237 return 0;
238}
239
240static int __devexit rx8581_remove(struct i2c_client *client)
241{
242 struct rtc_device *rtc = i2c_get_clientdata(client);
243
244 rtc_device_unregister(rtc);
245
246 return 0;
247}
248
249static const struct i2c_device_id rx8581_id[] = {
250 { "rx8581", 0 },
251 { }
252};
253MODULE_DEVICE_TABLE(i2c, rx8581_id);
254
255static struct i2c_driver rx8581_driver = {
256 .driver = {
257 .name = "rtc-rx8581",
258 .owner = THIS_MODULE,
259 },
260 .probe = rx8581_probe,
261 .remove = __devexit_p(rx8581_remove),
262 .id_table = rx8581_id,
263};
264
265static int __init rx8581_init(void)
266{
267 return i2c_add_driver(&rx8581_driver);
268}
269
270static void __exit rx8581_exit(void)
271{
272 i2c_del_driver(&rx8581_driver);
273}
274
275MODULE_AUTHOR("Martyn Welch <martyn.welch@gefanuc.com>");
276MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
277MODULE_LICENSE("GPL");
278MODULE_VERSION(DRV_VERSION);
279
280module_init(rx8581_init);
281module_exit(rx8581_exit);
diff --git a/drivers/rtc/rtc-s35390a.c b/drivers/rtc/rtc-s35390a.c
index a6fa1f2f2ca6..def4d396d0b0 100644
--- a/drivers/rtc/rtc-s35390a.c
+++ b/drivers/rtc/rtc-s35390a.c
@@ -104,12 +104,12 @@ static int s35390a_disable_test_mode(struct s35390a *s35390a)
104static char s35390a_hr2reg(struct s35390a *s35390a, int hour) 104static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
105{ 105{
106 if (s35390a->twentyfourhour) 106 if (s35390a->twentyfourhour)
107 return BIN2BCD(hour); 107 return bin2bcd(hour);
108 108
109 if (hour < 12) 109 if (hour < 12)
110 return BIN2BCD(hour); 110 return bin2bcd(hour);
111 111
112 return 0x40 | BIN2BCD(hour - 12); 112 return 0x40 | bin2bcd(hour - 12);
113} 113}
114 114
115static int s35390a_reg2hr(struct s35390a *s35390a, char reg) 115static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
@@ -117,9 +117,9 @@ static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
117 unsigned hour; 117 unsigned hour;
118 118
119 if (s35390a->twentyfourhour) 119 if (s35390a->twentyfourhour)
120 return BCD2BIN(reg & 0x3f); 120 return bcd2bin(reg & 0x3f);
121 121
122 hour = BCD2BIN(reg & 0x3f); 122 hour = bcd2bin(reg & 0x3f);
123 if (reg & 0x40) 123 if (reg & 0x40)
124 hour += 12; 124 hour += 12;
125 125
@@ -137,13 +137,13 @@ static int s35390a_set_datetime(struct i2c_client *client, struct rtc_time *tm)
137 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, 137 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
138 tm->tm_wday); 138 tm->tm_wday);
139 139
140 buf[S35390A_BYTE_YEAR] = BIN2BCD(tm->tm_year - 100); 140 buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
141 buf[S35390A_BYTE_MONTH] = BIN2BCD(tm->tm_mon + 1); 141 buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
142 buf[S35390A_BYTE_DAY] = BIN2BCD(tm->tm_mday); 142 buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
143 buf[S35390A_BYTE_WDAY] = BIN2BCD(tm->tm_wday); 143 buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
144 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour); 144 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
145 buf[S35390A_BYTE_MINS] = BIN2BCD(tm->tm_min); 145 buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
146 buf[S35390A_BYTE_SECS] = BIN2BCD(tm->tm_sec); 146 buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
147 147
148 /* This chip expects the bits of each byte to be in reverse order */ 148 /* This chip expects the bits of each byte to be in reverse order */
149 for (i = 0; i < 7; ++i) 149 for (i = 0; i < 7; ++i)
@@ -168,13 +168,13 @@ static int s35390a_get_datetime(struct i2c_client *client, struct rtc_time *tm)
168 for (i = 0; i < 7; ++i) 168 for (i = 0; i < 7; ++i)
169 buf[i] = bitrev8(buf[i]); 169 buf[i] = bitrev8(buf[i]);
170 170
171 tm->tm_sec = BCD2BIN(buf[S35390A_BYTE_SECS]); 171 tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
172 tm->tm_min = BCD2BIN(buf[S35390A_BYTE_MINS]); 172 tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
173 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]); 173 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
174 tm->tm_wday = BCD2BIN(buf[S35390A_BYTE_WDAY]); 174 tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
175 tm->tm_mday = BCD2BIN(buf[S35390A_BYTE_DAY]); 175 tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
176 tm->tm_mon = BCD2BIN(buf[S35390A_BYTE_MONTH]) - 1; 176 tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
177 tm->tm_year = BCD2BIN(buf[S35390A_BYTE_YEAR]) + 100; 177 tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
178 178
179 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, " 179 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
180 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec, 180 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
diff --git a/drivers/rtc/rtc-s3c.c b/drivers/rtc/rtc-s3c.c
index e7d19b6c265a..f59277bbedaa 100644
--- a/drivers/rtc/rtc-s3c.c
+++ b/drivers/rtc/rtc-s3c.c
@@ -134,12 +134,12 @@ static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
134 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday, 134 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
135 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec); 135 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
136 136
137 BCD_TO_BIN(rtc_tm->tm_sec); 137 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
138 BCD_TO_BIN(rtc_tm->tm_min); 138 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
139 BCD_TO_BIN(rtc_tm->tm_hour); 139 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
140 BCD_TO_BIN(rtc_tm->tm_mday); 140 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
141 BCD_TO_BIN(rtc_tm->tm_mon); 141 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
142 BCD_TO_BIN(rtc_tm->tm_year); 142 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
143 143
144 rtc_tm->tm_year += 100; 144 rtc_tm->tm_year += 100;
145 rtc_tm->tm_mon -= 1; 145 rtc_tm->tm_mon -= 1;
@@ -163,12 +163,12 @@ static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
163 return -EINVAL; 163 return -EINVAL;
164 } 164 }
165 165
166 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_RTCSEC); 166 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
167 writeb(BIN2BCD(tm->tm_min), base + S3C2410_RTCMIN); 167 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
168 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_RTCHOUR); 168 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
169 writeb(BIN2BCD(tm->tm_mday), base + S3C2410_RTCDATE); 169 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
170 writeb(BIN2BCD(tm->tm_mon + 1), base + S3C2410_RTCMON); 170 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
171 writeb(BIN2BCD(year), base + S3C2410_RTCYEAR); 171 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
172 172
173 return 0; 173 return 0;
174} 174}
@@ -199,34 +199,34 @@ static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
199 /* decode the alarm enable field */ 199 /* decode the alarm enable field */
200 200
201 if (alm_en & S3C2410_RTCALM_SECEN) 201 if (alm_en & S3C2410_RTCALM_SECEN)
202 BCD_TO_BIN(alm_tm->tm_sec); 202 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
203 else 203 else
204 alm_tm->tm_sec = 0xff; 204 alm_tm->tm_sec = 0xff;
205 205
206 if (alm_en & S3C2410_RTCALM_MINEN) 206 if (alm_en & S3C2410_RTCALM_MINEN)
207 BCD_TO_BIN(alm_tm->tm_min); 207 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
208 else 208 else
209 alm_tm->tm_min = 0xff; 209 alm_tm->tm_min = 0xff;
210 210
211 if (alm_en & S3C2410_RTCALM_HOUREN) 211 if (alm_en & S3C2410_RTCALM_HOUREN)
212 BCD_TO_BIN(alm_tm->tm_hour); 212 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
213 else 213 else
214 alm_tm->tm_hour = 0xff; 214 alm_tm->tm_hour = 0xff;
215 215
216 if (alm_en & S3C2410_RTCALM_DAYEN) 216 if (alm_en & S3C2410_RTCALM_DAYEN)
217 BCD_TO_BIN(alm_tm->tm_mday); 217 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
218 else 218 else
219 alm_tm->tm_mday = 0xff; 219 alm_tm->tm_mday = 0xff;
220 220
221 if (alm_en & S3C2410_RTCALM_MONEN) { 221 if (alm_en & S3C2410_RTCALM_MONEN) {
222 BCD_TO_BIN(alm_tm->tm_mon); 222 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
223 alm_tm->tm_mon -= 1; 223 alm_tm->tm_mon -= 1;
224 } else { 224 } else {
225 alm_tm->tm_mon = 0xff; 225 alm_tm->tm_mon = 0xff;
226 } 226 }
227 227
228 if (alm_en & S3C2410_RTCALM_YEAREN) 228 if (alm_en & S3C2410_RTCALM_YEAREN)
229 BCD_TO_BIN(alm_tm->tm_year); 229 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
230 else 230 else
231 alm_tm->tm_year = 0xffff; 231 alm_tm->tm_year = 0xffff;
232 232
@@ -250,17 +250,17 @@ static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
250 250
251 if (tm->tm_sec < 60 && tm->tm_sec >= 0) { 251 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
252 alrm_en |= S3C2410_RTCALM_SECEN; 252 alrm_en |= S3C2410_RTCALM_SECEN;
253 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_ALMSEC); 253 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
254 } 254 }
255 255
256 if (tm->tm_min < 60 && tm->tm_min >= 0) { 256 if (tm->tm_min < 60 && tm->tm_min >= 0) {
257 alrm_en |= S3C2410_RTCALM_MINEN; 257 alrm_en |= S3C2410_RTCALM_MINEN;
258 writeb(BIN2BCD(tm->tm_min), base + S3C2410_ALMMIN); 258 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
259 } 259 }
260 260
261 if (tm->tm_hour < 24 && tm->tm_hour >= 0) { 261 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
262 alrm_en |= S3C2410_RTCALM_HOUREN; 262 alrm_en |= S3C2410_RTCALM_HOUREN;
263 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_ALMHOUR); 263 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
264 } 264 }
265 265
266 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en); 266 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
@@ -455,6 +455,8 @@ static int __devinit s3c_rtc_probe(struct platform_device *pdev)
455 455
456 s3c_rtc_setfreq(&pdev->dev, 1); 456 s3c_rtc_setfreq(&pdev->dev, 1);
457 457
458 device_init_wakeup(&pdev->dev, 1);
459
458 /* register RTC and exit */ 460 /* register RTC and exit */
459 461
460 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops, 462 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
@@ -507,7 +509,7 @@ static int s3c_rtc_resume(struct platform_device *pdev)
507#define s3c_rtc_resume NULL 509#define s3c_rtc_resume NULL
508#endif 510#endif
509 511
510static struct platform_driver s3c2410_rtcdrv = { 512static struct platform_driver s3c2410_rtc_driver = {
511 .probe = s3c_rtc_probe, 513 .probe = s3c_rtc_probe,
512 .remove = __devexit_p(s3c_rtc_remove), 514 .remove = __devexit_p(s3c_rtc_remove),
513 .suspend = s3c_rtc_suspend, 515 .suspend = s3c_rtc_suspend,
@@ -523,12 +525,12 @@ static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics
523static int __init s3c_rtc_init(void) 525static int __init s3c_rtc_init(void)
524{ 526{
525 printk(banner); 527 printk(banner);
526 return platform_driver_register(&s3c2410_rtcdrv); 528 return platform_driver_register(&s3c2410_rtc_driver);
527} 529}
528 530
529static void __exit s3c_rtc_exit(void) 531static void __exit s3c_rtc_exit(void)
530{ 532{
531 platform_driver_unregister(&s3c2410_rtcdrv); 533 platform_driver_unregister(&s3c2410_rtc_driver);
532} 534}
533 535
534module_init(s3c_rtc_init); 536module_init(s3c_rtc_init);
diff --git a/drivers/rtc/rtc-sh.c b/drivers/rtc/rtc-sh.c
index 1f88e9e914ec..aaf9d6a337cc 100644
--- a/drivers/rtc/rtc-sh.c
+++ b/drivers/rtc/rtc-sh.c
@@ -257,12 +257,6 @@ static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
257 spin_unlock_irq(&rtc->lock); 257 spin_unlock_irq(&rtc->lock);
258} 258}
259 259
260static void sh_rtc_release(struct device *dev)
261{
262 sh_rtc_setpie(dev, 0);
263 sh_rtc_setaie(dev, 0);
264}
265
266static int sh_rtc_proc(struct device *dev, struct seq_file *seq) 260static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
267{ 261{
268 struct sh_rtc *rtc = dev_get_drvdata(dev); 262 struct sh_rtc *rtc = dev_get_drvdata(dev);
@@ -330,23 +324,23 @@ static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
330 324
331 sec128 = readb(rtc->regbase + R64CNT); 325 sec128 = readb(rtc->regbase + R64CNT);
332 326
333 tm->tm_sec = BCD2BIN(readb(rtc->regbase + RSECCNT)); 327 tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
334 tm->tm_min = BCD2BIN(readb(rtc->regbase + RMINCNT)); 328 tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
335 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT)); 329 tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
336 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT)); 330 tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
337 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT)); 331 tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
338 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1; 332 tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
339 333
340 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { 334 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
341 yr = readw(rtc->regbase + RYRCNT); 335 yr = readw(rtc->regbase + RYRCNT);
342 yr100 = BCD2BIN(yr >> 8); 336 yr100 = bcd2bin(yr >> 8);
343 yr &= 0xff; 337 yr &= 0xff;
344 } else { 338 } else {
345 yr = readb(rtc->regbase + RYRCNT); 339 yr = readb(rtc->regbase + RYRCNT);
346 yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20); 340 yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
347 } 341 }
348 342
349 tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900; 343 tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
350 344
351 sec2 = readb(rtc->regbase + R64CNT); 345 sec2 = readb(rtc->regbase + R64CNT);
352 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF; 346 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
@@ -388,20 +382,20 @@ static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
388 tmp &= ~RCR2_START; 382 tmp &= ~RCR2_START;
389 writeb(tmp, rtc->regbase + RCR2); 383 writeb(tmp, rtc->regbase + RCR2);
390 384
391 writeb(BIN2BCD(tm->tm_sec), rtc->regbase + RSECCNT); 385 writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
392 writeb(BIN2BCD(tm->tm_min), rtc->regbase + RMINCNT); 386 writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
393 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT); 387 writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
394 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT); 388 writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
395 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT); 389 writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
396 writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT); 390 writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
397 391
398 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { 392 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
399 year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) | 393 year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
400 BIN2BCD(tm->tm_year % 100); 394 bin2bcd(tm->tm_year % 100);
401 writew(year, rtc->regbase + RYRCNT); 395 writew(year, rtc->regbase + RYRCNT);
402 } else { 396 } else {
403 year = tm->tm_year % 100; 397 year = tm->tm_year % 100;
404 writeb(BIN2BCD(year), rtc->regbase + RYRCNT); 398 writeb(bin2bcd(year), rtc->regbase + RYRCNT);
405 } 399 }
406 400
407 /* Start RTC */ 401 /* Start RTC */
@@ -423,7 +417,7 @@ static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
423 byte = readb(rtc->regbase + reg_off); 417 byte = readb(rtc->regbase + reg_off);
424 if (byte & AR_ENB) { 418 if (byte & AR_ENB) {
425 byte &= ~AR_ENB; /* strip the enable bit */ 419 byte &= ~AR_ENB; /* strip the enable bit */
426 value = BCD2BIN(byte); 420 value = bcd2bin(byte);
427 } 421 }
428 422
429 return value; 423 return value;
@@ -461,7 +455,7 @@ static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
461 if (value < 0) 455 if (value < 0)
462 writeb(0, rtc->regbase + reg_off); 456 writeb(0, rtc->regbase + reg_off);
463 else 457 else
464 writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off); 458 writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
465} 459}
466 460
467static int sh_rtc_check_alarm(struct rtc_time *tm) 461static int sh_rtc_check_alarm(struct rtc_time *tm)
@@ -559,7 +553,6 @@ static int sh_rtc_irq_set_freq(struct device *dev, int freq)
559} 553}
560 554
561static struct rtc_class_ops sh_rtc_ops = { 555static struct rtc_class_ops sh_rtc_ops = {
562 .release = sh_rtc_release,
563 .ioctl = sh_rtc_ioctl, 556 .ioctl = sh_rtc_ioctl,
564 .read_time = sh_rtc_read_time, 557 .read_time = sh_rtc_read_time,
565 .set_time = sh_rtc_set_time, 558 .set_time = sh_rtc_set_time,
@@ -575,7 +568,7 @@ static int __devinit sh_rtc_probe(struct platform_device *pdev)
575 struct sh_rtc *rtc; 568 struct sh_rtc *rtc;
576 struct resource *res; 569 struct resource *res;
577 unsigned int tmp; 570 unsigned int tmp;
578 int ret = -ENOENT; 571 int ret;
579 572
580 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL); 573 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
581 if (unlikely(!rtc)) 574 if (unlikely(!rtc))
@@ -584,26 +577,33 @@ static int __devinit sh_rtc_probe(struct platform_device *pdev)
584 spin_lock_init(&rtc->lock); 577 spin_lock_init(&rtc->lock);
585 578
586 /* get periodic/carry/alarm irqs */ 579 /* get periodic/carry/alarm irqs */
587 rtc->periodic_irq = platform_get_irq(pdev, 0); 580 ret = platform_get_irq(pdev, 0);
588 if (unlikely(rtc->periodic_irq < 0)) { 581 if (unlikely(ret < 0)) {
582 ret = -ENOENT;
589 dev_err(&pdev->dev, "No IRQ for period\n"); 583 dev_err(&pdev->dev, "No IRQ for period\n");
590 goto err_badres; 584 goto err_badres;
591 } 585 }
586 rtc->periodic_irq = ret;
592 587
593 rtc->carry_irq = platform_get_irq(pdev, 1); 588 ret = platform_get_irq(pdev, 1);
594 if (unlikely(rtc->carry_irq < 0)) { 589 if (unlikely(ret < 0)) {
590 ret = -ENOENT;
595 dev_err(&pdev->dev, "No IRQ for carry\n"); 591 dev_err(&pdev->dev, "No IRQ for carry\n");
596 goto err_badres; 592 goto err_badres;
597 } 593 }
594 rtc->carry_irq = ret;
598 595
599 rtc->alarm_irq = platform_get_irq(pdev, 2); 596 ret = platform_get_irq(pdev, 2);
600 if (unlikely(rtc->alarm_irq < 0)) { 597 if (unlikely(ret < 0)) {
598 ret = -ENOENT;
601 dev_err(&pdev->dev, "No IRQ for alarm\n"); 599 dev_err(&pdev->dev, "No IRQ for alarm\n");
602 goto err_badres; 600 goto err_badres;
603 } 601 }
602 rtc->alarm_irq = ret;
604 603
605 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 604 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
606 if (unlikely(res == NULL)) { 605 if (unlikely(res == NULL)) {
606 ret = -ENOENT;
607 dev_err(&pdev->dev, "No IO resource\n"); 607 dev_err(&pdev->dev, "No IO resource\n");
608 goto err_badres; 608 goto err_badres;
609 } 609 }
diff --git a/drivers/rtc/rtc-starfire.c b/drivers/rtc/rtc-starfire.c
new file mode 100644
index 000000000000..7ccb0dd700af
--- /dev/null
+++ b/drivers/rtc/rtc-starfire.c
@@ -0,0 +1,120 @@
1/* rtc-starfire.c: Starfire platform RTC driver.
2 *
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/time.h>
10#include <linux/rtc.h>
11#include <linux/platform_device.h>
12
13#include <asm/oplib.h>
14
15MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
16MODULE_DESCRIPTION("Starfire RTC driver");
17MODULE_LICENSE("GPL");
18
19struct starfire_rtc {
20 struct rtc_device *rtc;
21 spinlock_t lock;
22};
23
24static u32 starfire_get_time(void)
25{
26 static char obp_gettod[32];
27 static u32 unix_tod;
28
29 sprintf(obp_gettod, "h# %08x unix-gettod",
30 (unsigned int) (long) &unix_tod);
31 prom_feval(obp_gettod);
32
33 return unix_tod;
34}
35
36static int starfire_read_time(struct device *dev, struct rtc_time *tm)
37{
38 struct starfire_rtc *p = dev_get_drvdata(dev);
39 unsigned long flags, secs;
40
41 spin_lock_irqsave(&p->lock, flags);
42 secs = starfire_get_time();
43 spin_unlock_irqrestore(&p->lock, flags);
44
45 rtc_time_to_tm(secs, tm);
46
47 return 0;
48}
49
50static int starfire_set_time(struct device *dev, struct rtc_time *tm)
51{
52 unsigned long secs;
53 int err;
54
55 err = rtc_tm_to_time(tm, &secs);
56 if (err)
57 return err;
58
59 /* Do nothing, time is set using the service processor
60 * console on this platform.
61 */
62 return 0;
63}
64
65static const struct rtc_class_ops starfire_rtc_ops = {
66 .read_time = starfire_read_time,
67 .set_time = starfire_set_time,
68};
69
70static int __devinit starfire_rtc_probe(struct platform_device *pdev)
71{
72 struct starfire_rtc *p = kzalloc(sizeof(*p), GFP_KERNEL);
73
74 if (!p)
75 return -ENOMEM;
76
77 spin_lock_init(&p->lock);
78
79 p->rtc = rtc_device_register("starfire", &pdev->dev,
80 &starfire_rtc_ops, THIS_MODULE);
81 if (IS_ERR(p->rtc)) {
82 int err = PTR_ERR(p->rtc);
83 kfree(p);
84 return err;
85 }
86 platform_set_drvdata(pdev, p);
87 return 0;
88}
89
90static int __devexit starfire_rtc_remove(struct platform_device *pdev)
91{
92 struct starfire_rtc *p = platform_get_drvdata(pdev);
93
94 rtc_device_unregister(p->rtc);
95 kfree(p);
96
97 return 0;
98}
99
100static struct platform_driver starfire_rtc_driver = {
101 .driver = {
102 .name = "rtc-starfire",
103 .owner = THIS_MODULE,
104 },
105 .probe = starfire_rtc_probe,
106 .remove = __devexit_p(starfire_rtc_remove),
107};
108
109static int __init starfire_rtc_init(void)
110{
111 return platform_driver_register(&starfire_rtc_driver);
112}
113
114static void __exit starfire_rtc_exit(void)
115{
116 platform_driver_unregister(&starfire_rtc_driver);
117}
118
119module_init(starfire_rtc_init);
120module_exit(starfire_rtc_exit);
diff --git a/drivers/rtc/rtc-stk17ta8.c b/drivers/rtc/rtc-stk17ta8.c
index 31d3c8c28588..f4cd46e15af9 100644
--- a/drivers/rtc/rtc-stk17ta8.c
+++ b/drivers/rtc/rtc-stk17ta8.c
@@ -82,14 +82,14 @@ static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
82 flags = readb(pdata->ioaddr + RTC_FLAGS); 82 flags = readb(pdata->ioaddr + RTC_FLAGS);
83 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS); 83 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
84 84
85 writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR); 85 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
86 writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH); 86 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
87 writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY); 87 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
88 writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE); 88 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
89 writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS); 89 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
90 writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES); 90 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
91 writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS); 91 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
92 writeb(BIN2BCD((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY); 92 writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
93 93
94 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS); 94 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
95 return 0; 95 return 0;
@@ -120,14 +120,14 @@ static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
120 year = readb(ioaddr + RTC_YEAR); 120 year = readb(ioaddr + RTC_YEAR);
121 century = readb(ioaddr + RTC_CENTURY); 121 century = readb(ioaddr + RTC_CENTURY);
122 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS); 122 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
123 tm->tm_sec = BCD2BIN(second); 123 tm->tm_sec = bcd2bin(second);
124 tm->tm_min = BCD2BIN(minute); 124 tm->tm_min = bcd2bin(minute);
125 tm->tm_hour = BCD2BIN(hour); 125 tm->tm_hour = bcd2bin(hour);
126 tm->tm_mday = BCD2BIN(day); 126 tm->tm_mday = bcd2bin(day);
127 tm->tm_wday = BCD2BIN(week); 127 tm->tm_wday = bcd2bin(week);
128 tm->tm_mon = BCD2BIN(month) - 1; 128 tm->tm_mon = bcd2bin(month) - 1;
129 /* year is 1900 + tm->tm_year */ 129 /* year is 1900 + tm->tm_year */
130 tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900; 130 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
131 131
132 if (rtc_valid_tm(tm) < 0) { 132 if (rtc_valid_tm(tm) < 0) {
133 dev_err(dev, "retrieved date/time is not valid.\n"); 133 dev_err(dev, "retrieved date/time is not valid.\n");
@@ -148,16 +148,16 @@ static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
148 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS); 148 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
149 149
150 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 150 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
151 0x80 : BIN2BCD(pdata->alrm_mday), 151 0x80 : bin2bcd(pdata->alrm_mday),
152 ioaddr + RTC_DATE_ALARM); 152 ioaddr + RTC_DATE_ALARM);
153 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? 153 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
154 0x80 : BIN2BCD(pdata->alrm_hour), 154 0x80 : bin2bcd(pdata->alrm_hour),
155 ioaddr + RTC_HOURS_ALARM); 155 ioaddr + RTC_HOURS_ALARM);
156 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? 156 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
157 0x80 : BIN2BCD(pdata->alrm_min), 157 0x80 : bin2bcd(pdata->alrm_min),
158 ioaddr + RTC_MINUTES_ALARM); 158 ioaddr + RTC_MINUTES_ALARM);
159 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? 159 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
160 0x80 : BIN2BCD(pdata->alrm_sec), 160 0x80 : bin2bcd(pdata->alrm_sec),
161 ioaddr + RTC_SECONDS_ALARM); 161 ioaddr + RTC_SECONDS_ALARM);
162 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS); 162 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
163 readb(ioaddr + RTC_FLAGS); /* clear interrupts */ 163 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
@@ -215,17 +215,6 @@ static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
215 return IRQ_HANDLED; 215 return IRQ_HANDLED;
216} 216}
217 217
218static void stk17ta8_rtc_release(struct device *dev)
219{
220 struct platform_device *pdev = to_platform_device(dev);
221 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
222
223 if (pdata->irq >= 0) {
224 pdata->irqen = 0;
225 stk17ta8_rtc_update_alarm(pdata);
226 }
227}
228
229static int stk17ta8_rtc_ioctl(struct device *dev, unsigned int cmd, 218static int stk17ta8_rtc_ioctl(struct device *dev, unsigned int cmd,
230 unsigned long arg) 219 unsigned long arg)
231{ 220{
@@ -254,7 +243,6 @@ static const struct rtc_class_ops stk17ta8_rtc_ops = {
254 .set_time = stk17ta8_rtc_set_time, 243 .set_time = stk17ta8_rtc_set_time,
255 .read_alarm = stk17ta8_rtc_read_alarm, 244 .read_alarm = stk17ta8_rtc_read_alarm,
256 .set_alarm = stk17ta8_rtc_set_alarm, 245 .set_alarm = stk17ta8_rtc_set_alarm,
257 .release = stk17ta8_rtc_release,
258 .ioctl = stk17ta8_rtc_ioctl, 246 .ioctl = stk17ta8_rtc_ioctl,
259}; 247};
260 248
@@ -292,7 +280,6 @@ static struct bin_attribute stk17ta8_nvram_attr = {
292 .attr = { 280 .attr = {
293 .name = "nvram", 281 .name = "nvram",
294 .mode = S_IRUGO | S_IWUSR, 282 .mode = S_IRUGO | S_IWUSR,
295 .owner = THIS_MODULE,
296 }, 283 },
297 .size = RTC_OFFSET, 284 .size = RTC_OFFSET,
298 .read = stk17ta8_nvram_read, 285 .read = stk17ta8_nvram_read,
diff --git a/drivers/rtc/rtc-sun4v.c b/drivers/rtc/rtc-sun4v.c
new file mode 100644
index 000000000000..2012ccbb4a53
--- /dev/null
+++ b/drivers/rtc/rtc-sun4v.c
@@ -0,0 +1,153 @@
1/* rtc-sun4c.c: Hypervisor based RTC for SUN4V systems.
2 *
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/delay.h>
9#include <linux/init.h>
10#include <linux/time.h>
11#include <linux/rtc.h>
12#include <linux/platform_device.h>
13
14#include <asm/hypervisor.h>
15
16MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
17MODULE_DESCRIPTION("SUN4V RTC driver");
18MODULE_LICENSE("GPL");
19
20struct sun4v_rtc {
21 struct rtc_device *rtc;
22 spinlock_t lock;
23};
24
25static unsigned long hypervisor_get_time(void)
26{
27 unsigned long ret, time;
28 int retries = 10000;
29
30retry:
31 ret = sun4v_tod_get(&time);
32 if (ret == HV_EOK)
33 return time;
34 if (ret == HV_EWOULDBLOCK) {
35 if (--retries > 0) {
36 udelay(100);
37 goto retry;
38 }
39 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
40 return 0;
41 }
42 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
43 return 0;
44}
45
46static int sun4v_read_time(struct device *dev, struct rtc_time *tm)
47{
48 struct sun4v_rtc *p = dev_get_drvdata(dev);
49 unsigned long flags, secs;
50
51 spin_lock_irqsave(&p->lock, flags);
52 secs = hypervisor_get_time();
53 spin_unlock_irqrestore(&p->lock, flags);
54
55 rtc_time_to_tm(secs, tm);
56
57 return 0;
58}
59
60static int hypervisor_set_time(unsigned long secs)
61{
62 unsigned long ret;
63 int retries = 10000;
64
65retry:
66 ret = sun4v_tod_set(secs);
67 if (ret == HV_EOK)
68 return 0;
69 if (ret == HV_EWOULDBLOCK) {
70 if (--retries > 0) {
71 udelay(100);
72 goto retry;
73 }
74 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
75 return -EAGAIN;
76 }
77 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
78 return -EOPNOTSUPP;
79}
80
81static int sun4v_set_time(struct device *dev, struct rtc_time *tm)
82{
83 struct sun4v_rtc *p = dev_get_drvdata(dev);
84 unsigned long flags, secs;
85 int err;
86
87 err = rtc_tm_to_time(tm, &secs);
88 if (err)
89 return err;
90
91 spin_lock_irqsave(&p->lock, flags);
92 err = hypervisor_set_time(secs);
93 spin_unlock_irqrestore(&p->lock, flags);
94
95 return err;
96}
97
98static const struct rtc_class_ops sun4v_rtc_ops = {
99 .read_time = sun4v_read_time,
100 .set_time = sun4v_set_time,
101};
102
103static int __devinit sun4v_rtc_probe(struct platform_device *pdev)
104{
105 struct sun4v_rtc *p = kzalloc(sizeof(*p), GFP_KERNEL);
106
107 if (!p)
108 return -ENOMEM;
109
110 spin_lock_init(&p->lock);
111
112 p->rtc = rtc_device_register("sun4v", &pdev->dev,
113 &sun4v_rtc_ops, THIS_MODULE);
114 if (IS_ERR(p->rtc)) {
115 int err = PTR_ERR(p->rtc);
116 kfree(p);
117 return err;
118 }
119 platform_set_drvdata(pdev, p);
120 return 0;
121}
122
123static int __devexit sun4v_rtc_remove(struct platform_device *pdev)
124{
125 struct sun4v_rtc *p = platform_get_drvdata(pdev);
126
127 rtc_device_unregister(p->rtc);
128 kfree(p);
129
130 return 0;
131}
132
133static struct platform_driver sun4v_rtc_driver = {
134 .driver = {
135 .name = "rtc-sun4v",
136 .owner = THIS_MODULE,
137 },
138 .probe = sun4v_rtc_probe,
139 .remove = __devexit_p(sun4v_rtc_remove),
140};
141
142static int __init sun4v_rtc_init(void)
143{
144 return platform_driver_register(&sun4v_rtc_driver);
145}
146
147static void __exit sun4v_rtc_exit(void)
148{
149 platform_driver_unregister(&sun4v_rtc_driver);
150}
151
152module_init(sun4v_rtc_init);
153module_exit(sun4v_rtc_exit);
diff --git a/drivers/rtc/rtc-twl4030.c b/drivers/rtc/rtc-twl4030.c
new file mode 100644
index 000000000000..abe87a4d2665
--- /dev/null
+++ b/drivers/rtc/rtc-twl4030.c
@@ -0,0 +1,564 @@
1/*
2 * rtc-twl4030.c -- TWL4030 Real Time Clock interface
3 *
4 * Copyright (C) 2007 MontaVista Software, Inc
5 * Author: Alexandre Rusev <source@mvista.com>
6 *
7 * Based on original TI driver twl4030-rtc.c
8 * Copyright (C) 2006 Texas Instruments, Inc.
9 *
10 * Based on rtc-omap.c
11 * Copyright (C) 2003 MontaVista Software, Inc.
12 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
13 * Copyright (C) 2006 David Brownell
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/rtc.h>
26#include <linux/bcd.h>
27#include <linux/platform_device.h>
28#include <linux/interrupt.h>
29
30#include <linux/i2c/twl4030.h>
31
32
33/*
34 * RTC block register offsets (use TWL_MODULE_RTC)
35 */
36#define REG_SECONDS_REG 0x00
37#define REG_MINUTES_REG 0x01
38#define REG_HOURS_REG 0x02
39#define REG_DAYS_REG 0x03
40#define REG_MONTHS_REG 0x04
41#define REG_YEARS_REG 0x05
42#define REG_WEEKS_REG 0x06
43
44#define REG_ALARM_SECONDS_REG 0x07
45#define REG_ALARM_MINUTES_REG 0x08
46#define REG_ALARM_HOURS_REG 0x09
47#define REG_ALARM_DAYS_REG 0x0A
48#define REG_ALARM_MONTHS_REG 0x0B
49#define REG_ALARM_YEARS_REG 0x0C
50
51#define REG_RTC_CTRL_REG 0x0D
52#define REG_RTC_STATUS_REG 0x0E
53#define REG_RTC_INTERRUPTS_REG 0x0F
54
55#define REG_RTC_COMP_LSB_REG 0x10
56#define REG_RTC_COMP_MSB_REG 0x11
57
58/* RTC_CTRL_REG bitfields */
59#define BIT_RTC_CTRL_REG_STOP_RTC_M 0x01
60#define BIT_RTC_CTRL_REG_ROUND_30S_M 0x02
61#define BIT_RTC_CTRL_REG_AUTO_COMP_M 0x04
62#define BIT_RTC_CTRL_REG_MODE_12_24_M 0x08
63#define BIT_RTC_CTRL_REG_TEST_MODE_M 0x10
64#define BIT_RTC_CTRL_REG_SET_32_COUNTER_M 0x20
65#define BIT_RTC_CTRL_REG_GET_TIME_M 0x40
66
67/* RTC_STATUS_REG bitfields */
68#define BIT_RTC_STATUS_REG_RUN_M 0x02
69#define BIT_RTC_STATUS_REG_1S_EVENT_M 0x04
70#define BIT_RTC_STATUS_REG_1M_EVENT_M 0x08
71#define BIT_RTC_STATUS_REG_1H_EVENT_M 0x10
72#define BIT_RTC_STATUS_REG_1D_EVENT_M 0x20
73#define BIT_RTC_STATUS_REG_ALARM_M 0x40
74#define BIT_RTC_STATUS_REG_POWER_UP_M 0x80
75
76/* RTC_INTERRUPTS_REG bitfields */
77#define BIT_RTC_INTERRUPTS_REG_EVERY_M 0x03
78#define BIT_RTC_INTERRUPTS_REG_IT_TIMER_M 0x04
79#define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M 0x08
80
81
82/* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
83#define ALL_TIME_REGS 6
84
85/*----------------------------------------------------------------------*/
86
87/*
88 * Supports 1 byte read from TWL4030 RTC register.
89 */
90static int twl4030_rtc_read_u8(u8 *data, u8 reg)
91{
92 int ret;
93
94 ret = twl4030_i2c_read_u8(TWL4030_MODULE_RTC, data, reg);
95 if (ret < 0)
96 pr_err("twl4030_rtc: Could not read TWL4030"
97 "register %X - error %d\n", reg, ret);
98 return ret;
99}
100
101/*
102 * Supports 1 byte write to TWL4030 RTC registers.
103 */
104static int twl4030_rtc_write_u8(u8 data, u8 reg)
105{
106 int ret;
107
108 ret = twl4030_i2c_write_u8(TWL4030_MODULE_RTC, data, reg);
109 if (ret < 0)
110 pr_err("twl4030_rtc: Could not write TWL4030"
111 "register %X - error %d\n", reg, ret);
112 return ret;
113}
114
115/*
116 * Cache the value for timer/alarm interrupts register; this is
117 * only changed by callers holding rtc ops lock (or resume).
118 */
119static unsigned char rtc_irq_bits;
120
121/*
122 * Enable timer and/or alarm interrupts.
123 */
124static int set_rtc_irq_bit(unsigned char bit)
125{
126 unsigned char val;
127 int ret;
128
129 val = rtc_irq_bits | bit;
130 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
131 if (ret == 0)
132 rtc_irq_bits = val;
133
134 return ret;
135}
136
137/*
138 * Disable timer and/or alarm interrupts.
139 */
140static int mask_rtc_irq_bit(unsigned char bit)
141{
142 unsigned char val;
143 int ret;
144
145 val = rtc_irq_bits & ~bit;
146 ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
147 if (ret == 0)
148 rtc_irq_bits = val;
149
150 return ret;
151}
152
153static inline int twl4030_rtc_alarm_irq_set_state(int enabled)
154{
155 int ret;
156
157 if (enabled)
158 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
159 else
160 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
161
162 return ret;
163}
164
165static inline int twl4030_rtc_irq_set_state(int enabled)
166{
167 int ret;
168
169 if (enabled)
170 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
171 else
172 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
173
174 return ret;
175}
176
177/*
178 * Gets current TWL4030 RTC time and date parameters.
179 *
180 * The RTC's time/alarm representation is not what gmtime(3) requires
181 * Linux to use:
182 *
183 * - Months are 1..12 vs Linux 0-11
184 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
185 */
186static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
187{
188 unsigned char rtc_data[ALL_TIME_REGS + 1];
189 int ret;
190 u8 save_control;
191
192 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
193 if (ret < 0)
194 return ret;
195
196 save_control |= BIT_RTC_CTRL_REG_GET_TIME_M;
197
198 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
199 if (ret < 0)
200 return ret;
201
202 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
203 REG_SECONDS_REG, ALL_TIME_REGS);
204
205 if (ret < 0) {
206 dev_err(dev, "rtc_read_time error %d\n", ret);
207 return ret;
208 }
209
210 tm->tm_sec = bcd2bin(rtc_data[0]);
211 tm->tm_min = bcd2bin(rtc_data[1]);
212 tm->tm_hour = bcd2bin(rtc_data[2]);
213 tm->tm_mday = bcd2bin(rtc_data[3]);
214 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
215 tm->tm_year = bcd2bin(rtc_data[5]) + 100;
216
217 return ret;
218}
219
220static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
221{
222 unsigned char save_control;
223 unsigned char rtc_data[ALL_TIME_REGS + 1];
224 int ret;
225
226 rtc_data[1] = bin2bcd(tm->tm_sec);
227 rtc_data[2] = bin2bcd(tm->tm_min);
228 rtc_data[3] = bin2bcd(tm->tm_hour);
229 rtc_data[4] = bin2bcd(tm->tm_mday);
230 rtc_data[5] = bin2bcd(tm->tm_mon + 1);
231 rtc_data[6] = bin2bcd(tm->tm_year - 100);
232
233 /* Stop RTC while updating the TC registers */
234 ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
235 if (ret < 0)
236 goto out;
237
238 save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
239 twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
240 if (ret < 0)
241 goto out;
242
243 /* update all the time registers in one shot */
244 ret = twl4030_i2c_write(TWL4030_MODULE_RTC, rtc_data,
245 REG_SECONDS_REG, ALL_TIME_REGS);
246 if (ret < 0) {
247 dev_err(dev, "rtc_set_time error %d\n", ret);
248 goto out;
249 }
250
251 /* Start back RTC */
252 save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
253 ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
254
255out:
256 return ret;
257}
258
259/*
260 * Gets current TWL4030 RTC alarm time.
261 */
262static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
263{
264 unsigned char rtc_data[ALL_TIME_REGS + 1];
265 int ret;
266
267 ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
268 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
269 if (ret < 0) {
270 dev_err(dev, "rtc_read_alarm error %d\n", ret);
271 return ret;
272 }
273
274 /* some of these fields may be wildcard/"match all" */
275 alm->time.tm_sec = bcd2bin(rtc_data[0]);
276 alm->time.tm_min = bcd2bin(rtc_data[1]);
277 alm->time.tm_hour = bcd2bin(rtc_data[2]);
278 alm->time.tm_mday = bcd2bin(rtc_data[3]);
279 alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1;
280 alm->time.tm_year = bcd2bin(rtc_data[5]) + 100;
281
282 /* report cached alarm enable state */
283 if (rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M)
284 alm->enabled = 1;
285
286 return ret;
287}
288
289static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
290{
291 unsigned char alarm_data[ALL_TIME_REGS + 1];
292 int ret;
293
294 ret = twl4030_rtc_alarm_irq_set_state(0);
295 if (ret)
296 goto out;
297
298 alarm_data[1] = bin2bcd(alm->time.tm_sec);
299 alarm_data[2] = bin2bcd(alm->time.tm_min);
300 alarm_data[3] = bin2bcd(alm->time.tm_hour);
301 alarm_data[4] = bin2bcd(alm->time.tm_mday);
302 alarm_data[5] = bin2bcd(alm->time.tm_mon + 1);
303 alarm_data[6] = bin2bcd(alm->time.tm_year - 100);
304
305 /* update all the alarm registers in one shot */
306 ret = twl4030_i2c_write(TWL4030_MODULE_RTC, alarm_data,
307 REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
308 if (ret) {
309 dev_err(dev, "rtc_set_alarm error %d\n", ret);
310 goto out;
311 }
312
313 if (alm->enabled)
314 ret = twl4030_rtc_alarm_irq_set_state(1);
315out:
316 return ret;
317}
318
319#ifdef CONFIG_RTC_INTF_DEV
320
321static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
322 unsigned long arg)
323{
324 switch (cmd) {
325 case RTC_AIE_OFF:
326 return twl4030_rtc_alarm_irq_set_state(0);
327 case RTC_AIE_ON:
328 return twl4030_rtc_alarm_irq_set_state(1);
329 case RTC_UIE_OFF:
330 return twl4030_rtc_irq_set_state(0);
331 case RTC_UIE_ON:
332 return twl4030_rtc_irq_set_state(1);
333
334 default:
335 return -ENOIOCTLCMD;
336 }
337}
338
339#else
340#define omap_rtc_ioctl NULL
341#endif
342
343static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
344{
345 unsigned long events = 0;
346 int ret = IRQ_NONE;
347 int res;
348 u8 rd_reg;
349
350#ifdef CONFIG_LOCKDEP
351 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
352 * we don't want and can't tolerate. Although it might be
353 * friendlier not to borrow this thread context...
354 */
355 local_irq_enable();
356#endif
357
358 res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
359 if (res)
360 goto out;
361 /*
362 * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
363 * only one (ALARM or RTC) interrupt source may be enabled
364 * at time, we also could check our results
365 * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
366 */
367 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
368 events |= RTC_IRQF | RTC_AF;
369 else
370 events |= RTC_IRQF | RTC_UF;
371
372 res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
373 REG_RTC_STATUS_REG);
374 if (res)
375 goto out;
376
377 /* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1
378 * needs 2 reads to clear the interrupt. One read is done in
379 * do_twl4030_pwrirq(). Doing the second read, to clear
380 * the bit.
381 *
382 * FIXME the reason PWR_ISR1 needs an extra read is that
383 * RTC_IF retriggered until we cleared REG_ALARM_M above.
384 * But re-reading like this is a bad hack; by doing so we
385 * risk wrongly clearing status for some other IRQ (losing
386 * the interrupt). Be smarter about handling RTC_UF ...
387 */
388 res = twl4030_i2c_read_u8(TWL4030_MODULE_INT,
389 &rd_reg, TWL4030_INT_PWR_ISR1);
390 if (res)
391 goto out;
392
393 /* Notify RTC core on event */
394 rtc_update_irq(rtc, 1, events);
395
396 ret = IRQ_HANDLED;
397out:
398 return ret;
399}
400
401static struct rtc_class_ops twl4030_rtc_ops = {
402 .ioctl = twl4030_rtc_ioctl,
403 .read_time = twl4030_rtc_read_time,
404 .set_time = twl4030_rtc_set_time,
405 .read_alarm = twl4030_rtc_read_alarm,
406 .set_alarm = twl4030_rtc_set_alarm,
407};
408
409/*----------------------------------------------------------------------*/
410
411static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
412{
413 struct rtc_device *rtc;
414 int ret = 0;
415 int irq = platform_get_irq(pdev, 0);
416 u8 rd_reg;
417
418 if (irq < 0)
419 return irq;
420
421 rtc = rtc_device_register(pdev->name,
422 &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
423 if (IS_ERR(rtc)) {
424 ret = -EINVAL;
425 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
426 PTR_ERR(rtc));
427 goto out0;
428
429 }
430
431 platform_set_drvdata(pdev, rtc);
432
433 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
434
435 if (ret < 0)
436 goto out1;
437
438 if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
439 dev_warn(&pdev->dev, "Power up reset detected.\n");
440
441 if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
442 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
443
444 /* Clear RTC Power up reset and pending alarm interrupts */
445 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
446 if (ret < 0)
447 goto out1;
448
449 ret = request_irq(irq, twl4030_rtc_interrupt,
450 IRQF_TRIGGER_RISING,
451 rtc->dev.bus_id, rtc);
452 if (ret < 0) {
453 dev_err(&pdev->dev, "IRQ is not free.\n");
454 goto out1;
455 }
456
457 /* Check RTC module status, Enable if it is off */
458 ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
459 if (ret < 0)
460 goto out2;
461
462 if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
463 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
464 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
465 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
466 if (ret < 0)
467 goto out2;
468 }
469
470 /* init cached IRQ enable bits */
471 ret = twl4030_rtc_read_u8(&rtc_irq_bits, REG_RTC_INTERRUPTS_REG);
472 if (ret < 0)
473 goto out2;
474
475 return ret;
476
477
478out2:
479 free_irq(irq, rtc);
480out1:
481 rtc_device_unregister(rtc);
482out0:
483 return ret;
484}
485
486/*
487 * Disable all TWL4030 RTC module interrupts.
488 * Sets status flag to free.
489 */
490static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
491{
492 /* leave rtc running, but disable irqs */
493 struct rtc_device *rtc = platform_get_drvdata(pdev);
494 int irq = platform_get_irq(pdev, 0);
495
496 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
497 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
498
499 free_irq(irq, rtc);
500
501 rtc_device_unregister(rtc);
502 platform_set_drvdata(pdev, NULL);
503 return 0;
504}
505
506static void twl4030_rtc_shutdown(struct platform_device *pdev)
507{
508 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
509 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
510}
511
512#ifdef CONFIG_PM
513
514static unsigned char irqstat;
515
516static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
517{
518 irqstat = rtc_irq_bits;
519
520 /* REVISIT alarm may need to wake us from sleep */
521 mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
522 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
523 return 0;
524}
525
526static int twl4030_rtc_resume(struct platform_device *pdev)
527{
528 set_rtc_irq_bit(irqstat);
529 return 0;
530}
531
532#else
533#define twl4030_rtc_suspend NULL
534#define twl4030_rtc_resume NULL
535#endif
536
537MODULE_ALIAS("platform:twl4030_rtc");
538
539static struct platform_driver twl4030rtc_driver = {
540 .probe = twl4030_rtc_probe,
541 .remove = __devexit_p(twl4030_rtc_remove),
542 .shutdown = twl4030_rtc_shutdown,
543 .suspend = twl4030_rtc_suspend,
544 .resume = twl4030_rtc_resume,
545 .driver = {
546 .owner = THIS_MODULE,
547 .name = "twl4030_rtc",
548 },
549};
550
551static int __init twl4030_rtc_init(void)
552{
553 return platform_driver_register(&twl4030rtc_driver);
554}
555module_init(twl4030_rtc_init);
556
557static void __exit twl4030_rtc_exit(void)
558{
559 platform_driver_unregister(&twl4030rtc_driver);
560}
561module_exit(twl4030_rtc_exit);
562
563MODULE_AUTHOR("Texas Instruments, MontaVista Software");
564MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-v3020.c b/drivers/rtc/rtc-v3020.c
index 10025d840268..14d4f036a768 100644
--- a/drivers/rtc/rtc-v3020.c
+++ b/drivers/rtc/rtc-v3020.c
@@ -92,19 +92,19 @@ static int v3020_read_time(struct device *dev, struct rtc_time *dt)
92 92
93 /* ...and then read constant values. */ 93 /* ...and then read constant values. */
94 tmp = v3020_get_reg(chip, V3020_SECONDS); 94 tmp = v3020_get_reg(chip, V3020_SECONDS);
95 dt->tm_sec = BCD2BIN(tmp); 95 dt->tm_sec = bcd2bin(tmp);
96 tmp = v3020_get_reg(chip, V3020_MINUTES); 96 tmp = v3020_get_reg(chip, V3020_MINUTES);
97 dt->tm_min = BCD2BIN(tmp); 97 dt->tm_min = bcd2bin(tmp);
98 tmp = v3020_get_reg(chip, V3020_HOURS); 98 tmp = v3020_get_reg(chip, V3020_HOURS);
99 dt->tm_hour = BCD2BIN(tmp); 99 dt->tm_hour = bcd2bin(tmp);
100 tmp = v3020_get_reg(chip, V3020_MONTH_DAY); 100 tmp = v3020_get_reg(chip, V3020_MONTH_DAY);
101 dt->tm_mday = BCD2BIN(tmp); 101 dt->tm_mday = bcd2bin(tmp);
102 tmp = v3020_get_reg(chip, V3020_MONTH); 102 tmp = v3020_get_reg(chip, V3020_MONTH);
103 dt->tm_mon = BCD2BIN(tmp) - 1; 103 dt->tm_mon = bcd2bin(tmp) - 1;
104 tmp = v3020_get_reg(chip, V3020_WEEK_DAY); 104 tmp = v3020_get_reg(chip, V3020_WEEK_DAY);
105 dt->tm_wday = BCD2BIN(tmp); 105 dt->tm_wday = bcd2bin(tmp);
106 tmp = v3020_get_reg(chip, V3020_YEAR); 106 tmp = v3020_get_reg(chip, V3020_YEAR);
107 dt->tm_year = BCD2BIN(tmp)+100; 107 dt->tm_year = bcd2bin(tmp)+100;
108 108
109#ifdef DEBUG 109#ifdef DEBUG
110 printk("\n%s : Read RTC values\n",__func__); 110 printk("\n%s : Read RTC values\n",__func__);
@@ -136,13 +136,13 @@ static int v3020_set_time(struct device *dev, struct rtc_time *dt)
136#endif 136#endif
137 137
138 /* Write all the values to ram... */ 138 /* Write all the values to ram... */
139 v3020_set_reg(chip, V3020_SECONDS, BIN2BCD(dt->tm_sec)); 139 v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec));
140 v3020_set_reg(chip, V3020_MINUTES, BIN2BCD(dt->tm_min)); 140 v3020_set_reg(chip, V3020_MINUTES, bin2bcd(dt->tm_min));
141 v3020_set_reg(chip, V3020_HOURS, BIN2BCD(dt->tm_hour)); 141 v3020_set_reg(chip, V3020_HOURS, bin2bcd(dt->tm_hour));
142 v3020_set_reg(chip, V3020_MONTH_DAY, BIN2BCD(dt->tm_mday)); 142 v3020_set_reg(chip, V3020_MONTH_DAY, bin2bcd(dt->tm_mday));
143 v3020_set_reg(chip, V3020_MONTH, BIN2BCD(dt->tm_mon + 1)); 143 v3020_set_reg(chip, V3020_MONTH, bin2bcd(dt->tm_mon + 1));
144 v3020_set_reg(chip, V3020_WEEK_DAY, BIN2BCD(dt->tm_wday)); 144 v3020_set_reg(chip, V3020_WEEK_DAY, bin2bcd(dt->tm_wday));
145 v3020_set_reg(chip, V3020_YEAR, BIN2BCD(dt->tm_year % 100)); 145 v3020_set_reg(chip, V3020_YEAR, bin2bcd(dt->tm_year % 100));
146 146
147 /* ...and set the clock. */ 147 /* ...and set the clock. */
148 v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0); 148 v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0);
diff --git a/drivers/rtc/rtc-vr41xx.c b/drivers/rtc/rtc-vr41xx.c
index 884b635f028b..834dcc6d785f 100644
--- a/drivers/rtc/rtc-vr41xx.c
+++ b/drivers/rtc/rtc-vr41xx.c
@@ -360,7 +360,7 @@ static int __devinit rtc_probe(struct platform_device *pdev)
360 spin_unlock_irq(&rtc_lock); 360 spin_unlock_irq(&rtc_lock);
361 361
362 aie_irq = platform_get_irq(pdev, 0); 362 aie_irq = platform_get_irq(pdev, 0);
363 if (aie_irq < 0 || aie_irq >= NR_IRQS) { 363 if (aie_irq < 0 || aie_irq >= nr_irqs) {
364 retval = -EBUSY; 364 retval = -EBUSY;
365 goto err_device_unregister; 365 goto err_device_unregister;
366 } 366 }
@@ -371,7 +371,7 @@ static int __devinit rtc_probe(struct platform_device *pdev)
371 goto err_device_unregister; 371 goto err_device_unregister;
372 372
373 pie_irq = platform_get_irq(pdev, 1); 373 pie_irq = platform_get_irq(pdev, 1);
374 if (pie_irq < 0 || pie_irq >= NR_IRQS) 374 if (pie_irq < 0 || pie_irq >= nr_irqs)
375 goto err_free_irq; 375 goto err_free_irq;
376 376
377 retval = request_irq(pie_irq, rtclong1_interrupt, IRQF_DISABLED, 377 retval = request_irq(pie_irq, rtclong1_interrupt, IRQF_DISABLED,
diff --git a/drivers/rtc/rtc-wm8350.c b/drivers/rtc/rtc-wm8350.c
new file mode 100644
index 000000000000..5c5e3aa91385
--- /dev/null
+++ b/drivers/rtc/rtc-wm8350.c
@@ -0,0 +1,514 @@
1/*
2 * Real Time Clock driver for Wolfson Microelectronics WM8350
3 *
4 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
5 *
6 * Author: Liam Girdwood
7 * linux@wolfsonmicro.com
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/time.h>
19#include <linux/rtc.h>
20#include <linux/bcd.h>
21#include <linux/interrupt.h>
22#include <linux/ioctl.h>
23#include <linux/completion.h>
24#include <linux/mfd/wm8350/rtc.h>
25#include <linux/mfd/wm8350/core.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28
29#define WM8350_SET_ALM_RETRIES 5
30#define WM8350_SET_TIME_RETRIES 5
31#define WM8350_GET_TIME_RETRIES 5
32
33#define to_wm8350_from_rtc_dev(d) container_of(d, struct wm8350, rtc.pdev.dev)
34
35/*
36 * Read current time and date in RTC
37 */
38static int wm8350_rtc_readtime(struct device *dev, struct rtc_time *tm)
39{
40 struct wm8350 *wm8350 = dev_get_drvdata(dev);
41 u16 time1[4], time2[4];
42 int retries = WM8350_GET_TIME_RETRIES, ret;
43
44 /*
45 * Read the time twice and compare.
46 * If time1 == time2, then time is valid else retry.
47 */
48 do {
49 ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
50 4, time1);
51 if (ret < 0)
52 return ret;
53 ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
54 4, time2);
55 if (ret < 0)
56 return ret;
57
58 if (memcmp(time1, time2, sizeof(time1)) == 0) {
59 tm->tm_sec = time1[0] & WM8350_RTC_SECS_MASK;
60
61 tm->tm_min = (time1[0] & WM8350_RTC_MINS_MASK)
62 >> WM8350_RTC_MINS_SHIFT;
63
64 tm->tm_hour = time1[1] & WM8350_RTC_HRS_MASK;
65
66 tm->tm_wday = ((time1[1] >> WM8350_RTC_DAY_SHIFT)
67 & 0x7) - 1;
68
69 tm->tm_mon = ((time1[2] & WM8350_RTC_MTH_MASK)
70 >> WM8350_RTC_MTH_SHIFT) - 1;
71
72 tm->tm_mday = (time1[2] & WM8350_RTC_DATE_MASK);
73
74 tm->tm_year = ((time1[3] & WM8350_RTC_YHUNDREDS_MASK)
75 >> WM8350_RTC_YHUNDREDS_SHIFT) * 100;
76 tm->tm_year += time1[3] & WM8350_RTC_YUNITS_MASK;
77
78 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon,
79 tm->tm_year);
80 tm->tm_year -= 1900;
81
82 dev_dbg(dev, "Read (%d left): %04x %04x %04x %04x\n",
83 retries,
84 time1[0], time1[1], time1[2], time1[3]);
85
86 return 0;
87 }
88 } while (retries--);
89
90 dev_err(dev, "timed out reading RTC time\n");
91 return -EIO;
92}
93
94/*
95 * Set current time and date in RTC
96 */
97static int wm8350_rtc_settime(struct device *dev, struct rtc_time *tm)
98{
99 struct wm8350 *wm8350 = dev_get_drvdata(dev);
100 u16 time[4];
101 u16 rtc_ctrl;
102 int ret, retries = WM8350_SET_TIME_RETRIES;
103
104 time[0] = tm->tm_sec;
105 time[0] |= tm->tm_min << WM8350_RTC_MINS_SHIFT;
106 time[1] = tm->tm_hour;
107 time[1] |= (tm->tm_wday + 1) << WM8350_RTC_DAY_SHIFT;
108 time[2] = tm->tm_mday;
109 time[2] |= (tm->tm_mon + 1) << WM8350_RTC_MTH_SHIFT;
110 time[3] = ((tm->tm_year + 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT;
111 time[3] |= (tm->tm_year + 1900) % 100;
112
113 dev_dbg(dev, "Setting: %04x %04x %04x %04x\n",
114 time[0], time[1], time[2], time[3]);
115
116 /* Set RTC_SET to stop the clock */
117 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, WM8350_RTC_SET);
118 if (ret < 0)
119 return ret;
120
121 /* Wait until confirmation of stopping */
122 do {
123 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
124 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
125 } while (retries-- && !(rtc_ctrl & WM8350_RTC_STS));
126
127 if (!retries) {
128 dev_err(dev, "timed out on set confirmation\n");
129 return -EIO;
130 }
131
132 /* Write time to RTC */
133 ret = wm8350_block_write(wm8350, WM8350_RTC_SECONDS_MINUTES, 4, time);
134 if (ret < 0)
135 return ret;
136
137 /* Clear RTC_SET to start the clock */
138 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
139 WM8350_RTC_SET);
140 return ret;
141}
142
143/*
144 * Read alarm time and date in RTC
145 */
146static int wm8350_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
147{
148 struct wm8350 *wm8350 = dev_get_drvdata(dev);
149 struct rtc_time *tm = &alrm->time;
150 u16 time[4];
151 int ret;
152
153 ret = wm8350_block_read(wm8350, WM8350_ALARM_SECONDS_MINUTES, 4, time);
154 if (ret < 0)
155 return ret;
156
157 tm->tm_sec = time[0] & WM8350_RTC_ALMSECS_MASK;
158 if (tm->tm_sec == WM8350_RTC_ALMSECS_MASK)
159 tm->tm_sec = -1;
160
161 tm->tm_min = time[0] & WM8350_RTC_ALMMINS_MASK;
162 if (tm->tm_min == WM8350_RTC_ALMMINS_MASK)
163 tm->tm_min = -1;
164 else
165 tm->tm_min >>= WM8350_RTC_ALMMINS_SHIFT;
166
167 tm->tm_hour = time[1] & WM8350_RTC_ALMHRS_MASK;
168 if (tm->tm_hour == WM8350_RTC_ALMHRS_MASK)
169 tm->tm_hour = -1;
170
171 tm->tm_wday = ((time[1] >> WM8350_RTC_ALMDAY_SHIFT) & 0x7) - 1;
172 if (tm->tm_wday > 7)
173 tm->tm_wday = -1;
174
175 tm->tm_mon = time[2] & WM8350_RTC_ALMMTH_MASK;
176 if (tm->tm_mon == WM8350_RTC_ALMMTH_MASK)
177 tm->tm_mon = -1;
178 else
179 tm->tm_mon = (tm->tm_mon >> WM8350_RTC_ALMMTH_SHIFT) - 1;
180
181 tm->tm_mday = (time[2] & WM8350_RTC_ALMDATE_MASK);
182 if (tm->tm_mday == WM8350_RTC_ALMDATE_MASK)
183 tm->tm_mday = -1;
184
185 tm->tm_year = -1;
186
187 alrm->enabled = !(time[3] & WM8350_RTC_ALMSTS);
188
189 return 0;
190}
191
192static int wm8350_rtc_stop_alarm(struct wm8350 *wm8350)
193{
194 int retries = WM8350_SET_ALM_RETRIES;
195 u16 rtc_ctrl;
196 int ret;
197
198 /* Set RTC_SET to stop the clock */
199 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
200 WM8350_RTC_ALMSET);
201 if (ret < 0)
202 return ret;
203
204 /* Wait until confirmation of stopping */
205 do {
206 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
207 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
208 } while (retries-- && !(rtc_ctrl & WM8350_RTC_ALMSTS));
209
210 if (!(rtc_ctrl & WM8350_RTC_ALMSTS))
211 return -ETIMEDOUT;
212
213 return 0;
214}
215
216static int wm8350_rtc_start_alarm(struct wm8350 *wm8350)
217{
218 int ret;
219 int retries = WM8350_SET_ALM_RETRIES;
220 u16 rtc_ctrl;
221
222 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
223 WM8350_RTC_ALMSET);
224 if (ret < 0)
225 return ret;
226
227 /* Wait until confirmation */
228 do {
229 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
230 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
231 } while (retries-- && rtc_ctrl & WM8350_RTC_ALMSTS);
232
233 if (rtc_ctrl & WM8350_RTC_ALMSTS)
234 return -ETIMEDOUT;
235
236 return 0;
237}
238
239static int wm8350_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
240{
241 struct wm8350 *wm8350 = dev_get_drvdata(dev);
242 struct rtc_time *tm = &alrm->time;
243 u16 time[3];
244 int ret;
245
246 memset(time, 0, sizeof(time));
247
248 if (tm->tm_sec != -1)
249 time[0] |= tm->tm_sec;
250 else
251 time[0] |= WM8350_RTC_ALMSECS_MASK;
252
253 if (tm->tm_min != -1)
254 time[0] |= tm->tm_min << WM8350_RTC_ALMMINS_SHIFT;
255 else
256 time[0] |= WM8350_RTC_ALMMINS_MASK;
257
258 if (tm->tm_hour != -1)
259 time[1] |= tm->tm_hour;
260 else
261 time[1] |= WM8350_RTC_ALMHRS_MASK;
262
263 if (tm->tm_wday != -1)
264 time[1] |= (tm->tm_wday + 1) << WM8350_RTC_ALMDAY_SHIFT;
265 else
266 time[1] |= WM8350_RTC_ALMDAY_MASK;
267
268 if (tm->tm_mday != -1)
269 time[2] |= tm->tm_mday;
270 else
271 time[2] |= WM8350_RTC_ALMDATE_MASK;
272
273 if (tm->tm_mon != -1)
274 time[2] |= (tm->tm_mon + 1) << WM8350_RTC_ALMMTH_SHIFT;
275 else
276 time[2] |= WM8350_RTC_ALMMTH_MASK;
277
278 ret = wm8350_rtc_stop_alarm(wm8350);
279 if (ret < 0)
280 return ret;
281
282 /* Write time to RTC */
283 ret = wm8350_block_write(wm8350, WM8350_ALARM_SECONDS_MINUTES,
284 3, time);
285 if (ret < 0)
286 return ret;
287
288 if (alrm->enabled)
289 ret = wm8350_rtc_start_alarm(wm8350);
290
291 return ret;
292}
293
294/*
295 * Handle commands from user-space
296 */
297static int wm8350_rtc_ioctl(struct device *dev, unsigned int cmd,
298 unsigned long arg)
299{
300 struct wm8350 *wm8350 = dev_get_drvdata(dev);
301
302 switch (cmd) {
303 case RTC_AIE_OFF:
304 return wm8350_rtc_stop_alarm(wm8350);
305 case RTC_AIE_ON:
306 return wm8350_rtc_start_alarm(wm8350);
307
308 case RTC_UIE_OFF:
309 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
310 break;
311 case RTC_UIE_ON:
312 wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_SEC);
313 break;
314
315 default:
316 return -ENOIOCTLCMD;
317 }
318
319 return 0;
320}
321
322static void wm8350_rtc_alarm_handler(struct wm8350 *wm8350, int irq,
323 void *data)
324{
325 struct rtc_device *rtc = wm8350->rtc.rtc;
326 int ret;
327
328 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
329
330 /* Make it one shot */
331 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
332 WM8350_RTC_ALMSET);
333 if (ret != 0) {
334 dev_err(&(wm8350->rtc.pdev->dev),
335 "Failed to disable alarm: %d\n", ret);
336 }
337}
338
339static void wm8350_rtc_update_handler(struct wm8350 *wm8350, int irq,
340 void *data)
341{
342 struct rtc_device *rtc = wm8350->rtc.rtc;
343
344 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_UF);
345}
346
347static const struct rtc_class_ops wm8350_rtc_ops = {
348 .ioctl = wm8350_rtc_ioctl,
349 .read_time = wm8350_rtc_readtime,
350 .set_time = wm8350_rtc_settime,
351 .read_alarm = wm8350_rtc_readalarm,
352 .set_alarm = wm8350_rtc_setalarm,
353};
354
355#ifdef CONFIG_PM
356static int wm8350_rtc_suspend(struct platform_device *pdev, pm_message_t state)
357{
358 struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
359 int ret = 0;
360 u16 reg;
361
362 reg = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
363
364 if (device_may_wakeup(&wm8350->rtc.pdev->dev) &&
365 reg & WM8350_RTC_ALMSTS) {
366 ret = wm8350_rtc_stop_alarm(wm8350);
367 if (ret != 0)
368 dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n",
369 ret);
370 }
371
372 return ret;
373}
374
375static int wm8350_rtc_resume(struct platform_device *pdev)
376{
377 struct wm8350 *wm8350 = dev_get_drvdata(&pdev->dev);
378 int ret;
379
380 if (wm8350->rtc.alarm_enabled) {
381 ret = wm8350_rtc_start_alarm(wm8350);
382 if (ret != 0)
383 dev_err(&pdev->dev,
384 "Failed to restart RTC alarm: %d\n", ret);
385 }
386
387 return 0;
388}
389
390#else
391#define wm8350_rtc_suspend NULL
392#define wm8350_rtc_resume NULL
393#endif
394
395static int wm8350_rtc_probe(struct platform_device *pdev)
396{
397 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
398 struct wm8350_rtc *wm_rtc = &wm8350->rtc;
399 int ret = 0;
400 u16 timectl, power5;
401
402 timectl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
403 if (timectl & WM8350_RTC_BCD) {
404 dev_err(&pdev->dev, "RTC BCD mode not supported\n");
405 return -EINVAL;
406 }
407 if (timectl & WM8350_RTC_12HR) {
408 dev_err(&pdev->dev, "RTC 12 hour mode not supported\n");
409 return -EINVAL;
410 }
411
412 /* enable the RTC if it's not already enabled */
413 power5 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
414 if (!(power5 & WM8350_RTC_TICK_ENA)) {
415 dev_info(wm8350->dev, "Starting RTC\n");
416
417 wm8350_reg_unlock(wm8350);
418
419 ret = wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5,
420 WM8350_RTC_TICK_ENA);
421 if (ret < 0) {
422 dev_err(&pdev->dev, "failed to enable RTC: %d\n", ret);
423 return ret;
424 }
425
426 wm8350_reg_lock(wm8350);
427 }
428
429 if (timectl & WM8350_RTC_STS) {
430 int retries;
431
432 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
433 WM8350_RTC_SET);
434 if (ret < 0) {
435 dev_err(&pdev->dev, "failed to start: %d\n", ret);
436 return ret;
437 }
438
439 retries = WM8350_SET_TIME_RETRIES;
440 do {
441 timectl = wm8350_reg_read(wm8350,
442 WM8350_RTC_TIME_CONTROL);
443 } while (timectl & WM8350_RTC_STS && retries--);
444
445 if (retries == 0) {
446 dev_err(&pdev->dev, "failed to start: timeout\n");
447 return -ENODEV;
448 }
449 }
450
451 device_init_wakeup(&pdev->dev, 1);
452
453 wm_rtc->rtc = rtc_device_register("wm8350", &pdev->dev,
454 &wm8350_rtc_ops, THIS_MODULE);
455 if (IS_ERR(wm_rtc->rtc)) {
456 ret = PTR_ERR(wm_rtc->rtc);
457 dev_err(&pdev->dev, "failed to register RTC: %d\n", ret);
458 return ret;
459 }
460
461 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
462 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_PER);
463
464 wm8350_register_irq(wm8350, WM8350_IRQ_RTC_SEC,
465 wm8350_rtc_update_handler, NULL);
466
467 wm8350_register_irq(wm8350, WM8350_IRQ_RTC_ALM,
468 wm8350_rtc_alarm_handler, NULL);
469 wm8350_unmask_irq(wm8350, WM8350_IRQ_RTC_ALM);
470
471 return 0;
472}
473
474static int __devexit wm8350_rtc_remove(struct platform_device *pdev)
475{
476 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
477 struct wm8350_rtc *wm_rtc = &wm8350->rtc;
478
479 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
480
481 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC);
482 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_ALM);
483
484 rtc_device_unregister(wm_rtc->rtc);
485
486 return 0;
487}
488
489static struct platform_driver wm8350_rtc_driver = {
490 .probe = wm8350_rtc_probe,
491 .remove = __devexit_p(wm8350_rtc_remove),
492 .suspend = wm8350_rtc_suspend,
493 .resume = wm8350_rtc_resume,
494 .driver = {
495 .name = "wm8350-rtc",
496 },
497};
498
499static int __init wm8350_rtc_init(void)
500{
501 return platform_driver_register(&wm8350_rtc_driver);
502}
503module_init(wm8350_rtc_init);
504
505static void __exit wm8350_rtc_exit(void)
506{
507 platform_driver_unregister(&wm8350_rtc_driver);
508}
509module_exit(wm8350_rtc_exit);
510
511MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
512MODULE_DESCRIPTION("RTC driver for the WM8350");
513MODULE_LICENSE("GPL");
514MODULE_ALIAS("platform:wm8350-rtc");
diff --git a/drivers/rtc/rtc-x1205.c b/drivers/rtc/rtc-x1205.c
index 7dcfba1bbfe1..310c10795e9a 100644
--- a/drivers/rtc/rtc-x1205.c
+++ b/drivers/rtc/rtc-x1205.c
@@ -118,13 +118,13 @@ static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
118 for (i = 0; i <= 4; i++) 118 for (i = 0; i <= 4; i++)
119 buf[i] &= 0x7F; 119 buf[i] &= 0x7F;
120 120
121 tm->tm_sec = BCD2BIN(buf[CCR_SEC]); 121 tm->tm_sec = bcd2bin(buf[CCR_SEC]);
122 tm->tm_min = BCD2BIN(buf[CCR_MIN]); 122 tm->tm_min = bcd2bin(buf[CCR_MIN]);
123 tm->tm_hour = BCD2BIN(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */ 123 tm->tm_hour = bcd2bin(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
124 tm->tm_mday = BCD2BIN(buf[CCR_MDAY]); 124 tm->tm_mday = bcd2bin(buf[CCR_MDAY]);
125 tm->tm_mon = BCD2BIN(buf[CCR_MONTH]) - 1; /* mon is 0-11 */ 125 tm->tm_mon = bcd2bin(buf[CCR_MONTH]) - 1; /* mon is 0-11 */
126 tm->tm_year = BCD2BIN(buf[CCR_YEAR]) 126 tm->tm_year = bcd2bin(buf[CCR_YEAR])
127 + (BCD2BIN(buf[CCR_Y2K]) * 100) - 1900; 127 + (bcd2bin(buf[CCR_Y2K]) * 100) - 1900;
128 tm->tm_wday = buf[CCR_WDAY]; 128 tm->tm_wday = buf[CCR_WDAY];
129 129
130 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 130 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
@@ -174,11 +174,11 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
174 __func__, 174 __func__,
175 tm->tm_sec, tm->tm_min, tm->tm_hour); 175 tm->tm_sec, tm->tm_min, tm->tm_hour);
176 176
177 buf[CCR_SEC] = BIN2BCD(tm->tm_sec); 177 buf[CCR_SEC] = bin2bcd(tm->tm_sec);
178 buf[CCR_MIN] = BIN2BCD(tm->tm_min); 178 buf[CCR_MIN] = bin2bcd(tm->tm_min);
179 179
180 /* set hour and 24hr bit */ 180 /* set hour and 24hr bit */
181 buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_HR_MIL; 181 buf[CCR_HOUR] = bin2bcd(tm->tm_hour) | X1205_HR_MIL;
182 182
183 /* should we also set the date? */ 183 /* should we also set the date? */
184 if (datetoo) { 184 if (datetoo) {
@@ -187,15 +187,15 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
187 __func__, 187 __func__,
188 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 188 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
189 189
190 buf[CCR_MDAY] = BIN2BCD(tm->tm_mday); 190 buf[CCR_MDAY] = bin2bcd(tm->tm_mday);
191 191
192 /* month, 1 - 12 */ 192 /* month, 1 - 12 */
193 buf[CCR_MONTH] = BIN2BCD(tm->tm_mon + 1); 193 buf[CCR_MONTH] = bin2bcd(tm->tm_mon + 1);
194 194
195 /* year, since the rtc epoch*/ 195 /* year, since the rtc epoch*/
196 buf[CCR_YEAR] = BIN2BCD(tm->tm_year % 100); 196 buf[CCR_YEAR] = bin2bcd(tm->tm_year % 100);
197 buf[CCR_WDAY] = tm->tm_wday & 0x07; 197 buf[CCR_WDAY] = tm->tm_wday & 0x07;
198 buf[CCR_Y2K] = BIN2BCD(tm->tm_year / 100); 198 buf[CCR_Y2K] = bin2bcd(tm->tm_year / 100);
199 } 199 }
200 200
201 /* If writing alarm registers, set compare bits on registers 0-4 */ 201 /* If writing alarm registers, set compare bits on registers 0-4 */
@@ -437,7 +437,7 @@ static int x1205_validate_client(struct i2c_client *client)
437 return -EIO; 437 return -EIO;
438 } 438 }
439 439
440 value = BCD2BIN(reg & probe_limits_pattern[i].mask); 440 value = bcd2bin(reg & probe_limits_pattern[i].mask);
441 441
442 if (value > probe_limits_pattern[i].max || 442 if (value > probe_limits_pattern[i].max ||
443 value < probe_limits_pattern[i].min) { 443 value < probe_limits_pattern[i].min) {