2 * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
4 * Based on code by Randy Vinson <rvinson@mvista.com>,
5 * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
7 * Copyright (C) 2006-2007 Freescale Semiconductor
9 * 2005 (c) MontaVista Software, Inc. This file is licensed under
10 * the terms of the GNU General Public License version 2. This program
11 * is licensed "as is" without any warranty of any kind, whether express
15 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
16 * recommened in .../Documentation/i2c/writing-clients section
17 * "Sending and receiving", using SMBus level communication is preferred.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/i2c.h>
24 #include <linux/rtc.h>
25 #include <linux/bcd.h>
26 #include <linux/workqueue.h>
28 #define DS1374_REG_TOD0 0x00 /* Time of Day */
29 #define DS1374_REG_TOD1 0x01
30 #define DS1374_REG_TOD2 0x02
31 #define DS1374_REG_TOD3 0x03
32 #define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */
33 #define DS1374_REG_WDALM1 0x05
34 #define DS1374_REG_WDALM2 0x06
35 #define DS1374_REG_CR 0x07 /* Control */
36 #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
37 #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
38 #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
39 #define DS1374_REG_SR 0x08 /* Status */
40 #define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */
41 #define DS1374_REG_SR_AF 0x01 /* Alarm Flag */
42 #define DS1374_REG_TCR 0x09 /* Trickle Charge */
44 static const struct i2c_device_id ds1374_id
[] = {
48 MODULE_DEVICE_TABLE(i2c
, ds1374_id
);
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.
63 static struct i2c_driver ds1374_driver
;
65 static int ds1374_read_rtc(struct i2c_client
*client
, u32
*time
,
77 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, nbytes
, buf
);
84 for (i
= nbytes
- 1, *time
= 0; i
>= 0; i
--)
85 *time
= (*time
<< 8) | buf
[i
];
90 static int ds1374_write_rtc(struct i2c_client
*client
, u32 time
,
101 for (i
= 0; i
< nbytes
; i
++) {
102 buf
[i
] = time
& 0xff;
106 return i2c_smbus_write_i2c_block_data(client
, reg
, nbytes
, buf
);
109 static int ds1374_check_rtc_status(struct i2c_client
*client
)
114 stat
= i2c_smbus_read_byte_data(client
, DS1374_REG_SR
);
118 if (stat
& DS1374_REG_SR_OSF
)
119 dev_warn(&client
->dev
,
120 "oscillator discontinuity flagged, "
121 "time unreliable\n");
123 stat
&= ~(DS1374_REG_SR_OSF
| DS1374_REG_SR_AF
);
125 ret
= i2c_smbus_write_byte_data(client
, DS1374_REG_SR
, stat
);
129 /* If the alarm is pending, clear it before requesting
130 * the interrupt, so an interrupt event isn't reported
131 * before everything is initialized.
134 control
= i2c_smbus_read_byte_data(client
, DS1374_REG_CR
);
138 control
&= ~(DS1374_REG_CR_WACE
| DS1374_REG_CR_AIE
);
139 return i2c_smbus_write_byte_data(client
, DS1374_REG_CR
, control
);
142 static int ds1374_read_time(struct device
*dev
, struct rtc_time
*time
)
144 struct i2c_client
*client
= to_i2c_client(dev
);
148 ret
= ds1374_read_rtc(client
, &itime
, DS1374_REG_TOD0
, 4);
150 rtc_time_to_tm(itime
, time
);
155 static int ds1374_set_time(struct device
*dev
, struct rtc_time
*time
)
157 struct i2c_client
*client
= to_i2c_client(dev
);
160 rtc_tm_to_time(time
, &itime
);
161 return ds1374_write_rtc(client
, itime
, DS1374_REG_TOD0
, 4);
164 /* The ds1374 has a decrementer for an alarm, rather than a comparator.
165 * If the time of day is changed, then the alarm will need to be
168 static int ds1374_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
170 struct i2c_client
*client
= to_i2c_client(dev
);
171 struct ds1374
*ds1374
= i2c_get_clientdata(client
);
176 if (client
->irq
<= 0)
179 mutex_lock(&ds1374
->mutex
);
181 cr
= ret
= i2c_smbus_read_byte_data(client
, DS1374_REG_CR
);
185 sr
= ret
= i2c_smbus_read_byte_data(client
, DS1374_REG_SR
);
189 ret
= ds1374_read_rtc(client
, &now
, DS1374_REG_TOD0
, 4);
193 ret
= ds1374_read_rtc(client
, &cur_alarm
, DS1374_REG_WDALM0
, 3);
197 rtc_time_to_tm(now
+ cur_alarm
, &alarm
->time
);
198 alarm
->enabled
= !!(cr
& DS1374_REG_CR_WACE
);
199 alarm
->pending
= !!(sr
& DS1374_REG_SR_AF
);
202 mutex_unlock(&ds1374
->mutex
);
206 static int ds1374_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
208 struct i2c_client
*client
= to_i2c_client(dev
);
209 struct ds1374
*ds1374
= i2c_get_clientdata(client
);
211 unsigned long new_alarm
, itime
;
215 if (client
->irq
<= 0)
218 ret
= ds1374_read_time(dev
, &now
);
222 rtc_tm_to_time(&alarm
->time
, &new_alarm
);
223 rtc_tm_to_time(&now
, &itime
);
225 /* This can happen due to races, in addition to dates that are
226 * truly in the past. To avoid requiring the caller to check for
227 * races, dates in the past are assumed to be in the recent past
228 * (i.e. not something that we'd rather the caller know about via
229 * an error), and the alarm is set to go off as soon as possible.
231 if (time_before_eq(new_alarm
, itime
))
236 mutex_lock(&ds1374
->mutex
);
238 ret
= cr
= i2c_smbus_read_byte_data(client
, DS1374_REG_CR
);
242 /* Disable any existing alarm before setting the new one
243 * (or lack thereof). */
244 cr
&= ~DS1374_REG_CR_WACE
;
246 ret
= i2c_smbus_write_byte_data(client
, DS1374_REG_CR
, cr
);
250 ret
= ds1374_write_rtc(client
, new_alarm
, DS1374_REG_WDALM0
, 3);
254 if (alarm
->enabled
) {
255 cr
|= DS1374_REG_CR_WACE
| DS1374_REG_CR_AIE
;
256 cr
&= ~DS1374_REG_CR_WDALM
;
258 ret
= i2c_smbus_write_byte_data(client
, DS1374_REG_CR
, cr
);
262 mutex_unlock(&ds1374
->mutex
);
266 static irqreturn_t
ds1374_irq(int irq
, void *dev_id
)
268 struct i2c_client
*client
= dev_id
;
269 struct ds1374
*ds1374
= i2c_get_clientdata(client
);
271 disable_irq_nosync(irq
);
272 schedule_work(&ds1374
->work
);
276 static void ds1374_work(struct work_struct
*work
)
278 struct ds1374
*ds1374
= container_of(work
, struct ds1374
, work
);
279 struct i2c_client
*client
= ds1374
->client
;
282 mutex_lock(&ds1374
->mutex
);
284 stat
= i2c_smbus_read_byte_data(client
, DS1374_REG_SR
);
288 if (stat
& DS1374_REG_SR_AF
) {
289 stat
&= ~DS1374_REG_SR_AF
;
290 i2c_smbus_write_byte_data(client
, DS1374_REG_SR
, stat
);
292 control
= i2c_smbus_read_byte_data(client
, DS1374_REG_CR
);
296 control
&= ~(DS1374_REG_CR_WACE
| DS1374_REG_CR_AIE
);
297 i2c_smbus_write_byte_data(client
, DS1374_REG_CR
, control
);
299 rtc_update_irq(ds1374
->rtc
, 1, RTC_AF
| RTC_IRQF
);
303 if (!ds1374
->exiting
)
304 enable_irq(client
->irq
);
306 mutex_unlock(&ds1374
->mutex
);
309 static int ds1374_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
311 struct i2c_client
*client
= to_i2c_client(dev
);
312 struct ds1374
*ds1374
= i2c_get_clientdata(client
);
313 int ret
= -ENOIOCTLCMD
;
315 mutex_lock(&ds1374
->mutex
);
319 ret
= i2c_smbus_read_byte_data(client
, DS1374_REG_CR
);
323 ret
&= ~DS1374_REG_CR_WACE
;
325 ret
= i2c_smbus_write_byte_data(client
, DS1374_REG_CR
, ret
);
332 ret
= i2c_smbus_read_byte_data(client
, DS1374_REG_CR
);
336 ret
|= DS1374_REG_CR_WACE
| DS1374_REG_CR_AIE
;
337 ret
&= ~DS1374_REG_CR_WDALM
;
339 ret
= i2c_smbus_write_byte_data(client
, DS1374_REG_CR
, ret
);
347 mutex_unlock(&ds1374
->mutex
);
351 static const struct rtc_class_ops ds1374_rtc_ops
= {
352 .read_time
= ds1374_read_time
,
353 .set_time
= ds1374_set_time
,
354 .read_alarm
= ds1374_read_alarm
,
355 .set_alarm
= ds1374_set_alarm
,
356 .ioctl
= ds1374_ioctl
,
359 static int ds1374_probe(struct i2c_client
*client
,
360 const struct i2c_device_id
*id
)
362 struct ds1374
*ds1374
;
365 ds1374
= kzalloc(sizeof(struct ds1374
), GFP_KERNEL
);
369 ds1374
->client
= client
;
370 i2c_set_clientdata(client
, ds1374
);
372 INIT_WORK(&ds1374
->work
, ds1374_work
);
373 mutex_init(&ds1374
->mutex
);
375 ret
= ds1374_check_rtc_status(client
);
379 if (client
->irq
> 0) {
380 ret
= request_irq(client
->irq
, ds1374_irq
, 0,
383 dev_err(&client
->dev
, "unable to request IRQ\n");
388 ds1374
->rtc
= rtc_device_register(client
->name
, &client
->dev
,
389 &ds1374_rtc_ops
, THIS_MODULE
);
390 if (IS_ERR(ds1374
->rtc
)) {
391 ret
= PTR_ERR(ds1374
->rtc
);
392 dev_err(&client
->dev
, "unable to register the class device\n");
400 free_irq(client
->irq
, client
);
403 i2c_set_clientdata(client
, NULL
);
408 static int __devexit
ds1374_remove(struct i2c_client
*client
)
410 struct ds1374
*ds1374
= i2c_get_clientdata(client
);
412 if (client
->irq
> 0) {
413 mutex_lock(&ds1374
->mutex
);
415 mutex_unlock(&ds1374
->mutex
);
417 free_irq(client
->irq
, client
);
418 flush_scheduled_work();
421 rtc_device_unregister(ds1374
->rtc
);
422 i2c_set_clientdata(client
, NULL
);
428 static int ds1374_suspend(struct i2c_client
*client
, pm_message_t state
)
430 if (client
->irq
>= 0 && device_may_wakeup(&client
->dev
))
431 enable_irq_wake(client
->irq
);
435 static int ds1374_resume(struct i2c_client
*client
)
437 if (client
->irq
>= 0 && device_may_wakeup(&client
->dev
))
438 disable_irq_wake(client
->irq
);
442 #define ds1374_suspend NULL
443 #define ds1374_resume NULL
446 static struct i2c_driver ds1374_driver
= {
448 .name
= "rtc-ds1374",
449 .owner
= THIS_MODULE
,
451 .probe
= ds1374_probe
,
452 .suspend
= ds1374_suspend
,
453 .resume
= ds1374_resume
,
454 .remove
= __devexit_p(ds1374_remove
),
455 .id_table
= ds1374_id
,
458 static int __init
ds1374_init(void)
460 return i2c_add_driver(&ds1374_driver
);
463 static void __exit
ds1374_exit(void)
465 i2c_del_driver(&ds1374_driver
);
468 module_init(ds1374_init
);
469 module_exit(ds1374_exit
);
471 MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
472 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
473 MODULE_LICENSE("GPL");