blob: 491e8e4b300b05b7e6c466545750b22522907b41 [file] [log] [blame]
Alessandro Zummo1d98af82006-03-27 01:16:47 -08001/*
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>
Alexandre Belloni803bb302016-06-26 22:57:52 +020019#include <linux/platform_data/rtc-m48t86.h>
Alessandro Zummo1d98af82006-03-27 01:16:47 -080020#include <linux/bcd.h>
H Hartley Sweeten8057c862017-02-10 11:11:56 -070021#include <linux/io.h>
Alessandro Zummo1d98af82006-03-27 01:16:47 -080022
H Hartley Sweeten68b54f42017-02-10 11:11:55 -070023#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)
H Hartley Sweetenb180cf82017-02-10 11:11:57 -070041#define M48T86_NVRAM(x) (0x0e + (x))
42#define M48T86_NVRAM_LEN 114
Alessandro Zummo1d98af82006-03-27 01:16:47 -080043
H Hartley Sweeten8057c862017-02-10 11:11:56 -070044struct 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
51static 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
65static 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
Alessandro Zummo1d98af82006-03-27 01:16:47 -080078static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
79{
80 unsigned char reg;
Alessandro Zummo1d98af82006-03-27 01:16:47 -080081
H Hartley Sweeten8057c862017-02-10 11:11:56 -070082 reg = m48t86_readb(dev, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080083
H Hartley Sweeten68b54f42017-02-10 11:11:55 -070084 if (reg & M48T86_B_DM) {
Alessandro Zummo1d98af82006-03-27 01:16:47 -080085 /* data (binary) mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070086 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080090 /* tm_mon is 0-11 */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070091 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080094 } else {
95 /* bcd mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070096 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));
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800101 /* tm_mon is 0-11 */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700102 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));
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800105 }
106
107 /* correct the hour if the clock is in 12h mode */
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700108 if (!(reg & M48T86_B_H24))
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700109 if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800110 tm->tm_hour += 12;
111
Wan ZongShun52142ed2010-08-10 18:02:17 -0700112 return rtc_valid_tm(tm);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800113}
114
115static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
116{
117 unsigned char reg;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800118
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700119 reg = m48t86_readb(dev, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800120
121 /* update flag and 24h mode */
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700122 reg |= M48T86_B_SET | M48T86_B_H24;
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700123 m48t86_writeb(dev, reg, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800124
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700125 if (reg & M48T86_B_DM) {
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800126 /* data (binary) mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700127 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800134 } else {
135 /* bcd mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700136 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800143 }
144
145 /* update ended */
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700146 reg &= ~M48T86_B_SET;
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700147 m48t86_writeb(dev, reg, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800148
149 return 0;
150}
151
152static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
153{
154 unsigned char reg;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800155
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700156 reg = m48t86_readb(dev, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800157
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800158 seq_printf(seq, "mode\t\t: %s\n",
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700159 (reg & M48T86_B_DM) ? "binary" : "bcd");
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800160
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700161 reg = m48t86_readb(dev, M48T86_D);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800162
163 seq_printf(seq, "battery\t\t: %s\n",
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700164 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800165
166 return 0;
167}
168
David Brownellff8371a2006-09-30 23:28:17 -0700169static const struct rtc_class_ops m48t86_rtc_ops = {
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800170 .read_time = m48t86_rtc_read_time,
171 .set_time = m48t86_rtc_set_time,
172 .proc = m48t86_rtc_proc,
173};
174
H Hartley Sweetenb180cf82017-02-10 11:11:57 -0700175static 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
188static 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
201static BIN_ATTR(nvram, 0644, m48t86_nvram_read, m48t86_nvram_write,
202 M48T86_NVRAM_LEN);
203
H Hartley Sweeten3ea07122017-02-15 09:35:23 -0700204/*
205 * The RTC is an optional feature at purchase time on some Technologic Systems
206 * boards. Verify that it actually exists by checking if the last two bytes
207 * of the NVRAM can be changed.
208 *
209 * This is based on the method used in their rtc7800.c example.
210 */
211static bool m48t86_verify_chip(struct platform_device *pdev)
212{
213 unsigned int offset0 = M48T86_NVRAM(M48T86_NVRAM_LEN - 2);
214 unsigned int offset1 = M48T86_NVRAM(M48T86_NVRAM_LEN - 1);
215 unsigned char tmp0, tmp1;
216
217 tmp0 = m48t86_readb(&pdev->dev, offset0);
218 tmp1 = m48t86_readb(&pdev->dev, offset1);
219
220 m48t86_writeb(&pdev->dev, 0x00, offset0);
221 m48t86_writeb(&pdev->dev, 0x55, offset1);
222 if (m48t86_readb(&pdev->dev, offset1) == 0x55) {
223 m48t86_writeb(&pdev->dev, 0xaa, offset1);
224 if (m48t86_readb(&pdev->dev, offset1) == 0xaa &&
225 m48t86_readb(&pdev->dev, offset0) == 0x00) {
226 m48t86_writeb(&pdev->dev, tmp0, offset0);
227 m48t86_writeb(&pdev->dev, tmp1, offset1);
228
229 return true;
230 }
231 }
232 return false;
233}
234
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700235static int m48t86_rtc_probe(struct platform_device *pdev)
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800236{
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700237 struct m48t86_rtc_info *info;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800238 unsigned char reg;
Jingoo Hand5b6bb02013-04-29 16:19:42 -0700239
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700240 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
241 if (!info)
242 return -ENOMEM;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800243
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700244 info->ops = dev_get_platdata(&pdev->dev);
245 if (!info->ops) {
246 struct resource *res;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800247
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700248 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249 if (!res)
250 return -ENODEV;
251 info->index_reg = devm_ioremap_resource(&pdev->dev, res);
252 if (IS_ERR(info->index_reg))
253 return PTR_ERR(info->index_reg);
254
255 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
256 if (!res)
257 return -ENODEV;
258 info->data_reg = devm_ioremap_resource(&pdev->dev, res);
259 if (IS_ERR(info->data_reg))
260 return PTR_ERR(info->data_reg);
261 }
262
263 dev_set_drvdata(&pdev->dev, info);
264
H Hartley Sweeten3ea07122017-02-15 09:35:23 -0700265 if (!m48t86_verify_chip(pdev)) {
266 dev_info(&pdev->dev, "RTC not present\n");
267 return -ENODEV;
268 }
269
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700270 info->rtc = devm_rtc_device_register(&pdev->dev, "m48t86",
271 &m48t86_rtc_ops, THIS_MODULE);
272 if (IS_ERR(info->rtc))
273 return PTR_ERR(info->rtc);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800274
275 /* read battery status */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700276 reg = m48t86_readb(&pdev->dev, M48T86_D);
277 dev_info(&pdev->dev, "battery %s\n",
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700278 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800279
H Hartley Sweetenb180cf82017-02-10 11:11:57 -0700280 if (device_create_bin_file(&pdev->dev, &bin_attr_nvram))
281 dev_err(&pdev->dev, "failed to create nvram sysfs entry\n");
282
283 return 0;
284}
285
286static int m48t86_rtc_remove(struct platform_device *pdev)
287{
288 device_remove_bin_file(&pdev->dev, &bin_attr_nvram);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800289 return 0;
290}
291
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800292static struct platform_driver m48t86_rtc_platform_driver = {
293 .driver = {
294 .name = "rtc-m48t86",
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800295 },
296 .probe = m48t86_rtc_probe,
H Hartley Sweetenb180cf82017-02-10 11:11:57 -0700297 .remove = m48t86_rtc_remove,
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800298};
299
Axel Lin0c4eae62012-01-10 15:10:48 -0800300module_platform_driver(m48t86_rtc_platform_driver);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800301
302MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
303MODULE_DESCRIPTION("M48T86 RTC driver");
304MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700305MODULE_ALIAS("platform:rtc-m48t86");