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>
25 #include <linux/hwmon.h>
27 #define DS3232_REG_SECONDS 0x00
28 #define DS3232_REG_MINUTES 0x01
29 #define DS3232_REG_HOURS 0x02
30 #define DS3232_REG_AMPM 0x02
31 #define DS3232_REG_DAY 0x03
32 #define DS3232_REG_DATE 0x04
33 #define DS3232_REG_MONTH 0x05
34 #define DS3232_REG_CENTURY 0x05
35 #define DS3232_REG_YEAR 0x06
36 #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */
37 #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */
38 #define DS3232_REG_CR 0x0E /* Control register */
39 # define DS3232_REG_CR_nEOSC 0x80
40 # define DS3232_REG_CR_INTCN 0x04
41 # define DS3232_REG_CR_A2IE 0x02
42 # define DS3232_REG_CR_A1IE 0x01
44 #define DS3232_REG_SR 0x0F /* control/status register */
45 # define DS3232_REG_SR_OSF 0x80
46 # define DS3232_REG_SR_BSY 0x04
47 # define DS3232_REG_SR_A2F 0x02
48 # define DS3232_REG_SR_A1F 0x01
50 #define DS3232_REG_TEMPERATURE 0x11
54 struct regmap
*regmap
;
56 struct rtc_device
*rtc
;
61 static int ds3232_check_rtc_status(struct device
*dev
)
63 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
67 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
71 if (stat
& DS3232_REG_SR_OSF
)
73 "oscillator discontinuity flagged, "
76 stat
&= ~(DS3232_REG_SR_OSF
| DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
78 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_SR
, stat
);
82 /* If the alarm is pending, clear it before requesting
83 * the interrupt, so an interrupt event isn't reported
84 * before everything is initialized.
87 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
91 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
92 control
|= DS3232_REG_CR_INTCN
;
94 return regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
97 static int ds3232_read_time(struct device
*dev
, struct rtc_time
*time
)
99 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
102 unsigned int year
, month
, day
, hour
, minute
, second
;
103 unsigned int week
, twelve_hr
, am_pm
;
104 unsigned int century
, add_century
= 0;
106 ret
= regmap_bulk_read(ds3232
->regmap
, DS3232_REG_SECONDS
, buf
, 7);
118 /* Extract additional information for AM/PM and century */
120 twelve_hr
= hour
& 0x40;
122 century
= month
& 0x80;
124 /* Write to rtc_time structure */
126 time
->tm_sec
= bcd2bin(second
);
127 time
->tm_min
= bcd2bin(minute
);
129 /* Convert to 24 hr */
131 time
->tm_hour
= bcd2bin(hour
& 0x1F) + 12;
133 time
->tm_hour
= bcd2bin(hour
& 0x1F);
135 time
->tm_hour
= bcd2bin(hour
);
138 /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
139 time
->tm_wday
= bcd2bin(week
) - 1;
140 time
->tm_mday
= bcd2bin(day
);
141 /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
142 time
->tm_mon
= bcd2bin(month
& 0x7F) - 1;
146 time
->tm_year
= bcd2bin(year
) + add_century
;
148 return rtc_valid_tm(time
);
151 static int ds3232_set_time(struct device
*dev
, struct rtc_time
*time
)
153 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
156 /* Extract time from rtc_time and load into ds3232*/
158 buf
[0] = bin2bcd(time
->tm_sec
);
159 buf
[1] = bin2bcd(time
->tm_min
);
160 buf
[2] = bin2bcd(time
->tm_hour
);
161 /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
162 buf
[3] = bin2bcd(time
->tm_wday
+ 1);
163 buf
[4] = bin2bcd(time
->tm_mday
); /* Date */
164 /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
165 buf
[5] = bin2bcd(time
->tm_mon
+ 1);
166 if (time
->tm_year
>= 100) {
168 buf
[6] = bin2bcd(time
->tm_year
- 100);
170 buf
[6] = bin2bcd(time
->tm_year
);
173 return regmap_bulk_write(ds3232
->regmap
, DS3232_REG_SECONDS
, buf
, 7);
177 * DS3232 has two alarm, we only use alarm1
178 * According to linux specification, only support one-shot alarm
179 * no periodic alarm mode
181 static int ds3232_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
183 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
188 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
191 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
194 ret
= regmap_bulk_read(ds3232
->regmap
, DS3232_REG_ALARM1
, buf
, 4);
198 alarm
->time
.tm_sec
= bcd2bin(buf
[0] & 0x7F);
199 alarm
->time
.tm_min
= bcd2bin(buf
[1] & 0x7F);
200 alarm
->time
.tm_hour
= bcd2bin(buf
[2] & 0x7F);
201 alarm
->time
.tm_mday
= bcd2bin(buf
[3] & 0x7F);
203 alarm
->enabled
= !!(control
& DS3232_REG_CR_A1IE
);
204 alarm
->pending
= !!(stat
& DS3232_REG_SR_A1F
);
212 * linux rtc-module does not support wday alarm
213 * and only 24h time mode supported indeed
215 static int ds3232_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
217 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
222 if (ds3232
->irq
<= 0)
225 buf
[0] = bin2bcd(alarm
->time
.tm_sec
);
226 buf
[1] = bin2bcd(alarm
->time
.tm_min
);
227 buf
[2] = bin2bcd(alarm
->time
.tm_hour
);
228 buf
[3] = bin2bcd(alarm
->time
.tm_mday
);
230 /* clear alarm interrupt enable bit */
231 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
234 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
235 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
239 /* clear any pending alarm flag */
240 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
243 stat
&= ~(DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
244 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_SR
, stat
);
248 ret
= regmap_bulk_write(ds3232
->regmap
, DS3232_REG_ALARM1
, buf
, 4);
252 if (alarm
->enabled
) {
253 control
|= DS3232_REG_CR_A1IE
;
254 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
260 static int ds3232_update_alarm(struct device
*dev
, unsigned int enabled
)
262 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
266 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
271 /* enable alarm1 interrupt */
272 control
|= DS3232_REG_CR_A1IE
;
274 /* disable alarm1 interrupt */
275 control
&= ~(DS3232_REG_CR_A1IE
);
276 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
, control
);
282 * Temperature sensor support for ds3232/ds3234 devices.
283 * A user-initiated temperature conversion is not started by this function,
284 * so the temperature is updated once every 64 seconds.
286 static int ds3232_hwmon_read_temp(struct device
*dev
, long int *mC
)
288 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
293 ret
= regmap_bulk_read(ds3232
->regmap
, DS3232_REG_TEMPERATURE
, temp_buf
,
299 * Temperature is represented as a 10-bit code with a resolution of
300 * 0.25 degree celsius and encoded in two's complement format.
302 temp
= (temp_buf
[0] << 8) | temp_buf
[1];
309 static umode_t
ds3232_hwmon_is_visible(const void *data
,
310 enum hwmon_sensor_types type
,
311 u32 attr
, int channel
)
313 if (type
!= hwmon_temp
)
317 case hwmon_temp_input
:
324 static int ds3232_hwmon_read(struct device
*dev
,
325 enum hwmon_sensor_types type
,
326 u32 attr
, int channel
, long *temp
)
331 case hwmon_temp_input
:
332 err
= ds3232_hwmon_read_temp(dev
, temp
);
342 static u32 ds3232_hwmon_chip_config
[] = {
347 static const struct hwmon_channel_info ds3232_hwmon_chip
= {
349 .config
= ds3232_hwmon_chip_config
,
352 static u32 ds3232_hwmon_temp_config
[] = {
357 static const struct hwmon_channel_info ds3232_hwmon_temp
= {
359 .config
= ds3232_hwmon_temp_config
,
362 static const struct hwmon_channel_info
*ds3232_hwmon_info
[] = {
368 static const struct hwmon_ops ds3232_hwmon_hwmon_ops
= {
369 .is_visible
= ds3232_hwmon_is_visible
,
370 .read
= ds3232_hwmon_read
,
373 static const struct hwmon_chip_info ds3232_hwmon_chip_info
= {
374 .ops
= &ds3232_hwmon_hwmon_ops
,
375 .info
= ds3232_hwmon_info
,
378 static void ds3232_hwmon_register(struct device
*dev
, const char *name
)
380 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
381 struct device
*hwmon_dev
;
383 if (!IS_ENABLED(CONFIG_RTC_DRV_DS3232_HWMON
))
386 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, name
, ds3232
,
387 &ds3232_hwmon_chip_info
,
389 if (IS_ERR(hwmon_dev
)) {
390 dev_err(dev
, "unable to register hwmon device %ld\n",
395 static int ds3232_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
397 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
399 if (ds3232
->irq
<= 0)
402 return ds3232_update_alarm(dev
, enabled
);
405 static irqreturn_t
ds3232_irq(int irq
, void *dev_id
)
407 struct device
*dev
= dev_id
;
408 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
409 struct mutex
*lock
= &ds3232
->rtc
->ops_lock
;
415 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_SR
, &stat
);
419 if (stat
& DS3232_REG_SR_A1F
) {
420 ret
= regmap_read(ds3232
->regmap
, DS3232_REG_CR
, &control
);
422 dev_warn(ds3232
->dev
,
423 "Read Control Register error %d\n", ret
);
425 /* disable alarm1 interrupt */
426 control
&= ~(DS3232_REG_CR_A1IE
);
427 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_CR
,
430 dev_warn(ds3232
->dev
,
431 "Write Control Register error %d\n",
436 /* clear the alarm pend flag */
437 stat
&= ~DS3232_REG_SR_A1F
;
438 ret
= regmap_write(ds3232
->regmap
, DS3232_REG_SR
, stat
);
440 dev_warn(ds3232
->dev
,
441 "Write Status Register error %d\n",
446 rtc_update_irq(ds3232
->rtc
, 1, RTC_AF
| RTC_IRQF
);
456 static const struct rtc_class_ops ds3232_rtc_ops
= {
457 .read_time
= ds3232_read_time
,
458 .set_time
= ds3232_set_time
,
459 .read_alarm
= ds3232_read_alarm
,
460 .set_alarm
= ds3232_set_alarm
,
461 .alarm_irq_enable
= ds3232_alarm_irq_enable
,
464 static int ds3232_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
467 struct ds3232
*ds3232
;
470 ds3232
= devm_kzalloc(dev
, sizeof(*ds3232
), GFP_KERNEL
);
474 ds3232
->regmap
= regmap
;
477 dev_set_drvdata(dev
, ds3232
);
479 ret
= ds3232_check_rtc_status(dev
);
484 device_init_wakeup(dev
, 1);
486 ds3232_hwmon_register(dev
, name
);
488 ds3232
->rtc
= devm_rtc_device_register(dev
, name
, &ds3232_rtc_ops
,
490 if (IS_ERR(ds3232
->rtc
))
491 return PTR_ERR(ds3232
->rtc
);
493 if (ds3232
->irq
> 0) {
494 ret
= devm_request_threaded_irq(dev
, ds3232
->irq
, NULL
,
496 IRQF_SHARED
| IRQF_ONESHOT
,
499 device_set_wakeup_capable(dev
, 0);
501 dev_err(dev
, "unable to request IRQ\n");
508 #ifdef CONFIG_PM_SLEEP
509 static int ds3232_suspend(struct device
*dev
)
511 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
513 if (device_may_wakeup(dev
)) {
514 if (enable_irq_wake(ds3232
->irq
))
515 dev_warn_once(dev
, "Cannot set wakeup source\n");
521 static int ds3232_resume(struct device
*dev
)
523 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
525 if (device_may_wakeup(dev
))
526 disable_irq_wake(ds3232
->irq
);
532 static const struct dev_pm_ops ds3232_pm_ops
= {
533 SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend
, ds3232_resume
)
536 #if IS_ENABLED(CONFIG_I2C)
538 static int ds3232_i2c_probe(struct i2c_client
*client
,
539 const struct i2c_device_id
*id
)
541 struct regmap
*regmap
;
542 static const struct regmap_config config
= {
545 .max_register
= 0x13,
548 regmap
= devm_regmap_init_i2c(client
, &config
);
549 if (IS_ERR(regmap
)) {
550 dev_err(&client
->dev
, "%s: regmap allocation failed: %ld\n",
551 __func__
, PTR_ERR(regmap
));
552 return PTR_ERR(regmap
);
555 return ds3232_probe(&client
->dev
, regmap
, client
->irq
, client
->name
);
558 static const struct i2c_device_id ds3232_id
[] = {
562 MODULE_DEVICE_TABLE(i2c
, ds3232_id
);
564 static const struct of_device_id ds3232_of_match
[] = {
565 { .compatible
= "dallas,ds3232" },
568 MODULE_DEVICE_TABLE(of
, ds3232_of_match
);
570 static struct i2c_driver ds3232_driver
= {
572 .name
= "rtc-ds3232",
573 .of_match_table
= of_match_ptr(ds3232_of_match
),
574 .pm
= &ds3232_pm_ops
,
576 .probe
= ds3232_i2c_probe
,
577 .id_table
= ds3232_id
,
580 static int ds3232_register_driver(void)
582 return i2c_add_driver(&ds3232_driver
);
585 static void ds3232_unregister_driver(void)
587 i2c_del_driver(&ds3232_driver
);
592 static int ds3232_register_driver(void)
597 static void ds3232_unregister_driver(void)
603 #if IS_ENABLED(CONFIG_SPI_MASTER)
605 static int ds3234_probe(struct spi_device
*spi
)
609 static const struct regmap_config config
= {
612 .max_register
= 0x13,
613 .write_flag_mask
= 0x80,
615 struct regmap
*regmap
;
617 regmap
= devm_regmap_init_spi(spi
, &config
);
618 if (IS_ERR(regmap
)) {
619 dev_err(&spi
->dev
, "%s: regmap allocation failed: %ld\n",
620 __func__
, PTR_ERR(regmap
));
621 return PTR_ERR(regmap
);
624 spi
->mode
= SPI_MODE_3
;
625 spi
->bits_per_word
= 8;
628 res
= regmap_read(regmap
, DS3232_REG_SECONDS
, &tmp
);
635 * BIT 7 6 5 4 3 2 1 0
636 * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
641 * BIT 7 6 5 4 3 2 1 0
642 * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
646 res
= regmap_read(regmap
, DS3232_REG_CR
, &tmp
);
649 res
= regmap_write(regmap
, DS3232_REG_CR
, tmp
& 0x1c);
653 res
= regmap_read(regmap
, DS3232_REG_SR
, &tmp
);
656 res
= regmap_write(regmap
, DS3232_REG_SR
, tmp
& 0x88);
660 /* Print our settings */
661 res
= regmap_read(regmap
, DS3232_REG_CR
, &tmp
);
664 dev_info(&spi
->dev
, "Control Reg: 0x%02x\n", tmp
);
666 res
= regmap_read(regmap
, DS3232_REG_SR
, &tmp
);
669 dev_info(&spi
->dev
, "Ctrl/Stat Reg: 0x%02x\n", tmp
);
671 return ds3232_probe(&spi
->dev
, regmap
, spi
->irq
, "ds3234");
674 static struct spi_driver ds3234_driver
= {
678 .probe
= ds3234_probe
,
681 static int ds3234_register_driver(void)
683 return spi_register_driver(&ds3234_driver
);
686 static void ds3234_unregister_driver(void)
688 spi_unregister_driver(&ds3234_driver
);
693 static int ds3234_register_driver(void)
698 static void ds3234_unregister_driver(void)
704 static int __init
ds323x_init(void)
708 ret
= ds3232_register_driver();
710 pr_err("Failed to register ds3232 driver: %d\n", ret
);
714 ret
= ds3234_register_driver();
716 pr_err("Failed to register ds3234 driver: %d\n", ret
);
717 ds3232_unregister_driver();
722 module_init(ds323x_init
)
724 static void __exit
ds323x_exit(void)
726 ds3234_unregister_driver();
727 ds3232_unregister_driver();
729 module_exit(ds323x_exit
)
731 MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
732 MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
733 MODULE_DESCRIPTION("Maxim/Dallas DS3232/DS3234 RTC Driver");
734 MODULE_LICENSE("GPL");
735 MODULE_ALIAS("spi:ds3234");