2 * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
4 * Copyright (C) 2009-2011 Freescale Semiconductor.
5 * Author: Jack Lan <jack.lan@freescale.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
14 * recommened in .../Documentation/i2c/writing-clients section
15 * "Sending and receiving", using SMBus level communication is preferred.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
21 #include <linux/i2c.h>
22 #include <linux/rtc.h>
23 #include <linux/bcd.h>
24 #include <linux/workqueue.h>
25 #include <linux/slab.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
51 struct i2c_client
*client
;
52 struct rtc_device
*rtc
;
53 struct work_struct work
;
55 /* The mutex protects alarm operations, and prevents a race
56 * between the enable_irq() in the workqueue and the free_irq()
57 * in the remove function.
64 static struct i2c_driver ds3232_driver
;
66 static int ds3232_check_rtc_status(struct i2c_client
*client
)
71 stat
= i2c_smbus_read_byte_data(client
, DS3232_REG_SR
);
75 if (stat
& DS3232_REG_SR_OSF
)
76 dev_warn(&client
->dev
,
77 "oscillator discontinuity flagged, "
80 stat
&= ~(DS3232_REG_SR_OSF
| DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
82 ret
= i2c_smbus_write_byte_data(client
, DS3232_REG_SR
, stat
);
86 /* If the alarm is pending, clear it before requesting
87 * the interrupt, so an interrupt event isn't reported
88 * before everything is initialized.
91 control
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
95 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
96 control
|= DS3232_REG_CR_INTCN
;
98 return i2c_smbus_write_byte_data(client
, DS3232_REG_CR
, control
);
101 static int ds3232_read_time(struct device
*dev
, struct rtc_time
*time
)
103 struct i2c_client
*client
= to_i2c_client(dev
);
106 unsigned int year
, month
, day
, hour
, minute
, second
;
107 unsigned int week
, twelve_hr
, am_pm
;
108 unsigned int century
, add_century
= 0;
110 ret
= i2c_smbus_read_i2c_block_data(client
, DS3232_REG_SECONDS
, 7, buf
);
125 /* Extract additional information for AM/PM and century */
127 twelve_hr
= hour
& 0x40;
129 century
= month
& 0x80;
131 /* Write to rtc_time structure */
133 time
->tm_sec
= bcd2bin(second
);
134 time
->tm_min
= bcd2bin(minute
);
136 /* Convert to 24 hr */
138 time
->tm_hour
= bcd2bin(hour
& 0x1F) + 12;
140 time
->tm_hour
= bcd2bin(hour
& 0x1F);
142 time
->tm_hour
= bcd2bin(hour
);
145 /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
146 time
->tm_wday
= bcd2bin(week
) - 1;
147 time
->tm_mday
= bcd2bin(day
);
148 /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
149 time
->tm_mon
= bcd2bin(month
& 0x7F) - 1;
153 time
->tm_year
= bcd2bin(year
) + add_century
;
155 return rtc_valid_tm(time
);
158 static int ds3232_set_time(struct device
*dev
, struct rtc_time
*time
)
160 struct i2c_client
*client
= to_i2c_client(dev
);
163 /* Extract time from rtc_time and load into ds3232*/
165 buf
[0] = bin2bcd(time
->tm_sec
);
166 buf
[1] = bin2bcd(time
->tm_min
);
167 buf
[2] = bin2bcd(time
->tm_hour
);
168 /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
169 buf
[3] = bin2bcd(time
->tm_wday
+ 1);
170 buf
[4] = bin2bcd(time
->tm_mday
); /* Date */
171 /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
172 buf
[5] = bin2bcd(time
->tm_mon
+ 1);
173 if (time
->tm_year
>= 100) {
175 buf
[6] = bin2bcd(time
->tm_year
- 100);
177 buf
[6] = bin2bcd(time
->tm_year
);
180 return i2c_smbus_write_i2c_block_data(client
,
181 DS3232_REG_SECONDS
, 7, buf
);
185 * DS3232 has two alarm, we only use alarm1
186 * According to linux specification, only support one-shot alarm
187 * no periodic alarm mode
189 static int ds3232_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
191 struct i2c_client
*client
= to_i2c_client(dev
);
192 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
197 mutex_lock(&ds3232
->mutex
);
199 ret
= i2c_smbus_read_byte_data(client
, DS3232_REG_SR
);
203 ret
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
207 ret
= i2c_smbus_read_i2c_block_data(client
, DS3232_REG_ALARM1
, 4, buf
);
211 alarm
->time
.tm_sec
= bcd2bin(buf
[0] & 0x7F);
212 alarm
->time
.tm_min
= bcd2bin(buf
[1] & 0x7F);
213 alarm
->time
.tm_hour
= bcd2bin(buf
[2] & 0x7F);
214 alarm
->time
.tm_mday
= bcd2bin(buf
[3] & 0x7F);
216 alarm
->time
.tm_mon
= -1;
217 alarm
->time
.tm_year
= -1;
218 alarm
->time
.tm_wday
= -1;
219 alarm
->time
.tm_yday
= -1;
220 alarm
->time
.tm_isdst
= -1;
222 alarm
->enabled
= !!(control
& DS3232_REG_CR_A1IE
);
223 alarm
->pending
= !!(stat
& DS3232_REG_SR_A1F
);
227 mutex_unlock(&ds3232
->mutex
);
232 * linux rtc-module does not support wday alarm
233 * and only 24h time mode supported indeed
235 static int ds3232_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
237 struct i2c_client
*client
= to_i2c_client(dev
);
238 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
243 if (client
->irq
<= 0)
246 mutex_lock(&ds3232
->mutex
);
248 buf
[0] = bin2bcd(alarm
->time
.tm_sec
);
249 buf
[1] = bin2bcd(alarm
->time
.tm_min
);
250 buf
[2] = bin2bcd(alarm
->time
.tm_hour
);
251 buf
[3] = bin2bcd(alarm
->time
.tm_mday
);
253 /* clear alarm interrupt enable bit */
254 ret
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
258 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
259 ret
= i2c_smbus_write_byte_data(client
, DS3232_REG_CR
, control
);
263 /* clear any pending alarm flag */
264 ret
= i2c_smbus_read_byte_data(client
, DS3232_REG_SR
);
268 stat
&= ~(DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
269 ret
= i2c_smbus_write_byte_data(client
, DS3232_REG_SR
, stat
);
273 ret
= i2c_smbus_write_i2c_block_data(client
, DS3232_REG_ALARM1
, 4, buf
);
275 if (alarm
->enabled
) {
276 control
|= DS3232_REG_CR_A1IE
;
277 ret
= i2c_smbus_write_byte_data(client
, DS3232_REG_CR
, control
);
280 mutex_unlock(&ds3232
->mutex
);
284 static void ds3232_update_alarm(struct i2c_client
*client
)
286 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
291 mutex_lock(&ds3232
->mutex
);
293 ret
= i2c_smbus_read_i2c_block_data(client
, DS3232_REG_ALARM1
, 4, buf
);
297 buf
[0] = bcd2bin(buf
[0]) < 0 || (ds3232
->rtc
->irq_data
& RTC_UF
) ?
299 buf
[1] = bcd2bin(buf
[1]) < 0 || (ds3232
->rtc
->irq_data
& RTC_UF
) ?
301 buf
[2] = bcd2bin(buf
[2]) < 0 || (ds3232
->rtc
->irq_data
& RTC_UF
) ?
303 buf
[3] = bcd2bin(buf
[3]) < 0 || (ds3232
->rtc
->irq_data
& RTC_UF
) ?
306 ret
= i2c_smbus_write_i2c_block_data(client
, DS3232_REG_ALARM1
, 4, buf
);
310 control
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
314 if (ds3232
->rtc
->irq_data
& (RTC_AF
| RTC_UF
))
315 /* enable alarm1 interrupt */
316 control
|= DS3232_REG_CR_A1IE
;
318 /* disable alarm1 interrupt */
319 control
&= ~(DS3232_REG_CR_A1IE
);
320 i2c_smbus_write_byte_data(client
, DS3232_REG_CR
, control
);
323 mutex_unlock(&ds3232
->mutex
);
326 static int ds3232_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
328 struct i2c_client
*client
= to_i2c_client(dev
);
329 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
331 if (client
->irq
<= 0)
335 ds3232
->rtc
->irq_data
|= RTC_AF
;
337 ds3232
->rtc
->irq_data
&= ~RTC_AF
;
339 ds3232_update_alarm(client
);
343 static irqreturn_t
ds3232_irq(int irq
, void *dev_id
)
345 struct i2c_client
*client
= dev_id
;
346 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
348 disable_irq_nosync(irq
);
351 * If rtc as a wakeup source, can't schedule the work
352 * at system resume flow, because at this time the i2c bus
353 * has not been resumed.
355 if (!ds3232
->suspended
)
356 schedule_work(&ds3232
->work
);
361 static void ds3232_work(struct work_struct
*work
)
363 struct ds3232
*ds3232
= container_of(work
, struct ds3232
, work
);
364 struct i2c_client
*client
= ds3232
->client
;
367 mutex_lock(&ds3232
->mutex
);
369 stat
= i2c_smbus_read_byte_data(client
, DS3232_REG_SR
);
373 if (stat
& DS3232_REG_SR_A1F
) {
374 control
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
376 pr_warn("Read DS3232 Control Register error."
377 "Disable IRQ%d.\n", client
->irq
);
379 /* disable alarm1 interrupt */
380 control
&= ~(DS3232_REG_CR_A1IE
);
381 i2c_smbus_write_byte_data(client
, DS3232_REG_CR
,
384 /* clear the alarm pend flag */
385 stat
&= ~DS3232_REG_SR_A1F
;
386 i2c_smbus_write_byte_data(client
, DS3232_REG_SR
, stat
);
388 rtc_update_irq(ds3232
->rtc
, 1, RTC_AF
| RTC_IRQF
);
390 if (!ds3232
->exiting
)
391 enable_irq(client
->irq
);
396 mutex_unlock(&ds3232
->mutex
);
399 static const struct rtc_class_ops ds3232_rtc_ops
= {
400 .read_time
= ds3232_read_time
,
401 .set_time
= ds3232_set_time
,
402 .read_alarm
= ds3232_read_alarm
,
403 .set_alarm
= ds3232_set_alarm
,
404 .alarm_irq_enable
= ds3232_alarm_irq_enable
,
407 static int ds3232_probe(struct i2c_client
*client
,
408 const struct i2c_device_id
*id
)
410 struct ds3232
*ds3232
;
413 ds3232
= devm_kzalloc(&client
->dev
, sizeof(struct ds3232
), GFP_KERNEL
);
417 ds3232
->client
= client
;
418 i2c_set_clientdata(client
, ds3232
);
420 INIT_WORK(&ds3232
->work
, ds3232_work
);
421 mutex_init(&ds3232
->mutex
);
423 ret
= ds3232_check_rtc_status(client
);
427 if (client
->irq
> 0) {
428 ret
= devm_request_irq(&client
->dev
, client
->irq
, ds3232_irq
,
429 IRQF_SHARED
, "ds3232", client
);
431 dev_err(&client
->dev
, "unable to request IRQ\n");
433 device_init_wakeup(&client
->dev
, 1);
435 ds3232
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
436 &ds3232_rtc_ops
, THIS_MODULE
);
437 return PTR_ERR_OR_ZERO(ds3232
->rtc
);
440 static int ds3232_remove(struct i2c_client
*client
)
442 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
444 if (client
->irq
>= 0) {
445 mutex_lock(&ds3232
->mutex
);
447 mutex_unlock(&ds3232
->mutex
);
449 devm_free_irq(&client
->dev
, client
->irq
, client
);
450 cancel_work_sync(&ds3232
->work
);
456 #ifdef CONFIG_PM_SLEEP
457 static int ds3232_suspend(struct device
*dev
)
459 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
460 struct i2c_client
*client
= to_i2c_client(dev
);
462 if (device_can_wakeup(dev
)) {
463 ds3232
->suspended
= true;
464 irq_set_irq_wake(client
->irq
, 1);
470 static int ds3232_resume(struct device
*dev
)
472 struct ds3232
*ds3232
= dev_get_drvdata(dev
);
473 struct i2c_client
*client
= to_i2c_client(dev
);
475 if (ds3232
->suspended
) {
476 ds3232
->suspended
= false;
478 /* Clear the hardware alarm pend flag */
479 schedule_work(&ds3232
->work
);
481 irq_set_irq_wake(client
->irq
, 0);
488 static const struct dev_pm_ops ds3232_pm_ops
= {
489 SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend
, ds3232_resume
)
492 static const struct i2c_device_id ds3232_id
[] = {
496 MODULE_DEVICE_TABLE(i2c
, ds3232_id
);
498 static struct i2c_driver ds3232_driver
= {
500 .name
= "rtc-ds3232",
501 .owner
= THIS_MODULE
,
502 .pm
= &ds3232_pm_ops
,
504 .probe
= ds3232_probe
,
505 .remove
= ds3232_remove
,
506 .id_table
= ds3232_id
,
509 module_i2c_driver(ds3232_driver
);
511 MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
512 MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
513 MODULE_LICENSE("GPL");