2d261d4ce7ac8b8cff9c9ca5a45c4235506a7724
2 * Copyright (C) 1998 Itai Nahshon, Michael Schimek
4 * The original code was derived from and inspired by
5 * the I2C driver from the Linux kernel.
6 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
9 #ifdef HAVE_XORG_CONFIG_H
10 #include <xorg-config.h>
18 #include "xf86_OSproc.h"
22 #include <X11/Xproto.h>
23 #include "scrnintstr.h"
24 #include "regionstr.h"
25 #include "windowstr.h"
26 #include "pixmapstr.h"
30 #include "dixstruct.h"
34 #define I2C_TIMEOUT(x) /*(x)*/ /* Report timeouts */
35 #define I2C_TRACE(x) /*(x)*/ /* Report progress */
37 /* This is the default I2CUDelay function if not supplied by the driver.
38 * High level I2C interfaces implementing the bus protocol in hardware
39 * should supply this function too.
41 * Delay execution at least usec microseconds.
42 * All values 0 to 1e6 inclusive must be expected.
46 I2CUDelay(I2CBusPtr b
, int usec
)
48 struct timeval begin
, cur
;
53 X_GETTIMEOFDAY(&begin
);
55 /* It would be nice to use {xf86}usleep,
56 * but usleep (1) takes >10000 usec !
59 d_secs
= (cur
.tv_sec
- begin
.tv_sec
);
60 d_usecs
= (cur
.tv_usec
- begin
.tv_usec
);
61 diff
= d_secs
* 1000000 + d_usecs
;
62 } while (diff
>= 0 && diff
< (usec
+ 1));
66 /* Most drivers will register just with GetBits/PutBits functions.
67 * The following functions implement a software I2C protocol
68 * by using the promitive functions given by the driver.
69 * ================================================================
71 * It is assumed that there is just one master on the I2C bus, therefore
72 * there is no explicit test for conflits.
75 #define RISEFALLTIME 2 /* usec, actually 300 to 1000 ns according to the i2c specs */
77 /* Some devices will hold SCL low to slow down the bus or until
78 * ready for transmission.
80 * This condition will be noticed when the master tries to raise
81 * the SCL line. You can set the timeout to zero if the slave device
82 * does not support this clock synchronization.
86 I2CRaiseSCL(I2CBusPtr b
, int sda
, int timeout
)
90 b
->I2CPutBits(b
, 1, sda
);
91 b
->I2CUDelay(b
, b
->RiseFallTime
);
93 for (i
= timeout
; i
> 0; i
-= b
->RiseFallTime
) {
94 b
->I2CGetBits(b
, &scl
, &sda
);
97 b
->I2CUDelay(b
, b
->RiseFallTime
);
102 ("[I2CRaiseSCL(<%s>, %d, %d) timeout]", b
->BusName
, sda
,
110 /* Send a start signal on the I2C bus. The start signal notifies
111 * devices that a new transaction is initiated by the bus master.
113 * The start signal is always followed by a slave address.
114 * Slave addresses are 8+ bits. The first 7 bits identify the
115 * device and the last bit signals if this is a read (1) or
116 * write (0) operation.
118 * There may be more than one start signal on one transaction.
119 * This happens for example on some devices that allow reading
120 * of registers. First send a start bit followed by the device
121 * address (with the last bit 0) and the register number. Then send
122 * a new start bit with the device address (with the last bit 1)
123 * and then read the value from the device.
125 * Note this is function does not implement a multiple master
126 * arbitration procedure.
130 I2CStart(I2CBusPtr b
, int timeout
)
132 if (!I2CRaiseSCL(b
, 1, timeout
))
135 b
->I2CPutBits(b
, 1, 0);
136 b
->I2CUDelay(b
, b
->HoldTime
);
137 b
->I2CPutBits(b
, 0, 0);
138 b
->I2CUDelay(b
, b
->HoldTime
);
140 I2C_TRACE(ErrorF("\ni2c: <"));
145 /* This is the default I2CStop function if not supplied by the driver.
147 * Signal devices on the I2C bus that a transaction on the
148 * bus has finished. There may be more than one start signal
149 * on a transaction but only one stop signal.
155 I2CBusPtr b
= d
->pI2CBus
;
157 b
->I2CPutBits(b
, 0, 0);
158 b
->I2CUDelay(b
, b
->RiseFallTime
);
160 b
->I2CPutBits(b
, 1, 0);
161 b
->I2CUDelay(b
, b
->HoldTime
);
162 b
->I2CPutBits(b
, 1, 1);
163 b
->I2CUDelay(b
, b
->HoldTime
);
165 I2C_TRACE(ErrorF(">\n"));
168 /* Write/Read a single bit to/from a device.
169 * Return FALSE if a timeout occurs.
173 I2CWriteBit(I2CBusPtr b
, int sda
, int timeout
)
177 b
->I2CPutBits(b
, 0, sda
);
178 b
->I2CUDelay(b
, b
->RiseFallTime
);
180 r
= I2CRaiseSCL(b
, sda
, timeout
);
181 b
->I2CUDelay(b
, b
->HoldTime
);
183 b
->I2CPutBits(b
, 0, sda
);
184 b
->I2CUDelay(b
, b
->HoldTime
);
190 I2CReadBit(I2CBusPtr b
, int *psda
, int timeout
)
195 r
= I2CRaiseSCL(b
, 1, timeout
);
196 b
->I2CUDelay(b
, b
->HoldTime
);
198 b
->I2CGetBits(b
, &scl
, psda
);
200 b
->I2CPutBits(b
, 0, 1);
201 b
->I2CUDelay(b
, b
->HoldTime
);
206 /* This is the default I2CPutByte function if not supplied by the driver.
208 * A single byte is sent to the device.
209 * The function returns FALSE if a timeout occurs, you should send
210 * a stop condition afterwards to reset the bus.
213 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
214 * or slows down the bus for more than BitTimeout usecs for each bit,
215 * or does not send an ACK bit (0) to acknowledge the transmission within
216 * AcknTimeout usecs, but a NACK (1) bit.
218 * AcknTimeout must be at least b->HoldTime, the other timeouts can be
219 * zero according to the comment on I2CRaiseSCL.
223 I2CPutByte(I2CDevPtr d
, I2CByte data
)
227 I2CBusPtr b
= d
->pI2CBus
;
229 if (!I2CWriteBit(b
, (data
>> 7) & 1, d
->ByteTimeout
))
232 for (i
= 6; i
>= 0; i
--)
233 if (!I2CWriteBit(b
, (data
>> i
) & 1, d
->BitTimeout
))
236 b
->I2CPutBits(b
, 0, 1);
237 b
->I2CUDelay(b
, b
->RiseFallTime
);
239 r
= I2CRaiseSCL(b
, 1, b
->HoldTime
);
242 for (i
= d
->AcknTimeout
; i
> 0; i
-= b
->HoldTime
) {
243 b
->I2CUDelay(b
, b
->HoldTime
);
244 b
->I2CGetBits(b
, &scl
, &sda
);
250 I2C_TIMEOUT(ErrorF("[I2CPutByte(<%s>, 0x%02x, %d, %d, %d) timeout]",
251 b
->BusName
, data
, d
->BitTimeout
,
252 d
->ByteTimeout
, d
->AcknTimeout
));
256 I2C_TRACE(ErrorF("W%02x%c ", (int) data
, sda
? '-' : '+'));
259 b
->I2CPutBits(b
, 0, 1);
260 b
->I2CUDelay(b
, b
->HoldTime
);
265 /* This is the default I2CGetByte function if not supplied by the driver.
267 * A single byte is read from the device.
268 * The function returns FALSE if a timeout occurs, you should send
269 * a stop condition afterwards to reset the bus.
272 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
273 * or slows down the bus for more than b->BitTimeout usecs for each bit.
275 * ByteTimeout must be at least b->HoldTime, the other timeouts can be
276 * zero according to the comment on I2CRaiseSCL.
278 * For the <last> byte in a sequence the acknowledge bit NACK (1),
279 * otherwise ACK (0) will be sent.
283 I2CGetByte(I2CDevPtr d
, I2CByte
* data
, Bool last
)
286 I2CBusPtr b
= d
->pI2CBus
;
288 b
->I2CPutBits(b
, 0, 1);
289 b
->I2CUDelay(b
, b
->RiseFallTime
);
291 if (!I2CReadBit(b
, &sda
, d
->ByteTimeout
))
294 *data
= (sda
> 0) << 7;
296 for (i
= 6; i
>= 0; i
--)
297 if (!I2CReadBit(b
, &sda
, d
->BitTimeout
))
300 *data
|= (sda
> 0) << i
;
302 if (!I2CWriteBit(b
, last
? 1 : 0, d
->BitTimeout
))
305 I2C_TRACE(ErrorF("R%02x%c ", (int) *data
, last
? '+' : '-'));
310 /* This is the default I2CAddress function if not supplied by the driver.
312 * It creates the start condition, followed by the d->SlaveAddr.
313 * Higher level functions must call this routine rather than
314 * I2CStart/PutByte because a hardware I2C master may not be able
315 * to send a slave address without a start condition.
317 * The same timeouts apply as with I2CPutByte and additional a
318 * StartTimeout, similar to the ByteTimeout but for the start
321 * In case of a timeout, the bus is left in a clean idle condition.
322 * I. e. you *must not* send a Stop. If this function succeeds, you *must*.
324 * The slave address format is 16 bit, with the legacy _8_bit_ slave address
325 * in the least significant byte. This is, the slave address must include the
326 * R/_W flag as least significant bit.
328 * The most significant byte of the address will be sent _after_ the LSB,
329 * but only if the LSB indicates:
330 * a) an 11 bit address, this is LSB = 1111 0xxx.
331 * b) a 'general call address', this is LSB = 0000 000x - see the I2C specs
336 I2CAddress(I2CDevPtr d
, I2CSlaveAddr addr
)
338 if (I2CStart(d
->pI2CBus
, d
->StartTimeout
)) {
339 if (I2CPutByte(d
, addr
& 0xFF)) {
340 if ((addr
& 0xF8) != 0xF0 && (addr
& 0xFE) != 0x00)
343 if (I2CPutByte(d
, (addr
>> 8) & 0xFF))
353 /* These are the hardware independent I2C helper functions.
354 * ========================================================
357 /* Function for probing. Just send the slave address
358 * and return true if the device responds. The slave address
359 * must have the lsb set to reflect a read (1) or write (0) access.
360 * Don't expect a read- or write-only device will respond otherwise.
364 xf86I2CProbeAddress(I2CBusPtr b
, I2CSlaveAddr addr
)
369 d
.DevName
= "Probing";
370 d
.BitTimeout
= b
->BitTimeout
;
371 d
.ByteTimeout
= b
->ByteTimeout
;
372 d
.AcknTimeout
= b
->AcknTimeout
;
373 d
.StartTimeout
= b
->StartTimeout
;
378 r
= b
->I2CAddress(&d
, addr
);
386 /* All functions below are related to devices and take the
387 * slave address and timeout values from an I2CDevRec. They
388 * return FALSE in case of an error (presumably a timeout).
391 /* General purpose read and write function.
394 * Send a start condition
395 * Send the slave address (1 or 2 bytes) with write flag
396 * Write n bytes from WriteBuffer
398 * Send a start condition [again]
399 * Send the slave address (1 or 2 bytes) with read flag
400 * Read n bytes to ReadBuffer
401 * 3rd, if a Start condition has been successfully sent,
402 * Send a Stop condition.
404 * The functions exits immediately when an error occures,
405 * not proceeding any data left. However, step 3 will
406 * be executed anyway to leave the bus in clean idle state.
410 I2CWriteRead(I2CDevPtr d
,
411 I2CByte
* WriteBuffer
, int nWrite
, I2CByte
* ReadBuffer
, int nRead
)
414 I2CBusPtr b
= d
->pI2CBus
;
417 if (r
&& nWrite
> 0) {
418 r
= b
->I2CAddress(d
, d
->SlaveAddr
& ~1);
420 for (; nWrite
> 0; WriteBuffer
++, nWrite
--)
421 if (!(r
= b
->I2CPutByte(d
, *WriteBuffer
)))
427 if (r
&& nRead
> 0) {
428 r
= b
->I2CAddress(d
, d
->SlaveAddr
| 1);
430 for (; nRead
> 0; ReadBuffer
++, nRead
--)
431 if (!(r
= b
->I2CGetByte(d
, ReadBuffer
, nRead
== 1)))
443 /* wrapper - for compatibility and convinience */
446 xf86I2CWriteRead(I2CDevPtr d
,
447 I2CByte
* WriteBuffer
, int nWrite
,
448 I2CByte
* ReadBuffer
, int nRead
)
450 I2CBusPtr b
= d
->pI2CBus
;
452 return b
->I2CWriteRead(d
, WriteBuffer
, nWrite
, ReadBuffer
, nRead
);
455 /* Read a byte, the only readable register of a device.
459 xf86I2CReadStatus(I2CDevPtr d
, I2CByte
* pbyte
)
461 return xf86I2CWriteRead(d
, NULL
, 0, pbyte
, 1);
464 /* Read a byte from one of the registers determined by its sub-address.
468 xf86I2CReadByte(I2CDevPtr d
, I2CByte subaddr
, I2CByte
* pbyte
)
470 return xf86I2CWriteRead(d
, &subaddr
, 1, pbyte
, 1);
473 /* Read bytes from subsequent registers determined by the
474 * sub-address of the first register.
478 xf86I2CReadBytes(I2CDevPtr d
, I2CByte subaddr
, I2CByte
* pbyte
, int n
)
480 return xf86I2CWriteRead(d
, &subaddr
, 1, pbyte
, n
);
483 /* Read a word (high byte, then low byte) from one of the registers
484 * determined by its sub-address.
488 xf86I2CReadWord(I2CDevPtr d
, I2CByte subaddr
, unsigned short *pword
)
492 if (!xf86I2CWriteRead(d
, &subaddr
, 1, rb
, 2))
495 *pword
= (rb
[0] << 8) | rb
[1];
500 /* Write a byte to one of the registers determined by its sub-address.
504 xf86I2CWriteByte(I2CDevPtr d
, I2CByte subaddr
, I2CByte byte
)
511 return xf86I2CWriteRead(d
, wb
, 2, NULL
, 0);
514 /* Write bytes to subsequent registers determined by the
515 * sub-address of the first register.
519 xf86I2CWriteBytes(I2CDevPtr d
, I2CByte subaddr
,
520 I2CByte
* WriteBuffer
, int nWrite
)
522 I2CBusPtr b
= d
->pI2CBus
;
526 r
= b
->I2CAddress(d
, d
->SlaveAddr
& ~1);
528 if ((r
= b
->I2CPutByte(d
, subaddr
)))
529 for (; nWrite
> 0; WriteBuffer
++, nWrite
--)
530 if (!(r
= b
->I2CPutByte(d
, *WriteBuffer
)))
540 /* Write a word (high byte, then low byte) to one of the registers
541 * determined by its sub-address.
545 xf86I2CWriteWord(I2CDevPtr d
, I2CByte subaddr
, unsigned short word
)
553 return xf86I2CWriteRead(d
, wb
, 3, NULL
, 0);
556 /* Write a vector of bytes to not adjacent registers. This vector is,
557 * 1st byte sub-address, 2nd byte value, 3rd byte sub-address asf.
558 * This function is intended to initialize devices. Note this function
559 * exits immediately when an error occurs, some registers may
560 * remain uninitialized.
564 xf86I2CWriteVec(I2CDevPtr d
, I2CByte
* vec
, int nValues
)
566 I2CBusPtr b
= d
->pI2CBus
;
571 for (; nValues
> 0; nValues
--, vec
+= 2) {
572 if (!(r
= b
->I2CAddress(d
, d
->SlaveAddr
& ~1)))
577 if (!(r
= b
->I2CPutByte(d
, vec
[0])))
580 if (!(r
= b
->I2CPutByte(d
, vec
[1])))
591 /* Administrative functions.
592 * =========================
595 /* Allocates an I2CDevRec for you and initializes with propper defaults
596 * you may modify before calling xf86I2CDevInit. Your I2CDevRec must
597 * contain at least a SlaveAddr, and a pI2CBus pointer to the bus this
598 * device shall be linked to.
600 * See function I2CAddress for the slave address format. Always set
601 * the least significant bit, indicating a read or write access, to zero.
605 xf86CreateI2CDevRec(void)
607 return calloc(1, sizeof(I2CDevRec
));
610 /* Unlink an I2C device. If you got the I2CDevRec from xf86CreateI2CDevRec
611 * you should set <unalloc> to free it.
615 xf86DestroyI2CDevRec(I2CDevPtr d
, Bool unalloc
)
620 /* Remove this from the list of active I2C devices. */
622 for (p
= &d
->pI2CBus
->FirstDev
; *p
!= NULL
; p
= &(*p
)->NextDev
)
628 xf86DrvMsg(d
->pI2CBus
->scrnIndex
, X_INFO
,
629 "I2C device \"%s:%s\" removed.\n",
630 d
->pI2CBus
->BusName
, d
->DevName
);
637 /* I2C transmissions are related to an I2CDevRec you must link to a
638 * previously registered bus (see xf86I2CBusInit) before attempting
639 * to read and write data. You may call xf86I2CProbeAddress first to
640 * see if the device in question is present on this bus.
642 * xf86I2CDevInit will not allocate an I2CBusRec for you, instead you
643 * may enter a pointer to a statically allocated I2CDevRec or the (modified)
644 * result of xf86CreateI2CDevRec.
646 * If you don't specify timeouts for the device (n <= 0), it will inherit
647 * the bus-wide defaults. The function returns TRUE on success.
651 xf86I2CDevInit(I2CDevPtr d
)
656 (b
= d
->pI2CBus
) == NULL
||
657 (d
->SlaveAddr
& 1) || xf86I2CFindDev(b
, d
->SlaveAddr
) != NULL
)
660 if (d
->BitTimeout
<= 0)
661 d
->BitTimeout
= b
->BitTimeout
;
662 if (d
->ByteTimeout
<= 0)
663 d
->ByteTimeout
= b
->ByteTimeout
;
664 if (d
->AcknTimeout
<= 0)
665 d
->AcknTimeout
= b
->AcknTimeout
;
666 if (d
->StartTimeout
<= 0)
667 d
->StartTimeout
= b
->StartTimeout
;
669 d
->NextDev
= b
->FirstDev
;
672 xf86DrvMsg(b
->scrnIndex
, X_INFO
,
673 "I2C device \"%s:%s\" registered at address 0x%02X.\n",
674 b
->BusName
, d
->DevName
, d
->SlaveAddr
);
680 xf86I2CFindDev(I2CBusPtr b
, I2CSlaveAddr addr
)
685 for (d
= b
->FirstDev
; d
!= NULL
; d
= d
->NextDev
)
686 if (d
->SlaveAddr
== addr
)
693 static I2CBusPtr I2CBusList
;
695 /* Allocates an I2CBusRec for you and initializes with propper defaults
696 * you may modify before calling xf86I2CBusInit. Your I2CBusRec must
697 * contain at least a BusName, a scrnIndex (or -1), and a complete set
698 * of either high or low level I2C function pointers. You may pass
699 * bus-wide timeouts, otherwise inplausible values will be replaced
700 * with safe defaults.
704 xf86CreateI2CBusRec(void)
708 b
= (I2CBusPtr
) calloc(1, sizeof(I2CBusRec
));
713 b
->HoldTime
= 5; /* 100 kHz bus */
718 b
->RiseFallTime
= RISEFALLTIME
;
724 /* Unregister an I2C bus. If you got the I2CBusRec from xf86CreateI2CBusRec
725 * you should set <unalloc> to free it. If you set <devs_too>, the function
726 * xf86DestroyI2CDevRec will be called for all devices linked to the bus
727 * first, passing down the <unalloc> option.
731 xf86DestroyI2CBusRec(I2CBusPtr b
, Bool unalloc
, Bool devs_too
)
736 /* Remove this from the list of active I2C buses */
738 for (p
= &I2CBusList
; *p
!= NULL
; p
= &(*p
)->NextBus
)
744 if (b
->FirstDev
!= NULL
) {
748 while ((d
= b
->FirstDev
) != NULL
) {
749 b
->FirstDev
= d
->NextDev
;
750 xf86DestroyI2CDevRec(d
, unalloc
);
756 "i2c bug: Attempt to remove I2C bus \"%s\", "
757 "but device list is not empty.\n", b
->BusName
);
763 xf86DrvMsg(b
->scrnIndex
, X_INFO
, "I2C bus \"%s\" removed.\n",
771 /* I2C masters have to register themselves using this function.
772 * It will not allocate an I2CBusRec for you, instead you may enter
773 * a pointer to a statically allocated I2CBusRec or the (modified)
774 * result of xf86CreateI2CBusRec. Returns TRUE on success.
776 * At this point there won't be any traffic on the I2C bus.
780 xf86I2CBusInit(I2CBusPtr b
)
782 /* I2C buses must be identified by a unique scrnIndex
783 * and name. If scrnIndex is unspecified (a negative value),
784 * then the name must be unique throughout the server.
787 if (b
->BusName
== NULL
|| xf86I2CFindBus(b
->scrnIndex
, b
->BusName
) != NULL
)
790 /* If the high level functions are not
791 * supplied, use the generic functions.
792 * In this case we need the low-level
795 if (b
->I2CWriteRead
== NULL
) {
796 b
->I2CWriteRead
= I2CWriteRead
;
798 if (b
->I2CPutBits
== NULL
|| b
->I2CGetBits
== NULL
) {
799 if (b
->I2CPutByte
== NULL
||
800 b
->I2CGetByte
== NULL
||
801 b
->I2CAddress
== NULL
||
802 b
->I2CStart
== NULL
|| b
->I2CStop
== NULL
)
806 b
->I2CPutByte
= I2CPutByte
;
807 b
->I2CGetByte
= I2CGetByte
;
808 b
->I2CAddress
= I2CAddress
;
809 b
->I2CStop
= I2CStop
;
810 b
->I2CStart
= I2CStart
;
814 if (b
->I2CUDelay
== NULL
)
815 b
->I2CUDelay
= I2CUDelay
;
819 if (b
->BitTimeout
<= 0)
820 b
->BitTimeout
= b
->HoldTime
;
821 if (b
->ByteTimeout
<= 0)
822 b
->ByteTimeout
= b
->HoldTime
;
823 if (b
->AcknTimeout
<= 0)
824 b
->AcknTimeout
= b
->HoldTime
;
825 if (b
->StartTimeout
<= 0)
826 b
->StartTimeout
= b
->HoldTime
;
828 /* Put new bus on list. */
830 b
->NextBus
= I2CBusList
;
833 xf86DrvMsg(b
->scrnIndex
, X_INFO
, "I2C bus \"%s\" initialized.\n",
840 xf86I2CFindBus(int scrnIndex
, char *name
)
845 for (p
= I2CBusList
; p
!= NULL
; p
= p
->NextBus
)
846 if (scrnIndex
< 0 || p
->scrnIndex
== scrnIndex
)
847 if (!strcmp(p
->BusName
, name
))
854 * Return an array of I2CBusPtr's related to a screen. The caller is
855 * responsible for freeing the array.
858 xf86I2CGetScreenBuses(int scrnIndex
, I2CBusPtr
** pppI2CBus
)
866 for (pI2CBus
= I2CBusList
; pI2CBus
; pI2CBus
= pI2CBus
->NextBus
) {
867 if ((pI2CBus
->scrnIndex
>= 0) && (pI2CBus
->scrnIndex
!= scrnIndex
))
875 *pppI2CBus
= xnfrealloc(*pppI2CBus
, n
* sizeof(I2CBusPtr
));
876 (*pppI2CBus
)[n
- 1] = pI2CBus
;