diff options
author | Kim B. Heino <Kim.Heino@bluegiga.com> | 2008-07-24 00:30:34 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-07-24 13:47:33 -0400 |
commit | 8fc2c767b06067b417c565c4e75731e68ed41fd8 (patch) | |
tree | 7ae425a9fb09bacec67c55f1a412e4585e1836a9 | |
parent | 5ad31a575157147b43fa84ef1e21471661653878 (diff) |
rtc: add support for ST M41T94 SPI RTC
This patch adds kernel driver for M41T94 RTC chip connected via SPI.
I've tested it on two different AT91-based hardwares.
This is third revision of the patch: some comments made by
Alessandro Zummo fixed.
Revision two added support for century bit and fixes.
Signed-off-by: Kim B. Heino <Kim.Heino@bluegiga.com>
Signed-off-by: Alessandro Zummo <a.zummo@towertech.it>
Cc: David Brownell <david-b@pacbell.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | drivers/rtc/Kconfig | 9 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
-rw-r--r-- | drivers/rtc/rtc-m41t94.c | 173 |
3 files changed, 183 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index fc85bf2e4a97..beffb834c440 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
@@ -273,6 +273,15 @@ comment "SPI RTC drivers" | |||
273 | 273 | ||
274 | if SPI_MASTER | 274 | if SPI_MASTER |
275 | 275 | ||
276 | config RTC_DRV_M41T94 | ||
277 | tristate "ST M41T94" | ||
278 | help | ||
279 | If you say yes here you will get support for the | ||
280 | ST M41T94 SPI RTC chip. | ||
281 | |||
282 | This driver can also be built as a module. If so, the module | ||
283 | will be called rtc-m41t94. | ||
284 | |||
276 | config RTC_DRV_MAX6902 | 285 | config RTC_DRV_MAX6902 |
277 | tristate "Maxim MAX6902" | 286 | tristate "Maxim MAX6902" |
278 | help | 287 | help |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index b5d9d67df887..b0e1af54f800 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
@@ -34,6 +34,7 @@ obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o | |||
34 | obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o | 34 | obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o |
35 | obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o | 35 | obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o |
36 | obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o | 36 | obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o |
37 | obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o | ||
37 | obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o | 38 | obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o |
38 | obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o | 39 | obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o |
39 | obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o | 40 | obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o |
diff --git a/drivers/rtc/rtc-m41t94.c b/drivers/rtc/rtc-m41t94.c new file mode 100644 index 000000000000..9b19499c829e --- /dev/null +++ b/drivers/rtc/rtc-m41t94.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /* | ||
2 | * Driver for ST M41T94 SPI RTC | ||
3 | * | ||
4 | * Copyright (C) 2008 Kim B. Heino | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License version 2 as | ||
8 | * published by the Free Software Foundation. | ||
9 | */ | ||
10 | |||
11 | #include <linux/module.h> | ||
12 | #include <linux/kernel.h> | ||
13 | #include <linux/platform_device.h> | ||
14 | #include <linux/rtc.h> | ||
15 | #include <linux/spi/spi.h> | ||
16 | #include <linux/bcd.h> | ||
17 | |||
18 | #define M41T94_REG_SECONDS 0x01 | ||
19 | #define M41T94_REG_MINUTES 0x02 | ||
20 | #define M41T94_REG_HOURS 0x03 | ||
21 | #define M41T94_REG_WDAY 0x04 | ||
22 | #define M41T94_REG_DAY 0x05 | ||
23 | #define M41T94_REG_MONTH 0x06 | ||
24 | #define M41T94_REG_YEAR 0x07 | ||
25 | #define M41T94_REG_HT 0x0c | ||
26 | |||
27 | #define M41T94_BIT_HALT 0x40 | ||
28 | #define M41T94_BIT_STOP 0x80 | ||
29 | #define M41T94_BIT_CB 0x40 | ||
30 | #define M41T94_BIT_CEB 0x80 | ||
31 | |||
32 | static int m41t94_set_time(struct device *dev, struct rtc_time *tm) | ||
33 | { | ||
34 | struct spi_device *spi = to_spi_device(dev); | ||
35 | u8 buf[8]; /* write cmd + 7 registers */ | ||
36 | |||
37 | dev_dbg(dev, "%s secs=%d, mins=%d, " | ||
38 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", | ||
39 | "write", tm->tm_sec, tm->tm_min, | ||
40 | tm->tm_hour, tm->tm_mday, | ||
41 | tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
42 | |||
43 | buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */ | ||
44 | buf[M41T94_REG_SECONDS] = BIN2BCD(tm->tm_sec); | ||
45 | buf[M41T94_REG_MINUTES] = BIN2BCD(tm->tm_min); | ||
46 | buf[M41T94_REG_HOURS] = BIN2BCD(tm->tm_hour); | ||
47 | buf[M41T94_REG_WDAY] = BIN2BCD(tm->tm_wday + 1); | ||
48 | buf[M41T94_REG_DAY] = BIN2BCD(tm->tm_mday); | ||
49 | buf[M41T94_REG_MONTH] = BIN2BCD(tm->tm_mon + 1); | ||
50 | |||
51 | buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB; | ||
52 | if (tm->tm_year >= 100) | ||
53 | buf[M41T94_REG_HOURS] |= M41T94_BIT_CB; | ||
54 | buf[M41T94_REG_YEAR] = BIN2BCD(tm->tm_year % 100); | ||
55 | |||
56 | return spi_write(spi, buf, 8); | ||
57 | } | ||
58 | |||
59 | static int m41t94_read_time(struct device *dev, struct rtc_time *tm) | ||
60 | { | ||
61 | struct spi_device *spi = to_spi_device(dev); | ||
62 | u8 buf[2]; | ||
63 | int ret, hour; | ||
64 | |||
65 | /* clear halt update bit */ | ||
66 | ret = spi_w8r8(spi, M41T94_REG_HT); | ||
67 | if (ret < 0) | ||
68 | return ret; | ||
69 | if (ret & M41T94_BIT_HALT) { | ||
70 | buf[0] = 0x80 | M41T94_REG_HT; | ||
71 | buf[1] = ret & ~M41T94_BIT_HALT; | ||
72 | spi_write(spi, buf, 2); | ||
73 | } | ||
74 | |||
75 | /* clear stop bit */ | ||
76 | ret = spi_w8r8(spi, M41T94_REG_SECONDS); | ||
77 | if (ret < 0) | ||
78 | return ret; | ||
79 | if (ret & M41T94_BIT_STOP) { | ||
80 | buf[0] = 0x80 | M41T94_REG_SECONDS; | ||
81 | buf[1] = ret & ~M41T94_BIT_STOP; | ||
82 | spi_write(spi, buf, 2); | ||
83 | } | ||
84 | |||
85 | tm->tm_sec = BCD2BIN(spi_w8r8(spi, M41T94_REG_SECONDS)); | ||
86 | tm->tm_min = BCD2BIN(spi_w8r8(spi, M41T94_REG_MINUTES)); | ||
87 | hour = spi_w8r8(spi, M41T94_REG_HOURS); | ||
88 | tm->tm_hour = BCD2BIN(hour & 0x3f); | ||
89 | tm->tm_wday = BCD2BIN(spi_w8r8(spi, M41T94_REG_WDAY)) - 1; | ||
90 | tm->tm_mday = BCD2BIN(spi_w8r8(spi, M41T94_REG_DAY)); | ||
91 | tm->tm_mon = BCD2BIN(spi_w8r8(spi, M41T94_REG_MONTH)) - 1; | ||
92 | tm->tm_year = BCD2BIN(spi_w8r8(spi, M41T94_REG_YEAR)); | ||
93 | if ((hour & M41T94_BIT_CB) || !(hour & M41T94_BIT_CEB)) | ||
94 | tm->tm_year += 100; | ||
95 | |||
96 | dev_dbg(dev, "%s secs=%d, mins=%d, " | ||
97 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", | ||
98 | "read", tm->tm_sec, tm->tm_min, | ||
99 | tm->tm_hour, tm->tm_mday, | ||
100 | tm->tm_mon, tm->tm_year, tm->tm_wday); | ||
101 | |||
102 | /* initial clock setting can be undefined */ | ||
103 | return rtc_valid_tm(tm); | ||
104 | } | ||
105 | |||
106 | static const struct rtc_class_ops m41t94_rtc_ops = { | ||
107 | .read_time = m41t94_read_time, | ||
108 | .set_time = m41t94_set_time, | ||
109 | }; | ||
110 | |||
111 | static struct spi_driver m41t94_driver; | ||
112 | |||
113 | static int __devinit m41t94_probe(struct spi_device *spi) | ||
114 | { | ||
115 | struct rtc_device *rtc; | ||
116 | int res; | ||
117 | |||
118 | spi->bits_per_word = 8; | ||
119 | spi_setup(spi); | ||
120 | |||
121 | res = spi_w8r8(spi, M41T94_REG_SECONDS); | ||
122 | if (res < 0) { | ||
123 | dev_err(&spi->dev, "not found.\n"); | ||
124 | return res; | ||
125 | } | ||
126 | |||
127 | rtc = rtc_device_register(m41t94_driver.driver.name, | ||
128 | &spi->dev, &m41t94_rtc_ops, THIS_MODULE); | ||
129 | if (IS_ERR(rtc)) | ||
130 | return PTR_ERR(rtc); | ||
131 | |||
132 | dev_set_drvdata(&spi->dev, rtc); | ||
133 | |||
134 | return 0; | ||
135 | } | ||
136 | |||
137 | static int __devexit m41t94_remove(struct spi_device *spi) | ||
138 | { | ||
139 | struct rtc_device *rtc = platform_get_drvdata(spi); | ||
140 | |||
141 | if (rtc) | ||
142 | rtc_device_unregister(rtc); | ||
143 | |||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | static struct spi_driver m41t94_driver = { | ||
148 | .driver = { | ||
149 | .name = "rtc-m41t94", | ||
150 | .bus = &spi_bus_type, | ||
151 | .owner = THIS_MODULE, | ||
152 | }, | ||
153 | .probe = m41t94_probe, | ||
154 | .remove = __devexit_p(m41t94_remove), | ||
155 | }; | ||
156 | |||
157 | static __init int m41t94_init(void) | ||
158 | { | ||
159 | return spi_register_driver(&m41t94_driver); | ||
160 | } | ||
161 | |||
162 | module_init(m41t94_init); | ||
163 | |||
164 | static __exit void m41t94_exit(void) | ||
165 | { | ||
166 | spi_unregister_driver(&m41t94_driver); | ||
167 | } | ||
168 | |||
169 | module_exit(m41t94_exit); | ||
170 | |||
171 | MODULE_AUTHOR("Kim B. Heino <Kim.Heino@bluegiga.com>"); | ||
172 | MODULE_DESCRIPTION("Driver for ST M41T94 SPI RTC"); | ||
173 | MODULE_LICENSE("GPL"); | ||