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>
22 #include <linux/slab.h>
28 #define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
29 #define M48T59_WRITE(val, reg) \
30 (pdata->write_byte(dev, pdata->offset + reg, val))
32 #define M48T59_SET_BITS(mask, reg) \
33 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
34 #define M48T59_CLEAR_BITS(mask, reg) \
35 M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
37 struct m48t59_private {
40 struct rtc_device *rtc;
41 spinlock_t lock; /* serialize the NVRAM and RTC access */
45 * This is the generic access method when the chip is memory-mapped
48 m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
50 struct platform_device *pdev = to_platform_device(dev);
51 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
53 writeb(val, m48t59->ioaddr+ofs);
57 m48t59_mem_readb(struct device *dev, u32 ofs)
59 struct platform_device *pdev = to_platform_device(dev);
60 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
62 return readb(m48t59->ioaddr+ofs);
66 * NOTE: M48T59 only uses BCD mode
68 static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
70 struct platform_device *pdev = to_platform_device(dev);
71 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
72 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
76 spin_lock_irqsave(&m48t59->lock, flags);
77 /* Issue the READ command */
78 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
80 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
82 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
83 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
85 val = M48T59_READ(M48T59_WDAY);
86 if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
87 (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
88 dev_dbg(dev, "Century bit is enabled\n");
89 tm->tm_year += 100; /* one century */
92 /* Sun SPARC machines count years since 1968 */
96 tm->tm_wday = bcd2bin(val & 0x07);
97 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
98 tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
99 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
101 /* Clear the READ bit */
102 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
103 spin_unlock_irqrestore(&m48t59->lock, flags);
105 dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
106 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
107 tm->tm_hour, tm->tm_min, tm->tm_sec);
108 return rtc_valid_tm(tm);
111 static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
113 struct platform_device *pdev = to_platform_device(dev);
114 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
115 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
118 int year = tm->tm_year;
121 /* Sun SPARC machines count years since 1968 */
125 dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
126 year + 1900, tm->tm_mon, tm->tm_mday,
127 tm->tm_hour, tm->tm_min, tm->tm_sec);
132 spin_lock_irqsave(&m48t59->lock, flags);
133 /* Issue the WRITE command */
134 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
136 M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
137 M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
138 M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
139 M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
141 M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
142 M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
144 if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
145 val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
146 val |= (bin2bcd(tm->tm_wday) & 0x07);
147 M48T59_WRITE(val, M48T59_WDAY);
149 /* Clear the WRITE bit */
150 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
151 spin_unlock_irqrestore(&m48t59->lock, flags);
156 * Read alarm time and date in RTC
158 static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
160 struct platform_device *pdev = to_platform_device(dev);
161 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
162 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
163 struct rtc_time *tm = &alrm->time;
167 /* If no irq, we don't support ALARM */
168 if (m48t59->irq == NO_IRQ)
171 spin_lock_irqsave(&m48t59->lock, flags);
172 /* Issue the READ command */
173 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
175 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
177 /* Sun SPARC machines count years since 1968 */
181 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
183 val = M48T59_READ(M48T59_WDAY);
184 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
185 tm->tm_year += 100; /* one century */
187 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE));
188 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR));
189 tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN));
190 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC));
192 /* Clear the READ bit */
193 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
194 spin_unlock_irqrestore(&m48t59->lock, flags);
196 dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
197 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
198 tm->tm_hour, tm->tm_min, tm->tm_sec);
199 return rtc_valid_tm(tm);
203 * Set alarm time and date in RTC
205 static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
207 struct platform_device *pdev = to_platform_device(dev);
208 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
209 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
210 struct rtc_time *tm = &alrm->time;
211 u8 mday, hour, min, sec;
213 int year = tm->tm_year;
216 /* Sun SPARC machines count years since 1968 */
220 /* If no irq, we don't support ALARM */
221 if (m48t59->irq == NO_IRQ)
228 * 0xff means "always match"
231 mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
233 mday = M48T59_READ(M48T59_MDAY);
236 hour = (hour < 24) ? bin2bcd(hour) : 0x00;
239 min = (min < 60) ? bin2bcd(min) : 0x00;
242 sec = (sec < 60) ? bin2bcd(sec) : 0x00;
244 spin_lock_irqsave(&m48t59->lock, flags);
245 /* Issue the WRITE command */
246 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
248 M48T59_WRITE(mday, M48T59_ALARM_DATE);
249 M48T59_WRITE(hour, M48T59_ALARM_HOUR);
250 M48T59_WRITE(min, M48T59_ALARM_MIN);
251 M48T59_WRITE(sec, M48T59_ALARM_SEC);
253 /* Clear the WRITE bit */
254 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
255 spin_unlock_irqrestore(&m48t59->lock, flags);
257 dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
258 year + 1900, tm->tm_mon, tm->tm_mday,
259 tm->tm_hour, tm->tm_min, tm->tm_sec);
264 * Handle commands from user-space
266 static int m48t59_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
268 struct platform_device *pdev = to_platform_device(dev);
269 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
270 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
273 spin_lock_irqsave(&m48t59->lock, flags);
275 M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR);
277 M48T59_WRITE(0x00, M48T59_INTR);
278 spin_unlock_irqrestore(&m48t59->lock, flags);
283 static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq)
285 struct platform_device *pdev = to_platform_device(dev);
286 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
287 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
291 spin_lock_irqsave(&m48t59->lock, flags);
292 val = M48T59_READ(M48T59_FLAGS);
293 spin_unlock_irqrestore(&m48t59->lock, flags);
295 seq_printf(seq, "battery\t\t: %s\n",
296 (val & M48T59_FLAGS_BF) ? "low" : "normal");
301 * IRQ handler for the RTC
303 static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id)
305 struct device *dev = (struct device *)dev_id;
306 struct platform_device *pdev = to_platform_device(dev);
307 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
308 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
311 spin_lock(&m48t59->lock);
312 event = M48T59_READ(M48T59_FLAGS);
313 spin_unlock(&m48t59->lock);
315 if (event & M48T59_FLAGS_AF) {
316 rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF));
323 static const struct rtc_class_ops m48t59_rtc_ops = {
324 .read_time = m48t59_rtc_read_time,
325 .set_time = m48t59_rtc_set_time,
326 .read_alarm = m48t59_rtc_readalarm,
327 .set_alarm = m48t59_rtc_setalarm,
328 .proc = m48t59_rtc_proc,
329 .alarm_irq_enable = m48t59_rtc_alarm_irq_enable,
332 static const struct rtc_class_ops m48t02_rtc_ops = {
333 .read_time = m48t59_rtc_read_time,
334 .set_time = m48t59_rtc_set_time,
337 static ssize_t m48t59_nvram_read(struct file *filp, struct kobject *kobj,
338 struct bin_attribute *bin_attr,
339 char *buf, loff_t pos, size_t size)
341 struct device *dev = container_of(kobj, struct device, kobj);
342 struct platform_device *pdev = to_platform_device(dev);
343 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
344 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
348 spin_lock_irqsave(&m48t59->lock, flags);
350 for (; cnt < size; cnt++)
351 *buf++ = M48T59_READ(cnt);
353 spin_unlock_irqrestore(&m48t59->lock, flags);
358 static ssize_t m48t59_nvram_write(struct file *filp, struct kobject *kobj,
359 struct bin_attribute *bin_attr,
360 char *buf, loff_t pos, size_t size)
362 struct device *dev = container_of(kobj, struct device, kobj);
363 struct platform_device *pdev = to_platform_device(dev);
364 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
365 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
369 spin_lock_irqsave(&m48t59->lock, flags);
371 for (; cnt < size; cnt++)
372 M48T59_WRITE(*buf++, cnt);
374 spin_unlock_irqrestore(&m48t59->lock, flags);
379 static struct bin_attribute m48t59_nvram_attr = {
382 .mode = S_IRUGO | S_IWUSR,
384 .read = m48t59_nvram_read,
385 .write = m48t59_nvram_write,
388 static int m48t59_rtc_probe(struct platform_device *pdev)
390 struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
391 struct m48t59_private *m48t59 = NULL;
392 struct resource *res;
395 const struct rtc_class_ops *ops;
397 /* This chip could be memory-mapped or I/O-mapped */
398 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
400 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
405 if (res->flags & IORESOURCE_IO) {
406 /* If we are I/O-mapped, the platform should provide
407 * the operations accessing chip registers.
409 if (!pdata || !pdata->write_byte || !pdata->read_byte)
411 } else if (res->flags & IORESOURCE_MEM) {
412 /* we are memory-mapped */
414 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
418 /* Ensure we only kmalloc platform data once */
419 pdev->dev.platform_data = pdata;
422 pdata->type = M48T59RTC_TYPE_M48T59;
424 /* Try to use the generic memory read/write ops */
425 if (!pdata->write_byte)
426 pdata->write_byte = m48t59_mem_writeb;
427 if (!pdata->read_byte)
428 pdata->read_byte = m48t59_mem_readb;
431 m48t59 = devm_kzalloc(&pdev->dev, sizeof(*m48t59), GFP_KERNEL);
435 m48t59->ioaddr = pdata->ioaddr;
437 if (!m48t59->ioaddr) {
438 /* ioaddr not mapped externally */
439 m48t59->ioaddr = devm_ioremap(&pdev->dev, res->start,
445 /* Try to get irq number. We also can work in
446 * the mode without IRQ.
448 m48t59->irq = platform_get_irq(pdev, 0);
449 if (m48t59->irq <= 0)
450 m48t59->irq = NO_IRQ;
452 if (m48t59->irq != NO_IRQ) {
453 ret = devm_request_irq(&pdev->dev, m48t59->irq,
454 m48t59_rtc_interrupt, IRQF_SHARED,
455 "rtc-m48t59", &pdev->dev);
459 switch (pdata->type) {
460 case M48T59RTC_TYPE_M48T59:
462 ops = &m48t59_rtc_ops;
463 pdata->offset = 0x1ff0;
465 case M48T59RTC_TYPE_M48T02:
467 ops = &m48t02_rtc_ops;
468 pdata->offset = 0x7f0;
470 case M48T59RTC_TYPE_M48T08:
472 ops = &m48t02_rtc_ops;
473 pdata->offset = 0x1ff0;
476 dev_err(&pdev->dev, "Unknown RTC type\n");
480 spin_lock_init(&m48t59->lock);
481 platform_set_drvdata(pdev, m48t59);
483 m48t59->rtc = devm_rtc_device_register(&pdev->dev, name, ops,
485 if (IS_ERR(m48t59->rtc))
486 return PTR_ERR(m48t59->rtc);
488 m48t59_nvram_attr.size = pdata->offset;
490 ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
497 static int m48t59_rtc_remove(struct platform_device *pdev)
499 sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
503 /* work with hotplug and coldplug */
504 MODULE_ALIAS("platform:rtc-m48t59");
506 static struct platform_driver m48t59_rtc_driver = {
508 .name = "rtc-m48t59",
510 .probe = m48t59_rtc_probe,
511 .remove = m48t59_rtc_remove,
514 module_platform_driver(m48t59_rtc_driver);
516 MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
517 MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
518 MODULE_LICENSE("GPL");