4 * Copyright (c) 2007 Wind River Systems, Inc.
6 * Author: Mark Zhan <rongkai.zhan@windriver.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/rtc.h>
20 #include <linux/rtc/m48t59.h>
21 #include <linux/bcd.h>
27 #define M48T59_READ(reg) pdata->read_byte(dev, reg)
28 #define M48T59_WRITE(val, reg) pdata->write_byte(dev, reg, val)
30 #define M48T59_SET_BITS(mask, reg) \
31 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
32 #define M48T59_CLEAR_BITS(mask, reg) \
33 M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
35 struct m48t59_private
{
37 unsigned int size
; /* iomem size */
39 struct rtc_device
*rtc
;
40 spinlock_t lock
; /* serialize the NVRAM and RTC access */
44 * This is the generic access method when the chip is memory-mapped
47 m48t59_mem_writeb(struct device
*dev
, u32 ofs
, u8 val
)
49 struct platform_device
*pdev
= to_platform_device(dev
);
50 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
52 writeb(val
, m48t59
->ioaddr
+ofs
);
56 m48t59_mem_readb(struct device
*dev
, u32 ofs
)
58 struct platform_device
*pdev
= to_platform_device(dev
);
59 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
61 return readb(m48t59
->ioaddr
+ofs
);
65 * NOTE: M48T59 only uses BCD mode
67 static int m48t59_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
69 struct platform_device
*pdev
= to_platform_device(dev
);
70 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
71 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
75 spin_lock_irqsave(&m48t59
->lock
, flags
);
76 /* Issue the READ command */
77 M48T59_SET_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
79 tm
->tm_year
= BCD2BIN(M48T59_READ(M48T59_YEAR
));
81 tm
->tm_mon
= BCD2BIN(M48T59_READ(M48T59_MONTH
)) - 1;
82 tm
->tm_mday
= BCD2BIN(M48T59_READ(M48T59_MDAY
));
84 val
= M48T59_READ(M48T59_WDAY
);
85 if ((val
& M48T59_WDAY_CEB
) && (val
& M48T59_WDAY_CB
)) {
86 dev_dbg(dev
, "Century bit is enabled\n");
87 tm
->tm_year
+= 100; /* one century */
90 tm
->tm_wday
= BCD2BIN(val
& 0x07);
91 tm
->tm_hour
= BCD2BIN(M48T59_READ(M48T59_HOUR
) & 0x3F);
92 tm
->tm_min
= BCD2BIN(M48T59_READ(M48T59_MIN
) & 0x7F);
93 tm
->tm_sec
= BCD2BIN(M48T59_READ(M48T59_SEC
) & 0x7F);
95 /* Clear the READ bit */
96 M48T59_CLEAR_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
97 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
99 dev_dbg(dev
, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
100 tm
->tm_year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
101 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
105 static int m48t59_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
107 struct platform_device
*pdev
= to_platform_device(dev
);
108 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
109 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
113 dev_dbg(dev
, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
114 tm
->tm_year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
115 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
117 spin_lock_irqsave(&m48t59
->lock
, flags
);
118 /* Issue the WRITE command */
119 M48T59_SET_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
121 M48T59_WRITE((BIN2BCD(tm
->tm_sec
) & 0x7F), M48T59_SEC
);
122 M48T59_WRITE((BIN2BCD(tm
->tm_min
) & 0x7F), M48T59_MIN
);
123 M48T59_WRITE((BIN2BCD(tm
->tm_hour
) & 0x3F), M48T59_HOUR
);
124 M48T59_WRITE((BIN2BCD(tm
->tm_mday
) & 0x3F), M48T59_MDAY
);
126 M48T59_WRITE((BIN2BCD(tm
->tm_mon
+ 1) & 0x1F), M48T59_MONTH
);
127 M48T59_WRITE(BIN2BCD(tm
->tm_year
% 100), M48T59_YEAR
);
130 val
= (M48T59_WDAY_CEB
| M48T59_WDAY_CB
);
131 val
|= (BIN2BCD(tm
->tm_wday
) & 0x07);
132 M48T59_WRITE(val
, M48T59_WDAY
);
134 /* Clear the WRITE bit */
135 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
136 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
141 * Read alarm time and date in RTC
143 static int m48t59_rtc_readalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
145 struct platform_device
*pdev
= to_platform_device(dev
);
146 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
147 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
148 struct rtc_time
*tm
= &alrm
->time
;
152 /* If no irq, we don't support ALARM */
153 if (m48t59
->irq
== NO_IRQ
)
156 spin_lock_irqsave(&m48t59
->lock
, flags
);
157 /* Issue the READ command */
158 M48T59_SET_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
160 tm
->tm_year
= BCD2BIN(M48T59_READ(M48T59_YEAR
));
162 tm
->tm_mon
= BCD2BIN(M48T59_READ(M48T59_MONTH
)) - 1;
164 val
= M48T59_READ(M48T59_WDAY
);
165 if ((val
& M48T59_WDAY_CEB
) && (val
& M48T59_WDAY_CB
))
166 tm
->tm_year
+= 100; /* one century */
168 tm
->tm_mday
= BCD2BIN(M48T59_READ(M48T59_ALARM_DATE
));
169 tm
->tm_hour
= BCD2BIN(M48T59_READ(M48T59_ALARM_HOUR
));
170 tm
->tm_min
= BCD2BIN(M48T59_READ(M48T59_ALARM_MIN
));
171 tm
->tm_sec
= BCD2BIN(M48T59_READ(M48T59_ALARM_SEC
));
173 /* Clear the READ bit */
174 M48T59_CLEAR_BITS(M48T59_CNTL_READ
, M48T59_CNTL
);
175 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
177 dev_dbg(dev
, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
178 tm
->tm_year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
179 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
184 * Set alarm time and date in RTC
186 static int m48t59_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
188 struct platform_device
*pdev
= to_platform_device(dev
);
189 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
190 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
191 struct rtc_time
*tm
= &alrm
->time
;
192 u8 mday
, hour
, min
, sec
;
195 /* If no irq, we don't support ALARM */
196 if (m48t59
->irq
== NO_IRQ
)
200 * 0xff means "always match"
203 mday
= (mday
>= 1 && mday
<= 31) ? BIN2BCD(mday
) : 0xff;
205 mday
= M48T59_READ(M48T59_MDAY
);
208 hour
= (hour
< 24) ? BIN2BCD(hour
) : 0x00;
211 min
= (min
< 60) ? BIN2BCD(min
) : 0x00;
214 sec
= (sec
< 60) ? BIN2BCD(sec
) : 0x00;
216 spin_lock_irqsave(&m48t59
->lock
, flags
);
217 /* Issue the WRITE command */
218 M48T59_SET_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
220 M48T59_WRITE(mday
, M48T59_ALARM_DATE
);
221 M48T59_WRITE(hour
, M48T59_ALARM_HOUR
);
222 M48T59_WRITE(min
, M48T59_ALARM_MIN
);
223 M48T59_WRITE(sec
, M48T59_ALARM_SEC
);
225 /* Clear the WRITE bit */
226 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE
, M48T59_CNTL
);
227 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
229 dev_dbg(dev
, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
230 tm
->tm_year
+ 1900, tm
->tm_mon
, tm
->tm_mday
,
231 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
236 * Handle commands from user-space
238 static int m48t59_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
241 struct platform_device
*pdev
= to_platform_device(dev
);
242 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
243 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
247 spin_lock_irqsave(&m48t59
->lock
, flags
);
249 case RTC_AIE_OFF
: /* alarm interrupt off */
250 M48T59_WRITE(0x00, M48T59_INTR
);
252 case RTC_AIE_ON
: /* alarm interrupt on */
253 M48T59_WRITE(M48T59_INTR_AFE
, M48T59_INTR
);
259 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
264 static int m48t59_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
266 struct platform_device
*pdev
= to_platform_device(dev
);
267 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
268 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
272 spin_lock_irqsave(&m48t59
->lock
, flags
);
273 val
= M48T59_READ(M48T59_FLAGS
);
274 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
276 seq_printf(seq
, "battery\t\t: %s\n",
277 (val
& M48T59_FLAGS_BF
) ? "low" : "normal");
282 * IRQ handler for the RTC
284 static irqreturn_t
m48t59_rtc_interrupt(int irq
, void *dev_id
)
286 struct device
*dev
= (struct device
*)dev_id
;
287 struct platform_device
*pdev
= to_platform_device(dev
);
288 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
289 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
292 spin_lock(&m48t59
->lock
);
293 event
= M48T59_READ(M48T59_FLAGS
);
294 spin_unlock(&m48t59
->lock
);
296 if (event
& M48T59_FLAGS_AF
) {
297 rtc_update_irq(m48t59
->rtc
, 1, (RTC_AF
| RTC_IRQF
));
304 static const struct rtc_class_ops m48t59_rtc_ops
= {
305 .ioctl
= m48t59_rtc_ioctl
,
306 .read_time
= m48t59_rtc_read_time
,
307 .set_time
= m48t59_rtc_set_time
,
308 .read_alarm
= m48t59_rtc_readalarm
,
309 .set_alarm
= m48t59_rtc_setalarm
,
310 .proc
= m48t59_rtc_proc
,
313 static ssize_t
m48t59_nvram_read(struct kobject
*kobj
,
314 struct bin_attribute
*bin_attr
,
315 char *buf
, loff_t pos
, size_t size
)
317 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
318 struct platform_device
*pdev
= to_platform_device(dev
);
319 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
320 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
324 for (; size
> 0 && pos
< M48T59_NVRAM_SIZE
; cnt
++, size
--) {
325 spin_lock_irqsave(&m48t59
->lock
, flags
);
326 *buf
++ = M48T59_READ(cnt
);
327 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
333 static ssize_t
m48t59_nvram_write(struct kobject
*kobj
,
334 struct bin_attribute
*bin_attr
,
335 char *buf
, loff_t pos
, size_t size
)
337 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
338 struct platform_device
*pdev
= to_platform_device(dev
);
339 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
340 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
344 for (; size
> 0 && pos
< M48T59_NVRAM_SIZE
; cnt
++, size
--) {
345 spin_lock_irqsave(&m48t59
->lock
, flags
);
346 M48T59_WRITE(*buf
++, cnt
);
347 spin_unlock_irqrestore(&m48t59
->lock
, flags
);
353 static struct bin_attribute m48t59_nvram_attr
= {
356 .mode
= S_IRUGO
| S_IWUGO
,
357 .owner
= THIS_MODULE
,
359 .read
= m48t59_nvram_read
,
360 .write
= m48t59_nvram_write
,
363 static int __devinit
m48t59_rtc_probe(struct platform_device
*pdev
)
365 struct m48t59_plat_data
*pdata
= pdev
->dev
.platform_data
;
366 struct m48t59_private
*m48t59
= NULL
;
367 struct resource
*res
;
370 /* This chip could be memory-mapped or I/O-mapped */
371 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
373 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
378 if (res
->flags
& IORESOURCE_IO
) {
379 /* If we are I/O-mapped, the platform should provide
380 * the operations accessing chip registers.
382 if (!pdata
|| !pdata
->write_byte
|| !pdata
->read_byte
)
384 } else if (res
->flags
& IORESOURCE_MEM
) {
385 /* we are memory-mapped */
387 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
390 /* Ensure we only kmalloc platform data once */
391 pdev
->dev
.platform_data
= pdata
;
394 /* Try to use the generic memory read/write ops */
395 if (!pdata
->write_byte
)
396 pdata
->write_byte
= m48t59_mem_writeb
;
397 if (!pdata
->read_byte
)
398 pdata
->read_byte
= m48t59_mem_readb
;
401 m48t59
= kzalloc(sizeof(*m48t59
), GFP_KERNEL
);
405 m48t59
->size
= res
->end
- res
->start
+ 1;
406 m48t59
->ioaddr
= ioremap(res
->start
, m48t59
->size
);
410 /* Try to get irq number. We also can work in
411 * the mode without IRQ.
413 m48t59
->irq
= platform_get_irq(pdev
, 0);
415 m48t59
->irq
= NO_IRQ
;
417 if (m48t59
->irq
!= NO_IRQ
) {
418 ret
= request_irq(m48t59
->irq
, m48t59_rtc_interrupt
,
419 IRQF_SHARED
, "rtc-m48t59", &pdev
->dev
);
424 m48t59
->rtc
= rtc_device_register("m48t59", &pdev
->dev
,
425 &m48t59_rtc_ops
, THIS_MODULE
);
426 if (IS_ERR(m48t59
->rtc
)) {
427 ret
= PTR_ERR(m48t59
->rtc
);
431 ret
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &m48t59_nvram_attr
);
435 spin_lock_init(&m48t59
->lock
);
436 platform_set_drvdata(pdev
, m48t59
);
440 if (!IS_ERR(m48t59
->rtc
))
441 rtc_device_unregister(m48t59
->rtc
);
442 if (m48t59
->irq
!= NO_IRQ
)
443 free_irq(m48t59
->irq
, &pdev
->dev
);
445 iounmap(m48t59
->ioaddr
);
451 static int __devexit
m48t59_rtc_remove(struct platform_device
*pdev
)
453 struct m48t59_private
*m48t59
= platform_get_drvdata(pdev
);
455 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &m48t59_nvram_attr
);
456 if (!IS_ERR(m48t59
->rtc
))
457 rtc_device_unregister(m48t59
->rtc
);
459 iounmap(m48t59
->ioaddr
);
460 if (m48t59
->irq
!= NO_IRQ
)
461 free_irq(m48t59
->irq
, &pdev
->dev
);
462 platform_set_drvdata(pdev
, NULL
);
467 static struct platform_driver m48t59_rtc_driver
= {
469 .name
= "rtc-m48t59",
470 .owner
= THIS_MODULE
,
472 .probe
= m48t59_rtc_probe
,
473 .remove
= __devexit_p(m48t59_rtc_remove
),
476 static int __init
m48t59_rtc_init(void)
478 return platform_driver_register(&m48t59_rtc_driver
);
481 static void __exit
m48t59_rtc_exit(void)
483 platform_driver_unregister(&m48t59_rtc_driver
);
486 module_init(m48t59_rtc_init
);
487 module_exit(m48t59_rtc_exit
);
489 MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
490 MODULE_DESCRIPTION("M48T59 RTC driver");
491 MODULE_LICENSE("GPL");