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