2 * An rtc driver for the Dallas DS1511
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
5 * Copyright (C) 2007 Andrew Sharp <andy.sharp@lsi.com>
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.
11 * Real time clock driver for the Dallas 1511 chip, which also
12 * contains a watchdog timer. There is a tiny amount of code that
13 * platform code could use to mess with the watchdog device a little
14 * bit, but not a full watchdog driver.
17 #include <linux/bcd.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/gfp.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/rtc.h>
24 #include <linux/platform_device.h>
26 #include <linux/module.h>
39 DS1511_AM3_HOUR
= 0xa,
40 DS1511_AM4_DATE
= 0xb,
43 DS1511_CONTROL_A
= 0xe,
44 DS1511_CONTROL_B
= 0xf,
45 DS1511_RAMADDR_LSB
= 0x10,
49 #define DS1511_BLF1 0x80
50 #define DS1511_BLF2 0x40
51 #define DS1511_PRS 0x20
52 #define DS1511_PAB 0x10
53 #define DS1511_TDF 0x08
54 #define DS1511_KSF 0x04
55 #define DS1511_WDF 0x02
56 #define DS1511_IRQF 0x01
57 #define DS1511_TE 0x80
58 #define DS1511_CS 0x40
59 #define DS1511_BME 0x20
60 #define DS1511_TPE 0x10
61 #define DS1511_TIE 0x08
62 #define DS1511_KIE 0x04
63 #define DS1511_WDE 0x02
64 #define DS1511_WDS 0x01
65 #define DS1511_RAM_MAX 0x100
67 #define RTC_CMD DS1511_CONTROL_B
68 #define RTC_CMD1 DS1511_CONTROL_A
70 #define RTC_ALARM_SEC DS1511_AM1_SEC
71 #define RTC_ALARM_MIN DS1511_AM2_MIN
72 #define RTC_ALARM_HOUR DS1511_AM3_HOUR
73 #define RTC_ALARM_DATE DS1511_AM4_DATE
75 #define RTC_SEC DS1511_SEC
76 #define RTC_MIN DS1511_MIN
77 #define RTC_HOUR DS1511_HOUR
78 #define RTC_DOW DS1511_DOW
79 #define RTC_DOM DS1511_DOM
80 #define RTC_MON DS1511_MONTH
81 #define RTC_YEAR DS1511_YEAR
82 #define RTC_CENTURY DS1511_CENTURY
84 #define RTC_TIE DS1511_TIE
85 #define RTC_TE DS1511_TE
87 struct rtc_plat_data
{
88 struct rtc_device
*rtc
;
89 void __iomem
*ioaddr
; /* virtual base address */
99 static DEFINE_SPINLOCK(ds1511_lock
);
101 static __iomem
char *ds1511_base
;
102 static u32 reg_spacing
= 1;
105 rtc_write(uint8_t val
, uint32_t reg
)
107 writeb(val
, ds1511_base
+ (reg
* reg_spacing
));
111 rtc_write_alarm(uint8_t val
, enum ds1511reg reg
)
113 rtc_write((val
| 0x80), reg
);
116 static noinline
uint8_t
117 rtc_read(enum ds1511reg reg
)
119 return readb(ds1511_base
+ (reg
* reg_spacing
));
123 rtc_disable_update(void)
125 rtc_write((rtc_read(RTC_CMD
) & ~RTC_TE
), RTC_CMD
);
129 rtc_enable_update(void)
131 rtc_write((rtc_read(RTC_CMD
) | RTC_TE
), RTC_CMD
);
135 * #define DS1511_WDOG_RESET_SUPPORT
137 * Uncomment this if you want to use these routines in
138 * some platform code.
140 #ifdef DS1511_WDOG_RESET_SUPPORT
142 * just enough code to set the watchdog timer so that it
143 * will reboot the system
146 ds1511_wdog_set(unsigned long deciseconds
)
149 * the wdog timer can take 99.99 seconds
151 deciseconds
%= 10000;
153 * set the wdog values in the wdog registers
155 rtc_write(bin2bcd(deciseconds
% 100), DS1511_WD_MSEC
);
156 rtc_write(bin2bcd(deciseconds
/ 100), DS1511_WD_SEC
);
158 * set wdog enable and wdog 'steering' bit to issue a reset
160 rtc_write(rtc_read(RTC_CMD
) | DS1511_WDE
| DS1511_WDS
, RTC_CMD
);
164 ds1511_wdog_disable(void)
167 * clear wdog enable and wdog 'steering' bits
169 rtc_write(rtc_read(RTC_CMD
) & ~(DS1511_WDE
| DS1511_WDS
), RTC_CMD
);
171 * clear the wdog counter
173 rtc_write(0, DS1511_WD_MSEC
);
174 rtc_write(0, DS1511_WD_SEC
);
179 * set the rtc chip's idea of the time.
180 * stupidly, some callers call with year unmolested;
181 * and some call with year = year - 1900. thanks.
183 static int ds1511_rtc_set_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
185 u8 mon
, day
, dow
, hrs
, min
, sec
, yrs
, cen
;
189 * won't have to change this for a while
191 if (rtc_tm
->tm_year
< 1900)
192 rtc_tm
->tm_year
+= 1900;
194 if (rtc_tm
->tm_year
< 1970)
197 yrs
= rtc_tm
->tm_year
% 100;
198 cen
= rtc_tm
->tm_year
/ 100;
199 mon
= rtc_tm
->tm_mon
+ 1; /* tm_mon starts at zero */
200 day
= rtc_tm
->tm_mday
;
201 dow
= rtc_tm
->tm_wday
& 0x7; /* automatic BCD */
202 hrs
= rtc_tm
->tm_hour
;
203 min
= rtc_tm
->tm_min
;
204 sec
= rtc_tm
->tm_sec
;
206 if ((mon
> 12) || (day
== 0))
209 if (day
> rtc_month_days(rtc_tm
->tm_mon
, rtc_tm
->tm_year
))
212 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
216 * each register is a different number of valid bits
218 sec
= bin2bcd(sec
) & 0x7f;
219 min
= bin2bcd(min
) & 0x7f;
220 hrs
= bin2bcd(hrs
) & 0x3f;
221 day
= bin2bcd(day
) & 0x3f;
222 mon
= bin2bcd(mon
) & 0x1f;
223 yrs
= bin2bcd(yrs
) & 0xff;
224 cen
= bin2bcd(cen
) & 0xff;
226 spin_lock_irqsave(&ds1511_lock
, flags
);
227 rtc_disable_update();
228 rtc_write(cen
, RTC_CENTURY
);
229 rtc_write(yrs
, RTC_YEAR
);
230 rtc_write((rtc_read(RTC_MON
) & 0xe0) | mon
, RTC_MON
);
231 rtc_write(day
, RTC_DOM
);
232 rtc_write(hrs
, RTC_HOUR
);
233 rtc_write(min
, RTC_MIN
);
234 rtc_write(sec
, RTC_SEC
);
235 rtc_write(dow
, RTC_DOW
);
237 spin_unlock_irqrestore(&ds1511_lock
, flags
);
242 static int ds1511_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
244 unsigned int century
;
247 spin_lock_irqsave(&ds1511_lock
, flags
);
248 rtc_disable_update();
250 rtc_tm
->tm_sec
= rtc_read(RTC_SEC
) & 0x7f;
251 rtc_tm
->tm_min
= rtc_read(RTC_MIN
) & 0x7f;
252 rtc_tm
->tm_hour
= rtc_read(RTC_HOUR
) & 0x3f;
253 rtc_tm
->tm_mday
= rtc_read(RTC_DOM
) & 0x3f;
254 rtc_tm
->tm_wday
= rtc_read(RTC_DOW
) & 0x7;
255 rtc_tm
->tm_mon
= rtc_read(RTC_MON
) & 0x1f;
256 rtc_tm
->tm_year
= rtc_read(RTC_YEAR
) & 0x7f;
257 century
= rtc_read(RTC_CENTURY
);
260 spin_unlock_irqrestore(&ds1511_lock
, flags
);
262 rtc_tm
->tm_sec
= bcd2bin(rtc_tm
->tm_sec
);
263 rtc_tm
->tm_min
= bcd2bin(rtc_tm
->tm_min
);
264 rtc_tm
->tm_hour
= bcd2bin(rtc_tm
->tm_hour
);
265 rtc_tm
->tm_mday
= bcd2bin(rtc_tm
->tm_mday
);
266 rtc_tm
->tm_wday
= bcd2bin(rtc_tm
->tm_wday
);
267 rtc_tm
->tm_mon
= bcd2bin(rtc_tm
->tm_mon
);
268 rtc_tm
->tm_year
= bcd2bin(rtc_tm
->tm_year
);
269 century
= bcd2bin(century
) * 100;
272 * Account for differences between how the RTC uses the values
273 * and how they are defined in a struct rtc_time;
275 century
+= rtc_tm
->tm_year
;
276 rtc_tm
->tm_year
= century
- 1900;
280 if (rtc_valid_tm(rtc_tm
) < 0) {
281 dev_err(dev
, "retrieved date/time is not valid.\n");
282 rtc_time_to_tm(0, rtc_tm
);
288 * write the alarm register settings
290 * we only have the use to interrupt every second, otherwise
291 * known as the update interrupt, or the interrupt if the whole
292 * date/hours/mins/secs matches. the ds1511 has many more
293 * permutations, but the kernel doesn't.
296 ds1511_rtc_update_alarm(struct rtc_plat_data
*pdata
)
300 spin_lock_irqsave(&pdata
->lock
, flags
);
301 rtc_write(pdata
->alrm_mday
< 0 || (pdata
->irqen
& RTC_UF
) ?
302 0x80 : bin2bcd(pdata
->alrm_mday
) & 0x3f,
304 rtc_write(pdata
->alrm_hour
< 0 || (pdata
->irqen
& RTC_UF
) ?
305 0x80 : bin2bcd(pdata
->alrm_hour
) & 0x3f,
307 rtc_write(pdata
->alrm_min
< 0 || (pdata
->irqen
& RTC_UF
) ?
308 0x80 : bin2bcd(pdata
->alrm_min
) & 0x7f,
310 rtc_write(pdata
->alrm_sec
< 0 || (pdata
->irqen
& RTC_UF
) ?
311 0x80 : bin2bcd(pdata
->alrm_sec
) & 0x7f,
313 rtc_write(rtc_read(RTC_CMD
) | (pdata
->irqen
? RTC_TIE
: 0), RTC_CMD
);
314 rtc_read(RTC_CMD1
); /* clear interrupts */
315 spin_unlock_irqrestore(&pdata
->lock
, flags
);
319 ds1511_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
321 struct platform_device
*pdev
= to_platform_device(dev
);
322 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
327 pdata
->alrm_mday
= alrm
->time
.tm_mday
;
328 pdata
->alrm_hour
= alrm
->time
.tm_hour
;
329 pdata
->alrm_min
= alrm
->time
.tm_min
;
330 pdata
->alrm_sec
= alrm
->time
.tm_sec
;
332 pdata
->irqen
|= RTC_AF
;
334 ds1511_rtc_update_alarm(pdata
);
339 ds1511_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
341 struct platform_device
*pdev
= to_platform_device(dev
);
342 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
347 alrm
->time
.tm_mday
= pdata
->alrm_mday
< 0 ? 0 : pdata
->alrm_mday
;
348 alrm
->time
.tm_hour
= pdata
->alrm_hour
< 0 ? 0 : pdata
->alrm_hour
;
349 alrm
->time
.tm_min
= pdata
->alrm_min
< 0 ? 0 : pdata
->alrm_min
;
350 alrm
->time
.tm_sec
= pdata
->alrm_sec
< 0 ? 0 : pdata
->alrm_sec
;
351 alrm
->enabled
= (pdata
->irqen
& RTC_AF
) ? 1 : 0;
356 ds1511_interrupt(int irq
, void *dev_id
)
358 struct platform_device
*pdev
= dev_id
;
359 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
360 unsigned long events
= 0;
362 spin_lock(&pdata
->lock
);
364 * read and clear interrupt
366 if (rtc_read(RTC_CMD1
) & DS1511_IRQF
) {
368 if (rtc_read(RTC_ALARM_SEC
) & 0x80)
372 rtc_update_irq(pdata
->rtc
, 1, events
);
374 spin_unlock(&pdata
->lock
);
375 return events
? IRQ_HANDLED
: IRQ_NONE
;
378 static int ds1511_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
380 struct platform_device
*pdev
= to_platform_device(dev
);
381 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
386 pdata
->irqen
|= RTC_AF
;
388 pdata
->irqen
&= ~RTC_AF
;
389 ds1511_rtc_update_alarm(pdata
);
393 static const struct rtc_class_ops ds1511_rtc_ops
= {
394 .read_time
= ds1511_rtc_read_time
,
395 .set_time
= ds1511_rtc_set_time
,
396 .read_alarm
= ds1511_rtc_read_alarm
,
397 .set_alarm
= ds1511_rtc_set_alarm
,
398 .alarm_irq_enable
= ds1511_rtc_alarm_irq_enable
,
402 ds1511_nvram_read(struct file
*filp
, struct kobject
*kobj
,
403 struct bin_attribute
*ba
,
404 char *buf
, loff_t pos
, size_t size
)
408 rtc_write(pos
, DS1511_RAMADDR_LSB
);
409 for (count
= 0; count
< size
; count
++)
410 *buf
++ = rtc_read(DS1511_RAMDATA
);
416 ds1511_nvram_write(struct file
*filp
, struct kobject
*kobj
,
417 struct bin_attribute
*bin_attr
,
418 char *buf
, loff_t pos
, size_t size
)
422 rtc_write(pos
, DS1511_RAMADDR_LSB
);
423 for (count
= 0; count
< size
; count
++)
424 rtc_write(*buf
++, DS1511_RAMDATA
);
429 static struct bin_attribute ds1511_nvram_attr
= {
432 .mode
= S_IRUGO
| S_IWUSR
,
434 .size
= DS1511_RAM_MAX
,
435 .read
= ds1511_nvram_read
,
436 .write
= ds1511_nvram_write
,
439 static int ds1511_rtc_probe(struct platform_device
*pdev
)
441 struct resource
*res
;
442 struct rtc_plat_data
*pdata
;
445 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
449 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
450 ds1511_base
= devm_ioremap_resource(&pdev
->dev
, res
);
451 if (IS_ERR(ds1511_base
))
452 return PTR_ERR(ds1511_base
);
453 pdata
->ioaddr
= ds1511_base
;
454 pdata
->irq
= platform_get_irq(pdev
, 0);
457 * turn on the clock and the crystal, etc.
459 rtc_write(DS1511_BME
, RTC_CMD
);
460 rtc_write(0, RTC_CMD1
);
462 * clear the wdog counter
464 rtc_write(0, DS1511_WD_MSEC
);
465 rtc_write(0, DS1511_WD_SEC
);
472 * check for a dying bat-tree
474 if (rtc_read(RTC_CMD1
) & DS1511_BLF1
)
475 dev_warn(&pdev
->dev
, "voltage-low detected.\n");
477 spin_lock_init(&pdata
->lock
);
478 platform_set_drvdata(pdev
, pdata
);
480 pdata
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
481 &ds1511_rtc_ops
, THIS_MODULE
);
482 if (IS_ERR(pdata
->rtc
))
483 return PTR_ERR(pdata
->rtc
);
486 * if the platform has an interrupt in mind for this device,
487 * then by all means, set it
489 if (pdata
->irq
> 0) {
491 if (devm_request_irq(&pdev
->dev
, pdata
->irq
, ds1511_interrupt
,
492 IRQF_SHARED
, pdev
->name
, pdev
) < 0) {
494 dev_warn(&pdev
->dev
, "interrupt not available.\n");
499 ret
= sysfs_create_bin_file(&pdev
->dev
.kobj
, &ds1511_nvram_attr
);
501 dev_err(&pdev
->dev
, "Unable to create sysfs entry: %s\n",
502 ds1511_nvram_attr
.attr
.name
);
507 static int ds1511_rtc_remove(struct platform_device
*pdev
)
509 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
511 sysfs_remove_bin_file(&pdev
->dev
.kobj
, &ds1511_nvram_attr
);
512 if (pdata
->irq
> 0) {
514 * disable the alarm interrupt
516 rtc_write(rtc_read(RTC_CMD
) & ~RTC_TIE
, RTC_CMD
);
522 /* work with hotplug and coldplug */
523 MODULE_ALIAS("platform:ds1511");
525 static struct platform_driver ds1511_rtc_driver
= {
526 .probe
= ds1511_rtc_probe
,
527 .remove
= ds1511_rtc_remove
,
533 module_platform_driver(ds1511_rtc_driver
);
535 MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
536 MODULE_DESCRIPTION("Dallas DS1511 RTC driver");
537 MODULE_LICENSE("GPL");