2 * An rtc driver for the Dallas DS1742
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/bcd.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/rtc.h>
17 #include <linux/platform_device.h>
20 #define DRV_VERSION "0.2"
22 #define RTC_REG_SIZE 0x800
23 #define RTC_OFFSET 0x7f8
25 #define RTC_CONTROL (RTC_OFFSET + 0)
26 #define RTC_CENTURY (RTC_OFFSET + 0)
27 #define RTC_SECONDS (RTC_OFFSET + 1)
28 #define RTC_MINUTES (RTC_OFFSET + 2)
29 #define RTC_HOURS (RTC_OFFSET + 3)
30 #define RTC_DAY (RTC_OFFSET + 4)
31 #define RTC_DATE (RTC_OFFSET + 5)
32 #define RTC_MONTH (RTC_OFFSET + 6)
33 #define RTC_YEAR (RTC_OFFSET + 7)
35 #define RTC_CENTURY_MASK 0x3f
36 #define RTC_SECONDS_MASK 0x7f
37 #define RTC_DAY_MASK 0x07
39 /* Bits in the Control/Century register */
40 #define RTC_WRITE 0x80
43 /* Bits in the Seconds register */
46 /* Bits in the Day register */
47 #define RTC_BATT_FLAG 0x80
49 struct rtc_plat_data
{
50 struct rtc_device
*rtc
;
52 unsigned long baseaddr
;
53 unsigned long last_jiffies
;
56 static int ds1742_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
58 struct platform_device
*pdev
= to_platform_device(dev
);
59 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
60 void __iomem
*ioaddr
= pdata
->ioaddr
;
63 century
= BIN2BCD((tm
->tm_year
+ 1900) / 100);
65 writeb(RTC_WRITE
, ioaddr
+ RTC_CONTROL
);
67 writeb(BIN2BCD(tm
->tm_year
% 100), ioaddr
+ RTC_YEAR
);
68 writeb(BIN2BCD(tm
->tm_mon
+ 1), ioaddr
+ RTC_MONTH
);
69 writeb(BIN2BCD(tm
->tm_wday
) & RTC_DAY_MASK
, ioaddr
+ RTC_DAY
);
70 writeb(BIN2BCD(tm
->tm_mday
), ioaddr
+ RTC_DATE
);
71 writeb(BIN2BCD(tm
->tm_hour
), ioaddr
+ RTC_HOURS
);
72 writeb(BIN2BCD(tm
->tm_min
), ioaddr
+ RTC_MINUTES
);
73 writeb(BIN2BCD(tm
->tm_sec
) & RTC_SECONDS_MASK
, ioaddr
+ RTC_SECONDS
);
75 /* RTC_CENTURY and RTC_CONTROL share same register */
76 writeb(RTC_WRITE
| (century
& RTC_CENTURY_MASK
), ioaddr
+ RTC_CENTURY
);
77 writeb(century
& RTC_CENTURY_MASK
, ioaddr
+ RTC_CONTROL
);
81 static int ds1742_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
83 struct platform_device
*pdev
= to_platform_device(dev
);
84 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
85 void __iomem
*ioaddr
= pdata
->ioaddr
;
86 unsigned int year
, month
, day
, hour
, minute
, second
, week
;
89 /* give enough time to update RTC in case of continuous read */
90 if (pdata
->last_jiffies
== jiffies
)
92 pdata
->last_jiffies
= jiffies
;
93 writeb(RTC_READ
, ioaddr
+ RTC_CONTROL
);
94 second
= readb(ioaddr
+ RTC_SECONDS
) & RTC_SECONDS_MASK
;
95 minute
= readb(ioaddr
+ RTC_MINUTES
);
96 hour
= readb(ioaddr
+ RTC_HOURS
);
97 day
= readb(ioaddr
+ RTC_DATE
);
98 week
= readb(ioaddr
+ RTC_DAY
) & RTC_DAY_MASK
;
99 month
= readb(ioaddr
+ RTC_MONTH
);
100 year
= readb(ioaddr
+ RTC_YEAR
);
101 century
= readb(ioaddr
+ RTC_CENTURY
) & RTC_CENTURY_MASK
;
102 writeb(0, ioaddr
+ RTC_CONTROL
);
103 tm
->tm_sec
= BCD2BIN(second
);
104 tm
->tm_min
= BCD2BIN(minute
);
105 tm
->tm_hour
= BCD2BIN(hour
);
106 tm
->tm_mday
= BCD2BIN(day
);
107 tm
->tm_wday
= BCD2BIN(week
);
108 tm
->tm_mon
= BCD2BIN(month
) - 1;
109 /* year is 1900 + tm->tm_year */
110 tm
->tm_year
= BCD2BIN(year
) + BCD2BIN(century
) * 100 - 1900;
112 if (rtc_valid_tm(tm
) < 0) {
113 dev_err(dev
, "retrieved date/time is not valid.\n");
114 rtc_time_to_tm(0, tm
);
119 static const struct rtc_class_ops ds1742_rtc_ops
= {
120 .read_time
= ds1742_rtc_read_time
,
121 .set_time
= ds1742_rtc_set_time
,
124 static ssize_t
ds1742_nvram_read(struct kobject
*kobj
, char *buf
,
125 loff_t pos
, size_t size
)
127 struct platform_device
*pdev
=
128 to_platform_device(container_of(kobj
, struct device
, kobj
));
129 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
130 void __iomem
*ioaddr
= pdata
->ioaddr
;
133 for (count
= 0; size
> 0 && pos
< RTC_OFFSET
; count
++, size
--)
134 *buf
++ = readb(ioaddr
+ pos
++);
138 static ssize_t
ds1742_nvram_write(struct kobject
*kobj
, char *buf
,
139 loff_t pos
, size_t size
)
141 struct platform_device
*pdev
=
142 to_platform_device(container_of(kobj
, struct device
, kobj
));
143 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
144 void __iomem
*ioaddr
= pdata
->ioaddr
;
147 for (count
= 0; size
> 0 && pos
< RTC_OFFSET
; count
++, size
--)
148 writeb(*buf
++, ioaddr
+ pos
++);
152 static struct bin_attribute ds1742_nvram_attr
= {
155 .mode
= S_IRUGO
| S_IWUGO
,
156 .owner
= THIS_MODULE
,
159 .read
= ds1742_nvram_read
,
160 .write
= ds1742_nvram_write
,
163 static int __init
ds1742_rtc_probe(struct platform_device
*pdev
)
165 struct rtc_device
*rtc
;
166 struct resource
*res
;
167 unsigned int cen
, sec
;
168 struct rtc_plat_data
*pdata
= NULL
;
169 void __iomem
*ioaddr
= NULL
;
172 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
175 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
178 if (!request_mem_region(res
->start
, RTC_REG_SIZE
, pdev
->name
)) {
182 pdata
->baseaddr
= res
->start
;
183 ioaddr
= ioremap(pdata
->baseaddr
, RTC_REG_SIZE
);
188 pdata
->ioaddr
= ioaddr
;
190 /* turn RTC on if it was not on */
191 sec
= readb(ioaddr
+ RTC_SECONDS
);
192 if (sec
& RTC_STOP
) {
193 sec
&= RTC_SECONDS_MASK
;
194 cen
= readb(ioaddr
+ RTC_CENTURY
) & RTC_CENTURY_MASK
;
195 writeb(RTC_WRITE
, ioaddr
+ RTC_CONTROL
);
196 writeb(sec
, ioaddr
+ RTC_SECONDS
);
197 writeb(cen
& RTC_CENTURY_MASK
, ioaddr
+ RTC_CONTROL
);
199 if (!(readb(ioaddr
+ RTC_DAY
) & RTC_BATT_FLAG
))
200 dev_warn(&pdev
->dev
, "voltage-low detected.\n");
202 rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
203 &ds1742_rtc_ops
, THIS_MODULE
);
209 pdata
->last_jiffies
= jiffies
;
210 platform_set_drvdata(pdev
, pdata
);
211 ret
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &ds1742_nvram_attr
);
217 rtc_device_unregister(pdata
->rtc
);
221 release_mem_region(pdata
->baseaddr
, RTC_REG_SIZE
);
226 static int __devexit
ds1742_rtc_remove(struct platform_device
*pdev
)
228 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
230 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &ds1742_nvram_attr
);
231 rtc_device_unregister(pdata
->rtc
);
232 iounmap(pdata
->ioaddr
);
233 release_mem_region(pdata
->baseaddr
, RTC_REG_SIZE
);
238 static struct platform_driver ds1742_rtc_driver
= {
239 .probe
= ds1742_rtc_probe
,
240 .remove
= __devexit_p(ds1742_rtc_remove
),
243 .owner
= THIS_MODULE
,
247 static __init
int ds1742_init(void)
249 return platform_driver_register(&ds1742_rtc_driver
);
252 static __exit
void ds1742_exit(void)
254 return platform_driver_unregister(&ds1742_rtc_driver
);
257 module_init(ds1742_init
);
258 module_exit(ds1742_exit
);
260 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
261 MODULE_DESCRIPTION("Dallas DS1742 RTC driver");
262 MODULE_LICENSE("GPL");
263 MODULE_VERSION(DRV_VERSION
);