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