1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/delay.h>
33#include <linux/init.h>
34#include <linux/errno.h>
35#include <linux/i2c.h>
36#include <linux/i2c-algo-pcf.h>
37#include "i2c-algo-pcf.h"
38
39
40#define DEB2(x) if (i2c_debug >= 2) x
41#define DEB3(x) if (i2c_debug >= 3) x
42#define DEBPROTO(x) if (i2c_debug >= 9) x;
43
44#define DEF_TIMEOUT 16
45
46
47
48
49static int i2c_debug;
50
51
52
53#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
54#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
55#define get_own(adap) adap->getown(adap->data)
56#define get_clock(adap) adap->getclock(adap->data)
57#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
58#define i2c_inb(adap) adap->getpcf(adap->data, 0)
59
60
61
62static void i2c_start(struct i2c_algo_pcf_data *adap)
63{
64 DEBPROTO(printk(KERN_DEBUG "S "));
65 set_pcf(adap, 1, I2C_PCF_START);
66}
67
68static void i2c_repstart(struct i2c_algo_pcf_data *adap)
69{
70 DEBPROTO(printk(" Sr "));
71 set_pcf(adap, 1, I2C_PCF_REPSTART);
72}
73
74static void i2c_stop(struct i2c_algo_pcf_data *adap)
75{
76 DEBPROTO(printk("P\n"));
77 set_pcf(adap, 1, I2C_PCF_STOP);
78}
79
80static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
81{
82 DEB2(printk(KERN_INFO
83 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
84 *status));
85
86
87
88
89
90
91 set_pcf(adap, 1, I2C_PCF_PIN);
92 set_pcf(adap, 1, I2C_PCF_ESO);
93
94
95
96
97
98
99
100
101
102 if (adap->lab_mdelay)
103 mdelay(adap->lab_mdelay);
104
105 DEB2(printk(KERN_INFO
106 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
107 get_pcf(adap, 1)));
108}
109
110static int wait_for_bb(struct i2c_algo_pcf_data *adap)
111{
112
113 int timeout = DEF_TIMEOUT;
114 int status;
115
116 status = get_pcf(adap, 1);
117
118 while (!(status & I2C_PCF_BB) && --timeout) {
119 udelay(100);
120 status = get_pcf(adap, 1);
121 }
122
123 if (timeout == 0) {
124 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
125 return -ETIMEDOUT;
126 }
127
128 return 0;
129}
130
131static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
132{
133
134 int timeout = DEF_TIMEOUT;
135
136 *status = get_pcf(adap, 1);
137
138 while ((*status & I2C_PCF_PIN) && --timeout) {
139 adap->waitforpin(adap->data);
140 *status = get_pcf(adap, 1);
141 }
142 if (*status & I2C_PCF_LAB) {
143 handle_lab(adap, status);
144 return -EINTR;
145 }
146
147 if (timeout == 0)
148 return -ETIMEDOUT;
149
150 return 0;
151}
152
153
154
155
156
157
158
159
160
161
162
163
164static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
165{
166 unsigned char temp;
167
168 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
169 get_pcf(adap, 1)));
170
171
172 set_pcf(adap, 1, I2C_PCF_PIN);
173
174
175
176
177 if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
178 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
179 return -ENXIO;
180 }
181
182
183 i2c_outb(adap, get_own(adap));
184
185 if ((temp = i2c_inb(adap)) != get_own(adap)) {
186 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
187 return -ENXIO;
188 }
189
190
191 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
192
193 if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
194 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
195 return -ENXIO;
196 }
197
198
199 i2c_outb(adap, get_clock(adap));
200
201 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
202 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
203 return -ENXIO;
204 }
205
206
207 set_pcf(adap, 1, I2C_PCF_IDLE);
208
209
210 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
211 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
212 return -ENXIO;
213 }
214
215 printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
216
217 return 0;
218}
219
220static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
221 int count, int last)
222{
223 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
224 int wrcount, status, timeout;
225
226 for (wrcount=0; wrcount<count; ++wrcount) {
227 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
228 buf[wrcount] & 0xff));
229 i2c_outb(adap, buf[wrcount]);
230 timeout = wait_for_pin(adap, &status);
231 if (timeout) {
232 if (timeout == -EINTR)
233 return -EINTR;
234
235 i2c_stop(adap);
236 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
237 return -EREMOTEIO;
238 }
239 if (status & I2C_PCF_LRB) {
240 i2c_stop(adap);
241 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
242 return -EREMOTEIO;
243 }
244 }
245 if (last)
246 i2c_stop(adap);
247 else
248 i2c_repstart(adap);
249
250 return wrcount;
251}
252
253static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
254 int count, int last)
255{
256 int i, status;
257 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
258 int wfp;
259
260
261 for (i = 0; i <= count; i++) {
262
263 if ((wfp = wait_for_pin(adap, &status))) {
264 if (wfp == -EINTR)
265 return -EINTR;
266
267 i2c_stop(adap);
268 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
269 return -1;
270 }
271
272 if ((status & I2C_PCF_LRB) && (i != count)) {
273 i2c_stop(adap);
274 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
275 return -1;
276 }
277
278 if (i == count - 1) {
279 set_pcf(adap, 1, I2C_PCF_ESO);
280 } else if (i == count) {
281 if (last)
282 i2c_stop(adap);
283 else
284 i2c_repstart(adap);
285 }
286
287 if (i)
288 buf[i - 1] = i2c_inb(adap);
289 else
290 i2c_inb(adap);
291 }
292
293 return i - 1;
294}
295
296
297static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
298 struct i2c_msg *msg)
299{
300 unsigned short flags = msg->flags;
301 unsigned char addr;
302
303 addr = msg->addr << 1;
304 if (flags & I2C_M_RD)
305 addr |= 1;
306 if (flags & I2C_M_REV_DIR_ADDR)
307 addr ^= 1;
308 i2c_outb(adap, addr);
309
310 return 0;
311}
312
313static int pcf_xfer(struct i2c_adapter *i2c_adap,
314 struct i2c_msg *msgs,
315 int num)
316{
317 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
318 struct i2c_msg *pmsg;
319 int i;
320 int ret=0, timeout, status;
321
322 if (adap->xfer_begin)
323 adap->xfer_begin(adap->data);
324
325
326 timeout = wait_for_bb(adap);
327 if (timeout) {
328 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
329 "Timeout waiting for BB in pcf_xfer\n");)
330 i = -EIO;
331 goto out;
332 }
333
334 for (i = 0;ret >= 0 && i < num; i++) {
335 pmsg = &msgs[i];
336
337 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
338 pmsg->flags & I2C_M_RD ? "read" : "write",
339 pmsg->len, pmsg->addr, i + 1, num);)
340
341 ret = pcf_doAddress(adap, pmsg);
342
343
344 if (i == 0)
345 i2c_start(adap);
346
347
348 timeout = wait_for_pin(adap, &status);
349 if (timeout) {
350 if (timeout == -EINTR) {
351
352 i = -EINTR;
353 goto out;
354 }
355 i2c_stop(adap);
356 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
357 "for PIN(1) in pcf_xfer\n");)
358 i = -EREMOTEIO;
359 goto out;
360 }
361
362
363 if (status & I2C_PCF_LRB) {
364 i2c_stop(adap);
365 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
366 i = -EREMOTEIO;
367 goto out;
368 }
369
370 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
371 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
372
373 if (pmsg->flags & I2C_M_RD) {
374 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
375 (i + 1 == num));
376
377 if (ret != pmsg->len) {
378 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
379 "only read %d bytes.\n",ret));
380 } else {
381 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
382 }
383 } else {
384 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
385 (i + 1 == num));
386
387 if (ret != pmsg->len) {
388 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
389 "only wrote %d bytes.\n",ret));
390 } else {
391 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
392 }
393 }
394 }
395
396out:
397 if (adap->xfer_end)
398 adap->xfer_end(adap->data);
399 return i;
400}
401
402static u32 pcf_func(struct i2c_adapter *adap)
403{
404 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
405 I2C_FUNC_PROTOCOL_MANGLING;
406}
407
408
409static const struct i2c_algorithm pcf_algo = {
410 .master_xfer = pcf_xfer,
411 .functionality = pcf_func,
412};
413
414
415
416
417int i2c_pcf_add_bus(struct i2c_adapter *adap)
418{
419 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
420 int rval;
421
422 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
423
424
425 adap->algo = &pcf_algo;
426
427 if ((rval = pcf_init_8584(pcf_adap)))
428 return rval;
429
430 rval = i2c_add_adapter(adap);
431
432 return rval;
433}
434EXPORT_SYMBOL(i2c_pcf_add_bus);
435
436MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
437MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
438MODULE_LICENSE("GPL");
439
440module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
441MODULE_PARM_DESC(i2c_debug,
442 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
443