2 * Dallas DS1216 RTC driver
4 * Copyright (c) 2007 Thomas Bogendoerfer
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/bcd.h>
13 #define DRV_VERSION "0.1"
26 #define DS1216_HOUR_1224 (1 << 7)
27 #define DS1216_HOUR_AMPM (1 << 5)
30 struct rtc_device
*rtc
;
33 unsigned long baseaddr
;
36 static const u8 magic
[] = {
37 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
41 * Read the 64 bit we'd like to have - It a series
42 * of 64 bits showing up in the LSB of the base register.
45 static void ds1216_read(u8 __iomem
*ioaddr
, u8
*buf
)
50 for (i
= 0; i
< 8; i
++) {
52 for (j
= 0; j
< 8; j
++)
53 c
|= (readb(ioaddr
) & 0x1) << j
;
58 static void ds1216_write(u8 __iomem
*ioaddr
, const u8
*buf
)
63 for (i
= 0; i
< 8; i
++) {
65 for (j
= 0; j
< 8; j
++) {
72 static void ds1216_switch_ds_to_clock(u8 __iomem
*ioaddr
)
74 /* Reset magic pointer */
76 /* Write 64 bit magic to DS1216 */
77 ds1216_write(ioaddr
, magic
);
80 static int ds1216_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
82 struct platform_device
*pdev
= to_platform_device(dev
);
83 struct ds1216_priv
*priv
= platform_get_drvdata(pdev
);
84 struct ds1216_regs regs
;
86 ds1216_switch_ds_to_clock(priv
->ioaddr
);
87 ds1216_read(priv
->ioaddr
, (u8
*)®s
);
89 tm
->tm_sec
= bcd2bin(regs
.sec
);
90 tm
->tm_min
= bcd2bin(regs
.min
);
91 if (regs
.hour
& DS1216_HOUR_1224
) {
93 tm
->tm_hour
= bcd2bin(regs
.hour
& 0x1f);
94 if (regs
.hour
& DS1216_HOUR_AMPM
)
97 tm
->tm_hour
= bcd2bin(regs
.hour
& 0x3f);
98 tm
->tm_wday
= (regs
.wday
& 7) - 1;
99 tm
->tm_mday
= bcd2bin(regs
.mday
& 0x3f);
100 tm
->tm_mon
= bcd2bin(regs
.month
& 0x1f);
101 tm
->tm_year
= bcd2bin(regs
.year
);
102 if (tm
->tm_year
< 70)
107 static int ds1216_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
109 struct platform_device
*pdev
= to_platform_device(dev
);
110 struct ds1216_priv
*priv
= platform_get_drvdata(pdev
);
111 struct ds1216_regs regs
;
113 ds1216_switch_ds_to_clock(priv
->ioaddr
);
114 ds1216_read(priv
->ioaddr
, (u8
*)®s
);
116 regs
.tsec
= 0; /* clear 0.1 and 0.01 seconds */
117 regs
.sec
= bin2bcd(tm
->tm_sec
);
118 regs
.min
= bin2bcd(tm
->tm_min
);
119 regs
.hour
&= DS1216_HOUR_1224
;
120 if (regs
.hour
&& tm
->tm_hour
> 12) {
121 regs
.hour
|= DS1216_HOUR_AMPM
;
124 regs
.hour
|= bin2bcd(tm
->tm_hour
);
126 regs
.wday
|= tm
->tm_wday
;
127 regs
.mday
= bin2bcd(tm
->tm_mday
);
128 regs
.month
= bin2bcd(tm
->tm_mon
);
129 regs
.year
= bin2bcd(tm
->tm_year
% 100);
131 ds1216_switch_ds_to_clock(priv
->ioaddr
);
132 ds1216_write(priv
->ioaddr
, (u8
*)®s
);
136 static const struct rtc_class_ops ds1216_rtc_ops
= {
137 .read_time
= ds1216_rtc_read_time
,
138 .set_time
= ds1216_rtc_set_time
,
141 static int __devinit
ds1216_rtc_probe(struct platform_device
*pdev
)
143 struct rtc_device
*rtc
;
144 struct resource
*res
;
145 struct ds1216_priv
*priv
;
149 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
152 priv
= kzalloc(sizeof *priv
, GFP_KERNEL
);
155 priv
->size
= res
->end
- res
->start
+ 1;
156 if (!request_mem_region(res
->start
, priv
->size
, pdev
->name
)) {
160 priv
->baseaddr
= res
->start
;
161 priv
->ioaddr
= ioremap(priv
->baseaddr
, priv
->size
);
166 rtc
= rtc_device_register("ds1216", &pdev
->dev
,
167 &ds1216_rtc_ops
, THIS_MODULE
);
173 platform_set_drvdata(pdev
, priv
);
175 /* dummy read to get clock into a known state */
176 ds1216_read(priv
->ioaddr
, dummy
);
181 rtc_device_unregister(priv
->rtc
);
183 iounmap(priv
->ioaddr
);
185 release_mem_region(priv
->baseaddr
, priv
->size
);
190 static int __devexit
ds1216_rtc_remove(struct platform_device
*pdev
)
192 struct ds1216_priv
*priv
= platform_get_drvdata(pdev
);
194 rtc_device_unregister(priv
->rtc
);
195 iounmap(priv
->ioaddr
);
196 release_mem_region(priv
->baseaddr
, priv
->size
);
201 static struct platform_driver ds1216_rtc_platform_driver
= {
203 .name
= "rtc-ds1216",
204 .owner
= THIS_MODULE
,
206 .probe
= ds1216_rtc_probe
,
207 .remove
= __devexit_p(ds1216_rtc_remove
),
210 static int __init
ds1216_rtc_init(void)
212 return platform_driver_register(&ds1216_rtc_platform_driver
);
215 static void __exit
ds1216_rtc_exit(void)
217 platform_driver_unregister(&ds1216_rtc_platform_driver
);
220 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
221 MODULE_DESCRIPTION("DS1216 RTC driver");
222 MODULE_LICENSE("GPL");
223 MODULE_VERSION(DRV_VERSION
);
224 MODULE_ALIAS("platform:rtc-ds1216");
226 module_init(ds1216_rtc_init
);
227 module_exit(ds1216_rtc_exit
);