2 * drivers/i2c/chips/ds1374.c
4 * I2C client/driver for the Maxim/Dallas DS1374 Real-Time Clock
6 * Author: Randy Vinson <rvinson@mvista.com>
8 * Based on the m41t00.c by Mark Greer <mgreer@mvista.com>
10 * 2005 (c) MontaVista Software, Inc. This file is licensed under
11 * the terms of the GNU General Public License version 2. This program
12 * is licensed "as is" without any warranty of any kind, whether express
16 * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
17 * interface and the SMBus interface of the i2c subsystem.
18 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
19 * recommened in .../Documentation/i2c/writing-clients section
20 * "Sending and receiving", using SMBus level communication is preferred.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/i2c.h>
27 #include <linux/rtc.h>
28 #include <linux/bcd.h>
29 #include <linux/mutex.h>
31 #define DS1374_REG_TOD0 0x00
32 #define DS1374_REG_TOD1 0x01
33 #define DS1374_REG_TOD2 0x02
34 #define DS1374_REG_TOD3 0x03
35 #define DS1374_REG_WDALM0 0x04
36 #define DS1374_REG_WDALM1 0x05
37 #define DS1374_REG_WDALM2 0x06
38 #define DS1374_REG_CR 0x07
39 #define DS1374_REG_SR 0x08
40 #define DS1374_REG_SR_OSF 0x80
41 #define DS1374_REG_TCR 0x09
43 #define DS1374_DRV_NAME "ds1374"
45 static DEFINE_MUTEX(ds1374_mutex);
47 static struct i2c_driver ds1374_driver;
48 static struct i2c_client *save_client;
50 static unsigned short ignore[] = { I2C_CLIENT_END };
51 static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
53 static struct i2c_client_address_data addr_data = {
54 .normal_i2c = normal_addr,
59 static ulong ds1374_read_rtc(void)
62 int reg = DS1374_REG_WDALM0;
66 if ((tmp = i2c_smbus_read_byte_data(save_client, reg)) < 0) {
67 dev_warn(&save_client->dev,
68 "can't read from rtc chip\n");
71 time = (time << 8) | (tmp & 0xff);
76 static void ds1374_write_rtc(ulong time)
80 for (reg = DS1374_REG_TOD0; reg < DS1374_REG_WDALM0; reg++) {
81 if (i2c_smbus_write_byte_data(save_client, reg, time & 0xff)
83 dev_warn(&save_client->dev,
84 "can't write to rtc chip\n");
91 static void ds1374_check_rtc_status(void)
95 tmp = i2c_smbus_read_byte_data(save_client, DS1374_REG_SR);
97 dev_warn(&save_client->dev,
98 "can't read status from rtc chip\n");
101 if (tmp & DS1374_REG_SR_OSF) {
102 dev_warn(&save_client->dev,
103 "oscillator discontinuity flagged, time unreliable\n");
104 tmp &= ~DS1374_REG_SR_OSF;
105 tmp = i2c_smbus_write_byte_data(save_client, DS1374_REG_SR,
108 dev_warn(&save_client->dev,
109 "can't clear discontinuity notification\n");
113 ulong ds1374_get_rtc_time(void)
116 int limit = 10; /* arbitrary retry limit */
118 mutex_lock(&ds1374_mutex);
121 * Since the reads are being performed one byte at a time using
122 * the SMBus vs a 4-byte i2c transfer, there is a chance that a
123 * carry will occur during the read. To detect this, 2 reads are
124 * performed and compared.
127 t1 = ds1374_read_rtc();
128 t2 = ds1374_read_rtc();
129 } while (t1 != t2 && limit--);
131 mutex_unlock(&ds1374_mutex);
134 dev_warn(&save_client->dev,
135 "can't get consistent time from rtc chip\n");
142 static void ds1374_set_tlet(ulong arg)
145 int limit = 10; /* arbitrary retry limit */
149 mutex_lock(&ds1374_mutex);
152 * Since the writes are being performed one byte at a time using
153 * the SMBus vs a 4-byte i2c transfer, there is a chance that a
154 * carry will occur during the write. To detect this, the write
155 * value is read back and compared.
158 ds1374_write_rtc(t1);
159 t2 = ds1374_read_rtc();
160 } while (t1 != t2 && limit--);
162 mutex_unlock(&ds1374_mutex);
165 dev_warn(&save_client->dev,
166 "can't confirm time set from rtc chip\n");
169 static ulong new_time;
171 static DECLARE_TASKLET_DISABLED(ds1374_tasklet, ds1374_set_tlet,
174 int ds1374_set_rtc_time(ulong nowtime)
179 tasklet_schedule(&ds1374_tasklet);
181 ds1374_set_tlet((ulong) & new_time);
187 *****************************************************************************
191 *****************************************************************************
193 static int ds1374_probe(struct i2c_adapter *adap, int addr, int kind)
195 struct i2c_client *client;
198 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
202 strncpy(client->name, DS1374_DRV_NAME, I2C_NAME_SIZE);
204 client->adapter = adap;
205 client->driver = &ds1374_driver;
207 if ((rc = i2c_attach_client(client)) != 0) {
212 save_client = client;
214 ds1374_check_rtc_status();
219 static int ds1374_attach(struct i2c_adapter *adap)
221 return i2c_probe(adap, &addr_data, ds1374_probe);
224 static int ds1374_detach(struct i2c_client *client)
228 if ((rc = i2c_detach_client(client)) == 0) {
229 kfree(i2c_get_clientdata(client));
230 tasklet_kill(&ds1374_tasklet);
235 static struct i2c_driver ds1374_driver = {
237 .name = DS1374_DRV_NAME,
239 .id = I2C_DRIVERID_DS1374,
240 .attach_adapter = ds1374_attach,
241 .detach_client = ds1374_detach,
244 static int __init ds1374_init(void)
246 return i2c_add_driver(&ds1374_driver);
249 static void __exit ds1374_exit(void)
251 i2c_del_driver(&ds1374_driver);
254 module_init(ds1374_init);
255 module_exit(ds1374_exit);
257 MODULE_AUTHOR("Randy Vinson <rvinson@mvista.com>");
258 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC I2C Client Driver");
259 MODULE_LICENSE("GPL");