aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlessandro Zummo <a.zummo@towertech.it>2006-03-27 04:16:47 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-03-27 11:44:52 -0500
commit1d98af87270cc08bb8251e004b9dc63cc838f24b (patch)
tree05098581730851d32b59fed728a7c86e8193677b
parente842f1c8ff8a88f290e26d1139e89aad02c4e0c3 (diff)
[PATCH] RTC subsystem: M48T86 driver
Add a driver for the ST M48T86 / Dallas DS12887 RTC. This is a platform driver. The platform device must provide I/O routines to access the RTC. Signed-off-by: Alessandro Zummo <a.zummo@towertech.it> Cc: Greg KH <greg@kroah.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--drivers/rtc/Kconfig10
-rw-r--r--drivers/rtc/Makefile1
-rw-r--r--drivers/rtc/rtc-m48t86.c209
-rw-r--r--include/linux/m48t86.h16
4 files changed, 236 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 166232a3f56b..929dd8090578 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -117,6 +117,16 @@ config RTC_DRV_RS5C372
117 This driver can also be built as a module. If so, the module 117 This driver can also be built as a module. If so, the module
118 will be called rtc-rs5c372. 118 will be called rtc-rs5c372.
119 119
120config RTC_DRV_M48T86
121 tristate "ST M48T86/Dallas DS12887"
122 depends on RTC_CLASS
123 help
124 If you say Y here you will get support for the
125 ST M48T86 and Dallas DS12887 RTC chips.
126
127 This driver can also be built as a module. If so, the module
128 will be called rtc-m48t86.
129
120config RTC_DRV_EP93XX 130config RTC_DRV_EP93XX
121 tristate "Cirrus Logic EP93XX" 131 tristate "Cirrus Logic EP93XX"
122 depends on RTC_CLASS && ARCH_EP93XX 132 depends on RTC_CLASS && ARCH_EP93XX
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 56820d18161e..8d4c7fe88d58 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -16,5 +16,6 @@ obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
16obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o 16obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o
17obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o 17obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o
18obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o 18obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o
19obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o
19obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o 20obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o
20obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o 21obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o
diff --git a/drivers/rtc/rtc-m48t86.c b/drivers/rtc/rtc-m48t86.c
new file mode 100644
index 000000000000..db445c872b1b
--- /dev/null
+++ b/drivers/rtc/rtc-m48t86.c
@@ -0,0 +1,209 @@
1/*
2 * ST M48T86 / Dallas DS12887 RTC driver
3 * Copyright (c) 2006 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
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 * This drivers only supports the clock running in BCD and 24H mode.
12 * If it will be ever adapted to binary and 12H mode, care must be taken
13 * to not introduce bugs.
14 */
15
16#include <linux/module.h>
17#include <linux/rtc.h>
18#include <linux/platform_device.h>
19#include <linux/m48t86.h>
20#include <linux/bcd.h>
21
22#define M48T86_REG_SEC 0x00
23#define M48T86_REG_SECALRM 0x01
24#define M48T86_REG_MIN 0x02
25#define M48T86_REG_MINALRM 0x03
26#define M48T86_REG_HOUR 0x04
27#define M48T86_REG_HOURALRM 0x05
28#define M48T86_REG_DOW 0x06 /* 1 = sunday */
29#define M48T86_REG_DOM 0x07
30#define M48T86_REG_MONTH 0x08 /* 1 - 12 */
31#define M48T86_REG_YEAR 0x09 /* 0 - 99 */
32#define M48T86_REG_A 0x0A
33#define M48T86_REG_B 0x0B
34#define M48T86_REG_C 0x0C
35#define M48T86_REG_D 0x0D
36
37#define M48T86_REG_B_H24 (1 << 1)
38#define M48T86_REG_B_DM (1 << 2)
39#define M48T86_REG_B_SET (1 << 7)
40#define M48T86_REG_D_VRT (1 << 7)
41
42#define DRV_VERSION "0.1"
43
44
45static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
46{
47 unsigned char reg;
48 struct platform_device *pdev = to_platform_device(dev);
49 struct m48t86_ops *ops = pdev->dev.platform_data;
50
51 reg = ops->readb(M48T86_REG_B);
52
53 if (reg & M48T86_REG_B_DM) {
54 /* data (binary) mode */
55 tm->tm_sec = ops->readb(M48T86_REG_SEC);
56 tm->tm_min = ops->readb(M48T86_REG_MIN);
57 tm->tm_hour = ops->readb(M48T86_REG_HOUR) & 0x3F;
58 tm->tm_mday = ops->readb(M48T86_REG_DOM);
59 /* tm_mon is 0-11 */
60 tm->tm_mon = ops->readb(M48T86_REG_MONTH) - 1;
61 tm->tm_year = ops->readb(M48T86_REG_YEAR) + 100;
62 tm->tm_wday = ops->readb(M48T86_REG_DOW);
63 } else {
64 /* bcd mode */
65 tm->tm_sec = BCD2BIN(ops->readb(M48T86_REG_SEC));
66 tm->tm_min = BCD2BIN(ops->readb(M48T86_REG_MIN));
67 tm->tm_hour = BCD2BIN(ops->readb(M48T86_REG_HOUR) & 0x3F);
68 tm->tm_mday = BCD2BIN(ops->readb(M48T86_REG_DOM));
69 /* tm_mon is 0-11 */
70 tm->tm_mon = BCD2BIN(ops->readb(M48T86_REG_MONTH)) - 1;
71 tm->tm_year = BCD2BIN(ops->readb(M48T86_REG_YEAR)) + 100;
72 tm->tm_wday = BCD2BIN(ops->readb(M48T86_REG_DOW));
73 }
74
75 /* correct the hour if the clock is in 12h mode */
76 if (!(reg & M48T86_REG_B_H24))
77 if (ops->readb(M48T86_REG_HOUR) & 0x80)
78 tm->tm_hour += 12;
79
80 return 0;
81}
82
83static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
84{
85 unsigned char reg;
86 struct platform_device *pdev = to_platform_device(dev);
87 struct m48t86_ops *ops = pdev->dev.platform_data;
88
89 reg = ops->readb(M48T86_REG_B);
90
91 /* update flag and 24h mode */
92 reg |= M48T86_REG_B_SET | M48T86_REG_B_H24;
93 ops->writeb(reg, M48T86_REG_B);
94
95 if (reg & M48T86_REG_B_DM) {
96 /* data (binary) mode */
97 ops->writeb(tm->tm_sec, M48T86_REG_SEC);
98 ops->writeb(tm->tm_min, M48T86_REG_MIN);
99 ops->writeb(tm->tm_hour, M48T86_REG_HOUR);
100 ops->writeb(tm->tm_mday, M48T86_REG_DOM);
101 ops->writeb(tm->tm_mon + 1, M48T86_REG_MONTH);
102 ops->writeb(tm->tm_year % 100, M48T86_REG_YEAR);
103 ops->writeb(tm->tm_wday, M48T86_REG_DOW);
104 } else {
105 /* bcd mode */
106 ops->writeb(BIN2BCD(tm->tm_sec), M48T86_REG_SEC);
107 ops->writeb(BIN2BCD(tm->tm_min), M48T86_REG_MIN);
108 ops->writeb(BIN2BCD(tm->tm_hour), M48T86_REG_HOUR);
109 ops->writeb(BIN2BCD(tm->tm_mday), M48T86_REG_DOM);
110 ops->writeb(BIN2BCD(tm->tm_mon + 1), M48T86_REG_MONTH);
111 ops->writeb(BIN2BCD(tm->tm_year % 100), M48T86_REG_YEAR);
112 ops->writeb(BIN2BCD(tm->tm_wday), M48T86_REG_DOW);
113 }
114
115 /* update ended */
116 reg &= ~M48T86_REG_B_SET;
117 ops->writeb(reg, M48T86_REG_B);
118
119 return 0;
120}
121
122static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
123{
124 unsigned char reg;
125 struct platform_device *pdev = to_platform_device(dev);
126 struct m48t86_ops *ops = pdev->dev.platform_data;
127
128 reg = ops->readb(M48T86_REG_B);
129
130 seq_printf(seq, "24hr\t\t: %s\n",
131 (reg & M48T86_REG_B_H24) ? "yes" : "no");
132
133 seq_printf(seq, "mode\t\t: %s\n",
134 (reg & M48T86_REG_B_DM) ? "binary" : "bcd");
135
136 reg = ops->readb(M48T86_REG_D);
137
138 seq_printf(seq, "battery\t\t: %s\n",
139 (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
140
141 return 0;
142}
143
144static struct rtc_class_ops m48t86_rtc_ops = {
145 .read_time = m48t86_rtc_read_time,
146 .set_time = m48t86_rtc_set_time,
147 .proc = m48t86_rtc_proc,
148};
149
150static int __devinit m48t86_rtc_probe(struct platform_device *dev)
151{
152 unsigned char reg;
153 struct m48t86_ops *ops = dev->dev.platform_data;
154 struct rtc_device *rtc = rtc_device_register("m48t86",
155 &dev->dev, &m48t86_rtc_ops, THIS_MODULE);
156
157 if (IS_ERR(rtc)) {
158 dev_err(&dev->dev, "unable to register\n");
159 return PTR_ERR(rtc);
160 }
161
162 platform_set_drvdata(dev, rtc);
163
164 /* read battery status */
165 reg = ops->readb(M48T86_REG_D);
166 dev_info(&dev->dev, "battery %s\n",
167 (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
168
169 return 0;
170}
171
172static int __devexit m48t86_rtc_remove(struct platform_device *dev)
173{
174 struct rtc_device *rtc = platform_get_drvdata(dev);
175
176 if (rtc)
177 rtc_device_unregister(rtc);
178
179 platform_set_drvdata(dev, NULL);
180
181 return 0;
182}
183
184static struct platform_driver m48t86_rtc_platform_driver = {
185 .driver = {
186 .name = "rtc-m48t86",
187 .owner = THIS_MODULE,
188 },
189 .probe = m48t86_rtc_probe,
190 .remove = __devexit_p(m48t86_rtc_remove),
191};
192
193static int __init m48t86_rtc_init(void)
194{
195 return platform_driver_register(&m48t86_rtc_platform_driver);
196}
197
198static void __exit m48t86_rtc_exit(void)
199{
200 platform_driver_unregister(&m48t86_rtc_platform_driver);
201}
202
203MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
204MODULE_DESCRIPTION("M48T86 RTC driver");
205MODULE_LICENSE("GPL");
206MODULE_VERSION(DRV_VERSION);
207
208module_init(m48t86_rtc_init);
209module_exit(m48t86_rtc_exit);
diff --git a/include/linux/m48t86.h b/include/linux/m48t86.h
new file mode 100644
index 000000000000..9065199319d0
--- /dev/null
+++ b/include/linux/m48t86.h
@@ -0,0 +1,16 @@
1/*
2 * ST M48T86 / Dallas DS12887 RTC driver
3 * Copyright (c) 2006 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
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
12struct m48t86_ops
13{
14 void (*writeb)(unsigned char value, unsigned long addr);
15 unsigned char (*readb)(unsigned long addr);
16};