sky2: software rx/tx stats
[linux-2.6] / drivers / acorn / char / pcf8583.c
1 /*
2  *  linux/drivers/acorn/char/pcf8583.c
3  *
4  *  Copyright (C) 2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Driver for PCF8583 RTC & RAM chip
11  */
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/mc146818rtc.h>
17 #include <linux/init.h>
18 #include <linux/errno.h>
19 #include <linux/bcd.h>
20
21 #include "pcf8583.h"
22
23 static struct i2c_driver pcf8583_driver;
24
25 static unsigned short ignore[] = { I2C_CLIENT_END };
26 static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END };
27 static unsigned short *forces[] = { NULL };
28
29 static struct i2c_client_address_data addr_data = {
30         .normal_i2c             = normal_addr,
31         .probe                  = ignore,
32         .ignore                 = ignore,
33         .forces                 = forces,
34 };
35
36 #define set_ctrl(x, v) i2c_set_clientdata(x, (void *)(unsigned int)(v))
37 #define get_ctrl(x)    ((unsigned int)i2c_get_clientdata(x))
38
39 static int
40 pcf8583_attach(struct i2c_adapter *adap, int addr, int kind)
41 {
42         struct i2c_client *c;
43         unsigned char buf[1], ad[1] = { 0 };
44         struct i2c_msg msgs[2] = {
45                 {
46                         .addr = addr,
47                         .flags = 0,
48                         .len = 1,
49                         .buf = ad,
50                 }, {
51                         .addr = addr,
52                         .flags = I2C_M_RD,
53                         .len = 1,
54                         .buf = buf,
55                 }
56         };
57
58         c = kmalloc(sizeof(*c), GFP_KERNEL);
59         if (!c)
60                 return -ENOMEM;
61
62         memset(c, 0, sizeof(*c));
63         c->addr         = addr;
64         c->adapter      = adap;
65         c->driver       = &pcf8583_driver;
66
67         if (i2c_transfer(c->adapter, msgs, 2) == 2)
68                 set_ctrl(c, buf[0]);
69
70         return i2c_attach_client(c);
71 }
72
73 static int
74 pcf8583_probe(struct i2c_adapter *adap)
75 {
76         return i2c_probe(adap, &addr_data, pcf8583_attach);
77 }
78
79 static int
80 pcf8583_detach(struct i2c_client *client)
81 {
82         i2c_detach_client(client);
83         kfree(client);
84         return 0;
85 }
86
87 static int
88 pcf8583_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
89 {
90         unsigned char buf[8], addr[1] = { 1 };
91         struct i2c_msg msgs[2] = {
92                 {
93                         .addr = client->addr,
94                         .flags = 0,
95                         .len = 1,
96                         .buf = addr,
97                 }, {
98                         .addr = client->addr,
99                         .flags = I2C_M_RD,
100                         .len = 6,
101                         .buf = buf,
102                 }
103         };
104         int ret = -EIO;
105
106         memset(buf, 0, sizeof(buf));
107
108         ret = i2c_transfer(client->adapter, msgs, 2);
109         if (ret == 2) {
110                 dt->year_off = buf[4] >> 6;
111                 dt->wday     = buf[5] >> 5;
112
113                 buf[4] &= 0x3f;
114                 buf[5] &= 0x1f;
115
116                 dt->cs       = BCD_TO_BIN(buf[0]);
117                 dt->secs     = BCD_TO_BIN(buf[1]);
118                 dt->mins     = BCD_TO_BIN(buf[2]);
119                 dt->hours    = BCD_TO_BIN(buf[3]);
120                 dt->mday     = BCD_TO_BIN(buf[4]);
121                 dt->mon      = BCD_TO_BIN(buf[5]);
122
123                 ret = 0;
124         }
125
126         return ret;
127 }
128
129 static int
130 pcf8583_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
131 {
132         unsigned char buf[8];
133         int ret, len = 6;
134
135         buf[0] = 0;
136         buf[1] = get_ctrl(client) | 0x80;
137         buf[2] = BIN_TO_BCD(dt->cs);
138         buf[3] = BIN_TO_BCD(dt->secs);
139         buf[4] = BIN_TO_BCD(dt->mins);
140         buf[5] = BIN_TO_BCD(dt->hours);
141
142         if (datetoo) {
143                 len = 8;
144                 buf[6] = BIN_TO_BCD(dt->mday) | (dt->year_off << 6);
145                 buf[7] = BIN_TO_BCD(dt->mon)  | (dt->wday << 5);
146         }
147
148         ret = i2c_master_send(client, (char *)buf, len);
149         if (ret == len)
150                 ret = 0;
151
152         buf[1] = get_ctrl(client);
153         i2c_master_send(client, (char *)buf, 2);
154
155         return ret;
156 }
157
158 static int
159 pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
160 {
161         *ctrl = get_ctrl(client);
162         return 0;
163 }
164
165 static int
166 pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
167 {
168         unsigned char buf[2];
169
170         buf[0] = 0;
171         buf[1] = *ctrl;
172         set_ctrl(client, *ctrl);
173
174         return i2c_master_send(client, (char *)buf, 2);
175 }
176
177 static int
178 pcf8583_read_mem(struct i2c_client *client, struct mem *mem)
179 {
180         unsigned char addr[1];
181         struct i2c_msg msgs[2] = {
182                 {
183                         .addr = client->addr,
184                         .flags = 0,
185                         .len = 1,
186                         .buf = addr,
187                 }, {
188                         .addr = client->addr,
189                         .flags = I2C_M_RD,
190                         .len = mem->nr,
191                         .buf = mem->data,
192                 }
193         };
194
195         if (mem->loc < 8)
196                 return -EINVAL;
197
198         addr[0] = mem->loc;
199
200         return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
201 }
202
203 static int
204 pcf8583_write_mem(struct i2c_client *client, struct mem *mem)
205 {
206         unsigned char addr[1];
207         struct i2c_msg msgs[2] = {
208                 {
209                         .addr = client->addr,
210                         .flags = 0,
211                         .len = 1,
212                         .buf = addr,
213                 }, {
214                         .addr = client->addr,
215                         .flags = I2C_M_NOSTART,
216                         .len = mem->nr,
217                         .buf = mem->data,
218                 }
219         };
220
221         if (mem->loc < 8)
222                 return -EINVAL;
223
224         addr[0] = mem->loc;
225
226         return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
227 }
228
229 static int
230 pcf8583_command(struct i2c_client *client, unsigned int cmd, void *arg)
231 {
232         switch (cmd) {
233         case RTC_GETDATETIME:
234                 return pcf8583_get_datetime(client, arg);
235                 
236         case RTC_SETTIME:
237                 return pcf8583_set_datetime(client, arg, 0);
238
239         case RTC_SETDATETIME:
240                 return pcf8583_set_datetime(client, arg, 1);
241
242         case RTC_GETCTRL:
243                 return pcf8583_get_ctrl(client, arg);
244
245         case RTC_SETCTRL:
246                 return pcf8583_set_ctrl(client, arg);
247
248         case MEM_READ:
249                 return pcf8583_read_mem(client, arg);
250
251         case MEM_WRITE:
252                 return pcf8583_write_mem(client, arg);
253
254         default:
255                 return -EINVAL;
256         }
257 }
258
259 static struct i2c_driver pcf8583_driver = {
260         .driver = {
261                 .name   = "PCF8583",
262         },
263         .id             = I2C_DRIVERID_PCF8583,
264         .attach_adapter = pcf8583_probe,
265         .detach_client  = pcf8583_detach,
266         .command        = pcf8583_command
267 };
268
269 static __init int pcf8583_init(void)
270 {
271         return i2c_add_driver(&pcf8583_driver);
272 }
273
274 static __exit void pcf8583_exit(void)
275 {
276         i2c_del_driver(&pcf8583_driver);
277 }
278
279 module_init(pcf8583_init);
280 module_exit(pcf8583_exit);
281
282 MODULE_AUTHOR("Russell King");
283 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
284 MODULE_LICENSE("GPL");