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