Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / rtc / rtc-m48t86.c
blob8c0d1a6739adb23012a1c92bf9a102898b9a94cf
1 /*
2 * ST M48T86 / Dallas DS12887 RTC driver
3 * Copyright (c) 2006 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
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.
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.
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>
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
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)
42 #define DRV_VERSION "0.1"
45 static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
47 unsigned char reg;
48 struct platform_device *pdev = to_platform_device(dev);
49 struct m48t86_ops *ops = pdev->dev.platform_data;
51 reg = ops->readbyte(M48T86_REG_B);
53 if (reg & M48T86_REG_B_DM) {
54 /* data (binary) mode */
55 tm->tm_sec = ops->readbyte(M48T86_REG_SEC);
56 tm->tm_min = ops->readbyte(M48T86_REG_MIN);
57 tm->tm_hour = ops->readbyte(M48T86_REG_HOUR) & 0x3F;
58 tm->tm_mday = ops->readbyte(M48T86_REG_DOM);
59 /* tm_mon is 0-11 */
60 tm->tm_mon = ops->readbyte(M48T86_REG_MONTH) - 1;
61 tm->tm_year = ops->readbyte(M48T86_REG_YEAR) + 100;
62 tm->tm_wday = ops->readbyte(M48T86_REG_DOW);
63 } else {
64 /* bcd mode */
65 tm->tm_sec = BCD2BIN(ops->readbyte(M48T86_REG_SEC));
66 tm->tm_min = BCD2BIN(ops->readbyte(M48T86_REG_MIN));
67 tm->tm_hour = BCD2BIN(ops->readbyte(M48T86_REG_HOUR) & 0x3F);
68 tm->tm_mday = BCD2BIN(ops->readbyte(M48T86_REG_DOM));
69 /* tm_mon is 0-11 */
70 tm->tm_mon = BCD2BIN(ops->readbyte(M48T86_REG_MONTH)) - 1;
71 tm->tm_year = BCD2BIN(ops->readbyte(M48T86_REG_YEAR)) + 100;
72 tm->tm_wday = BCD2BIN(ops->readbyte(M48T86_REG_DOW));
75 /* correct the hour if the clock is in 12h mode */
76 if (!(reg & M48T86_REG_B_H24))
77 if (ops->readbyte(M48T86_REG_HOUR) & 0x80)
78 tm->tm_hour += 12;
80 return 0;
83 static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
85 unsigned char reg;
86 struct platform_device *pdev = to_platform_device(dev);
87 struct m48t86_ops *ops = pdev->dev.platform_data;
89 reg = ops->readbyte(M48T86_REG_B);
91 /* update flag and 24h mode */
92 reg |= M48T86_REG_B_SET | M48T86_REG_B_H24;
93 ops->writebyte(reg, M48T86_REG_B);
95 if (reg & M48T86_REG_B_DM) {
96 /* data (binary) mode */
97 ops->writebyte(tm->tm_sec, M48T86_REG_SEC);
98 ops->writebyte(tm->tm_min, M48T86_REG_MIN);
99 ops->writebyte(tm->tm_hour, M48T86_REG_HOUR);
100 ops->writebyte(tm->tm_mday, M48T86_REG_DOM);
101 ops->writebyte(tm->tm_mon + 1, M48T86_REG_MONTH);
102 ops->writebyte(tm->tm_year % 100, M48T86_REG_YEAR);
103 ops->writebyte(tm->tm_wday, M48T86_REG_DOW);
104 } else {
105 /* bcd mode */
106 ops->writebyte(BIN2BCD(tm->tm_sec), M48T86_REG_SEC);
107 ops->writebyte(BIN2BCD(tm->tm_min), M48T86_REG_MIN);
108 ops->writebyte(BIN2BCD(tm->tm_hour), M48T86_REG_HOUR);
109 ops->writebyte(BIN2BCD(tm->tm_mday), M48T86_REG_DOM);
110 ops->writebyte(BIN2BCD(tm->tm_mon + 1), M48T86_REG_MONTH);
111 ops->writebyte(BIN2BCD(tm->tm_year % 100), M48T86_REG_YEAR);
112 ops->writebyte(BIN2BCD(tm->tm_wday), M48T86_REG_DOW);
115 /* update ended */
116 reg &= ~M48T86_REG_B_SET;
117 ops->writebyte(reg, M48T86_REG_B);
119 return 0;
122 static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
124 unsigned char reg;
125 struct platform_device *pdev = to_platform_device(dev);
126 struct m48t86_ops *ops = pdev->dev.platform_data;
128 reg = ops->readbyte(M48T86_REG_B);
130 seq_printf(seq, "mode\t\t: %s\n",
131 (reg & M48T86_REG_B_DM) ? "binary" : "bcd");
133 reg = ops->readbyte(M48T86_REG_D);
135 seq_printf(seq, "battery\t\t: %s\n",
136 (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
138 return 0;
141 static struct rtc_class_ops m48t86_rtc_ops = {
142 .read_time = m48t86_rtc_read_time,
143 .set_time = m48t86_rtc_set_time,
144 .proc = m48t86_rtc_proc,
147 static int __devinit m48t86_rtc_probe(struct platform_device *dev)
149 unsigned char reg;
150 struct m48t86_ops *ops = dev->dev.platform_data;
151 struct rtc_device *rtc = rtc_device_register("m48t86",
152 &dev->dev, &m48t86_rtc_ops, THIS_MODULE);
154 if (IS_ERR(rtc))
155 return PTR_ERR(rtc);
157 platform_set_drvdata(dev, rtc);
159 /* read battery status */
160 reg = ops->readbyte(M48T86_REG_D);
161 dev_info(&dev->dev, "battery %s\n",
162 (reg & M48T86_REG_D_VRT) ? "ok" : "exhausted");
164 return 0;
167 static int __devexit m48t86_rtc_remove(struct platform_device *dev)
169 struct rtc_device *rtc = platform_get_drvdata(dev);
171 if (rtc)
172 rtc_device_unregister(rtc);
174 platform_set_drvdata(dev, NULL);
176 return 0;
179 static struct platform_driver m48t86_rtc_platform_driver = {
180 .driver = {
181 .name = "rtc-m48t86",
182 .owner = THIS_MODULE,
184 .probe = m48t86_rtc_probe,
185 .remove = __devexit_p(m48t86_rtc_remove),
188 static int __init m48t86_rtc_init(void)
190 return platform_driver_register(&m48t86_rtc_platform_driver);
193 static void __exit m48t86_rtc_exit(void)
195 platform_driver_unregister(&m48t86_rtc_platform_driver);
198 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
199 MODULE_DESCRIPTION("M48T86 RTC driver");
200 MODULE_LICENSE("GPL");
201 MODULE_VERSION(DRV_VERSION);
203 module_init(m48t86_rtc_init);
204 module_exit(m48t86_rtc_exit);