Subversion Repositories Kolibri OS

Rev

Rev 1125 | Rev 1963 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1125 serge 1
/* -------------------------------------------------------------------------
2
 * i2c-algo-bit.c i2c driver algorithms for bit-shift adapters
3
 * -------------------------------------------------------------------------
4
 *   Copyright (C) 1995-2000 Simon G. Vogl
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 as published by
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 * ------------------------------------------------------------------------- */
20
 
21
/* With some changes from Frodo Looijaard , Kyösti Mälkki
22
    and Jean Delvare  */
23
 
24
#include 
25
#include 
1403 serge 26
#include 
1125 serge 27
#include 
28
#include 
29
#include 
30
#include 
31
 
32
 
33
/* ----- global defines ----------------------------------------------- */
34
 
35
#ifdef DEBUG
36
#define bit_dbg(level, dev, format, args...) \
37
	do { \
38
		if (i2c_debug >= level) \
39
			dev_dbg(dev, format, ##args); \
40
	} while (0)
41
#else
42
#define bit_dbg(level, dev, format, args...) \
43
	do {} while (0)
44
#endif /* DEBUG */
45
 
46
 
47
/* ----- global variables ---------------------------------------------	*/
48
 
49
static int bit_test;	/* see if the line-setting functions work	*/
50
 
51
 
52
/* --- setting states on the bus with the right timing: ---------------	*/
53
 
54
#define setsda(adap, val)	adap->setsda(adap->data, val)
55
#define setscl(adap, val)	adap->setscl(adap->data, val)
56
#define getsda(adap)		adap->getsda(adap->data)
57
#define getscl(adap)		adap->getscl(adap->data)
58
 
59
static inline void sdalo(struct i2c_algo_bit_data *adap)
60
{
61
	setsda(adap, 0);
62
	udelay((adap->udelay + 1) / 2);
63
}
64
 
65
static inline void sdahi(struct i2c_algo_bit_data *adap)
66
{
67
	setsda(adap, 1);
68
	udelay((adap->udelay + 1) / 2);
69
}
70
 
71
static inline void scllo(struct i2c_algo_bit_data *adap)
72
{
73
	setscl(adap, 0);
74
	udelay(adap->udelay / 2);
75
}
76
 
77
/*
78
 * Raise scl line, and do checking for delays. This is necessary for slower
79
 * devices.
80
 */
81
static int sclhi(struct i2c_algo_bit_data *adap)
82
{
83
	unsigned long start;
84
 
85
	setscl(adap, 1);
86
 
87
	/* Not all adapters have scl sense line... */
88
	if (!adap->getscl)
89
		goto done;
90
 
91
//   start = jiffies;
92
	while (!getscl(adap)) {
93
		/* This hw knows how to read the clock line, so we wait
94
		 * until it actually gets high.  This is safer as some
95
		 * chips may hold it low ("clock stretching") while they
96
		 * are processing data internally.
97
		 */
98
//       if (time_after(jiffies, start + adap->timeout))
99
//           return -ETIMEDOUT;
100
 
101
        udelay(adap->udelay);
102
 
103
//       cond_resched();
104
	}
105
#ifdef DEBUG
106
	if (jiffies != start && i2c_debug >= 3)
107
		pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go "
108
			 "high\n", jiffies - start);
109
#endif
110
 
111
done:
112
	udelay(adap->udelay);
113
	return 0;
114
}
115
 
116
 
117
/* --- other auxiliary functions --------------------------------------	*/
118
static void i2c_start(struct i2c_algo_bit_data *adap)
119
{
120
	/* assert: scl, sda are high */
121
	setsda(adap, 0);
122
	udelay(adap->udelay);
123
	scllo(adap);
124
}
125
 
126
static void i2c_repstart(struct i2c_algo_bit_data *adap)
127
{
128
	/* assert: scl is low */
129
	sdahi(adap);
130
	sclhi(adap);
131
	setsda(adap, 0);
132
	udelay(adap->udelay);
133
	scllo(adap);
134
}
135
 
136
 
137
static void i2c_stop(struct i2c_algo_bit_data *adap)
138
{
139
	/* assert: scl is low */
140
	sdalo(adap);
141
	sclhi(adap);
142
	setsda(adap, 1);
143
	udelay(adap->udelay);
144
}
145
 
146
 
147
 
148
/* send a byte without start cond., look for arbitration,
149
   check ackn. from slave */
150
/* returns:
151
 * 1 if the device acknowledged
152
 * 0 if the device did not ack
153
 * -ETIMEDOUT if an error occurred (while raising the scl line)
154
 */
155
static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)
156
{
157
	int i;
158
	int sb;
159
	int ack;
160
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
161
 
162
	/* assert: scl is low */
163
	for (i = 7; i >= 0; i--) {
164
		sb = (c >> i) & 1;
165
		setsda(adap, sb);
166
		udelay((adap->udelay + 1) / 2);
167
		if (sclhi(adap) < 0) { /* timed out */
168
//           bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
169
//               "timeout at bit #%d\n", (int)c, i);
170
			return -ETIMEDOUT;
171
		}
172
		/* FIXME do arbitration here:
173
		 * if (sb && !getsda(adap)) -> ouch! Get out of here.
174
		 *
175
		 * Report a unique code, so higher level code can retry
176
		 * the whole (combined) message and *NOT* issue STOP.
177
		 */
178
		scllo(adap);
179
	}
180
	sdahi(adap);
181
	if (sclhi(adap) < 0) { /* timeout */
182
//       bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
183
//           "timeout at ack\n", (int)c);
184
		return -ETIMEDOUT;
185
	}
186
 
187
	/* read ack: SDA should be pulled down by slave, or it may
188
	 * NAK (usually to report problems with the data we wrote).
189
	 */
190
	ack = !getsda(adap);    /* ack: sda is pulled low -> success */
191
//   bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,
192
//       ack ? "A" : "NA");
193
 
194
	scllo(adap);
195
	return ack;
196
	/* assert: scl is low (sda undef) */
197
}
198
 
199
 
200
static int i2c_inb(struct i2c_adapter *i2c_adap)
201
{
202
	/* read byte via i2c port, without start/stop sequence	*/
203
	/* acknowledge is sent in i2c_read.			*/
204
	int i;
205
	unsigned char indata = 0;
206
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
207
 
208
	/* assert: scl is low */
209
	sdahi(adap);
210
	for (i = 0; i < 8; i++) {
211
		if (sclhi(adap) < 0) { /* timeout */
212
			bit_dbg(1, &i2c_adap->dev, "i2c_inb: timeout at bit "
213
				"#%d\n", 7 - i);
214
			return -ETIMEDOUT;
215
		}
216
		indata *= 2;
217
		if (getsda(adap))
218
			indata |= 0x01;
219
		setscl(adap, 0);
220
		udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
221
	}
222
	/* assert: scl is low */
223
	return indata;
224
}
225
 
226
/*
227
 * Sanity check for the adapter hardware - check the reaction of
228
 * the bus lines only if it seems to be idle.
229
 */
230
static int test_bus(struct i2c_algo_bit_data *adap, char *name)
231
{
232
	int scl, sda;
233
 
234
	if (adap->getscl == NULL)
235
		pr_info("%s: Testing SDA only, SCL is not readable\n", name);
236
 
237
	sda = getsda(adap);
238
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
239
	if (!scl || !sda) {
240
		printk(KERN_WARNING "%s: bus seems to be busy\n", name);
241
		goto bailout;
242
	}
243
 
244
	sdalo(adap);
245
	sda = getsda(adap);
246
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
247
	if (sda) {
248
		printk(KERN_WARNING "%s: SDA stuck high!\n", name);
249
		goto bailout;
250
	}
251
	if (!scl) {
252
		printk(KERN_WARNING "%s: SCL unexpected low "
253
		       "while pulling SDA low!\n", name);
254
		goto bailout;
255
	}
256
 
257
	sdahi(adap);
258
	sda = getsda(adap);
259
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
260
	if (!sda) {
261
		printk(KERN_WARNING "%s: SDA stuck low!\n", name);
262
		goto bailout;
263
	}
264
	if (!scl) {
265
		printk(KERN_WARNING "%s: SCL unexpected low "
266
		       "while pulling SDA high!\n", name);
267
		goto bailout;
268
	}
269
 
270
	scllo(adap);
271
	sda = getsda(adap);
272
	scl = (adap->getscl == NULL) ? 0 : getscl(adap);
273
	if (scl) {
274
		printk(KERN_WARNING "%s: SCL stuck high!\n", name);
275
		goto bailout;
276
	}
277
	if (!sda) {
278
		printk(KERN_WARNING "%s: SDA unexpected low "
279
		       "while pulling SCL low!\n", name);
280
		goto bailout;
281
	}
282
 
283
	sclhi(adap);
284
	sda = getsda(adap);
285
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
286
	if (!scl) {
287
		printk(KERN_WARNING "%s: SCL stuck low!\n", name);
288
		goto bailout;
289
	}
290
	if (!sda) {
291
		printk(KERN_WARNING "%s: SDA unexpected low "
292
		       "while pulling SCL high!\n", name);
293
		goto bailout;
294
	}
295
	pr_info("%s: Test OK\n", name);
296
	return 0;
297
bailout:
298
	sdahi(adap);
299
	sclhi(adap);
300
	return -ENODEV;
301
}
302
 
303
/* ----- Utility functions
304
 */
305
 
306
/* try_address tries to contact a chip for a number of
307
 * times before it gives up.
308
 * return values:
309
 * 1 chip answered
310
 * 0 chip did not answer
311
 * -x transmission error
312
 */
313
static int try_address(struct i2c_adapter *i2c_adap,
314
		       unsigned char addr, int retries)
315
{
316
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
317
	int i, ret = 0;
318
 
319
	for (i = 0; i <= retries; i++) {
320
		ret = i2c_outb(i2c_adap, addr);
321
		if (ret == 1 || i == retries)
322
			break;
323
		bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
324
		i2c_stop(adap);
325
		udelay(adap->udelay);
326
//       yield();
327
		bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
328
		i2c_start(adap);
329
	}
330
	if (i && ret)
331
		bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at "
332
			"0x%02x: %s\n", i + 1,
333
			addr & 1 ? "read from" : "write to", addr >> 1,
334
			ret == 1 ? "success" : "failed, timeout?");
335
	return ret;
336
}
337
 
338
static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
339
{
340
	const unsigned char *temp = msg->buf;
341
	int count = msg->len;
342
	unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
343
	int retval;
344
	int wrcount = 0;
345
 
346
	while (count > 0) {
347
		retval = i2c_outb(i2c_adap, *temp);
348
 
349
		/* OK/ACK; or ignored NAK */
350
		if ((retval > 0) || (nak_ok && (retval == 0))) {
351
			count--;
352
			temp++;
353
			wrcount++;
354
 
355
		/* A slave NAKing the master means the slave didn't like
356
		 * something about the data it saw.  For example, maybe
357
		 * the SMBus PEC was wrong.
358
		 */
359
		} else if (retval == 0) {
360
//           dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
361
			return -EIO;
362
 
363
		/* Timeout; or (someday) lost arbitration
364
		 *
365
		 * FIXME Lost ARB implies retrying the transaction from
366
		 * the first message, after the "winning" master issues
367
		 * its STOP.  As a rule, upper layer code has no reason
368
		 * to know or care about this ... it is *NOT* an error.
369
		 */
370
		} else {
371
 //          dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
372
 //                  retval);
373
			return retval;
374
		}
375
	}
376
	return wrcount;
377
}
378
 
379
static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
380
{
381
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
382
 
383
	/* assert: sda is high */
384
	if (is_ack)		/* send ack */
385
		setsda(adap, 0);
386
	udelay((adap->udelay + 1) / 2);
387
	if (sclhi(adap) < 0) {	/* timeout */
388
//       dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
389
//       return -ETIMEDOUT;
390
	}
391
	scllo(adap);
392
	return 0;
393
}
394
 
395
static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
396
{
397
	int inval;
398
	int rdcount = 0;	/* counts bytes read */
399
	unsigned char *temp = msg->buf;
400
	int count = msg->len;
401
	const unsigned flags = msg->flags;
402
 
403
	while (count > 0) {
404
		inval = i2c_inb(i2c_adap);
405
		if (inval >= 0) {
406
			*temp = inval;
407
			rdcount++;
408
		} else {   /* read timed out */
409
			break;
410
		}
411
 
412
		temp++;
413
		count--;
414
 
415
		/* Some SMBus transactions require that we receive the
416
		   transaction length as the first read byte. */
417
		if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
418
			if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
419
				if (!(flags & I2C_M_NO_RD_ACK))
420
					acknak(i2c_adap, 0);
421
//               dev_err(&i2c_adap->dev, "readbytes: invalid "
422
//                   "block length (%d)\n", inval);
423
				return -EREMOTEIO;
424
			}
425
			/* The original count value accounts for the extra
426
			   bytes, that is, either 1 for a regular transaction,
427
			   or 2 for a PEC transaction. */
428
			count += inval;
429
			msg->len += inval;
430
		}
431
 
432
//       bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
433
//           inval,
434
//           (flags & I2C_M_NO_RD_ACK)
435
//               ? "(no ack/nak)"
436
//               : (count ? "A" : "NA"));
437
 
438
		if (!(flags & I2C_M_NO_RD_ACK)) {
439
			inval = acknak(i2c_adap, count);
440
			if (inval < 0)
441
				return inval;
442
		}
443
	}
444
	return rdcount;
445
}
446
 
447
/* doAddress initiates the transfer by generating the start condition (in
448
 * try_address) and transmits the address in the necessary format to handle
449
 * reads, writes as well as 10bit-addresses.
450
 * returns:
451
 *  0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
452
 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
453
 *	-ETIMEDOUT, for example if the lines are stuck...)
454
 */
455
static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
456
{
457
	unsigned short flags = msg->flags;
458
	unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
459
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
460
 
461
	unsigned char addr;
462
	int ret, retries;
463
 
464
	retries = nak_ok ? 0 : i2c_adap->retries;
465
 
466
	if (flags & I2C_M_TEN) {
467
		/* a ten bit address */
468
		addr = 0xf0 | ((msg->addr >> 7) & 0x03);
469
		bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
470
		/* try extended address code...*/
471
		ret = try_address(i2c_adap, addr, retries);
472
		if ((ret != 1) && !nak_ok)  {
473
 //          dev_err(&i2c_adap->dev,
474
 //              "died at extended address code\n");
475
			return -EREMOTEIO;
476
		}
477
		/* the remaining 8 bit address */
478
		ret = i2c_outb(i2c_adap, msg->addr & 0x7f);
479
		if ((ret != 1) && !nak_ok) {
480
			/* the chip did not ack / xmission error occurred */
481
//           dev_err(&i2c_adap->dev, "died at 2nd address code\n");
482
			return -EREMOTEIO;
483
		}
484
		if (flags & I2C_M_RD) {
485
			bit_dbg(3, &i2c_adap->dev, "emitting repeated "
486
				"start condition\n");
487
			i2c_repstart(adap);
488
			/* okay, now switch into reading mode */
489
			addr |= 0x01;
490
			ret = try_address(i2c_adap, addr, retries);
491
			if ((ret != 1) && !nak_ok) {
492
//               dev_err(&i2c_adap->dev,
493
//                   "died at repeated address code\n");
494
				return -EREMOTEIO;
495
			}
496
		}
497
	} else {		/* normal 7bit address	*/
498
		addr = msg->addr << 1;
499
		if (flags & I2C_M_RD)
500
			addr |= 1;
501
		if (flags & I2C_M_REV_DIR_ADDR)
502
			addr ^= 1;
503
		ret = try_address(i2c_adap, addr, retries);
504
		if ((ret != 1) && !nak_ok)
505
			return -ENXIO;
506
	}
507
 
508
	return 0;
509
}
510
 
511
static int bit_xfer(struct i2c_adapter *i2c_adap,
512
		    struct i2c_msg msgs[], int num)
513
{
514
	struct i2c_msg *pmsg;
515
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
516
	int i, ret;
517
	unsigned short nak_ok;
518
 
519
	bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
520
	i2c_start(adap);
521
	for (i = 0; i < num; i++) {
522
		pmsg = &msgs[i];
523
		nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
524
		if (!(pmsg->flags & I2C_M_NOSTART)) {
525
			if (i) {
526
				bit_dbg(3, &i2c_adap->dev, "emitting "
527
					"repeated start condition\n");
528
				i2c_repstart(adap);
529
			}
530
			ret = bit_doAddress(i2c_adap, pmsg);
531
			if ((ret != 0) && !nak_ok) {
532
				bit_dbg(1, &i2c_adap->dev, "NAK from "
533
					"device addr 0x%02x msg #%d\n",
534
					msgs[i].addr, i);
535
				goto bailout;
536
			}
537
		}
538
		if (pmsg->flags & I2C_M_RD) {
539
			/* read bytes into buffer*/
540
			ret = readbytes(i2c_adap, pmsg);
541
			if (ret >= 1)
542
				bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
543
					ret, ret == 1 ? "" : "s");
544
			if (ret < pmsg->len) {
545
				if (ret >= 0)
546
					ret = -EREMOTEIO;
547
				goto bailout;
548
			}
549
		} else {
550
			/* write bytes from buffer */
551
			ret = sendbytes(i2c_adap, pmsg);
552
			if (ret >= 1)
553
				bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
554
					ret, ret == 1 ? "" : "s");
555
			if (ret < pmsg->len) {
556
				if (ret >= 0)
557
					ret = -EREMOTEIO;
558
				goto bailout;
559
			}
560
		}
561
	}
562
	ret = i;
563
 
564
bailout:
565
	bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
566
	i2c_stop(adap);
567
	return ret;
568
}
569
 
570
static u32 bit_func(struct i2c_adapter *adap)
571
{
572
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
573
	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
574
	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
575
	       I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
576
}
577
 
578
 
579
/* -----exported algorithm data: -------------------------------------	*/
580
 
581
static const struct i2c_algorithm i2c_bit_algo = {
582
	.master_xfer	= bit_xfer,
583
	.functionality	= bit_func,
584
};
585
 
586
/*
587
 * registering functions to load algorithms at runtime
588
 */
589
static int i2c_bit_prepare_bus(struct i2c_adapter *adap)
590
{
591
	struct i2c_algo_bit_data *bit_adap = adap->algo_data;
592
 
593
//   if (bit_test) {
594
//       int ret = test_bus(bit_adap, adap->name);
595
//       if (ret < 0)
596
//           return -ENODEV;
597
//   }
598
 
599
	/* register new adapter to i2c module... */
600
	adap->algo = &i2c_bit_algo;
601
	adap->retries = 3;
602
 
603
	return 0;
604
}
605
 
606
int i2c_bit_add_bus(struct i2c_adapter *adap)
607
{
608
	int err;
609
 
610
	err = i2c_bit_prepare_bus(adap);
611
	if (err)
612
		return err;
613
 
614
    return 0; //i2c_add_adapter(adap);
615
}
616