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>
10 #ifdef HAVE_XORG_CONFIG_H
11 #include <xorg-config.h>
19 #include "xf86_OSproc.h"
23 #include <X11/Xproto.h>
24 #include "scrnintstr.h"
25 #include "regionstr.h"
26 #include "windowstr.h"
27 #include "pixmapstr.h"
31 #include "dixstruct.h"
35 #define I2C_TIMEOUT(x) /*(x)*/ /* Report timeouts */
36 #define I2C_TRACE(x) /*(x)*/ /* Report progress */
38 /* Set which OSs have bad gettimeofday resolution. */
39 #if defined(SVR4) && !defined(sun)
40 #define BAD_GETTIMEOFDAY_RESOLUTION
44 /* This is the default I2CUDelay function if not supplied by the driver.
45 * High level I2C interfaces implementing the bus protocol in hardware
46 * should supply this function too.
48 * Delay execution at least usec microseconds.
49 * All values 0 to 1e6 inclusive must be expected.
52 #ifdef BAD_GETTIMEOFDAY_RESOLUTION
54 * This is temporary until a better, portable
55 * way is found. Adjust bogo_usec to match CPU speed.
57 static int bogo_usec
= 500;
60 I2CUDelay(I2CBusPtr b
, int usec
)
65 for (i
= usec
* bogo_usec
; i
> 0; i
--)
66 /* (perhaps hw delay action) */;
70 I2CUDelay(I2CBusPtr b
, int usec
)
72 struct timeval begin
, cur
;
77 X_GETTIMEOFDAY(&begin
);
79 /* It would be nice to use {xf86}usleep,
80 * but usleep (1) takes >10000 usec !
83 d_secs
= (cur
.tv_sec
- begin
.tv_sec
);
84 d_usecs
= (cur
.tv_usec
- begin
.tv_usec
);
85 diff
= d_secs
*1000000 + d_usecs
;
86 } while (diff
>=0 && diff
< (usec
+ 1));
91 /* Most drivers will register just with GetBits/PutBits functions.
92 * The following functions implement a software I2C protocol
93 * by using the promitive functions given by the driver.
94 * ================================================================
96 * It is assumed that there is just one master on the I2C bus, therefore
97 * there is no explicit test for conflits.
100 #define RISEFALLTIME 2 /* usec, actually 300 to 1000 ns according to the i2c specs */
102 /* Some devices will hold SCL low to slow down the bus or until
103 * ready for transmission.
105 * This condition will be noticed when the master tries to raise
106 * the SCL line. You can set the timeout to zero if the slave device
107 * does not support this clock synchronization.
111 I2CRaiseSCL(I2CBusPtr b
, int sda
, int timeout
)
115 b
->I2CPutBits(b
, 1, sda
);
116 b
->I2CUDelay(b
, b
->RiseFallTime
);
118 for (i
= timeout
; i
> 0; i
-= b
->RiseFallTime
) {
119 b
->I2CGetBits(b
, &scl
, &sda
);
121 b
->I2CUDelay(b
, b
->RiseFallTime
);
125 I2C_TIMEOUT(ErrorF("[I2CRaiseSCL(<%s>, %d, %d) timeout]", b
->BusName
, sda
, timeout
));
132 /* Send a start signal on the I2C bus. The start signal notifies
133 * devices that a new transaction is initiated by the bus master.
135 * The start signal is always followed by a slave address.
136 * Slave addresses are 8+ bits. The first 7 bits identify the
137 * device and the last bit signals if this is a read (1) or
138 * write (0) operation.
140 * There may be more than one start signal on one transaction.
141 * This happens for example on some devices that allow reading
142 * of registers. First send a start bit followed by the device
143 * address (with the last bit 0) and the register number. Then send
144 * a new start bit with the device address (with the last bit 1)
145 * and then read the value from the device.
147 * Note this is function does not implement a multiple master
148 * arbitration procedure.
152 I2CStart(I2CBusPtr b
, int timeout
)
154 if (!I2CRaiseSCL(b
, 1, timeout
))
157 b
->I2CPutBits(b
, 1, 0);
158 b
->I2CUDelay(b
, b
->HoldTime
);
159 b
->I2CPutBits(b
, 0, 0);
160 b
->I2CUDelay(b
, b
->HoldTime
);
162 I2C_TRACE(ErrorF("\ni2c: <"));
167 /* This is the default I2CStop function if not supplied by the driver.
169 * Signal devices on the I2C bus that a transaction on the
170 * bus has finished. There may be more than one start signal
171 * on a transaction but only one stop signal.
177 I2CBusPtr b
= d
->pI2CBus
;
179 b
->I2CPutBits(b
, 0, 0);
180 b
->I2CUDelay(b
, b
->RiseFallTime
);
182 b
->I2CPutBits(b
, 1, 0);
183 b
->I2CUDelay(b
, b
->HoldTime
);
184 b
->I2CPutBits(b
, 1, 1);
185 b
->I2CUDelay(b
, b
->HoldTime
);
187 I2C_TRACE(ErrorF(">\n"));
190 /* Write/Read a single bit to/from a device.
191 * Return FALSE if a timeout occurs.
195 I2CWriteBit(I2CBusPtr b
, int sda
, int timeout
)
199 b
->I2CPutBits(b
, 0, sda
);
200 b
->I2CUDelay(b
, b
->RiseFallTime
);
202 r
= I2CRaiseSCL(b
, sda
, timeout
);
203 b
->I2CUDelay(b
, b
->HoldTime
);
205 b
->I2CPutBits(b
, 0, sda
);
206 b
->I2CUDelay(b
, b
->HoldTime
);
212 I2CReadBit(I2CBusPtr b
, int *psda
, int timeout
)
217 r
= I2CRaiseSCL(b
, 1, timeout
);
218 b
->I2CUDelay(b
, b
->HoldTime
);
220 b
->I2CGetBits(b
, &scl
, psda
);
222 b
->I2CPutBits(b
, 0, 1);
223 b
->I2CUDelay(b
, b
->HoldTime
);
228 /* This is the default I2CPutByte function if not supplied by the driver.
230 * A single byte is sent to the device.
231 * The function returns FALSE if a timeout occurs, you should send
232 * a stop condition afterwards to reset the bus.
235 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
236 * or slows down the bus for more than BitTimeout usecs for each bit,
237 * or does not send an ACK bit (0) to acknowledge the transmission within
238 * AcknTimeout usecs, but a NACK (1) bit.
240 * AcknTimeout must be at least b->HoldTime, the other timeouts can be
241 * zero according to the comment on I2CRaiseSCL.
245 I2CPutByte(I2CDevPtr d
, I2CByte data
)
249 I2CBusPtr b
= d
->pI2CBus
;
251 if (!I2CWriteBit(b
, (data
>> 7) & 1, d
->ByteTimeout
))
254 for (i
= 6; i
>= 0; i
--)
255 if (!I2CWriteBit(b
, (data
>> i
) & 1, d
->BitTimeout
))
258 b
->I2CPutBits(b
, 0, 1);
259 b
->I2CUDelay(b
, b
->RiseFallTime
);
261 r
= I2CRaiseSCL(b
, 1, b
->HoldTime
);
264 for (i
= d
->AcknTimeout
; i
> 0; i
-= b
->HoldTime
) {
265 b
->I2CUDelay(b
, b
->HoldTime
);
266 b
->I2CGetBits(b
, &scl
, &sda
);
271 I2C_TIMEOUT(ErrorF("[I2CPutByte(<%s>, 0x%02x, %d, %d, %d) timeout]",
272 b
->BusName
, data
, d
->BitTimeout
,
273 d
->ByteTimeout
, d
->AcknTimeout
));
277 I2C_TRACE(ErrorF("W%02x%c ", (int) data
, sda
? '-' : '+'));
280 b
->I2CPutBits(b
, 0, 1);
281 b
->I2CUDelay(b
, b
->HoldTime
);
286 /* This is the default I2CGetByte function if not supplied by the driver.
288 * A single byte is read from the device.
289 * The function returns FALSE if a timeout occurs, you should send
290 * a stop condition afterwards to reset the bus.
293 * if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
294 * or slows down the bus for more than b->BitTimeout usecs for each bit.
296 * ByteTimeout must be at least b->HoldTime, the other timeouts can be
297 * zero according to the comment on I2CRaiseSCL.
299 * For the <last> byte in a sequence the acknowledge bit NACK (1),
300 * otherwise ACK (0) will be sent.
304 I2CGetByte(I2CDevPtr d
, I2CByte
*data
, Bool last
)
307 I2CBusPtr b
= d
->pI2CBus
;
309 b
->I2CPutBits(b
, 0, 1);
310 b
->I2CUDelay(b
, b
->RiseFallTime
);
312 if (!I2CReadBit(b
, &sda
, d
->ByteTimeout
))
315 *data
= (sda
> 0) << 7;
317 for (i
= 6; i
>= 0; i
--)
318 if (!I2CReadBit(b
, &sda
, d
->BitTimeout
))
321 *data
|= (sda
> 0) << i
;
323 if (!I2CWriteBit(b
, last
? 1 : 0, d
->BitTimeout
))
326 I2C_TRACE(ErrorF("R%02x%c ", (int) *data
, last
? '+' : '-'));
331 /* This is the default I2CAddress function if not supplied by the driver.
333 * It creates the start condition, followed by the d->SlaveAddr.
334 * Higher level functions must call this routine rather than
335 * I2CStart/PutByte because a hardware I2C master may not be able
336 * to send a slave address without a start condition.
338 * The same timeouts apply as with I2CPutByte and additional a
339 * StartTimeout, similar to the ByteTimeout but for the start
342 * In case of a timeout, the bus is left in a clean idle condition.
343 * I. e. you *must not* send a Stop. If this function succeeds, you *must*.
345 * The slave address format is 16 bit, with the legacy _8_bit_ slave address
346 * in the least significant byte. This is, the slave address must include the
347 * R/_W flag as least significant bit.
349 * The most significant byte of the address will be sent _after_ the LSB,
350 * but only if the LSB indicates:
351 * a) an 11 bit address, this is LSB = 1111 0xxx.
352 * b) a 'general call address', this is LSB = 0000 000x - see the I2C specs
357 I2CAddress(I2CDevPtr d
, I2CSlaveAddr addr
)
359 if (I2CStart(d
->pI2CBus
, d
->StartTimeout
)) {
360 if (I2CPutByte(d
, addr
& 0xFF)) {
361 if ((addr
& 0xF8) != 0xF0 &&
362 (addr
& 0xFE) != 0x00)
365 if (I2CPutByte(d
, (addr
>> 8) & 0xFF))
375 /* These are the hardware independent I2C helper functions.
376 * ========================================================
379 /* Function for probing. Just send the slave address
380 * and return true if the device responds. The slave address
381 * must have the lsb set to reflect a read (1) or write (0) access.
382 * Don't expect a read- or write-only device will respond otherwise.
386 xf86I2CProbeAddress(I2CBusPtr b
, I2CSlaveAddr addr
)
391 d
.DevName
= "Probing";
392 d
.BitTimeout
= b
->BitTimeout
;
393 d
.ByteTimeout
= b
->ByteTimeout
;
394 d
.AcknTimeout
= b
->AcknTimeout
;
395 d
.StartTimeout
= b
->StartTimeout
;
400 r
= b
->I2CAddress(&d
, addr
);
402 if (r
) b
->I2CStop(&d
);
407 /* All functions below are related to devices and take the
408 * slave address and timeout values from an I2CDevRec. They
409 * return FALSE in case of an error (presumably a timeout).
412 /* General purpose read and write function.
415 * Send a start condition
416 * Send the slave address (1 or 2 bytes) with write flag
417 * Write n bytes from WriteBuffer
419 * Send a start condition [again]
420 * Send the slave address (1 or 2 bytes) with read flag
421 * Read n bytes to ReadBuffer
422 * 3rd, if a Start condition has been successfully sent,
423 * Send a Stop condition.
425 * The functions exits immediately when an error occures,
426 * not proceeding any data left. However, step 3 will
427 * be executed anyway to leave the bus in clean idle state.
431 I2CWriteRead(I2CDevPtr d
,
432 I2CByte
*WriteBuffer
, int nWrite
,
433 I2CByte
*ReadBuffer
, int nRead
)
436 I2CBusPtr b
= d
->pI2CBus
;
439 if (r
&& nWrite
> 0) {
440 r
= b
->I2CAddress(d
, d
->SlaveAddr
& ~1);
442 for (; nWrite
> 0; WriteBuffer
++, nWrite
--)
443 if (!(r
= b
->I2CPutByte(d
, *WriteBuffer
)))
449 if (r
&& nRead
> 0) {
450 r
= b
->I2CAddress(d
, d
->SlaveAddr
| 1);
452 for (; nRead
> 0; ReadBuffer
++, nRead
--)
453 if (!(r
= b
->I2CGetByte(d
, ReadBuffer
, nRead
== 1)))
459 if (s
) b
->I2CStop(d
);
464 /* wrapper - for compatibility and convinience */
467 xf86I2CWriteRead(I2CDevPtr d
,
468 I2CByte
*WriteBuffer
, int nWrite
,
469 I2CByte
*ReadBuffer
, int nRead
)
471 I2CBusPtr b
= d
->pI2CBus
;
472 return b
->I2CWriteRead(d
,WriteBuffer
,nWrite
,ReadBuffer
,nRead
);
475 /* Read a byte, the only readable register of a device.
479 xf86I2CReadStatus(I2CDevPtr d
, I2CByte
*pbyte
)
481 return xf86I2CWriteRead(d
, NULL
, 0, pbyte
, 1);
484 /* Read a byte from one of the registers determined by its sub-address.
488 xf86I2CReadByte(I2CDevPtr d
, I2CByte subaddr
, I2CByte
*pbyte
)
490 return xf86I2CWriteRead(d
, &subaddr
, 1, pbyte
, 1);
493 /* Read bytes from subsequent registers determined by the
494 * sub-address of the first register.
498 xf86I2CReadBytes(I2CDevPtr d
, I2CByte subaddr
, I2CByte
*pbyte
, int n
)
500 return xf86I2CWriteRead(d
, &subaddr
, 1, pbyte
, n
);
503 /* Read a word (high byte, then low byte) from one of the registers
504 * determined by its sub-address.
508 xf86I2CReadWord(I2CDevPtr d
, I2CByte subaddr
, unsigned short *pword
)
512 if (!xf86I2CWriteRead(d
, &subaddr
, 1, rb
, 2)) return FALSE
;
514 *pword
= (rb
[0] << 8) | rb
[1];
519 /* Write a byte to one of the registers determined by its sub-address.
523 xf86I2CWriteByte(I2CDevPtr d
, I2CByte subaddr
, I2CByte byte
)
530 return xf86I2CWriteRead(d
, wb
, 2, NULL
, 0);
533 /* Write bytes to subsequent registers determined by the
534 * sub-address of the first register.
538 xf86I2CWriteBytes(I2CDevPtr d
, I2CByte subaddr
,
539 I2CByte
*WriteBuffer
, int nWrite
)
541 I2CBusPtr b
= d
->pI2CBus
;
545 r
= b
->I2CAddress(d
, d
->SlaveAddr
& ~1);
547 if ((r
= b
->I2CPutByte(d
, subaddr
)))
548 for (; nWrite
> 0; WriteBuffer
++, nWrite
--)
549 if (!(r
= b
->I2CPutByte(d
, *WriteBuffer
)))
559 /* Write a word (high byte, then low byte) to one of the registers
560 * determined by its sub-address.
564 xf86I2CWriteWord(I2CDevPtr d
, I2CByte subaddr
, unsigned short word
)
572 return xf86I2CWriteRead(d
, wb
, 3, NULL
, 0);
575 /* Write a vector of bytes to not adjacent registers. This vector is,
576 * 1st byte sub-address, 2nd byte value, 3rd byte sub-address asf.
577 * This function is intended to initialize devices. Note this function
578 * exits immediately when an error occurs, some registers may
579 * remain uninitialized.
583 xf86I2CWriteVec(I2CDevPtr d
, I2CByte
*vec
, int nValues
)
585 I2CBusPtr b
= d
->pI2CBus
;
590 for (; nValues
> 0; nValues
--, vec
+= 2) {
591 if (!(r
= b
->I2CAddress(d
, d
->SlaveAddr
& ~1)))
596 if (!(r
= b
->I2CPutByte(d
, vec
[0])))
599 if (!(r
= b
->I2CPutByte(d
, vec
[1])))
603 if (s
> 0) b
->I2CStop(d
);
609 /* Administrative functions.
610 * =========================
613 /* Allocates an I2CDevRec for you and initializes with propper defaults
614 * you may modify before calling xf86I2CDevInit. Your I2CDevRec must
615 * contain at least a SlaveAddr, and a pI2CBus pointer to the bus this
616 * device shall be linked to.
618 * See function I2CAddress for the slave address format. Always set
619 * the least significant bit, indicating a read or write access, to zero.
623 xf86CreateI2CDevRec(void)
625 return xcalloc(1, sizeof(I2CDevRec
));
628 /* Unlink an I2C device. If you got the I2CDevRec from xf86CreateI2CDevRec
629 * you should set <unalloc> to free it.
633 xf86DestroyI2CDevRec(I2CDevPtr d
, Bool unalloc
)
638 /* Remove this from the list of active I2C devices. */
640 for (p
= &d
->pI2CBus
->FirstDev
; *p
!= NULL
; p
= &(*p
)->NextDev
)
646 xf86DrvMsg(d
->pI2CBus
->scrnIndex
, X_INFO
,
647 "I2C device \"%s:%s\" removed.\n",
648 d
->pI2CBus
->BusName
, d
->DevName
);
650 if (unalloc
) xfree(d
);
654 /* I2C transmissions are related to an I2CDevRec you must link to a
655 * previously registered bus (see xf86I2CBusInit) before attempting
656 * to read and write data. You may call xf86I2CProbeAddress first to
657 * see if the device in question is present on this bus.
659 * xf86I2CDevInit will not allocate an I2CBusRec for you, instead you
660 * may enter a pointer to a statically allocated I2CDevRec or the (modified)
661 * result of xf86CreateI2CDevRec.
663 * If you don't specify timeouts for the device (n <= 0), it will inherit
664 * the bus-wide defaults. The function returns TRUE on success.
668 xf86I2CDevInit(I2CDevPtr d
)
673 (b
= d
->pI2CBus
) == NULL
||
674 (d
->SlaveAddr
& 1) ||
675 xf86I2CFindDev(b
, d
->SlaveAddr
) != NULL
)
678 if (d
->BitTimeout
<= 0) d
->BitTimeout
= b
->BitTimeout
;
679 if (d
->ByteTimeout
<= 0) d
->ByteTimeout
= b
->ByteTimeout
;
680 if (d
->AcknTimeout
<= 0) d
->AcknTimeout
= b
->AcknTimeout
;
681 if (d
->StartTimeout
<= 0) d
->StartTimeout
= b
->StartTimeout
;
683 d
->NextDev
= b
->FirstDev
;
686 xf86DrvMsg(b
->scrnIndex
, X_INFO
,
687 "I2C device \"%s:%s\" registered at address 0x%02X.\n",
688 b
->BusName
, d
->DevName
, d
->SlaveAddr
);
694 xf86I2CFindDev(I2CBusPtr b
, I2CSlaveAddr addr
)
699 for (d
= b
->FirstDev
; d
!= NULL
; d
= d
->NextDev
)
700 if (d
->SlaveAddr
== addr
)
707 static I2CBusPtr I2CBusList
;
709 /* Allocates an I2CBusRec for you and initializes with propper defaults
710 * you may modify before calling xf86I2CBusInit. Your I2CBusRec must
711 * contain at least a BusName, a scrnIndex (or -1), and a complete set
712 * of either high or low level I2C function pointers. You may pass
713 * bus-wide timeouts, otherwise inplausible values will be replaced
714 * with safe defaults.
718 xf86CreateI2CBusRec(void)
722 b
= (I2CBusPtr
) xcalloc(1, sizeof(I2CBusRec
));
726 b
->HoldTime
= 5; /* 100 kHz bus */
731 b
->RiseFallTime
= RISEFALLTIME
;
737 /* Unregister an I2C bus. If you got the I2CBusRec from xf86CreateI2CBusRec
738 * you should set <unalloc> to free it. If you set <devs_too>, the function
739 * xf86DestroyI2CDevRec will be called for all devices linked to the bus
740 * first, passing down the <unalloc> option.
744 xf86DestroyI2CBusRec(I2CBusPtr b
, Bool unalloc
, Bool devs_too
)
749 /* Remove this from the list of active I2C buses */
751 for (p
= &I2CBusList
; *p
!= NULL
; p
= &(*p
)->NextBus
)
757 if (b
->FirstDev
!= NULL
) {
761 while ((d
= b
->FirstDev
) != NULL
) {
762 b
->FirstDev
= d
->NextDev
;
763 xf86DestroyI2CDevRec(d
, unalloc
);
767 xf86Msg(X_ERROR
, "i2c bug: Attempt to remove I2C bus \"%s\", "
768 "but device list is not empty.\n",
775 xf86DrvMsg(b
->scrnIndex
, X_INFO
, "I2C bus \"%s\" removed.\n",
778 if (unalloc
) xfree(b
);
782 /* I2C masters have to register themselves using this function.
783 * It will not allocate an I2CBusRec for you, instead you may enter
784 * a pointer to a statically allocated I2CBusRec or the (modified)
785 * result of xf86CreateI2CBusRec. Returns TRUE on success.
787 * At this point there won't be any traffic on the I2C bus.
791 xf86I2CBusInit(I2CBusPtr b
)
793 /* I2C buses must be identified by a unique scrnIndex
794 * and name. If scrnIndex is unspecified (a negative value),
795 * then the name must be unique throughout the server.
798 if (b
->BusName
== NULL
||
799 xf86I2CFindBus(b
->scrnIndex
, b
->BusName
) != NULL
)
802 /* If the high level functions are not
803 * supplied, use the generic functions.
804 * In this case we need the low-level
807 if (b
->I2CWriteRead
== NULL
)
809 b
->I2CWriteRead
=I2CWriteRead
;
811 if (b
->I2CPutBits
== NULL
||
812 b
->I2CGetBits
== NULL
)
814 if (b
->I2CPutByte
== NULL
||
815 b
->I2CGetByte
== NULL
||
816 b
->I2CAddress
== NULL
||
817 b
->I2CStart
== NULL
||
821 b
->I2CPutByte
= I2CPutByte
;
822 b
->I2CGetByte
= I2CGetByte
;
823 b
->I2CAddress
= I2CAddress
;
824 b
->I2CStop
= I2CStop
;
825 b
->I2CStart
= I2CStart
;
829 if (b
->I2CUDelay
== NULL
)
830 b
->I2CUDelay
= I2CUDelay
;
832 if (b
->HoldTime
< 2) b
->HoldTime
= 5;
833 if (b
->BitTimeout
<= 0) b
->BitTimeout
= b
->HoldTime
;
834 if (b
->ByteTimeout
<= 0) b
->ByteTimeout
= b
->HoldTime
;
835 if (b
->AcknTimeout
<= 0) b
->AcknTimeout
= b
->HoldTime
;
836 if (b
->StartTimeout
<= 0) b
->StartTimeout
= b
->HoldTime
;
838 /* Put new bus on list. */
840 b
->NextBus
= I2CBusList
;
843 xf86DrvMsg(b
->scrnIndex
, X_INFO
, "I2C bus \"%s\" initialized.\n",
850 xf86I2CFindBus(int scrnIndex
, char *name
)
855 for (p
= I2CBusList
; p
!= NULL
; p
= p
->NextBus
)
856 if (scrnIndex
< 0 || p
->scrnIndex
== scrnIndex
)
857 if (!strcmp(p
->BusName
, name
))
864 * Return an array of I2CBusPtr's related to a screen. The caller is
865 * responsible for freeing the array.
868 xf86I2CGetScreenBuses(int scrnIndex
, I2CBusPtr
**pppI2CBus
)
876 for (pI2CBus
= I2CBusList
; pI2CBus
; pI2CBus
= pI2CBus
->NextBus
) {
877 if ((pI2CBus
->scrnIndex
>= 0) && (pI2CBus
->scrnIndex
!= scrnIndex
))
885 *pppI2CBus
= xnfrealloc(*pppI2CBus
, n
* sizeof(I2CBusPtr
));
886 *pppI2CBus
[n
- 1] = pI2CBus
;