2 * RTC client/driver for the Maxim/Dallas DS3232/DS3234 Real-Time Clock
4 * Copyright (C) 2009-2011 Freescale Semiconductor.
5 * Author: Jack Lan <jack.lan@freescale.com>
6 * Copyright (C) 2008 MIMOMax Wireless Ltd.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/rtc.h>
22 #include <linux/bcd.h>
23 #include <linux/slab.h>
24 #include <linux/regmap.h>
26 #define DS3232_REG_SECONDS 0x00
27 #define DS3232_REG_MINUTES 0x01
28 #define DS3232_REG_HOURS 0x02
29 #define DS3232_REG_AMPM 0x02
30 #define DS3232_REG_DAY 0x03
31 #define DS3232_REG_DATE 0x04
32 #define DS3232_REG_MONTH 0x05
33 #define DS3232_REG_CENTURY 0x05
34 #define DS3232_REG_YEAR 0x06
35 #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */
36 #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */
37 #define DS3232_REG_CR 0x0E /* Control register */
38 # define DS3232_REG_CR_nEOSC 0x80
39 # define DS3232_REG_CR_INTCN 0x04
40 # define DS3232_REG_CR_A2IE 0x02
41 # define DS3232_REG_CR_A1IE 0x01
43 #define DS3232_REG_SR 0x0F /* control/status register */
44 # define DS3232_REG_SR_OSF 0x80
45 # define DS3232_REG_SR_BSY 0x04
46 # define DS3232_REG_SR_A2F 0x02
47 # define DS3232_REG_SR_A1F 0x01
51 struct regmap
*regmap
;
53 struct rtc_device
*rtc
;
58 static int ds3232_check_rtc_status(struct device
*dev
)
60 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
64 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
68 if (stat
& DS3232_REG_SR_OSF
)
70 "oscillator discontinuity flagged, "
73 stat
&= ~(DS3232_REG_SR_OSF
| DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
75 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_SR
, stat
);
79 /* If the alarm is pending, clear it before requesting
80 * the interrupt, so an interrupt event isn't reported
81 * before everything is initialized.
84 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
88 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
89 control
|= DS3232_REG_CR_INTCN
;
91 return regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
94 static int ds3232_read_time(struct device
*dev
, struct rtc_time
*time
)
96 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
99 unsigned int year
, month
, day
, hour
, minute
, second
;
100 unsigned int week
, twelve_hr
, am_pm
;
101 unsigned int century
, add_century
= 0;
103 ret
= regmap_bulk_read(ds3232
->regmap
, DS3232_REG_SECONDS
, buf
, 7);
115 /* Extract additional information for AM/PM and century */
117 twelve_hr
= hour
& 0x40;
119 century
= month
& 0x80;
121 /* Write to rtc_time structure */
123 time
->tm_sec
= bcd2bin(second
);
124 time
->tm_min
= bcd2bin(minute
);
126 /* Convert to 24 hr */
128 time
->tm_hour
= bcd2bin(hour
& 0x1F) + 12;
130 time
->tm_hour
= bcd2bin(hour
& 0x1F);
132 time
->tm_hour
= bcd2bin(hour
);
135 /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
136 time
->tm_wday
= bcd2bin(week
) - 1;
137 time
->tm_mday
= bcd2bin(day
);
138 /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
139 time
->tm_mon
= bcd2bin(month
& 0x7F) - 1;
143 time
->tm_year
= bcd2bin(year
) + add_century
;
145 return rtc_valid_tm(time
);
148 static int ds3232_set_time(struct device
*dev
, struct rtc_time
*time
)
150 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
153 /* Extract time from rtc_time and load into ds3232*/
155 buf
[0] = bin2bcd(time
->tm_sec
);
156 buf
[1] = bin2bcd(time
->tm_min
);
157 buf
[2] = bin2bcd(time
->tm_hour
);
158 /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
159 buf
[3] = bin2bcd(time
->tm_wday
+ 1);
160 buf
[4] = bin2bcd(time
->tm_mday
); /* Date */
161 /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
162 buf
[5] = bin2bcd(time
->tm_mon
+ 1);
163 if (time
->tm_year
>= 100) {
165 buf
[6] = bin2bcd(time
->tm_year
- 100);
167 buf
[6] = bin2bcd(time
->tm_year
);
170 return regmap_bulk_write(ds3232
->regmap
, DS3232_REG_SECONDS
, buf
, 7);
174 * DS3232 has two alarm, we only use alarm1
175 * According to linux specification, only support one-shot alarm
176 * no periodic alarm mode
178 static int ds3232_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
180 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
185 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
188 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
191 ret
= regmap_bulk_read(ds3232
->regmap
, DS3232_REG_ALARM1
, buf
, 4);
195 alarm
->time
.tm_sec
= bcd2bin(buf
[0] & 0x7F);
196 alarm
->time
.tm_min
= bcd2bin(buf
[1] & 0x7F);
197 alarm
->time
.tm_hour
= bcd2bin(buf
[2] & 0x7F);
198 alarm
->time
.tm_mday
= bcd2bin(buf
[3] & 0x7F);
200 alarm
->time
.tm_mon
= -1;
201 alarm
->time
.tm_year
= -1;
202 alarm
->time
.tm_wday
= -1;
203 alarm
->time
.tm_yday
= -1;
204 alarm
->time
.tm_isdst
= -1;
206 alarm
->enabled
= !!(control
& DS3232_REG_CR_A1IE
);
207 alarm
->pending
= !!(stat
& DS3232_REG_SR_A1F
);
215 * linux rtc-module does not support wday alarm
216 * and only 24h time mode supported indeed
218 static int ds3232_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
220 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
225 if (ds3232
->irq
<= 0)
228 buf
[0] = bin2bcd(alarm
->time
.tm_sec
);
229 buf
[1] = bin2bcd(alarm
->time
.tm_min
);
230 buf
[2] = bin2bcd(alarm
->time
.tm_hour
);
231 buf
[3] = bin2bcd(alarm
->time
.tm_mday
);
233 /* clear alarm interrupt enable bit */
234 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
237 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
238 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
242 /* clear any pending alarm flag */
243 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
246 stat
&= ~(DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
247 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_SR
, stat
);
251 ret
= regmap_bulk_write(ds3232
->regmap
, DS3232_REG_ALARM1
, buf
, 4);
255 if (alarm
->enabled
) {
256 control
|= DS3232_REG_CR_A1IE
;
257 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
263 static int ds3232_update_alarm(struct device
*dev
, unsigned int enabled
)
265 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
269 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
274 /* enable alarm1 interrupt */
275 control
|= DS3232_REG_CR_A1IE
;
277 /* disable alarm1 interrupt */
278 control
&= ~(DS3232_REG_CR_A1IE
);
279 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
284 static int ds3232_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
286 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
288 if (ds3232
->irq
<= 0)
291 return ds3232_update_alarm(dev
, enabled
);
294 static irqreturn_t
ds3232_irq(int irq
, void *dev_id
)
296 struct device
*dev
= dev_id
;
297 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
298 struct mutex
*lock
= &ds3232
->rtc
->ops_lock
;
304 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
308 if (stat
& DS3232_REG_SR_A1F
) {
309 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
311 dev_warn(ds3232
->dev
,
312 "Read Control Register error %d\n", ret
);
314 /* disable alarm1 interrupt */
315 control
&= ~(DS3232_REG_CR_A1IE
);
316 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
,
319 dev_warn(ds3232
->dev
,
320 "Write Control Register error %d\n",
325 /* clear the alarm pend flag */
326 stat
&= ~DS3232_REG_SR_A1F
;
327 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_SR
, stat
);
329 dev_warn(ds3232
->dev
,
330 "Write Status Register error %d\n",
335 rtc_update_irq(ds3232
->rtc
, 1, RTC_AF
| RTC_IRQF
);
345 static const struct rtc_class_ops ds3232_rtc_ops
= {
346 .read_time
= ds3232_read_time
,
347 .set_time
= ds3232_set_time
,
348 .read_alarm
= ds3232_read_alarm
,
349 .set_alarm
= ds3232_set_alarm
,
350 .alarm_irq_enable
= ds3232_alarm_irq_enable
,
353 static int ds3232_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
356 struct ds3232
*ds3232
;
359 ds3232
= devm_kzalloc(dev
, sizeof(*ds3232
), GFP_KERNEL
);
363 ds3232
->regmap
= regmap
;
366 dev_set_drvdata(dev
, ds3232
);
368 ret
= ds3232_check_rtc_status(dev
);
372 ds3232
->rtc
= devm_rtc_device_register(dev
, name
, &ds3232_rtc_ops
,
374 if (IS_ERR(ds3232
->rtc
))
375 return PTR_ERR(ds3232
->rtc
);
377 if (ds3232
->irq
> 0) {
378 ret
= devm_request_threaded_irq(dev
, ds3232
->irq
, NULL
,
380 IRQF_SHARED
| IRQF_ONESHOT
,
384 dev_err(dev
, "unable to request IRQ\n");
386 device_init_wakeup(dev
, 1);
392 #ifdef CONFIG_PM_SLEEP
393 static int ds3232_suspend(struct device
*dev
)
395 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
397 if (device_may_wakeup(dev
)) {
398 if (enable_irq_wake(ds3232
->irq
))
399 dev_warn_once(dev
, "Cannot set wakeup source\n");
405 static int ds3232_resume(struct device
*dev
)
407 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
409 if (device_may_wakeup(dev
))
410 disable_irq_wake(ds3232
->irq
);
416 static const struct dev_pm_ops ds3232_pm_ops
= {
417 SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend
, ds3232_resume
)
420 #if IS_ENABLED(CONFIG_I2C)
422 static int ds3232_i2c_probe(struct i2c_client
*client
,
423 const struct i2c_device_id
*id
)
425 struct regmap
*regmap
;
426 static const struct regmap_config config
= {
431 regmap
= devm_regmap_init_i2c(client
, &config
);
432 if (IS_ERR(regmap
)) {
433 dev_err(&client
->dev
, "%s: regmap allocation failed: %ld\n",
434 __func__
, PTR_ERR(regmap
));
435 return PTR_ERR(regmap
);
438 return ds3232_probe(&client
->dev
, regmap
, client
->irq
, client
->name
);
441 static const struct i2c_device_id ds3232_id
[] = {
445 MODULE_DEVICE_TABLE(i2c
, ds3232_id
);
447 static struct i2c_driver ds3232_driver
= {
449 .name
= "rtc-ds3232",
450 .pm
= &ds3232_pm_ops
,
452 .probe
= ds3232_i2c_probe
,
453 .id_table
= ds3232_id
,
456 static int ds3232_register_driver(void)
458 return i2c_add_driver(&ds3232_driver
);
461 static void ds3232_unregister_driver(void)
463 i2c_del_driver(&ds3232_driver
);
468 static int ds3232_register_driver(void)
473 static void ds3232_unregister_driver(void)
479 #if IS_ENABLED(CONFIG_SPI_MASTER)
481 static int ds3234_probe(struct spi_device
*spi
)
485 static const struct regmap_config config
= {
488 .write_flag_mask
= 0x80,
490 struct regmap
*regmap
;
492 regmap
= devm_regmap_init_spi(spi
, &config
);
493 if (IS_ERR(regmap
)) {
494 dev_err(&spi
->dev
, "%s: regmap allocation failed: %ld\n",
495 __func__
, PTR_ERR(regmap
));
496 return PTR_ERR(regmap
);
499 spi
->mode
= SPI_MODE_3
;
500 spi
->bits_per_word
= 8;
503 res
= regmap_read(regmap
, DS3232_REG_SECONDS
, &tmp
);
510 * BIT 7 6 5 4 3 2 1 0
511 * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
516 * BIT 7 6 5 4 3 2 1 0
517 * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
521 res
= regmap_read(regmap
, DS3232_REG_CR
, &tmp
);
524 res
= regmap_write(regmap
, DS3232_REG_CR
, tmp
& 0x1c);
528 res
= regmap_read(regmap
, DS3232_REG_SR
, &tmp
);
531 res
= regmap_write(regmap
, DS3232_REG_SR
, tmp
& 0x88);
535 /* Print our settings */
536 res
= regmap_read(regmap
, DS3232_REG_CR
, &tmp
);
539 dev_info(&spi
->dev
, "Control Reg: 0x%02x\n", tmp
);
541 res
= regmap_read(regmap
, DS3232_REG_SR
, &tmp
);
544 dev_info(&spi
->dev
, "Ctrl/Stat Reg: 0x%02x\n", tmp
);
546 return ds3232_probe(&spi
->dev
, regmap
, spi
->irq
, "ds3234");
549 static struct spi_driver ds3234_driver
= {
553 .probe
= ds3234_probe
,
556 static int ds3234_register_driver(void)
558 return spi_register_driver(&ds3234_driver
);
561 static void ds3234_unregister_driver(void)
563 spi_unregister_driver(&ds3234_driver
);
568 static int ds3234_register_driver(void)
573 static void ds3234_unregister_driver(void)
579 static int __init
ds323x_init(void)
583 ret
= ds3232_register_driver();
585 pr_err("Failed to register ds3232 driver: %d\n", ret
);
589 ret
= ds3234_register_driver();
591 pr_err("Failed to register ds3234 driver: %d\n", ret
);
592 ds3232_unregister_driver();
597 module_init(ds323x_init
)
599 static void __exit
ds323x_exit(void)
601 ds3234_unregister_driver();
602 ds3232_unregister_driver();
604 module_exit(ds323x_exit
)
606 MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
607 MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
608 MODULE_DESCRIPTION("Maxim/Dallas DS3232/DS3234 RTC Driver");
609 MODULE_LICENSE("GPL");
610 MODULE_ALIAS("spi:ds3234");