Subversion Repositories Kolibri OS

Rev

Rev 1403 | Rev 3120 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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