Subversion Repositories Kolibri OS

Rev

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