blob: e26f007a1417b8312706c2fbfd164ecf4f945491 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Russell King1d1fd662005-10-30 19:07:59 +000012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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
23static struct i2c_driver pcf8583_driver;
24
25static unsigned short ignore[] = { I2C_CLIENT_END };
26static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END };
Russell Kinga9f7baf2005-09-20 21:01:13 +010027static unsigned short *forces[] = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static struct i2c_client_address_data addr_data = {
30 .normal_i2c = normal_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .probe = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .ignore = ignore,
Russell Kinga9f7baf2005-09-20 21:01:13 +010033 .forces = forces,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Russell King1d1fd662005-10-30 19:07:59 +000036#define set_ctrl(x, v) i2c_set_clientdata(x, (void *)(unsigned int)(v))
37#define get_ctrl(x) ((unsigned int)i2c_get_clientdata(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static int
40pcf8583_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] = {
Russell King1d1fd662005-10-30 19:07:59 +000045 {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 };
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)
Russell King1d1fd662005-10-30 19:07:59 +000068 set_ctrl(c, buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 return i2c_attach_client(c);
71}
72
73static int
74pcf8583_probe(struct i2c_adapter *adap)
75{
76 return i2c_probe(adap, &addr_data, pcf8583_attach);
77}
78
79static int
80pcf8583_detach(struct i2c_client *client)
81{
82 i2c_detach_client(client);
83 kfree(client);
84 return 0;
85}
86
87static int
88pcf8583_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] = {
Russell King1d1fd662005-10-30 19:07:59 +000092 {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 };
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
129static int
130pcf8583_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;
Russell King1d1fd662005-10-30 19:07:59 +0000136 buf[1] = get_ctrl(client) | 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 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
Russell King1d1fd662005-10-30 19:07:59 +0000152 buf[1] = get_ctrl(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 i2c_master_send(client, (char *)buf, 2);
154
155 return ret;
156}
157
158static int
159pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
160{
Russell King1d1fd662005-10-30 19:07:59 +0000161 *ctrl = get_ctrl(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return 0;
163}
164
165static int
166pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
167{
168 unsigned char buf[2];
169
170 buf[0] = 0;
171 buf[1] = *ctrl;
Russell King1d1fd662005-10-30 19:07:59 +0000172 set_ctrl(client, *ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return i2c_master_send(client, (char *)buf, 2);
175}
176
177static int
178pcf8583_read_mem(struct i2c_client *client, struct mem *mem)
179{
180 unsigned char addr[1];
181 struct i2c_msg msgs[2] = {
Russell King1d1fd662005-10-30 19:07:59 +0000182 {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 };
194
195 if (mem->loc < 8)
196 return -EINVAL;
197
198 addr[0] = mem->loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
201}
202
203static int
204pcf8583_write_mem(struct i2c_client *client, struct mem *mem)
205{
206 unsigned char addr[1];
207 struct i2c_msg msgs[2] = {
Russell King1d1fd662005-10-30 19:07:59 +0000208 {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 };
220
221 if (mem->loc < 8)
222 return -EINVAL;
223
224 addr[0] = mem->loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
227}
228
229static int
230pcf8583_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
259static struct i2c_driver pcf8583_driver = {
260 .name = "PCF8583",
261 .id = I2C_DRIVERID_PCF8583,
262 .flags = I2C_DF_NOTIFY,
263 .attach_adapter = pcf8583_probe,
264 .detach_client = pcf8583_detach,
265 .command = pcf8583_command
266};
267
268static __init int pcf8583_init(void)
269{
270 return i2c_add_driver(&pcf8583_driver);
271}
272
Russell King1d1fd662005-10-30 19:07:59 +0000273static __exit void pcf8583_exit(void)
274{
275 i2c_del_driver(&pcf8583_driver);
276}
277
278module_init(pcf8583_init);
279module_exit(pcf8583_exit);
280
281MODULE_AUTHOR("Russell King");
282MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
283MODULE_LICENSE("GPL");