2 * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C
4 * Copyright (C) 2009-2010 Freescale Semiconductor.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
13 * recommened in .../Documentation/i2c/writing-clients section
14 * "Sending and receiving", using SMBus level communication is preferred.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/rtc.h>
22 #include <linux/bcd.h>
23 #include <linux/workqueue.h>
24 #include <linux/slab.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
50 struct i2c_client
*client
;
51 struct rtc_device
*rtc
;
52 struct work_struct work
;
54 /* The mutex protects alarm operations, and prevents a race
55 * between the enable_irq() in the workqueue and the free_irq()
56 * in the remove function.
62 static struct i2c_driver ds3232_driver
;
64 static int ds3232_check_rtc_status(struct i2c_client
*client
)
69 stat
= i2c_smbus_read_byte_data(client
, DS3232_REG_SR
);
73 if (stat
& DS3232_REG_SR_OSF
)
74 dev_warn(&client
->dev
,
75 "oscillator discontinuity flagged, "
78 stat
&= ~(DS3232_REG_SR_OSF
| DS3232_REG_SR_A1F
| DS3232_REG_SR_A2F
);
80 ret
= i2c_smbus_write_byte_data(client
, DS3232_REG_SR
, stat
);
84 /* If the alarm is pending, clear it before requesting
85 * the interrupt, so an interrupt event isn't reported
86 * before everything is initialized.
89 control
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
93 control
&= ~(DS3232_REG_CR_A1IE
| DS3232_REG_CR_A2IE
);
94 control
|= DS3232_REG_CR_INTCN
;
96 return i2c_smbus_write_byte_data(client
, DS3232_REG_CR
, control
);
99 static int ds3232_read_time(struct device
*dev
, struct rtc_time
*time
)
101 struct i2c_client
*client
= to_i2c_client(dev
);
104 unsigned int year
, month
, day
, hour
, minute
, second
;
105 unsigned int week
, twelve_hr
, am_pm
;
106 unsigned int century
, add_century
= 0;
108 ret
= i2c_smbus_read_i2c_block_data(client
, DS3232_REG_SECONDS
, 7, buf
);
123 /* Extract additional information for AM/PM and century */
125 twelve_hr
= hour
& 0x40;
127 century
= month
& 0x80;
129 /* Write to rtc_time structure */
131 time
->tm_sec
= bcd2bin(second
);
132 time
->tm_min
= bcd2bin(minute
);
134 /* Convert to 24 hr */
136 time
->tm_hour
= bcd2bin(hour
& 0x1F) + 12;
138 time
->tm_hour
= bcd2bin(hour
& 0x1F);
140 time
->tm_hour
= bcd2bin(hour
);
143 time
->tm_wday
= bcd2bin(week
);
144 time
->tm_mday
= bcd2bin(day
);
145 time
->tm_mon
= bcd2bin(month
& 0x7F);
149 time
->tm_year
= bcd2bin(year
) + add_century
;
151 return rtc_valid_tm(time
);
154 static int ds3232_set_time(struct device
*dev
, struct rtc_time
*time
)
156 struct i2c_client
*client
= to_i2c_client(dev
);
159 /* Extract time from rtc_time and load into ds3232*/
161 buf
[0] = bin2bcd(time
->tm_sec
);
162 buf
[1] = bin2bcd(time
->tm_min
);
163 buf
[2] = bin2bcd(time
->tm_hour
);
164 buf
[3] = bin2bcd(time
->tm_wday
); /* Day of the week */
165 buf
[4] = bin2bcd(time
->tm_mday
); /* Date */
166 buf
[5] = bin2bcd(time
->tm_mon
);
167 if (time
->tm_year
>= 100) {
169 buf
[6] = bin2bcd(time
->tm_year
- 100);
171 buf
[6] = bin2bcd(time
->tm_year
);
174 return i2c_smbus_write_i2c_block_data(client
,
175 DS3232_REG_SECONDS
, 7, buf
);
178 static irqreturn_t
ds3232_irq(int irq
, void *dev_id
)
180 struct i2c_client
*client
= dev_id
;
181 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
183 disable_irq_nosync(irq
);
184 schedule_work(&ds3232
->work
);
188 static void ds3232_work(struct work_struct
*work
)
190 struct ds3232
*ds3232
= container_of(work
, struct ds3232
, work
);
191 struct i2c_client
*client
= ds3232
->client
;
194 mutex_lock(&ds3232
->mutex
);
196 stat
= i2c_smbus_read_byte_data(client
, DS3232_REG_SR
);
200 if (stat
& DS3232_REG_SR_A1F
) {
201 control
= i2c_smbus_read_byte_data(client
, DS3232_REG_CR
);
204 /* disable alarm1 interrupt */
205 control
&= ~(DS3232_REG_CR_A1IE
);
206 i2c_smbus_write_byte_data(client
, DS3232_REG_CR
, control
);
208 /* clear the alarm pend flag */
209 stat
&= ~DS3232_REG_SR_A1F
;
210 i2c_smbus_write_byte_data(client
, DS3232_REG_SR
, stat
);
212 rtc_update_irq(ds3232
->rtc
, 1, RTC_AF
| RTC_IRQF
);
216 if (!ds3232
->exiting
)
217 enable_irq(client
->irq
);
219 mutex_unlock(&ds3232
->mutex
);
222 static const struct rtc_class_ops ds3232_rtc_ops
= {
223 .read_time
= ds3232_read_time
,
224 .set_time
= ds3232_set_time
,
227 static int __devinit
ds3232_probe(struct i2c_client
*client
,
228 const struct i2c_device_id
*id
)
230 struct ds3232
*ds3232
;
233 ds3232
= kzalloc(sizeof(struct ds3232
), GFP_KERNEL
);
237 ds3232
->client
= client
;
238 i2c_set_clientdata(client
, ds3232
);
240 INIT_WORK(&ds3232
->work
, ds3232_work
);
241 mutex_init(&ds3232
->mutex
);
243 ret
= ds3232_check_rtc_status(client
);
247 ds3232
->rtc
= rtc_device_register(client
->name
, &client
->dev
,
248 &ds3232_rtc_ops
, THIS_MODULE
);
249 if (IS_ERR(ds3232
->rtc
)) {
250 ret
= PTR_ERR(ds3232
->rtc
);
251 dev_err(&client
->dev
, "unable to register the class device\n");
255 if (client
->irq
>= 0) {
256 ret
= request_irq(client
->irq
, ds3232_irq
, 0,
259 dev_err(&client
->dev
, "unable to request IRQ\n");
267 if (client
->irq
>= 0)
268 free_irq(client
->irq
, client
);
271 i2c_set_clientdata(client
, NULL
);
276 static int __devexit
ds3232_remove(struct i2c_client
*client
)
278 struct ds3232
*ds3232
= i2c_get_clientdata(client
);
280 if (client
->irq
>= 0) {
281 mutex_lock(&ds3232
->mutex
);
283 mutex_unlock(&ds3232
->mutex
);
285 free_irq(client
->irq
, client
);
286 flush_scheduled_work();
289 rtc_device_unregister(ds3232
->rtc
);
290 i2c_set_clientdata(client
, NULL
);
295 static const struct i2c_device_id ds3232_id
[] = {
299 MODULE_DEVICE_TABLE(i2c
, ds3232_id
);
301 static struct i2c_driver ds3232_driver
= {
303 .name
= "rtc-ds3232",
304 .owner
= THIS_MODULE
,
306 .probe
= ds3232_probe
,
307 .remove
= __devexit_p(ds3232_remove
),
308 .id_table
= ds3232_id
,
311 static int __init
ds3232_init(void)
313 return i2c_add_driver(&ds3232_driver
);
316 static void __exit
ds3232_exit(void)
318 i2c_del_driver(&ds3232_driver
);
321 module_init(ds3232_init
);
322 module_exit(ds3232_exit
);
324 MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
325 MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");
326 MODULE_LICENSE("GPL");