Pull new-efi-memmap into release branch
[pandora-kernel.git] / 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/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/bcd.h>
19
20 #include "pcf8583.h"
21
22 static struct i2c_driver pcf8583_driver;
23
24 static unsigned short ignore[] = { I2C_CLIENT_END };
25 static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END };
26 static unsigned short *forces[] = { NULL };
27
28 static struct i2c_client_address_data addr_data = {
29         .normal_i2c             = normal_addr,
30         .probe                  = ignore,
31         .ignore                 = ignore,
32         .forces                 = forces,
33 };
34
35 #define DAT(x) ((unsigned int)(x->dev.driver_data))
36
37 static int
38 pcf8583_attach(struct i2c_adapter *adap, int addr, int kind)
39 {
40         struct i2c_client *c;
41         unsigned char buf[1], ad[1] = { 0 };
42         struct i2c_msg msgs[2] = {
43                 { addr, 0,        1, ad  },
44                 { addr, I2C_M_RD, 1, buf }
45         };
46
47         c = kmalloc(sizeof(*c), GFP_KERNEL);
48         if (!c)
49                 return -ENOMEM;
50
51         memset(c, 0, sizeof(*c));
52         c->addr         = addr;
53         c->adapter      = adap;
54         c->driver       = &pcf8583_driver;
55
56         if (i2c_transfer(c->adapter, msgs, 2) == 2)
57                 DAT(c) = buf[0];
58
59         return i2c_attach_client(c);
60 }
61
62 static int
63 pcf8583_probe(struct i2c_adapter *adap)
64 {
65         return i2c_probe(adap, &addr_data, pcf8583_attach);
66 }
67
68 static int
69 pcf8583_detach(struct i2c_client *client)
70 {
71         i2c_detach_client(client);
72         kfree(client);
73         return 0;
74 }
75
76 static int
77 pcf8583_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
78 {
79         unsigned char buf[8], addr[1] = { 1 };
80         struct i2c_msg msgs[2] = {
81                 { client->addr, 0,        1, addr },
82                 { client->addr, I2C_M_RD, 6, buf  }
83         };
84         int ret = -EIO;
85
86         memset(buf, 0, sizeof(buf));
87
88         ret = i2c_transfer(client->adapter, msgs, 2);
89         if (ret == 2) {
90                 dt->year_off = buf[4] >> 6;
91                 dt->wday     = buf[5] >> 5;
92
93                 buf[4] &= 0x3f;
94                 buf[5] &= 0x1f;
95
96                 dt->cs       = BCD_TO_BIN(buf[0]);
97                 dt->secs     = BCD_TO_BIN(buf[1]);
98                 dt->mins     = BCD_TO_BIN(buf[2]);
99                 dt->hours    = BCD_TO_BIN(buf[3]);
100                 dt->mday     = BCD_TO_BIN(buf[4]);
101                 dt->mon      = BCD_TO_BIN(buf[5]);
102
103                 ret = 0;
104         }
105
106         return ret;
107 }
108
109 static int
110 pcf8583_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
111 {
112         unsigned char buf[8];
113         int ret, len = 6;
114
115         buf[0] = 0;
116         buf[1] = DAT(client) | 0x80;
117         buf[2] = BIN_TO_BCD(dt->cs);
118         buf[3] = BIN_TO_BCD(dt->secs);
119         buf[4] = BIN_TO_BCD(dt->mins);
120         buf[5] = BIN_TO_BCD(dt->hours);
121
122         if (datetoo) {
123                 len = 8;
124                 buf[6] = BIN_TO_BCD(dt->mday) | (dt->year_off << 6);
125                 buf[7] = BIN_TO_BCD(dt->mon)  | (dt->wday << 5);
126         }
127
128         ret = i2c_master_send(client, (char *)buf, len);
129         if (ret == len)
130                 ret = 0;
131
132         buf[1] = DAT(client);
133         i2c_master_send(client, (char *)buf, 2);
134
135         return ret;
136 }
137
138 static int
139 pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
140 {
141         *ctrl = DAT(client);
142         return 0;
143 }
144
145 static int
146 pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
147 {
148         unsigned char buf[2];
149
150         buf[0] = 0;
151         buf[1] = *ctrl;
152         DAT(client) = *ctrl;
153
154         return i2c_master_send(client, (char *)buf, 2);
155 }
156
157 static int
158 pcf8583_read_mem(struct i2c_client *client, struct mem *mem)
159 {
160         unsigned char addr[1];
161         struct i2c_msg msgs[2] = {
162                 { client->addr, 0,        1, addr },
163                 { client->addr, I2C_M_RD, 0, mem->data }
164         };
165
166         if (mem->loc < 8)
167                 return -EINVAL;
168
169         addr[0] = mem->loc;
170         msgs[1].len = mem->nr;
171
172         return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
173 }
174
175 static int
176 pcf8583_write_mem(struct i2c_client *client, struct mem *mem)
177 {
178         unsigned char addr[1];
179         struct i2c_msg msgs[2] = {
180                 { client->addr, 0, 1, addr },
181                 { client->addr, 0, 0, mem->data }
182         };
183
184         if (mem->loc < 8)
185                 return -EINVAL;
186
187         addr[0] = mem->loc;
188         msgs[1].len = mem->nr;
189
190         return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
191 }
192
193 static int
194 pcf8583_command(struct i2c_client *client, unsigned int cmd, void *arg)
195 {
196         switch (cmd) {
197         case RTC_GETDATETIME:
198                 return pcf8583_get_datetime(client, arg);
199                 
200         case RTC_SETTIME:
201                 return pcf8583_set_datetime(client, arg, 0);
202
203         case RTC_SETDATETIME:
204                 return pcf8583_set_datetime(client, arg, 1);
205
206         case RTC_GETCTRL:
207                 return pcf8583_get_ctrl(client, arg);
208
209         case RTC_SETCTRL:
210                 return pcf8583_set_ctrl(client, arg);
211
212         case MEM_READ:
213                 return pcf8583_read_mem(client, arg);
214
215         case MEM_WRITE:
216                 return pcf8583_write_mem(client, arg);
217
218         default:
219                 return -EINVAL;
220         }
221 }
222
223 static struct i2c_driver pcf8583_driver = {
224         .name           = "PCF8583",
225         .id             = I2C_DRIVERID_PCF8583,
226         .flags          = I2C_DF_NOTIFY,
227         .attach_adapter = pcf8583_probe,
228         .detach_client  = pcf8583_detach,
229         .command        = pcf8583_command
230 };
231
232 static __init int pcf8583_init(void)
233 {
234         return i2c_add_driver(&pcf8583_driver);
235 }
236
237 __initcall(pcf8583_init);