Subversion Repositories Kolibri OS

Rev

Rev 1403 | Go to most recent revision | Details | 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 
26
#include 
27
#include 
28
#include 
29
#include 
30
 
31
 
32
/* ----- global defines ----------------------------------------------- */
33
 
34
#ifdef DEBUG
35
#define bit_dbg(level, dev, format, args...) \
36
	do { \
37
		if (i2c_debug >= level) \
38
			dev_dbg(dev, format, ##args); \
39
	} while (0)
40
#else
41
#define bit_dbg(level, dev, format, args...) \
42
	do {} while (0)
43
#endif /* DEBUG */
44
 
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 */
167
//           bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
168
//               "timeout at bit #%d\n", (int)c, i);
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 */
181
//       bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
182
//           "timeout at ack\n", (int)c);
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 */
190
//   bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,
191
//       ack ? "A" : "NA");
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
 */
229
static int test_bus(struct i2c_algo_bit_data *adap, char *name)
230
{
231
	int scl, sda;
232
 
233
	if (adap->getscl == NULL)
234
		pr_info("%s: Testing SDA only, SCL is not readable\n", name);
235
 
236
	sda = getsda(adap);
237
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
238
	if (!scl || !sda) {
239
		printk(KERN_WARNING "%s: bus seems to be busy\n", name);
240
		goto bailout;
241
	}
242
 
243
	sdalo(adap);
244
	sda = getsda(adap);
245
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
246
	if (sda) {
247
		printk(KERN_WARNING "%s: SDA stuck high!\n", name);
248
		goto bailout;
249
	}
250
	if (!scl) {
251
		printk(KERN_WARNING "%s: SCL unexpected low "
252
		       "while pulling SDA low!\n", name);
253
		goto bailout;
254
	}
255
 
256
	sdahi(adap);
257
	sda = getsda(adap);
258
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
259
	if (!sda) {
260
		printk(KERN_WARNING "%s: SDA stuck low!\n", name);
261
		goto bailout;
262
	}
263
	if (!scl) {
264
		printk(KERN_WARNING "%s: SCL unexpected low "
265
		       "while pulling SDA high!\n", name);
266
		goto bailout;
267
	}
268
 
269
	scllo(adap);
270
	sda = getsda(adap);
271
	scl = (adap->getscl == NULL) ? 0 : getscl(adap);
272
	if (scl) {
273
		printk(KERN_WARNING "%s: SCL stuck high!\n", name);
274
		goto bailout;
275
	}
276
	if (!sda) {
277
		printk(KERN_WARNING "%s: SDA unexpected low "
278
		       "while pulling SCL low!\n", name);
279
		goto bailout;
280
	}
281
 
282
	sclhi(adap);
283
	sda = getsda(adap);
284
	scl = (adap->getscl == NULL) ? 1 : getscl(adap);
285
	if (!scl) {
286
		printk(KERN_WARNING "%s: SCL stuck low!\n", name);
287
		goto bailout;
288
	}
289
	if (!sda) {
290
		printk(KERN_WARNING "%s: SDA unexpected low "
291
		       "while pulling SCL high!\n", name);
292
		goto bailout;
293
	}
294
	pr_info("%s: Test OK\n", name);
295
	return 0;
296
bailout:
297
	sdahi(adap);
298
	sclhi(adap);
299
	return -ENODEV;
300
}
301
 
302
/* ----- Utility functions
303
 */
304
 
305
/* try_address tries to contact a chip for a number of
306
 * times before it gives up.
307
 * return values:
308
 * 1 chip answered
309
 * 0 chip did not answer
310
 * -x transmission error
311
 */
312
static int try_address(struct i2c_adapter *i2c_adap,
313
		       unsigned char addr, int retries)
314
{
315
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
316
	int i, ret = 0;
317
 
318
	for (i = 0; i <= retries; i++) {
319
		ret = i2c_outb(i2c_adap, addr);
320
		if (ret == 1 || i == retries)
321
			break;
322
		bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
323
		i2c_stop(adap);
324
		udelay(adap->udelay);
325
//       yield();
326
		bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
327
		i2c_start(adap);
328
	}
329
	if (i && ret)
330
		bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at "
331
			"0x%02x: %s\n", i + 1,
332
			addr & 1 ? "read from" : "write to", addr >> 1,
333
			ret == 1 ? "success" : "failed, timeout?");
334
	return ret;
335
}
336
 
337
static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
338
{
339
	const unsigned char *temp = msg->buf;
340
	int count = msg->len;
341
	unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
342
	int retval;
343
	int wrcount = 0;
344
 
345
	while (count > 0) {
346
		retval = i2c_outb(i2c_adap, *temp);
347
 
348
		/* OK/ACK; or ignored NAK */
349
		if ((retval > 0) || (nak_ok && (retval == 0))) {
350
			count--;
351
			temp++;
352
			wrcount++;
353
 
354
		/* A slave NAKing the master means the slave didn't like
355
		 * something about the data it saw.  For example, maybe
356
		 * the SMBus PEC was wrong.
357
		 */
358
		} else if (retval == 0) {
359
//           dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
360
			return -EIO;
361
 
362
		/* Timeout; or (someday) lost arbitration
363
		 *
364
		 * FIXME Lost ARB implies retrying the transaction from
365
		 * the first message, after the "winning" master issues
366
		 * its STOP.  As a rule, upper layer code has no reason
367
		 * to know or care about this ... it is *NOT* an error.
368
		 */
369
		} else {
370
 //          dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
371
 //                  retval);
372
			return retval;
373
		}
374
	}
375
	return wrcount;
376
}
377
 
378
static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
379
{
380
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
381
 
382
	/* assert: sda is high */
383
	if (is_ack)		/* send ack */
384
		setsda(adap, 0);
385
	udelay((adap->udelay + 1) / 2);
386
	if (sclhi(adap) < 0) {	/* timeout */
387
//       dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
388
//       return -ETIMEDOUT;
389
	}
390
	scllo(adap);
391
	return 0;
392
}
393
 
394
static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
395
{
396
	int inval;
397
	int rdcount = 0;	/* counts bytes read */
398
	unsigned char *temp = msg->buf;
399
	int count = msg->len;
400
	const unsigned flags = msg->flags;
401
 
402
	while (count > 0) {
403
		inval = i2c_inb(i2c_adap);
404
		if (inval >= 0) {
405
			*temp = inval;
406
			rdcount++;
407
		} else {   /* read timed out */
408
			break;
409
		}
410
 
411
		temp++;
412
		count--;
413
 
414
		/* Some SMBus transactions require that we receive the
415
		   transaction length as the first read byte. */
416
		if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
417
			if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
418
				if (!(flags & I2C_M_NO_RD_ACK))
419
					acknak(i2c_adap, 0);
420
//               dev_err(&i2c_adap->dev, "readbytes: invalid "
421
//                   "block length (%d)\n", inval);
422
				return -EREMOTEIO;
423
			}
424
			/* The original count value accounts for the extra
425
			   bytes, that is, either 1 for a regular transaction,
426
			   or 2 for a PEC transaction. */
427
			count += inval;
428
			msg->len += inval;
429
		}
430
 
431
//       bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
432
//           inval,
433
//           (flags & I2C_M_NO_RD_ACK)
434
//               ? "(no ack/nak)"
435
//               : (count ? "A" : "NA"));
436
 
437
		if (!(flags & I2C_M_NO_RD_ACK)) {
438
			inval = acknak(i2c_adap, count);
439
			if (inval < 0)
440
				return inval;
441
		}
442
	}
443
	return rdcount;
444
}
445
 
446
/* doAddress initiates the transfer by generating the start condition (in
447
 * try_address) and transmits the address in the necessary format to handle
448
 * reads, writes as well as 10bit-addresses.
449
 * returns:
450
 *  0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
451
 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
452
 *	-ETIMEDOUT, for example if the lines are stuck...)
453
 */
454
static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
455
{
456
	unsigned short flags = msg->flags;
457
	unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
458
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
459
 
460
	unsigned char addr;
461
	int ret, retries;
462
 
463
	retries = nak_ok ? 0 : i2c_adap->retries;
464
 
465
	if (flags & I2C_M_TEN) {
466
		/* a ten bit address */
467
		addr = 0xf0 | ((msg->addr >> 7) & 0x03);
468
		bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
469
		/* try extended address code...*/
470
		ret = try_address(i2c_adap, addr, retries);
471
		if ((ret != 1) && !nak_ok)  {
472
 //          dev_err(&i2c_adap->dev,
473
 //              "died at extended address code\n");
474
			return -EREMOTEIO;
475
		}
476
		/* the remaining 8 bit address */
477
		ret = i2c_outb(i2c_adap, msg->addr & 0x7f);
478
		if ((ret != 1) && !nak_ok) {
479
			/* the chip did not ack / xmission error occurred */
480
//           dev_err(&i2c_adap->dev, "died at 2nd address code\n");
481
			return -EREMOTEIO;
482
		}
483
		if (flags & I2C_M_RD) {
484
			bit_dbg(3, &i2c_adap->dev, "emitting repeated "
485
				"start condition\n");
486
			i2c_repstart(adap);
487
			/* okay, now switch into reading mode */
488
			addr |= 0x01;
489
			ret = try_address(i2c_adap, addr, retries);
490
			if ((ret != 1) && !nak_ok) {
491
//               dev_err(&i2c_adap->dev,
492
//                   "died at repeated address code\n");
493
				return -EREMOTEIO;
494
			}
495
		}
496
	} else {		/* normal 7bit address	*/
497
		addr = msg->addr << 1;
498
		if (flags & I2C_M_RD)
499
			addr |= 1;
500
		if (flags & I2C_M_REV_DIR_ADDR)
501
			addr ^= 1;
502
		ret = try_address(i2c_adap, addr, retries);
503
		if ((ret != 1) && !nak_ok)
504
			return -ENXIO;
505
	}
506
 
507
	return 0;
508
}
509
 
510
static int bit_xfer(struct i2c_adapter *i2c_adap,
511
		    struct i2c_msg msgs[], int num)
512
{
513
	struct i2c_msg *pmsg;
514
	struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
515
	int i, ret;
516
	unsigned short nak_ok;
517
 
518
	bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
519
	i2c_start(adap);
520
	for (i = 0; i < num; i++) {
521
		pmsg = &msgs[i];
522
		nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
523
		if (!(pmsg->flags & I2C_M_NOSTART)) {
524
			if (i) {
525
				bit_dbg(3, &i2c_adap->dev, "emitting "
526
					"repeated start condition\n");
527
				i2c_repstart(adap);
528
			}
529
			ret = bit_doAddress(i2c_adap, pmsg);
530
			if ((ret != 0) && !nak_ok) {
531
				bit_dbg(1, &i2c_adap->dev, "NAK from "
532
					"device addr 0x%02x msg #%d\n",
533
					msgs[i].addr, i);
534
				goto bailout;
535
			}
536
		}
537
		if (pmsg->flags & I2C_M_RD) {
538
			/* read bytes into buffer*/
539
			ret = readbytes(i2c_adap, pmsg);
540
			if (ret >= 1)
541
				bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
542
					ret, ret == 1 ? "" : "s");
543
			if (ret < pmsg->len) {
544
				if (ret >= 0)
545
					ret = -EREMOTEIO;
546
				goto bailout;
547
			}
548
		} else {
549
			/* write bytes from buffer */
550
			ret = sendbytes(i2c_adap, pmsg);
551
			if (ret >= 1)
552
				bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
553
					ret, ret == 1 ? "" : "s");
554
			if (ret < pmsg->len) {
555
				if (ret >= 0)
556
					ret = -EREMOTEIO;
557
				goto bailout;
558
			}
559
		}
560
	}
561
	ret = i;
562
 
563
bailout:
564
	bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
565
	i2c_stop(adap);
566
	return ret;
567
}
568
 
569
static u32 bit_func(struct i2c_adapter *adap)
570
{
571
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
572
	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
573
	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
574
	       I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
575
}
576
 
577
 
578
/* -----exported algorithm data: -------------------------------------	*/
579
 
580
static const struct i2c_algorithm i2c_bit_algo = {
581
	.master_xfer	= bit_xfer,
582
	.functionality	= bit_func,
583
};
584
 
585
/*
586
 * registering functions to load algorithms at runtime
587
 */
588
static int i2c_bit_prepare_bus(struct i2c_adapter *adap)
589
{
590
	struct i2c_algo_bit_data *bit_adap = adap->algo_data;
591
 
592
//   if (bit_test) {
593
//       int ret = test_bus(bit_adap, adap->name);
594
//       if (ret < 0)
595
//           return -ENODEV;
596
//   }
597
 
598
	/* register new adapter to i2c module... */
599
	adap->algo = &i2c_bit_algo;
600
	adap->retries = 3;
601
 
602
	return 0;
603
}
604
 
605
int i2c_bit_add_bus(struct i2c_adapter *adap)
606
{
607
	int err;
608
 
609
	err = i2c_bit_prepare_bus(adap);
610
	if (err)
611
		return err;
612
 
613
    return 0; //i2c_add_adapter(adap);
614
}
615