8d20b81dce0532567bc37038648557c61a36447a
[pandora-kernel.git] / drivers / i2c / chips / ds1374.c
1 /*
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>
29
30 #include <asm/rtc.h>
31
32 #define DS1374_REG_TOD0         0x00
33 #define DS1374_REG_TOD1         0x01
34 #define DS1374_REG_TOD2         0x02
35 #define DS1374_REG_TOD3         0x03
36 #define DS1374_REG_WDALM0       0x04
37 #define DS1374_REG_WDALM1       0x05
38 #define DS1374_REG_WDALM2       0x06
39 #define DS1374_REG_CR           0x07
40 #define DS1374_REG_SR           0x08
41 #define DS1374_REG_SR_OSF       0x80
42 #define DS1374_REG_TCR          0x09
43
44 #define DS1374_DRV_NAME         "ds1374"
45
46 static DECLARE_MUTEX(ds1374_mutex);
47
48 static struct i2c_driver ds1374_driver;
49 static struct i2c_client *save_client;
50
51 static unsigned short ignore[] = { I2C_CLIENT_END };
52 static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
53
54 static struct i2c_client_address_data addr_data = {
55         .normal_i2c = normal_addr,
56         .probe = ignore,
57         .ignore = ignore,
58         .force = ignore,
59 };
60
61 static ulong ds1374_read_rtc(void)
62 {
63         ulong time = 0;
64         int reg = DS1374_REG_WDALM0;
65
66         while (reg--) {
67                 s32 tmp;
68                 if ((tmp = i2c_smbus_read_byte_data(save_client, reg)) < 0) {
69                         dev_warn(&save_client->dev,
70                                  "can't read from rtc chip\n");
71                         return 0;
72                 }
73                 time = (time << 8) | (tmp & 0xff);
74         }
75         return time;
76 }
77
78 static void ds1374_write_rtc(ulong time)
79 {
80         int reg;
81
82         for (reg = DS1374_REG_TOD0; reg < DS1374_REG_WDALM0; reg++) {
83                 if (i2c_smbus_write_byte_data(save_client, reg, time & 0xff)
84                     < 0) {
85                         dev_warn(&save_client->dev,
86                                  "can't write to rtc chip\n");
87                         break;
88                 }
89                 time = time >> 8;
90         }
91 }
92
93 static void ds1374_check_rtc_status(void)
94 {
95         s32 tmp;
96
97         tmp = i2c_smbus_read_byte_data(save_client, DS1374_REG_SR);
98         if (tmp < 0) {
99                 dev_warn(&save_client->dev,
100                          "can't read status from rtc chip\n");
101                 return;
102         }
103         if (tmp & DS1374_REG_SR_OSF) {
104                 dev_warn(&save_client->dev,
105                          "oscillator discontinuity flagged, time unreliable\n");
106                 tmp &= ~DS1374_REG_SR_OSF;
107                 tmp = i2c_smbus_write_byte_data(save_client, DS1374_REG_SR,
108                                                 tmp & 0xff);
109                 if (tmp < 0)
110                         dev_warn(&save_client->dev,
111                                  "can't clear discontinuity notification\n");
112         }
113 }
114
115 ulong ds1374_get_rtc_time(void)
116 {
117         ulong t1, t2;
118         int limit = 10;         /* arbitrary retry limit */
119
120         down(&ds1374_mutex);
121
122         /*
123          * Since the reads are being performed one byte at a time using
124          * the SMBus vs a 4-byte i2c transfer, there is a chance that a
125          * carry will occur during the read. To detect this, 2 reads are
126          * performed and compared.
127          */
128         do {
129                 t1 = ds1374_read_rtc();
130                 t2 = ds1374_read_rtc();
131         } while (t1 != t2 && limit--);
132
133         up(&ds1374_mutex);
134
135         if (t1 != t2) {
136                 dev_warn(&save_client->dev,
137                          "can't get consistent time from rtc chip\n");
138                 t1 = 0;
139         }
140
141         return t1;
142 }
143
144 static void ds1374_set_tlet(ulong arg)
145 {
146         ulong t1, t2;
147         int limit = 10;         /* arbitrary retry limit */
148
149         t1 = *(ulong *) arg;
150
151         down(&ds1374_mutex);
152
153         /*
154          * Since the writes are being performed one byte at a time using
155          * the SMBus vs a 4-byte i2c transfer, there is a chance that a
156          * carry will occur during the write. To detect this, the write
157          * value is read back and compared.
158          */
159         do {
160                 ds1374_write_rtc(t1);
161                 t2 = ds1374_read_rtc();
162         } while (t1 != t2 && limit--);
163
164         up(&ds1374_mutex);
165
166         if (t1 != t2)
167                 dev_warn(&save_client->dev,
168                          "can't confirm time set from rtc chip\n");
169 }
170
171 ulong new_time;
172
173 DECLARE_TASKLET_DISABLED(ds1374_tasklet, ds1374_set_tlet, (ulong) & new_time);
174
175 int ds1374_set_rtc_time(ulong nowtime)
176 {
177         new_time = nowtime;
178
179         if (in_interrupt())
180                 tasklet_schedule(&ds1374_tasklet);
181         else
182                 ds1374_set_tlet((ulong) & new_time);
183
184         return 0;
185 }
186
187 /*
188  *****************************************************************************
189  *
190  *      Driver Interface
191  *
192  *****************************************************************************
193  */
194 static int ds1374_probe(struct i2c_adapter *adap, int addr, int kind)
195 {
196         struct i2c_client *client;
197         int rc;
198
199         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
200         if (!client)
201                 return -ENOMEM;
202
203         memset(client, 0, sizeof(struct i2c_client));
204         strncpy(client->name, DS1374_DRV_NAME, I2C_NAME_SIZE);
205         client->flags = I2C_DF_NOTIFY;
206         client->addr = addr;
207         client->adapter = adap;
208         client->driver = &ds1374_driver;
209
210         if ((rc = i2c_attach_client(client)) != 0) {
211                 kfree(client);
212                 return rc;
213         }
214
215         save_client = client;
216
217         ds1374_check_rtc_status();
218
219         return 0;
220 }
221
222 static int ds1374_attach(struct i2c_adapter *adap)
223 {
224         return i2c_probe(adap, &addr_data, ds1374_probe);
225 }
226
227 static int ds1374_detach(struct i2c_client *client)
228 {
229         int rc;
230
231         if ((rc = i2c_detach_client(client)) == 0) {
232                 kfree(i2c_get_clientdata(client));
233                 tasklet_kill(&ds1374_tasklet);
234         }
235         return rc;
236 }
237
238 static struct i2c_driver ds1374_driver = {
239         .owner = THIS_MODULE,
240         .name = DS1374_DRV_NAME,
241         .id = I2C_DRIVERID_DS1374,
242         .flags = I2C_DF_NOTIFY,
243         .attach_adapter = ds1374_attach,
244         .detach_client = ds1374_detach,
245 };
246
247 static int __init ds1374_init(void)
248 {
249         return i2c_add_driver(&ds1374_driver);
250 }
251
252 static void __exit ds1374_exit(void)
253 {
254         i2c_del_driver(&ds1374_driver);
255 }
256
257 module_init(ds1374_init);
258 module_exit(ds1374_exit);
259
260 MODULE_AUTHOR("Randy Vinson <rvinson@mvista.com>");
261 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC I2C Client Driver");
262 MODULE_LICENSE("GPL");