]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/rtc/rtc-m48t86.c
rtc: m48t86: add NVRAM support
[karo-tx-linux.git] / drivers / rtc / rtc-m48t86.c
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/platform_data/rtc-m48t86.h>
20 #include <linux/bcd.h>
21 #include <linux/io.h>
22
23 #define M48T86_SEC              0x00
24 #define M48T86_SECALRM          0x01
25 #define M48T86_MIN              0x02
26 #define M48T86_MINALRM          0x03
27 #define M48T86_HOUR             0x04
28 #define M48T86_HOURALRM         0x05
29 #define M48T86_DOW              0x06 /* 1 = sunday */
30 #define M48T86_DOM              0x07
31 #define M48T86_MONTH            0x08 /* 1 - 12 */
32 #define M48T86_YEAR             0x09 /* 0 - 99 */
33 #define M48T86_A                0x0a
34 #define M48T86_B                0x0b
35 #define M48T86_B_SET            BIT(7)
36 #define M48T86_B_DM             BIT(2)
37 #define M48T86_B_H24            BIT(1)
38 #define M48T86_C                0x0c
39 #define M48T86_D                0x0d
40 #define M48T86_D_VRT            BIT(7)
41 #define M48T86_NVRAM(x)         (0x0e + (x))
42 #define M48T86_NVRAM_LEN        114
43
44 struct m48t86_rtc_info {
45         void __iomem *index_reg;
46         void __iomem *data_reg;
47         struct rtc_device *rtc;
48         struct m48t86_ops *ops;
49 };
50
51 static unsigned char m48t86_readb(struct device *dev, unsigned long addr)
52 {
53         struct m48t86_rtc_info *info = dev_get_drvdata(dev);
54         unsigned char value;
55
56         if (info->ops) {
57                 value = info->ops->readbyte(addr);
58         } else {
59                 writeb(addr, info->index_reg);
60                 value = readb(info->data_reg);
61         }
62         return value;
63 }
64
65 static void m48t86_writeb(struct device *dev,
66                           unsigned char value, unsigned long addr)
67 {
68         struct m48t86_rtc_info *info = dev_get_drvdata(dev);
69
70         if (info->ops) {
71                 info->ops->writebyte(value, addr);
72         } else {
73                 writeb(addr, info->index_reg);
74                 writeb(value, info->data_reg);
75         }
76 }
77
78 static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
79 {
80         unsigned char reg;
81
82         reg = m48t86_readb(dev, M48T86_B);
83
84         if (reg & M48T86_B_DM) {
85                 /* data (binary) mode */
86                 tm->tm_sec      = m48t86_readb(dev, M48T86_SEC);
87                 tm->tm_min      = m48t86_readb(dev, M48T86_MIN);
88                 tm->tm_hour     = m48t86_readb(dev, M48T86_HOUR) & 0x3f;
89                 tm->tm_mday     = m48t86_readb(dev, M48T86_DOM);
90                 /* tm_mon is 0-11 */
91                 tm->tm_mon      = m48t86_readb(dev, M48T86_MONTH) - 1;
92                 tm->tm_year     = m48t86_readb(dev, M48T86_YEAR) + 100;
93                 tm->tm_wday     = m48t86_readb(dev, M48T86_DOW);
94         } else {
95                 /* bcd mode */
96                 tm->tm_sec      = bcd2bin(m48t86_readb(dev, M48T86_SEC));
97                 tm->tm_min      = bcd2bin(m48t86_readb(dev, M48T86_MIN));
98                 tm->tm_hour     = bcd2bin(m48t86_readb(dev, M48T86_HOUR) &
99                                           0x3f);
100                 tm->tm_mday     = bcd2bin(m48t86_readb(dev, M48T86_DOM));
101                 /* tm_mon is 0-11 */
102                 tm->tm_mon      = bcd2bin(m48t86_readb(dev, M48T86_MONTH)) - 1;
103                 tm->tm_year     = bcd2bin(m48t86_readb(dev, M48T86_YEAR)) + 100;
104                 tm->tm_wday     = bcd2bin(m48t86_readb(dev, M48T86_DOW));
105         }
106
107         /* correct the hour if the clock is in 12h mode */
108         if (!(reg & M48T86_B_H24))
109                 if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
110                         tm->tm_hour += 12;
111
112         return rtc_valid_tm(tm);
113 }
114
115 static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
116 {
117         unsigned char reg;
118
119         reg = m48t86_readb(dev, M48T86_B);
120
121         /* update flag and 24h mode */
122         reg |= M48T86_B_SET | M48T86_B_H24;
123         m48t86_writeb(dev, reg, M48T86_B);
124
125         if (reg & M48T86_B_DM) {
126                 /* data (binary) mode */
127                 m48t86_writeb(dev, tm->tm_sec, M48T86_SEC);
128                 m48t86_writeb(dev, tm->tm_min, M48T86_MIN);
129                 m48t86_writeb(dev, tm->tm_hour, M48T86_HOUR);
130                 m48t86_writeb(dev, tm->tm_mday, M48T86_DOM);
131                 m48t86_writeb(dev, tm->tm_mon + 1, M48T86_MONTH);
132                 m48t86_writeb(dev, tm->tm_year % 100, M48T86_YEAR);
133                 m48t86_writeb(dev, tm->tm_wday, M48T86_DOW);
134         } else {
135                 /* bcd mode */
136                 m48t86_writeb(dev, bin2bcd(tm->tm_sec), M48T86_SEC);
137                 m48t86_writeb(dev, bin2bcd(tm->tm_min), M48T86_MIN);
138                 m48t86_writeb(dev, bin2bcd(tm->tm_hour), M48T86_HOUR);
139                 m48t86_writeb(dev, bin2bcd(tm->tm_mday), M48T86_DOM);
140                 m48t86_writeb(dev, bin2bcd(tm->tm_mon + 1), M48T86_MONTH);
141                 m48t86_writeb(dev, bin2bcd(tm->tm_year % 100), M48T86_YEAR);
142                 m48t86_writeb(dev, bin2bcd(tm->tm_wday), M48T86_DOW);
143         }
144
145         /* update ended */
146         reg &= ~M48T86_B_SET;
147         m48t86_writeb(dev, reg, M48T86_B);
148
149         return 0;
150 }
151
152 static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
153 {
154         unsigned char reg;
155
156         reg = m48t86_readb(dev, M48T86_B);
157
158         seq_printf(seq, "mode\t\t: %s\n",
159                    (reg & M48T86_B_DM) ? "binary" : "bcd");
160
161         reg = m48t86_readb(dev, M48T86_D);
162
163         seq_printf(seq, "battery\t\t: %s\n",
164                    (reg & M48T86_D_VRT) ? "ok" : "exhausted");
165
166         return 0;
167 }
168
169 static const struct rtc_class_ops m48t86_rtc_ops = {
170         .read_time      = m48t86_rtc_read_time,
171         .set_time       = m48t86_rtc_set_time,
172         .proc           = m48t86_rtc_proc,
173 };
174
175 static ssize_t m48t86_nvram_read(struct file *filp, struct kobject *kobj,
176                                  struct bin_attribute *attr,
177                                  char *buf, loff_t off, size_t count)
178 {
179         struct device *dev = kobj_to_dev(kobj);
180         unsigned int i;
181
182         for (i = 0; i < count; i++)
183                 buf[i] = m48t86_readb(dev, M48T86_NVRAM(off + i));
184
185         return count;
186 }
187
188 static ssize_t m48t86_nvram_write(struct file *filp, struct kobject *kobj,
189                                   struct bin_attribute *attr,
190                                   char *buf, loff_t off, size_t count)
191 {
192         struct device *dev = kobj_to_dev(kobj);
193         unsigned int i;
194
195         for (i = 0; i < count; i++)
196                 m48t86_writeb(dev, buf[i], M48T86_NVRAM(off + i));
197
198         return count;
199 }
200
201 static BIN_ATTR(nvram, 0644, m48t86_nvram_read, m48t86_nvram_write,
202                 M48T86_NVRAM_LEN);
203
204 static int m48t86_rtc_probe(struct platform_device *pdev)
205 {
206         struct m48t86_rtc_info *info;
207         unsigned char reg;
208
209         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
210         if (!info)
211                 return -ENOMEM;
212
213         info->ops = dev_get_platdata(&pdev->dev);
214         if (!info->ops) {
215                 struct resource *res;
216
217                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218                 if (!res)
219                         return -ENODEV;
220                 info->index_reg = devm_ioremap_resource(&pdev->dev, res);
221                 if (IS_ERR(info->index_reg))
222                         return PTR_ERR(info->index_reg);
223
224                 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
225                 if (!res)
226                         return -ENODEV;
227                 info->data_reg = devm_ioremap_resource(&pdev->dev, res);
228                 if (IS_ERR(info->data_reg))
229                         return PTR_ERR(info->data_reg);
230         }
231
232         dev_set_drvdata(&pdev->dev, info);
233
234         info->rtc = devm_rtc_device_register(&pdev->dev, "m48t86",
235                                              &m48t86_rtc_ops, THIS_MODULE);
236         if (IS_ERR(info->rtc))
237                 return PTR_ERR(info->rtc);
238
239         /* read battery status */
240         reg = m48t86_readb(&pdev->dev, M48T86_D);
241         dev_info(&pdev->dev, "battery %s\n",
242                  (reg & M48T86_D_VRT) ? "ok" : "exhausted");
243
244         if (device_create_bin_file(&pdev->dev, &bin_attr_nvram))
245                 dev_err(&pdev->dev, "failed to create nvram sysfs entry\n");
246
247         return 0;
248 }
249
250 static int m48t86_rtc_remove(struct platform_device *pdev)
251 {
252         device_remove_bin_file(&pdev->dev, &bin_attr_nvram);
253         return 0;
254 }
255
256 static struct platform_driver m48t86_rtc_platform_driver = {
257         .driver         = {
258                 .name   = "rtc-m48t86",
259         },
260         .probe          = m48t86_rtc_probe,
261         .remove         = m48t86_rtc_remove,
262 };
263
264 module_platform_driver(m48t86_rtc_platform_driver);
265
266 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
267 MODULE_DESCRIPTION("M48T86 RTC driver");
268 MODULE_LICENSE("GPL");
269 MODULE_ALIAS("platform:rtc-m48t86");