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 */
45 struct i2c_client *client;
46 struct rtc_device *rtc;
47 struct work_struct work;
49 /* The mutex protects alarm operations, and prevents a race
50 * between the enable_irq() in the workqueue and the free_irq()
51 * in the remove function.
57 static struct i2c_driver ds1374_driver;
59 static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
71 ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
78 for (i = nbytes - 1, *time = 0; i >= 0; i--)
79 *time = (*time << 8) | buf[i];
84 static int ds1374_write_rtc(struct i2c_client *client, u32 time,
95 for (i = 0; i < nbytes; i++) {
100 return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
103 static int ds1374_check_rtc_status(struct i2c_client *client)
108 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
112 if (stat & DS1374_REG_SR_OSF)
113 dev_warn(&client->dev,
114 "oscillator discontinuity flagged, "
115 "time unreliable\n");
117 stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
119 ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
123 /* If the alarm is pending, clear it before requesting
124 * the interrupt, so an interrupt event isn't reported
125 * before everything is initialized.
128 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
132 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
133 return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
136 static int ds1374_read_time(struct device *dev, struct rtc_time *time)
138 struct i2c_client *client = to_i2c_client(dev);
142 ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
144 rtc_time_to_tm(itime, time);
149 static int ds1374_set_time(struct device *dev, struct rtc_time *time)
151 struct i2c_client *client = to_i2c_client(dev);
154 rtc_tm_to_time(time, &itime);
155 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
158 /* The ds1374 has a decrementer for an alarm, rather than a comparator.
159 * If the time of day is changed, then the alarm will need to be
162 static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
164 struct i2c_client *client = to_i2c_client(dev);
165 struct ds1374 *ds1374 = i2c_get_clientdata(client);
173 mutex_lock(&ds1374->mutex);
175 cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
179 sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
183 ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
187 ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
191 rtc_time_to_tm(now + cur_alarm, &alarm->time);
192 alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
193 alarm->pending = !!(sr & DS1374_REG_SR_AF);
196 mutex_unlock(&ds1374->mutex);
200 static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
202 struct i2c_client *client = to_i2c_client(dev);
203 struct ds1374 *ds1374 = i2c_get_clientdata(client);
205 unsigned long new_alarm, itime;
212 ret = ds1374_read_time(dev, &now);
216 rtc_tm_to_time(&alarm->time, &new_alarm);
217 rtc_tm_to_time(&now, &itime);
221 /* This can happen due to races, in addition to dates that are
222 * truly in the past. To avoid requiring the caller to check for
223 * races, dates in the past are assumed to be in the recent past
224 * (i.e. not something that we'd rather the caller know about via
225 * an error), and the alarm is set to go off as soon as possible.
230 mutex_lock(&ds1374->mutex);
232 ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
236 /* Disable any existing alarm before setting the new one
237 * (or lack thereof). */
238 cr &= ~DS1374_REG_CR_WACE;
240 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
244 ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
248 if (alarm->enabled) {
249 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
250 cr &= ~DS1374_REG_CR_WDALM;
252 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
256 mutex_unlock(&ds1374->mutex);
260 static irqreturn_t ds1374_irq(int irq, void *dev_id)
262 struct i2c_client *client = dev_id;
263 struct ds1374 *ds1374 = i2c_get_clientdata(client);
265 disable_irq_nosync(irq);
266 schedule_work(&ds1374->work);
270 static void ds1374_work(struct work_struct *work)
272 struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
273 struct i2c_client *client = ds1374->client;
276 mutex_lock(&ds1374->mutex);
278 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
282 if (stat & DS1374_REG_SR_AF) {
283 stat &= ~DS1374_REG_SR_AF;
284 i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
286 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
290 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
291 i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
293 /* rtc_update_irq() assumes that it is called
294 * from IRQ-disabled context.
297 rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
302 if (!ds1374->exiting)
303 enable_irq(client->irq);
305 mutex_unlock(&ds1374->mutex);
308 static int ds1374_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
310 struct i2c_client *client = to_i2c_client(dev);
311 struct ds1374 *ds1374 = i2c_get_clientdata(client);
312 int ret = -ENOIOCTLCMD;
314 mutex_lock(&ds1374->mutex);
318 ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
322 ret &= ~DS1374_REG_CR_WACE;
324 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
331 ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
335 ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
336 ret &= ~DS1374_REG_CR_WDALM;
338 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
346 mutex_unlock(&ds1374->mutex);
350 static const struct rtc_class_ops ds1374_rtc_ops = {
351 .read_time = ds1374_read_time,
352 .set_time = ds1374_set_time,
353 .read_alarm = ds1374_read_alarm,
354 .set_alarm = ds1374_set_alarm,
355 .ioctl = ds1374_ioctl,
358 static int ds1374_probe(struct i2c_client *client)
360 struct ds1374 *ds1374;
363 ds1374 = kzalloc(sizeof(struct ds1374), GFP_KERNEL);
367 ds1374->client = client;
368 i2c_set_clientdata(client, ds1374);
370 INIT_WORK(&ds1374->work, ds1374_work);
371 mutex_init(&ds1374->mutex);
373 ret = ds1374_check_rtc_status(client);
377 if (client->irq >= 0) {
378 ret = request_irq(client->irq, ds1374_irq, 0,
381 dev_err(&client->dev, "unable to request IRQ\n");
386 ds1374->rtc = rtc_device_register(client->name, &client->dev,
387 &ds1374_rtc_ops, THIS_MODULE);
388 if (IS_ERR(ds1374->rtc)) {
389 ret = PTR_ERR(ds1374->rtc);
390 dev_err(&client->dev, "unable to register the class device\n");
397 if (client->irq >= 0)
398 free_irq(client->irq, client);
401 i2c_set_clientdata(client, NULL);
406 static int __devexit ds1374_remove(struct i2c_client *client)
408 struct ds1374 *ds1374 = i2c_get_clientdata(client);
410 if (client->irq >= 0) {
411 mutex_lock(&ds1374->mutex);
413 mutex_unlock(&ds1374->mutex);
415 free_irq(client->irq, client);
416 flush_scheduled_work();
419 rtc_device_unregister(ds1374->rtc);
420 i2c_set_clientdata(client, NULL);
425 static struct i2c_driver ds1374_driver = {
427 .name = "rtc-ds1374",
428 .owner = THIS_MODULE,
430 .probe = ds1374_probe,
431 .remove = __devexit_p(ds1374_remove),
434 static int __init ds1374_init(void)
436 return i2c_add_driver(&ds1374_driver);
439 static void __exit ds1374_exit(void)
441 i2c_del_driver(&ds1374_driver);
444 module_init(ds1374_init);
445 module_exit(ds1374_exit);
447 MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
448 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
449 MODULE_LICENSE("GPL");