blob: 02af045305dd3ce156a7f60b14cc153e548ce60c [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>
Alessandro Zummo1d98af82006-03-27 01:16:47 -080019#include <linux/bcd.h>
H Hartley Sweeten8057c862017-02-10 11:11:56 -070020#include <linux/io.h>
Alessandro Zummo1d98af82006-03-27 01:16:47 -080021
H Hartley Sweeten68b54f42017-02-10 11:11:55 -070022#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)
H Hartley Sweetenb180cf82017-02-10 11:11:57 -070040#define M48T86_NVRAM(x) (0x0e + (x))
41#define M48T86_NVRAM_LEN 114
Alessandro Zummo1d98af82006-03-27 01:16:47 -080042
H Hartley Sweeten8057c862017-02-10 11:11:56 -070043struct m48t86_rtc_info {
44 void __iomem *index_reg;
45 void __iomem *data_reg;
46 struct rtc_device *rtc;
H Hartley Sweeten8057c862017-02-10 11:11:56 -070047};
48
49static unsigned char m48t86_readb(struct device *dev, unsigned long addr)
50{
51 struct m48t86_rtc_info *info = dev_get_drvdata(dev);
52 unsigned char value;
53
H Hartley Sweeten0500ce52017-02-15 09:35:27 -070054 writeb(addr, info->index_reg);
55 value = readb(info->data_reg);
56
H Hartley Sweeten8057c862017-02-10 11:11:56 -070057 return value;
58}
59
60static void m48t86_writeb(struct device *dev,
61 unsigned char value, unsigned long addr)
62{
63 struct m48t86_rtc_info *info = dev_get_drvdata(dev);
64
H Hartley Sweeten0500ce52017-02-15 09:35:27 -070065 writeb(addr, info->index_reg);
66 writeb(value, info->data_reg);
H Hartley Sweeten8057c862017-02-10 11:11:56 -070067}
68
Alessandro Zummo1d98af82006-03-27 01:16:47 -080069static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
70{
71 unsigned char reg;
Alessandro Zummo1d98af82006-03-27 01:16:47 -080072
H Hartley Sweeten8057c862017-02-10 11:11:56 -070073 reg = m48t86_readb(dev, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080074
H Hartley Sweeten68b54f42017-02-10 11:11:55 -070075 if (reg & M48T86_B_DM) {
Alessandro Zummo1d98af82006-03-27 01:16:47 -080076 /* data (binary) mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070077 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080081 /* tm_mon is 0-11 */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070082 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -080085 } else {
86 /* bcd mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070087 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));
Alessandro Zummo1d98af82006-03-27 01:16:47 -080092 /* tm_mon is 0-11 */
H Hartley Sweeten8057c862017-02-10 11:11:56 -070093 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));
Alessandro Zummo1d98af82006-03-27 01:16:47 -080096 }
97
98 /* correct the hour if the clock is in 12h mode */
H Hartley Sweeten68b54f42017-02-10 11:11:55 -070099 if (!(reg & M48T86_B_H24))
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700100 if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800101 tm->tm_hour += 12;
102
Wan ZongShun52142ed2010-08-10 18:02:17 -0700103 return rtc_valid_tm(tm);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800104}
105
106static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
107{
108 unsigned char reg;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800109
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700110 reg = m48t86_readb(dev, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800111
112 /* update flag and 24h mode */
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700113 reg |= M48T86_B_SET | M48T86_B_H24;
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700114 m48t86_writeb(dev, reg, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800115
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700116 if (reg & M48T86_B_DM) {
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800117 /* data (binary) mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700118 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800125 } else {
126 /* bcd mode */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700127 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800134 }
135
136 /* update ended */
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700137 reg &= ~M48T86_B_SET;
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700138 m48t86_writeb(dev, reg, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800139
140 return 0;
141}
142
143static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
144{
145 unsigned char reg;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800146
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700147 reg = m48t86_readb(dev, M48T86_B);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800148
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800149 seq_printf(seq, "mode\t\t: %s\n",
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700150 (reg & M48T86_B_DM) ? "binary" : "bcd");
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800151
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700152 reg = m48t86_readb(dev, M48T86_D);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800153
154 seq_printf(seq, "battery\t\t: %s\n",
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700155 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800156
157 return 0;
158}
159
David Brownellff8371a2006-09-30 23:28:17 -0700160static const struct rtc_class_ops m48t86_rtc_ops = {
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800161 .read_time = m48t86_rtc_read_time,
162 .set_time = m48t86_rtc_set_time,
163 .proc = m48t86_rtc_proc,
164};
165
H Hartley Sweetenb180cf82017-02-10 11:11:57 -0700166static 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)
169{
170 struct device *dev = kobj_to_dev(kobj);
171 unsigned int i;
172
173 for (i = 0; i < count; i++)
174 buf[i] = m48t86_readb(dev, M48T86_NVRAM(off + i));
175
176 return count;
177}
178
179static 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)
182{
183 struct device *dev = kobj_to_dev(kobj);
184 unsigned int i;
185
186 for (i = 0; i < count; i++)
187 m48t86_writeb(dev, buf[i], M48T86_NVRAM(off + i));
188
189 return count;
190}
191
192static BIN_ATTR(nvram, 0644, m48t86_nvram_read, m48t86_nvram_write,
193 M48T86_NVRAM_LEN);
194
H Hartley Sweeten3ea07122017-02-15 09:35:23 -0700195/*
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.
199 *
200 * This is based on the method used in their rtc7800.c example.
201 */
202static bool m48t86_verify_chip(struct platform_device *pdev)
203{
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;
207
208 tmp0 = m48t86_readb(&pdev->dev, offset0);
209 tmp1 = m48t86_readb(&pdev->dev, offset1);
210
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);
219
220 return true;
221 }
222 }
223 return false;
224}
225
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700226static int m48t86_rtc_probe(struct platform_device *pdev)
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800227{
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700228 struct m48t86_rtc_info *info;
H Hartley Sweeten0500ce52017-02-15 09:35:27 -0700229 struct resource *res;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800230 unsigned char reg;
Jingoo Hand5b6bb02013-04-29 16:19:42 -0700231
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700232 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
233 if (!info)
234 return -ENOMEM;
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800235
H Hartley Sweeten0500ce52017-02-15 09:35:27 -0700236 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800242
H Hartley Sweeten0500ce52017-02-15 09:35:27 -0700243 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);
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700249
250 dev_set_drvdata(&pdev->dev, info);
251
H Hartley Sweeten3ea07122017-02-15 09:35:23 -0700252 if (!m48t86_verify_chip(pdev)) {
253 dev_info(&pdev->dev, "RTC not present\n");
254 return -ENODEV;
255 }
256
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700257 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);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800261
262 /* read battery status */
H Hartley Sweeten8057c862017-02-10 11:11:56 -0700263 reg = m48t86_readb(&pdev->dev, M48T86_D);
264 dev_info(&pdev->dev, "battery %s\n",
H Hartley Sweeten68b54f42017-02-10 11:11:55 -0700265 (reg & M48T86_D_VRT) ? "ok" : "exhausted");
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800266
H Hartley Sweetenb180cf82017-02-10 11:11:57 -0700267 if (device_create_bin_file(&pdev->dev, &bin_attr_nvram))
268 dev_err(&pdev->dev, "failed to create nvram sysfs entry\n");
269
270 return 0;
271}
272
273static int m48t86_rtc_remove(struct platform_device *pdev)
274{
275 device_remove_bin_file(&pdev->dev, &bin_attr_nvram);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800276 return 0;
277}
278
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800279static struct platform_driver m48t86_rtc_platform_driver = {
280 .driver = {
281 .name = "rtc-m48t86",
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800282 },
283 .probe = m48t86_rtc_probe,
H Hartley Sweetenb180cf82017-02-10 11:11:57 -0700284 .remove = m48t86_rtc_remove,
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800285};
286
Axel Lin0c4eae62012-01-10 15:10:48 -0800287module_platform_driver(m48t86_rtc_platform_driver);
Alessandro Zummo1d98af82006-03-27 01:16:47 -0800288
289MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
290MODULE_DESCRIPTION("M48T86 RTC driver");
291MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700292MODULE_ALIAS("platform:rtc-m48t86");