1 // SPDX-License-Identifier: GPL-2.0-only
3 * An rtc driver for the Dallas DS1511
5 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * Copyright (C) 2007 Andrew Sharp <andy.sharp@lsi.com>
8 * Real time clock driver for the Dallas 1511 chip, which also
9 * contains a watchdog timer. There is a tiny amount of code that
10 * platform code could use to mess with the watchdog device a little
11 * bit, but not a full watchdog driver.
14 #include <linux/bcd.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/gfp.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/platform_device.h>
23 #include <linux/module.h>
25 #define DS1511_SEC 0x0
26 #define DS1511_MIN 0x1
27 #define DS1511_HOUR 0x2
28 #define DS1511_DOW 0x3
29 #define DS1511_DOM 0x4
30 #define DS1511_MONTH 0x5
31 #define DS1511_YEAR 0x6
32 #define DS1511_CENTURY 0x7
33 #define DS1511_AM1_SEC 0x8
34 #define DS1511_AM2_MIN 0x9
35 #define DS1511_AM3_HOUR 0xa
36 #define DS1511_AM4_DATE 0xb
37 #define DS1511_WD_MSEC 0xc
38 #define DS1511_WD_SEC 0xd
39 #define DS1511_CONTROL_A 0xe
40 #define DS1511_CONTROL_B 0xf
41 #define DS1511_RAMADDR_LSB 0x10
42 #define DS1511_RAMDATA 0x13
44 #define DS1511_BLF1 0x80
45 #define DS1511_BLF2 0x40
46 #define DS1511_PRS 0x20
47 #define DS1511_PAB 0x10
48 #define DS1511_TDF 0x08
49 #define DS1511_KSF 0x04
50 #define DS1511_WDF 0x02
51 #define DS1511_IRQF 0x01
52 #define DS1511_TE 0x80
53 #define DS1511_CS 0x40
54 #define DS1511_BME 0x20
55 #define DS1511_TPE 0x10
56 #define DS1511_TIE 0x08
57 #define DS1511_KIE 0x04
58 #define DS1511_WDE 0x02
59 #define DS1511_WDS 0x01
60 #define DS1511_RAM_MAX 0x100
63 struct rtc_device
*rtc
;
64 void __iomem
*ioaddr
; /* virtual base address */
69 static DEFINE_SPINLOCK(ds1511_lock
);
71 static __iomem
char *ds1511_base
;
72 static u32 reg_spacing
= 1;
74 static void rtc_write(uint8_t val
, uint32_t reg
)
76 writeb(val
, ds1511_base
+ (reg
* reg_spacing
));
79 static uint8_t rtc_read(uint32_t reg
)
81 return readb(ds1511_base
+ (reg
* reg_spacing
));
84 static void rtc_disable_update(void)
86 rtc_write((rtc_read(DS1511_CONTROL_B
) & ~DS1511_TE
), DS1511_CONTROL_B
);
89 static void rtc_enable_update(void)
91 rtc_write((rtc_read(DS1511_CONTROL_B
) | DS1511_TE
), DS1511_CONTROL_B
);
94 static int ds1511_rtc_set_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
96 u8 mon
, day
, dow
, hrs
, min
, sec
, yrs
, cen
;
99 yrs
= rtc_tm
->tm_year
% 100;
100 cen
= 19 + rtc_tm
->tm_year
/ 100;
101 mon
= rtc_tm
->tm_mon
+ 1; /* tm_mon starts at zero */
102 day
= rtc_tm
->tm_mday
;
103 dow
= rtc_tm
->tm_wday
& 0x7; /* automatic BCD */
104 hrs
= rtc_tm
->tm_hour
;
105 min
= rtc_tm
->tm_min
;
106 sec
= rtc_tm
->tm_sec
;
109 * each register is a different number of valid bits
111 sec
= bin2bcd(sec
) & 0x7f;
112 min
= bin2bcd(min
) & 0x7f;
113 hrs
= bin2bcd(hrs
) & 0x3f;
114 day
= bin2bcd(day
) & 0x3f;
115 mon
= bin2bcd(mon
) & 0x1f;
116 yrs
= bin2bcd(yrs
) & 0xff;
117 cen
= bin2bcd(cen
) & 0xff;
119 spin_lock_irqsave(&ds1511_lock
, flags
);
120 rtc_disable_update();
121 rtc_write(cen
, DS1511_CENTURY
);
122 rtc_write(yrs
, DS1511_YEAR
);
123 rtc_write((rtc_read(DS1511_MONTH
) & 0xe0) | mon
, DS1511_MONTH
);
124 rtc_write(day
, DS1511_DOM
);
125 rtc_write(hrs
, DS1511_HOUR
);
126 rtc_write(min
, DS1511_MIN
);
127 rtc_write(sec
, DS1511_SEC
);
128 rtc_write(dow
, DS1511_DOW
);
130 spin_unlock_irqrestore(&ds1511_lock
, flags
);
135 static int ds1511_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
137 unsigned int century
;
140 spin_lock_irqsave(&ds1511_lock
, flags
);
141 rtc_disable_update();
143 rtc_tm
->tm_sec
= rtc_read(DS1511_SEC
) & 0x7f;
144 rtc_tm
->tm_min
= rtc_read(DS1511_MIN
) & 0x7f;
145 rtc_tm
->tm_hour
= rtc_read(DS1511_HOUR
) & 0x3f;
146 rtc_tm
->tm_mday
= rtc_read(DS1511_DOM
) & 0x3f;
147 rtc_tm
->tm_wday
= rtc_read(DS1511_DOW
) & 0x7;
148 rtc_tm
->tm_mon
= rtc_read(DS1511_MONTH
) & 0x1f;
149 rtc_tm
->tm_year
= rtc_read(DS1511_YEAR
) & 0x7f;
150 century
= rtc_read(DS1511_CENTURY
);
153 spin_unlock_irqrestore(&ds1511_lock
, flags
);
155 rtc_tm
->tm_sec
= bcd2bin(rtc_tm
->tm_sec
);
156 rtc_tm
->tm_min
= bcd2bin(rtc_tm
->tm_min
);
157 rtc_tm
->tm_hour
= bcd2bin(rtc_tm
->tm_hour
);
158 rtc_tm
->tm_mday
= bcd2bin(rtc_tm
->tm_mday
);
159 rtc_tm
->tm_wday
= bcd2bin(rtc_tm
->tm_wday
);
160 rtc_tm
->tm_mon
= bcd2bin(rtc_tm
->tm_mon
);
161 rtc_tm
->tm_year
= bcd2bin(rtc_tm
->tm_year
);
162 century
= bcd2bin(century
) * 100;
165 * Account for differences between how the RTC uses the values
166 * and how they are defined in a struct rtc_time;
168 century
+= rtc_tm
->tm_year
;
169 rtc_tm
->tm_year
= century
- 1900;
176 static void ds1511_rtc_alarm_enable(unsigned int enabled
)
178 rtc_write(rtc_read(DS1511_CONTROL_B
) | (enabled
? DS1511_TIE
: 0), DS1511_CONTROL_B
);
181 static int ds1511_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
183 struct ds1511_data
*ds1511
= dev_get_drvdata(dev
);
186 spin_lock_irqsave(&ds1511
->lock
, flags
);
187 rtc_write(bin2bcd(alrm
->time
.tm_mday
) & 0x3f, DS1511_AM4_DATE
);
188 rtc_write(bin2bcd(alrm
->time
.tm_hour
) & 0x3f, DS1511_AM3_HOUR
);
189 rtc_write(bin2bcd(alrm
->time
.tm_min
) & 0x7f, DS1511_AM2_MIN
);
190 rtc_write(bin2bcd(alrm
->time
.tm_sec
) & 0x7f, DS1511_AM1_SEC
);
191 ds1511_rtc_alarm_enable(alrm
->enabled
);
193 rtc_read(DS1511_CONTROL_A
); /* clear interrupts */
194 spin_unlock_irqrestore(&ds1511
->lock
, flags
);
199 static int ds1511_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
201 alrm
->time
.tm_mday
= bcd2bin(rtc_read(DS1511_AM4_DATE
) & 0x3f);
202 alrm
->time
.tm_hour
= bcd2bin(rtc_read(DS1511_AM3_HOUR
) & 0x3f);
203 alrm
->time
.tm_min
= bcd2bin(rtc_read(DS1511_AM2_MIN
) & 0x7f);
204 alrm
->time
.tm_sec
= bcd2bin(rtc_read(DS1511_AM1_SEC
) & 0x7f);
205 alrm
->enabled
= !!(rtc_read(DS1511_CONTROL_B
) & DS1511_TIE
);
210 static irqreturn_t
ds1511_interrupt(int irq
, void *dev_id
)
212 struct platform_device
*pdev
= dev_id
;
213 struct ds1511_data
*ds1511
= platform_get_drvdata(pdev
);
214 unsigned long events
= 0;
216 spin_lock(&ds1511
->lock
);
218 * read and clear interrupt
220 if (rtc_read(DS1511_CONTROL_A
) & DS1511_IRQF
) {
221 events
= RTC_IRQF
| RTC_AF
;
222 rtc_update_irq(ds1511
->rtc
, 1, events
);
224 spin_unlock(&ds1511
->lock
);
225 return events
? IRQ_HANDLED
: IRQ_NONE
;
228 static int ds1511_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
230 struct ds1511_data
*ds1511
= dev_get_drvdata(dev
);
233 spin_lock_irqsave(&ds1511
->lock
, flags
);
234 ds1511_rtc_alarm_enable(enabled
);
235 spin_unlock_irqrestore(&ds1511
->lock
, flags
);
240 static const struct rtc_class_ops ds1511_rtc_ops
= {
241 .read_time
= ds1511_rtc_read_time
,
242 .set_time
= ds1511_rtc_set_time
,
243 .read_alarm
= ds1511_rtc_read_alarm
,
244 .set_alarm
= ds1511_rtc_set_alarm
,
245 .alarm_irq_enable
= ds1511_rtc_alarm_irq_enable
,
248 static int ds1511_nvram_read(void *priv
, unsigned int pos
, void *buf
,
253 rtc_write(pos
, DS1511_RAMADDR_LSB
);
254 for (i
= 0; i
< size
; i
++)
255 *(char *)buf
++ = rtc_read(DS1511_RAMDATA
);
260 static int ds1511_nvram_write(void *priv
, unsigned int pos
, void *buf
,
265 rtc_write(pos
, DS1511_RAMADDR_LSB
);
266 for (i
= 0; i
< size
; i
++)
267 rtc_write(*(char *)buf
++, DS1511_RAMDATA
);
272 static int ds1511_rtc_probe(struct platform_device
*pdev
)
274 struct ds1511_data
*ds1511
;
276 struct nvmem_config ds1511_nvmem_cfg
= {
277 .name
= "ds1511_nvram",
280 .size
= DS1511_RAM_MAX
,
281 .reg_read
= ds1511_nvram_read
,
282 .reg_write
= ds1511_nvram_write
,
286 ds1511
= devm_kzalloc(&pdev
->dev
, sizeof(*ds1511
), GFP_KERNEL
);
290 ds1511_base
= devm_platform_ioremap_resource(pdev
, 0);
291 if (IS_ERR(ds1511_base
))
292 return PTR_ERR(ds1511_base
);
293 ds1511
->ioaddr
= ds1511_base
;
294 ds1511
->irq
= platform_get_irq(pdev
, 0);
297 * turn on the clock and the crystal, etc.
299 rtc_write(DS1511_BME
, DS1511_CONTROL_B
);
300 rtc_write(0, DS1511_CONTROL_A
);
302 * clear the wdog counter
304 rtc_write(0, DS1511_WD_MSEC
);
305 rtc_write(0, DS1511_WD_SEC
);
312 * check for a dying bat-tree
314 if (rtc_read(DS1511_CONTROL_A
) & DS1511_BLF1
)
315 dev_warn(&pdev
->dev
, "voltage-low detected.\n");
317 spin_lock_init(&ds1511
->lock
);
318 platform_set_drvdata(pdev
, ds1511
);
320 ds1511
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
321 if (IS_ERR(ds1511
->rtc
))
322 return PTR_ERR(ds1511
->rtc
);
324 ds1511
->rtc
->ops
= &ds1511_rtc_ops
;
325 ds1511
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
326 ds1511
->rtc
->alarm_offset_max
= 28 * 24 * 60 * 60 - 1;
329 * if the platform has an interrupt in mind for this device,
330 * then by all means, set it
332 if (ds1511
->irq
> 0) {
333 rtc_read(DS1511_CONTROL_A
);
334 if (devm_request_irq(&pdev
->dev
, ds1511
->irq
, ds1511_interrupt
,
335 IRQF_SHARED
, pdev
->name
, pdev
) < 0) {
337 dev_warn(&pdev
->dev
, "interrupt not available.\n");
342 if (ds1511
->irq
== 0)
343 clear_bit(RTC_FEATURE_ALARM
, ds1511
->rtc
->features
);
345 ret
= devm_rtc_register_device(ds1511
->rtc
);
349 devm_rtc_nvmem_register(ds1511
->rtc
, &ds1511_nvmem_cfg
);
354 /* work with hotplug and coldplug */
355 MODULE_ALIAS("platform:ds1511");
357 static struct platform_driver ds1511_rtc_driver
= {
358 .probe
= ds1511_rtc_probe
,
364 module_platform_driver(ds1511_rtc_driver
);
366 MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
367 MODULE_DESCRIPTION("Dallas DS1511 RTC driver");
368 MODULE_LICENSE("GPL");