hwmon: (adt7475) fan stall prevention
[linux/fpc-iii.git] / drivers / rtc / rtc-m48t86.c
blob02af045305dd3ce156a7f60b14cc153e548ce60c
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/bcd.h>
20 #include <linux/io.h>
22 #define M48T86_SEC 0x00
23 #define M48T86_SECALRM 0x01
24 #define M48T86_MIN 0x02
25 #define M48T86_MINALRM 0x03
26 #define M48T86_HOUR 0x04
27 #define M48T86_HOURALRM 0x05
28 #define M48T86_DOW 0x06 /* 1 = sunday */
29 #define M48T86_DOM 0x07
30 #define M48T86_MONTH 0x08 /* 1 - 12 */
31 #define M48T86_YEAR 0x09 /* 0 - 99 */
32 #define M48T86_A 0x0a
33 #define M48T86_B 0x0b
34 #define M48T86_B_SET BIT(7)
35 #define M48T86_B_DM BIT(2)
36 #define M48T86_B_H24 BIT(1)
37 #define M48T86_C 0x0c
38 #define M48T86_D 0x0d
39 #define M48T86_D_VRT BIT(7)
40 #define M48T86_NVRAM(x) (0x0e + (x))
41 #define M48T86_NVRAM_LEN 114
43 struct m48t86_rtc_info {
44 void __iomem *index_reg;
45 void __iomem *data_reg;
46 struct rtc_device *rtc;
49 static unsigned char m48t86_readb(struct device *dev, unsigned long addr)
51 struct m48t86_rtc_info *info = dev_get_drvdata(dev);
52 unsigned char value;
54 writeb(addr, info->index_reg);
55 value = readb(info->data_reg);
57 return value;
60 static void m48t86_writeb(struct device *dev,
61 unsigned char value, unsigned long addr)
63 struct m48t86_rtc_info *info = dev_get_drvdata(dev);
65 writeb(addr, info->index_reg);
66 writeb(value, info->data_reg);
69 static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
71 unsigned char reg;
73 reg = m48t86_readb(dev, M48T86_B);
75 if (reg & M48T86_B_DM) {
76 /* data (binary) mode */
77 tm->tm_sec = m48t86_readb(dev, M48T86_SEC);
78 tm->tm_min = m48t86_readb(dev, M48T86_MIN);
79 tm->tm_hour = m48t86_readb(dev, M48T86_HOUR) & 0x3f;
80 tm->tm_mday = m48t86_readb(dev, M48T86_DOM);
81 /* tm_mon is 0-11 */
82 tm->tm_mon = m48t86_readb(dev, M48T86_MONTH) - 1;
83 tm->tm_year = m48t86_readb(dev, M48T86_YEAR) + 100;
84 tm->tm_wday = m48t86_readb(dev, M48T86_DOW);
85 } else {
86 /* bcd mode */
87 tm->tm_sec = bcd2bin(m48t86_readb(dev, M48T86_SEC));
88 tm->tm_min = bcd2bin(m48t86_readb(dev, M48T86_MIN));
89 tm->tm_hour = bcd2bin(m48t86_readb(dev, M48T86_HOUR) &
90 0x3f);
91 tm->tm_mday = bcd2bin(m48t86_readb(dev, M48T86_DOM));
92 /* tm_mon is 0-11 */
93 tm->tm_mon = bcd2bin(m48t86_readb(dev, M48T86_MONTH)) - 1;
94 tm->tm_year = bcd2bin(m48t86_readb(dev, M48T86_YEAR)) + 100;
95 tm->tm_wday = bcd2bin(m48t86_readb(dev, M48T86_DOW));
98 /* correct the hour if the clock is in 12h mode */
99 if (!(reg & M48T86_B_H24))
100 if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
101 tm->tm_hour += 12;
103 return rtc_valid_tm(tm);
106 static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
108 unsigned char reg;
110 reg = m48t86_readb(dev, M48T86_B);
112 /* update flag and 24h mode */
113 reg |= M48T86_B_SET | M48T86_B_H24;
114 m48t86_writeb(dev, reg, M48T86_B);
116 if (reg & M48T86_B_DM) {
117 /* data (binary) mode */
118 m48t86_writeb(dev, tm->tm_sec, M48T86_SEC);
119 m48t86_writeb(dev, tm->tm_min, M48T86_MIN);
120 m48t86_writeb(dev, tm->tm_hour, M48T86_HOUR);
121 m48t86_writeb(dev, tm->tm_mday, M48T86_DOM);
122 m48t86_writeb(dev, tm->tm_mon + 1, M48T86_MONTH);
123 m48t86_writeb(dev, tm->tm_year % 100, M48T86_YEAR);
124 m48t86_writeb(dev, tm->tm_wday, M48T86_DOW);
125 } else {
126 /* bcd mode */
127 m48t86_writeb(dev, bin2bcd(tm->tm_sec), M48T86_SEC);
128 m48t86_writeb(dev, bin2bcd(tm->tm_min), M48T86_MIN);
129 m48t86_writeb(dev, bin2bcd(tm->tm_hour), M48T86_HOUR);
130 m48t86_writeb(dev, bin2bcd(tm->tm_mday), M48T86_DOM);
131 m48t86_writeb(dev, bin2bcd(tm->tm_mon + 1), M48T86_MONTH);
132 m48t86_writeb(dev, bin2bcd(tm->tm_year % 100), M48T86_YEAR);
133 m48t86_writeb(dev, bin2bcd(tm->tm_wday), M48T86_DOW);
136 /* update ended */
137 reg &= ~M48T86_B_SET;
138 m48t86_writeb(dev, reg, M48T86_B);
140 return 0;
143 static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
145 unsigned char reg;
147 reg = m48t86_readb(dev, M48T86_B);
149 seq_printf(seq, "mode\t\t: %s\n",
150 (reg & M48T86_B_DM) ? "binary" : "bcd");
152 reg = m48t86_readb(dev, M48T86_D);
154 seq_printf(seq, "battery\t\t: %s\n",
155 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
157 return 0;
160 static const struct rtc_class_ops m48t86_rtc_ops = {
161 .read_time = m48t86_rtc_read_time,
162 .set_time = m48t86_rtc_set_time,
163 .proc = m48t86_rtc_proc,
166 static ssize_t m48t86_nvram_read(struct file *filp, struct kobject *kobj,
167 struct bin_attribute *attr,
168 char *buf, loff_t off, size_t count)
170 struct device *dev = kobj_to_dev(kobj);
171 unsigned int i;
173 for (i = 0; i < count; i++)
174 buf[i] = m48t86_readb(dev, M48T86_NVRAM(off + i));
176 return count;
179 static ssize_t m48t86_nvram_write(struct file *filp, struct kobject *kobj,
180 struct bin_attribute *attr,
181 char *buf, loff_t off, size_t count)
183 struct device *dev = kobj_to_dev(kobj);
184 unsigned int i;
186 for (i = 0; i < count; i++)
187 m48t86_writeb(dev, buf[i], M48T86_NVRAM(off + i));
189 return count;
192 static BIN_ATTR(nvram, 0644, m48t86_nvram_read, m48t86_nvram_write,
193 M48T86_NVRAM_LEN);
196 * The RTC is an optional feature at purchase time on some Technologic Systems
197 * boards. Verify that it actually exists by checking if the last two bytes
198 * of the NVRAM can be changed.
200 * This is based on the method used in their rtc7800.c example.
202 static bool m48t86_verify_chip(struct platform_device *pdev)
204 unsigned int offset0 = M48T86_NVRAM(M48T86_NVRAM_LEN - 2);
205 unsigned int offset1 = M48T86_NVRAM(M48T86_NVRAM_LEN - 1);
206 unsigned char tmp0, tmp1;
208 tmp0 = m48t86_readb(&pdev->dev, offset0);
209 tmp1 = m48t86_readb(&pdev->dev, offset1);
211 m48t86_writeb(&pdev->dev, 0x00, offset0);
212 m48t86_writeb(&pdev->dev, 0x55, offset1);
213 if (m48t86_readb(&pdev->dev, offset1) == 0x55) {
214 m48t86_writeb(&pdev->dev, 0xaa, offset1);
215 if (m48t86_readb(&pdev->dev, offset1) == 0xaa &&
216 m48t86_readb(&pdev->dev, offset0) == 0x00) {
217 m48t86_writeb(&pdev->dev, tmp0, offset0);
218 m48t86_writeb(&pdev->dev, tmp1, offset1);
220 return true;
223 return false;
226 static int m48t86_rtc_probe(struct platform_device *pdev)
228 struct m48t86_rtc_info *info;
229 struct resource *res;
230 unsigned char reg;
232 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
233 if (!info)
234 return -ENOMEM;
236 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
237 if (!res)
238 return -ENODEV;
239 info->index_reg = devm_ioremap_resource(&pdev->dev, res);
240 if (IS_ERR(info->index_reg))
241 return PTR_ERR(info->index_reg);
243 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
244 if (!res)
245 return -ENODEV;
246 info->data_reg = devm_ioremap_resource(&pdev->dev, res);
247 if (IS_ERR(info->data_reg))
248 return PTR_ERR(info->data_reg);
250 dev_set_drvdata(&pdev->dev, info);
252 if (!m48t86_verify_chip(pdev)) {
253 dev_info(&pdev->dev, "RTC not present\n");
254 return -ENODEV;
257 info->rtc = devm_rtc_device_register(&pdev->dev, "m48t86",
258 &m48t86_rtc_ops, THIS_MODULE);
259 if (IS_ERR(info->rtc))
260 return PTR_ERR(info->rtc);
262 /* read battery status */
263 reg = m48t86_readb(&pdev->dev, M48T86_D);
264 dev_info(&pdev->dev, "battery %s\n",
265 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
267 if (device_create_bin_file(&pdev->dev, &bin_attr_nvram))
268 dev_err(&pdev->dev, "failed to create nvram sysfs entry\n");
270 return 0;
273 static int m48t86_rtc_remove(struct platform_device *pdev)
275 device_remove_bin_file(&pdev->dev, &bin_attr_nvram);
276 return 0;
279 static struct platform_driver m48t86_rtc_platform_driver = {
280 .driver = {
281 .name = "rtc-m48t86",
283 .probe = m48t86_rtc_probe,
284 .remove = m48t86_rtc_remove,
287 module_platform_driver(m48t86_rtc_platform_driver);
289 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
290 MODULE_DESCRIPTION("M48T86 RTC driver");
291 MODULE_LICENSE("GPL");
292 MODULE_ALIAS("platform:rtc-m48t86");