blob: 03d09ed5ec2c009d21a4816f85e82829f76e2706 [file] [log] [blame]
Randy Vinsonc124a782005-06-03 14:36:06 -07001/*
2 * drivers/i2c/chips/ds1374.c
3 *
4 * I2C client/driver for the Maxim/Dallas DS1374 Real-Time Clock
5 *
6 * Author: Randy Vinson <rvinson@mvista.com>
7 *
8 * Based on the m41t00.c by Mark Greer <mgreer@mvista.com>
9 *
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
13 * or implied.
14 */
15/*
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.
21 */
22
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>
Arjan van de Venb3585e42006-01-11 10:50:26 +010029#include <linux/mutex.h>
Randy Vinsonc124a782005-06-03 14:36:06 -070030
Randy Vinsonc124a782005-06-03 14:36:06 -070031#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
42
43#define DS1374_DRV_NAME "ds1374"
44
Arjan van de Venb3585e42006-01-11 10:50:26 +010045static DEFINE_MUTEX(ds1374_mutex);
Randy Vinsonc124a782005-06-03 14:36:06 -070046
47static struct i2c_driver ds1374_driver;
48static struct i2c_client *save_client;
49
50static unsigned short ignore[] = { I2C_CLIENT_END };
51static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
52
53static struct i2c_client_address_data addr_data = {
54 .normal_i2c = normal_addr,
Randy Vinsonc124a782005-06-03 14:36:06 -070055 .probe = ignore,
Randy Vinsonc124a782005-06-03 14:36:06 -070056 .ignore = ignore,
Randy Vinsonc124a782005-06-03 14:36:06 -070057};
58
59static ulong ds1374_read_rtc(void)
60{
61 ulong time = 0;
62 int reg = DS1374_REG_WDALM0;
63
64 while (reg--) {
65 s32 tmp;
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");
69 return 0;
70 }
71 time = (time << 8) | (tmp & 0xff);
72 }
73 return time;
74}
75
76static void ds1374_write_rtc(ulong time)
77{
78 int reg;
79
80 for (reg = DS1374_REG_TOD0; reg < DS1374_REG_WDALM0; reg++) {
81 if (i2c_smbus_write_byte_data(save_client, reg, time & 0xff)
82 < 0) {
83 dev_warn(&save_client->dev,
84 "can't write to rtc chip\n");
85 break;
86 }
87 time = time >> 8;
88 }
89}
90
91static void ds1374_check_rtc_status(void)
92{
93 s32 tmp;
94
95 tmp = i2c_smbus_read_byte_data(save_client, DS1374_REG_SR);
96 if (tmp < 0) {
97 dev_warn(&save_client->dev,
98 "can't read status from rtc chip\n");
99 return;
100 }
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,
106 tmp & 0xff);
107 if (tmp < 0)
108 dev_warn(&save_client->dev,
109 "can't clear discontinuity notification\n");
110 }
111}
112
113ulong ds1374_get_rtc_time(void)
114{
115 ulong t1, t2;
116 int limit = 10; /* arbitrary retry limit */
117
Arjan van de Venb3585e42006-01-11 10:50:26 +0100118 mutex_lock(&ds1374_mutex);
Randy Vinsonc124a782005-06-03 14:36:06 -0700119
120 /*
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.
125 */
126 do {
127 t1 = ds1374_read_rtc();
128 t2 = ds1374_read_rtc();
129 } while (t1 != t2 && limit--);
130
Arjan van de Venb3585e42006-01-11 10:50:26 +0100131 mutex_unlock(&ds1374_mutex);
Randy Vinsonc124a782005-06-03 14:36:06 -0700132
133 if (t1 != t2) {
134 dev_warn(&save_client->dev,
135 "can't get consistent time from rtc chip\n");
136 t1 = 0;
137 }
138
139 return t1;
140}
141
142static void ds1374_set_tlet(ulong arg)
143{
144 ulong t1, t2;
145 int limit = 10; /* arbitrary retry limit */
146
147 t1 = *(ulong *) arg;
148
Arjan van de Venb3585e42006-01-11 10:50:26 +0100149 mutex_lock(&ds1374_mutex);
Randy Vinsonc124a782005-06-03 14:36:06 -0700150
151 /*
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.
156 */
157 do {
158 ds1374_write_rtc(t1);
159 t2 = ds1374_read_rtc();
160 } while (t1 != t2 && limit--);
161
Arjan van de Venb3585e42006-01-11 10:50:26 +0100162 mutex_unlock(&ds1374_mutex);
Randy Vinsonc124a782005-06-03 14:36:06 -0700163
164 if (t1 != t2)
165 dev_warn(&save_client->dev,
166 "can't confirm time set from rtc chip\n");
167}
168
Mark A. Greer8e14d6c2005-09-01 18:12:04 -0700169static ulong new_time;
Randy Vinsonc124a782005-06-03 14:36:06 -0700170
Ben Dooks6344a8e2005-10-26 21:09:41 +0200171static DECLARE_TASKLET_DISABLED(ds1374_tasklet, ds1374_set_tlet,
172 (ulong) & new_time);
Randy Vinsonc124a782005-06-03 14:36:06 -0700173
174int ds1374_set_rtc_time(ulong nowtime)
175{
176 new_time = nowtime;
177
178 if (in_interrupt())
179 tasklet_schedule(&ds1374_tasklet);
180 else
181 ds1374_set_tlet((ulong) & new_time);
182
183 return 0;
184}
185
186/*
187 *****************************************************************************
188 *
189 * Driver Interface
190 *
191 *****************************************************************************
192 */
193static int ds1374_probe(struct i2c_adapter *adap, int addr, int kind)
194{
195 struct i2c_client *client;
196 int rc;
197
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200198 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Randy Vinsonc124a782005-06-03 14:36:06 -0700199 if (!client)
200 return -ENOMEM;
201
Randy Vinsonc124a782005-06-03 14:36:06 -0700202 strncpy(client->name, DS1374_DRV_NAME, I2C_NAME_SIZE);
Randy Vinsonc124a782005-06-03 14:36:06 -0700203 client->addr = addr;
204 client->adapter = adap;
205 client->driver = &ds1374_driver;
206
207 if ((rc = i2c_attach_client(client)) != 0) {
208 kfree(client);
209 return rc;
210 }
211
212 save_client = client;
213
214 ds1374_check_rtc_status();
215
216 return 0;
217}
218
219static int ds1374_attach(struct i2c_adapter *adap)
220{
221 return i2c_probe(adap, &addr_data, ds1374_probe);
222}
223
224static int ds1374_detach(struct i2c_client *client)
225{
226 int rc;
227
228 if ((rc = i2c_detach_client(client)) == 0) {
229 kfree(i2c_get_clientdata(client));
230 tasklet_kill(&ds1374_tasklet);
231 }
232 return rc;
233}
234
235static struct i2c_driver ds1374_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100236 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100237 .name = DS1374_DRV_NAME,
238 },
Randy Vinsonc124a782005-06-03 14:36:06 -0700239 .id = I2C_DRIVERID_DS1374,
Randy Vinsonc124a782005-06-03 14:36:06 -0700240 .attach_adapter = ds1374_attach,
241 .detach_client = ds1374_detach,
242};
243
244static int __init ds1374_init(void)
245{
246 return i2c_add_driver(&ds1374_driver);
247}
248
249static void __exit ds1374_exit(void)
250{
251 i2c_del_driver(&ds1374_driver);
252}
253
254module_init(ds1374_init);
255module_exit(ds1374_exit);
256
257MODULE_AUTHOR("Randy Vinson <rvinson@mvista.com>");
258MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC I2C Client Driver");
259MODULE_LICENSE("GPL");