xserver-multidpi/hw/xfree86/i2c/xf86i2c.c
Dave Airlie 1f0e8bd5eb api: rework the X server driver API to avoid global arrays.
This is a squash merge containing all the API changes, as
well as the video ABI bump.

Its been squashed to make bisection easier.

Full patch log below:

commit b202738bbf0c5a1c1172767119c2c71f1e7f8070
Author: Aaron Plattner <aplattner@nvidia.com>
Date:   Mon May 14 15:16:11 2012 -0700

    xfree86: Bump video ABI to 13.0

    The ABI was broken by changes to convert from screen index numbers to ScreenPtr
    / ScrnInfoPtr in various structures and function signatures.

    Signed-off-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Dave Airlie <airlied@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 3d5f7d9f8d408bcad3f83277d255f25d3b0edbf3
Author: Dave Airlie <airlied@redhat.com>
Date:   Thu May 24 10:56:57 2012 +0100

    xf86: xf86ClearEntityListForScreen should take a pScrn

    When adding GPU screens this make life easier.

    (also fix comment, as pointed out by Alan)

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit afee8b5ab4501597ecc1ade34124d7ca227ab055
Author: Dave Airlie <airlied@redhat.com>
Date:   Thu May 24 07:07:32 2012 +0100

    xf86i2c: add pscrn for drivers to use

    This just adds a pScrn pointer into the struct for the drivers to use
    instead of scrnIndex. Mostly scrnIndex is used for logging, but some
    drivers use it to lookup xf86Screens, so let them stash a pScrn instead.

    Removing the scrnIndex is a bit more involved and I'm not sure its worth
    the effort. Doing i2c in the X server is legacy code as far as I'm concerned.

    Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit ea5092f1f679691d187f1eee9427e6057beec56e
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 19:25:20 2012 +0100

    dix/gc: consolidate GC object creation in one place

    The standard GC create and scratch GC create were 90% the same really,
    and I have a need in the future for creating GC objects without the
    other bits, so wanted to avoid a third copy.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 3d91482ea9b4883e64e496f2768168e0ffa21ba1
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 10:24:06 2012 +0100

    xf86: add a define to denote the new non-index interfaces are being used

    This can be used by drivers to provide compatible APIs.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 37c3ae3e6cd4f3dedc72f371096d6743f8f99df3
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 15:09:12 2012 +0100

    dix: make Create/Free scratch pixmaps take a ScreenPtr

    While technically an API/ABI change I doubt anyone uses it,
    but it helps in splitting screens up.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 75f2062a3fe94f04764ecc7d2ff2fbbeccb9da60
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 14:57:55 2012 +0100

    xf86/xv: remove scrnIndexfrom xf86FindXvOptions.

    Move this interface to taking an ScrnInfoPtr.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit f80c2374f40ea7b2ee0556e2e76cc07406f3d843
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 14:53:59 2012 +0100

    xf86: make xf86DeleteScreen take a ScrnInfoPtr (v2)

    stop passing indices into this function.

    v2: drop flags argument.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 58824e414f35682435f15bfe6c4b656bd90b9235
Author: Dave Airlie <airlied@redhat.com>
Date:   Wed May 23 14:48:09 2012 +0100

    xf86: fix xf86IsScreenPrimary interface to take a pScrn (API/ABI)

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 6b4fc1f9d391bcdf7ca288766e49bce60f4635cd
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:18:59 2012 +0100

    xserver: convert block/wakeup handlers to passing ScreenPtr (ABI/API) (v2)

    Instead of passing an index, pass the actual ScreenPtr. This allows
    more moving towards not abusing xf86Screens + screenInfo.

    v2: drop the blockData/wakeupData args as per ajax's suggestion.,
    fix docs.

    Reviewed-by: Keith Packard <keithp@keithp.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 790d003de20fb47674420a24dadd92412d78620d
Author: Dave Airlie <airlied@gmail.com>
Date:   Wed Apr 11 09:53:14 2012 +0100

    xf86/common: remove some more pScrn->pScreen uses

    remove some more conversions that appeared after api cleanups.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit aac85e18d1dd093f2cad6bd29375e40bd7af0b8f
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 16:34:53 2012 +0100

    ddc: change API to take ScrnInfoPtr (v2)

    This removes all xf86Screens usage from ddc code,
    it modifies the API for some functions to avoid taking indices.

    v2: address Alan's comments about dropping DDC2Init parameter.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit fe3f57b6eaf6860a33876a54f9439f69578f03a5
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 16:31:26 2012 +0100

    vbe: don't use index for VBEInterpretPanelID (API)

    Remove use of xf86screens from vbe module.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit abf1965f4ed91529036d3fdb470d6a3ce6f29675
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 16:25:11 2012 +0100

    int10/vbe: don't use xf86Screens. (ABI) (v3)

    Pass the ScrnInfoPtr instead of the index in the int10 struct.

    This saves us using it to dereference xf86Screens.

    v2: address Alan's comment to fix struct alignment.

    v3: squash in all the int10 fixes, test the vm86 code builds,
    after comments by Keith.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 23cca612b4fb5efc33683c7624b803b457387e3d
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:30:18 2012 +0100

    xserver: drop index argument to ScreenInit (ABI/API) (v2)

    This drops the index argument, its the same as pScreen->myNum,
    and its the last major index abuse I can find.

    v2: address Alan's review - update docs, fix xwin/xnest/darwin

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 40d360e2d7e832407f3ed64e3a02c27ecc89a960
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:23:01 2012 +0100

    xf86: migrate PointerMoved from index to ScrnInfoPtr (ABI/API)

    This migrates PointerMoved from an index to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit aa60a2f38679d0eeb979a9c2648c9bc771409bf9
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:20:46 2012 +0100

    xf86: migrate PMEvent to a ScrnInfoPtr (ABI/API)

    This migrates the PMEvent from index to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit d3f28ef44371ed4a039ffc5dd7eb6408d1269ba2
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:18:30 2012 +0100

    xf86: migrate SetDGAMode from index to ScrnInfoPtr (ABI/API)

    This migrates the SetDGAMode callback from an index to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit baf5e4818a74f2b68c3dfdcc56f54322351039a0
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:14:11 2012 +0100

    xf86: migrate ChangeGamma from index to ScrnInfoPtr (ABI/API) (v2)

    This migrates the ChangeGamma interface to avoid passing a index.

    v2: fix xf86RandR12.c + xf86cmap.c call

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 51e5f90ada929d6b23176090badbb42fdb3fa550
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:11:09 2012 +0100

    xf86/exa: migrate index to screen types for EnableDisableFBAccess (ABI/API)

    The EXA interface migrates to ScreenPtr,
    and the xf86 interface migrated to ScrnInfoPtr.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 94f1f21d17e86f96d4a54292a399160950087675
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 15:02:11 2012 +0100

    xf86: migrate ValidMode callback to ScrnInfoPtr (ABI/API)

    This migrates the ValidMode to passing a ScrnInfoPtr instead
    of an index.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 3f8f18198fed4f39ec805b508a3482e91eea26b2
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:59:46 2012 +0100

    xf86: migrate SwitchMode to taking ScrnInfoPtr (ABI/API) (v2)

    This migrate the SwitchMode interface to take a ScrnInfoPtr
    instead of an index.

    v2: drop flags.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit d06a038a5c49328ab3a8d969d24f9fcd22c63202
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:50:37 2012 +0100

    xf86: move AdjustFrame to passing ScrnInfoPtr (ABI/API) (v2)

    This converts AdjustFrame code paths to passing a ScrnInfoPtr
    instead of an integer index.

    v2: drop flags args.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 53d2f8608ffd4090d08e7d5cf2e92fb954959b90
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:41:27 2012 +0100

    xf86: modify FreeScreen callback to take pScrn instead of index. (ABI/API) (v2)

    Another index->pScrn conversion.

    v2: drop flags arg.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 60db37c0b247052e0f5c54b1921fe58a3609c2e3
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:35:41 2012 +0100

    xf86: change EnterVT/LeaveVT to take a ScrnInfoPtr (ABI/API break) (v2)

    This modifies the EnterVT/LeaveVT interfaces to take a ScrnInfoPtr
    instead of an index into xf86Screens. This allows dropping more
    public dereferences of the xf86Screens and screenInfo.

    v2: drop flags args as suggested by Keith, fix docs.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>

commit 06729dbbc804a20242e6499f446acb5d94023c3c
Author: Dave Airlie <airlied@gmail.com>
Date:   Tue Apr 10 14:04:59 2012 +0100

    xserver: remove index from CloseScreen (API/ABI breakage)

    This drops the index from the CloseScreen callback,
    its always been useless really, since the pScreen contains it.

    Reviewed-by: Alan Coopersmith <alan.coopersmith@oracle.com>
    Acked-by: Aaron Plattner <aplattner@nvidia.com>
    Reviewed-by: Adam Jackson <ajax@redhat.com>
    Signed-off-by: Dave Airlie <airlied@redhat.com>
2012-06-05 13:22:18 +01:00

881 lines
22 KiB
C

/*
* Copyright (C) 1998 Itai Nahshon, Michael Schimek
*
* The original code was derived from and inspired by
* the I2C driver from the Linux kernel.
* (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
*/
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#include <sys/time.h>
#include <string.h>
#include "misc.h"
#include "xf86.h"
#include "xf86_OSproc.h"
#include <X11/X.h>
#include <X11/Xos.h>
#include <X11/Xproto.h>
#include "scrnintstr.h"
#include "regionstr.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "validate.h"
#include "resource.h"
#include "gcstruct.h"
#include "dixstruct.h"
#include "xf86i2c.h"
#define I2C_TIMEOUT(x) /*(x)*/ /* Report timeouts */
#define I2C_TRACE(x) /*(x)*/ /* Report progress */
/* This is the default I2CUDelay function if not supplied by the driver.
* High level I2C interfaces implementing the bus protocol in hardware
* should supply this function too.
*
* Delay execution at least usec microseconds.
* All values 0 to 1e6 inclusive must be expected.
*/
static void
I2CUDelay(I2CBusPtr b, int usec)
{
struct timeval begin, cur;
long d_secs, d_usecs;
long diff;
if (usec > 0) {
X_GETTIMEOFDAY(&begin);
do {
/* It would be nice to use {xf86}usleep,
* but usleep (1) takes >10000 usec !
*/
X_GETTIMEOFDAY(&cur);
d_secs = (cur.tv_sec - begin.tv_sec);
d_usecs = (cur.tv_usec - begin.tv_usec);
diff = d_secs * 1000000 + d_usecs;
} while (diff >= 0 && diff < (usec + 1));
}
}
/* Most drivers will register just with GetBits/PutBits functions.
* The following functions implement a software I2C protocol
* by using the promitive functions given by the driver.
* ================================================================
*
* It is assumed that there is just one master on the I2C bus, therefore
* there is no explicit test for conflits.
*/
#define RISEFALLTIME 2 /* usec, actually 300 to 1000 ns according to the i2c specs */
/* Some devices will hold SCL low to slow down the bus or until
* ready for transmission.
*
* This condition will be noticed when the master tries to raise
* the SCL line. You can set the timeout to zero if the slave device
* does not support this clock synchronization.
*/
static Bool
I2CRaiseSCL(I2CBusPtr b, int sda, int timeout)
{
int i, scl;
b->I2CPutBits(b, 1, sda);
b->I2CUDelay(b, b->RiseFallTime);
for (i = timeout; i > 0; i -= b->RiseFallTime) {
b->I2CGetBits(b, &scl, &sda);
if (scl)
break;
b->I2CUDelay(b, b->RiseFallTime);
}
if (i <= 0) {
I2C_TIMEOUT(ErrorF
("[I2CRaiseSCL(<%s>, %d, %d) timeout]", b->BusName, sda,
timeout));
return FALSE;
}
return TRUE;
}
/* Send a start signal on the I2C bus. The start signal notifies
* devices that a new transaction is initiated by the bus master.
*
* The start signal is always followed by a slave address.
* Slave addresses are 8+ bits. The first 7 bits identify the
* device and the last bit signals if this is a read (1) or
* write (0) operation.
*
* There may be more than one start signal on one transaction.
* This happens for example on some devices that allow reading
* of registers. First send a start bit followed by the device
* address (with the last bit 0) and the register number. Then send
* a new start bit with the device address (with the last bit 1)
* and then read the value from the device.
*
* Note this is function does not implement a multiple master
* arbitration procedure.
*/
static Bool
I2CStart(I2CBusPtr b, int timeout)
{
if (!I2CRaiseSCL(b, 1, timeout))
return FALSE;
b->I2CPutBits(b, 1, 0);
b->I2CUDelay(b, b->HoldTime);
b->I2CPutBits(b, 0, 0);
b->I2CUDelay(b, b->HoldTime);
I2C_TRACE(ErrorF("\ni2c: <"));
return TRUE;
}
/* This is the default I2CStop function if not supplied by the driver.
*
* Signal devices on the I2C bus that a transaction on the
* bus has finished. There may be more than one start signal
* on a transaction but only one stop signal.
*/
static void
I2CStop(I2CDevPtr d)
{
I2CBusPtr b = d->pI2CBus;
b->I2CPutBits(b, 0, 0);
b->I2CUDelay(b, b->RiseFallTime);
b->I2CPutBits(b, 1, 0);
b->I2CUDelay(b, b->HoldTime);
b->I2CPutBits(b, 1, 1);
b->I2CUDelay(b, b->HoldTime);
I2C_TRACE(ErrorF(">\n"));
}
/* Write/Read a single bit to/from a device.
* Return FALSE if a timeout occurs.
*/
static Bool
I2CWriteBit(I2CBusPtr b, int sda, int timeout)
{
Bool r;
b->I2CPutBits(b, 0, sda);
b->I2CUDelay(b, b->RiseFallTime);
r = I2CRaiseSCL(b, sda, timeout);
b->I2CUDelay(b, b->HoldTime);
b->I2CPutBits(b, 0, sda);
b->I2CUDelay(b, b->HoldTime);
return r;
}
static Bool
I2CReadBit(I2CBusPtr b, int *psda, int timeout)
{
Bool r;
int scl;
r = I2CRaiseSCL(b, 1, timeout);
b->I2CUDelay(b, b->HoldTime);
b->I2CGetBits(b, &scl, psda);
b->I2CPutBits(b, 0, 1);
b->I2CUDelay(b, b->HoldTime);
return r;
}
/* This is the default I2CPutByte function if not supplied by the driver.
*
* A single byte is sent to the device.
* The function returns FALSE if a timeout occurs, you should send
* a stop condition afterwards to reset the bus.
*
* A timeout occurs,
* if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
* or slows down the bus for more than BitTimeout usecs for each bit,
* or does not send an ACK bit (0) to acknowledge the transmission within
* AcknTimeout usecs, but a NACK (1) bit.
*
* AcknTimeout must be at least b->HoldTime, the other timeouts can be
* zero according to the comment on I2CRaiseSCL.
*/
static Bool
I2CPutByte(I2CDevPtr d, I2CByte data)
{
Bool r;
int i, scl, sda;
I2CBusPtr b = d->pI2CBus;
if (!I2CWriteBit(b, (data >> 7) & 1, d->ByteTimeout))
return FALSE;
for (i = 6; i >= 0; i--)
if (!I2CWriteBit(b, (data >> i) & 1, d->BitTimeout))
return FALSE;
b->I2CPutBits(b, 0, 1);
b->I2CUDelay(b, b->RiseFallTime);
r = I2CRaiseSCL(b, 1, b->HoldTime);
if (r) {
for (i = d->AcknTimeout; i > 0; i -= b->HoldTime) {
b->I2CUDelay(b, b->HoldTime);
b->I2CGetBits(b, &scl, &sda);
if (sda == 0)
break;
}
if (i <= 0) {
I2C_TIMEOUT(ErrorF("[I2CPutByte(<%s>, 0x%02x, %d, %d, %d) timeout]",
b->BusName, data, d->BitTimeout,
d->ByteTimeout, d->AcknTimeout));
r = FALSE;
}
I2C_TRACE(ErrorF("W%02x%c ", (int) data, sda ? '-' : '+'));
}
b->I2CPutBits(b, 0, 1);
b->I2CUDelay(b, b->HoldTime);
return r;
}
/* This is the default I2CGetByte function if not supplied by the driver.
*
* A single byte is read from the device.
* The function returns FALSE if a timeout occurs, you should send
* a stop condition afterwards to reset the bus.
*
* A timeout occurs,
* if the slave pulls SCL to slow down the bus more than ByteTimeout usecs,
* or slows down the bus for more than b->BitTimeout usecs for each bit.
*
* ByteTimeout must be at least b->HoldTime, the other timeouts can be
* zero according to the comment on I2CRaiseSCL.
*
* For the <last> byte in a sequence the acknowledge bit NACK (1),
* otherwise ACK (0) will be sent.
*/
static Bool
I2CGetByte(I2CDevPtr d, I2CByte * data, Bool last)
{
int i, sda;
I2CBusPtr b = d->pI2CBus;
b->I2CPutBits(b, 0, 1);
b->I2CUDelay(b, b->RiseFallTime);
if (!I2CReadBit(b, &sda, d->ByteTimeout))
return FALSE;
*data = (sda > 0) << 7;
for (i = 6; i >= 0; i--)
if (!I2CReadBit(b, &sda, d->BitTimeout))
return FALSE;
else
*data |= (sda > 0) << i;
if (!I2CWriteBit(b, last ? 1 : 0, d->BitTimeout))
return FALSE;
I2C_TRACE(ErrorF("R%02x%c ", (int) *data, last ? '+' : '-'));
return TRUE;
}
/* This is the default I2CAddress function if not supplied by the driver.
*
* It creates the start condition, followed by the d->SlaveAddr.
* Higher level functions must call this routine rather than
* I2CStart/PutByte because a hardware I2C master may not be able
* to send a slave address without a start condition.
*
* The same timeouts apply as with I2CPutByte and additional a
* StartTimeout, similar to the ByteTimeout but for the start
* condition.
*
* In case of a timeout, the bus is left in a clean idle condition.
* I. e. you *must not* send a Stop. If this function succeeds, you *must*.
*
* The slave address format is 16 bit, with the legacy _8_bit_ slave address
* in the least significant byte. This is, the slave address must include the
* R/_W flag as least significant bit.
*
* The most significant byte of the address will be sent _after_ the LSB,
* but only if the LSB indicates:
* a) an 11 bit address, this is LSB = 1111 0xxx.
* b) a 'general call address', this is LSB = 0000 000x - see the I2C specs
* for more.
*/
static Bool
I2CAddress(I2CDevPtr d, I2CSlaveAddr addr)
{
if (I2CStart(d->pI2CBus, d->StartTimeout)) {
if (I2CPutByte(d, addr & 0xFF)) {
if ((addr & 0xF8) != 0xF0 && (addr & 0xFE) != 0x00)
return TRUE;
if (I2CPutByte(d, (addr >> 8) & 0xFF))
return TRUE;
}
I2CStop(d);
}
return FALSE;
}
/* These are the hardware independent I2C helper functions.
* ========================================================
*/
/* Function for probing. Just send the slave address
* and return true if the device responds. The slave address
* must have the lsb set to reflect a read (1) or write (0) access.
* Don't expect a read- or write-only device will respond otherwise.
*/
Bool
xf86I2CProbeAddress(I2CBusPtr b, I2CSlaveAddr addr)
{
int r;
I2CDevRec d;
d.DevName = "Probing";
d.BitTimeout = b->BitTimeout;
d.ByteTimeout = b->ByteTimeout;
d.AcknTimeout = b->AcknTimeout;
d.StartTimeout = b->StartTimeout;
d.SlaveAddr = addr;
d.pI2CBus = b;
d.NextDev = NULL;
r = b->I2CAddress(&d, addr);
if (r)
b->I2CStop(&d);
return r;
}
/* All functions below are related to devices and take the
* slave address and timeout values from an I2CDevRec. They
* return FALSE in case of an error (presumably a timeout).
*/
/* General purpose read and write function.
*
* 1st, if nWrite > 0
* Send a start condition
* Send the slave address (1 or 2 bytes) with write flag
* Write n bytes from WriteBuffer
* 2nd, if nRead > 0
* Send a start condition [again]
* Send the slave address (1 or 2 bytes) with read flag
* Read n bytes to ReadBuffer
* 3rd, if a Start condition has been successfully sent,
* Send a Stop condition.
*
* The functions exits immediately when an error occures,
* not proceeding any data left. However, step 3 will
* be executed anyway to leave the bus in clean idle state.
*/
static Bool
I2CWriteRead(I2CDevPtr d,
I2CByte * WriteBuffer, int nWrite, I2CByte * ReadBuffer, int nRead)
{
Bool r = TRUE;
I2CBusPtr b = d->pI2CBus;
int s = 0;
if (r && nWrite > 0) {
r = b->I2CAddress(d, d->SlaveAddr & ~1);
if (r) {
for (; nWrite > 0; WriteBuffer++, nWrite--)
if (!(r = b->I2CPutByte(d, *WriteBuffer)))
break;
s++;
}
}
if (r && nRead > 0) {
r = b->I2CAddress(d, d->SlaveAddr | 1);
if (r) {
for (; nRead > 0; ReadBuffer++, nRead--)
if (!(r = b->I2CGetByte(d, ReadBuffer, nRead == 1)))
break;
s++;
}
}
if (s)
b->I2CStop(d);
return r;
}
/* wrapper - for compatibility and convinience */
Bool
xf86I2CWriteRead(I2CDevPtr d,
I2CByte * WriteBuffer, int nWrite,
I2CByte * ReadBuffer, int nRead)
{
I2CBusPtr b = d->pI2CBus;
return b->I2CWriteRead(d, WriteBuffer, nWrite, ReadBuffer, nRead);
}
/* Read a byte, the only readable register of a device.
*/
Bool
xf86I2CReadStatus(I2CDevPtr d, I2CByte * pbyte)
{
return xf86I2CWriteRead(d, NULL, 0, pbyte, 1);
}
/* Read a byte from one of the registers determined by its sub-address.
*/
Bool
xf86I2CReadByte(I2CDevPtr d, I2CByte subaddr, I2CByte * pbyte)
{
return xf86I2CWriteRead(d, &subaddr, 1, pbyte, 1);
}
/* Read bytes from subsequent registers determined by the
* sub-address of the first register.
*/
Bool
xf86I2CReadBytes(I2CDevPtr d, I2CByte subaddr, I2CByte * pbyte, int n)
{
return xf86I2CWriteRead(d, &subaddr, 1, pbyte, n);
}
/* Read a word (high byte, then low byte) from one of the registers
* determined by its sub-address.
*/
Bool
xf86I2CReadWord(I2CDevPtr d, I2CByte subaddr, unsigned short *pword)
{
I2CByte rb[2];
if (!xf86I2CWriteRead(d, &subaddr, 1, rb, 2))
return FALSE;
*pword = (rb[0] << 8) | rb[1];
return TRUE;
}
/* Write a byte to one of the registers determined by its sub-address.
*/
Bool
xf86I2CWriteByte(I2CDevPtr d, I2CByte subaddr, I2CByte byte)
{
I2CByte wb[2];
wb[0] = subaddr;
wb[1] = byte;
return xf86I2CWriteRead(d, wb, 2, NULL, 0);
}
/* Write bytes to subsequent registers determined by the
* sub-address of the first register.
*/
Bool
xf86I2CWriteBytes(I2CDevPtr d, I2CByte subaddr,
I2CByte * WriteBuffer, int nWrite)
{
I2CBusPtr b = d->pI2CBus;
Bool r = TRUE;
if (nWrite > 0) {
r = b->I2CAddress(d, d->SlaveAddr & ~1);
if (r) {
if ((r = b->I2CPutByte(d, subaddr)))
for (; nWrite > 0; WriteBuffer++, nWrite--)
if (!(r = b->I2CPutByte(d, *WriteBuffer)))
break;
b->I2CStop(d);
}
}
return r;
}
/* Write a word (high byte, then low byte) to one of the registers
* determined by its sub-address.
*/
Bool
xf86I2CWriteWord(I2CDevPtr d, I2CByte subaddr, unsigned short word)
{
I2CByte wb[3];
wb[0] = subaddr;
wb[1] = word >> 8;
wb[2] = word & 0xFF;
return xf86I2CWriteRead(d, wb, 3, NULL, 0);
}
/* Write a vector of bytes to not adjacent registers. This vector is,
* 1st byte sub-address, 2nd byte value, 3rd byte sub-address asf.
* This function is intended to initialize devices. Note this function
* exits immediately when an error occurs, some registers may
* remain uninitialized.
*/
Bool
xf86I2CWriteVec(I2CDevPtr d, I2CByte * vec, int nValues)
{
I2CBusPtr b = d->pI2CBus;
Bool r = TRUE;
int s = 0;
if (nValues > 0) {
for (; nValues > 0; nValues--, vec += 2) {
if (!(r = b->I2CAddress(d, d->SlaveAddr & ~1)))
break;
s++;
if (!(r = b->I2CPutByte(d, vec[0])))
break;
if (!(r = b->I2CPutByte(d, vec[1])))
break;
}
if (s > 0)
b->I2CStop(d);
}
return r;
}
/* Administrative functions.
* =========================
*/
/* Allocates an I2CDevRec for you and initializes with propper defaults
* you may modify before calling xf86I2CDevInit. Your I2CDevRec must
* contain at least a SlaveAddr, and a pI2CBus pointer to the bus this
* device shall be linked to.
*
* See function I2CAddress for the slave address format. Always set
* the least significant bit, indicating a read or write access, to zero.
*/
I2CDevPtr
xf86CreateI2CDevRec(void)
{
return calloc(1, sizeof(I2CDevRec));
}
/* Unlink an I2C device. If you got the I2CDevRec from xf86CreateI2CDevRec
* you should set <unalloc> to free it.
*/
void
xf86DestroyI2CDevRec(I2CDevPtr d, Bool unalloc)
{
if (d) {
I2CDevPtr *p;
/* Remove this from the list of active I2C devices. */
for (p = &d->pI2CBus->FirstDev; *p != NULL; p = &(*p)->NextDev)
if (*p == d) {
*p = (*p)->NextDev;
break;
}
xf86DrvMsg(d->pI2CBus->scrnIndex, X_INFO,
"I2C device \"%s:%s\" removed.\n",
d->pI2CBus->BusName, d->DevName);
if (unalloc)
free(d);
}
}
/* I2C transmissions are related to an I2CDevRec you must link to a
* previously registered bus (see xf86I2CBusInit) before attempting
* to read and write data. You may call xf86I2CProbeAddress first to
* see if the device in question is present on this bus.
*
* xf86I2CDevInit will not allocate an I2CBusRec for you, instead you
* may enter a pointer to a statically allocated I2CDevRec or the (modified)
* result of xf86CreateI2CDevRec.
*
* If you don't specify timeouts for the device (n <= 0), it will inherit
* the bus-wide defaults. The function returns TRUE on success.
*/
Bool
xf86I2CDevInit(I2CDevPtr d)
{
I2CBusPtr b;
if (d == NULL ||
(b = d->pI2CBus) == NULL ||
(d->SlaveAddr & 1) || xf86I2CFindDev(b, d->SlaveAddr) != NULL)
return FALSE;
if (d->BitTimeout <= 0)
d->BitTimeout = b->BitTimeout;
if (d->ByteTimeout <= 0)
d->ByteTimeout = b->ByteTimeout;
if (d->AcknTimeout <= 0)
d->AcknTimeout = b->AcknTimeout;
if (d->StartTimeout <= 0)
d->StartTimeout = b->StartTimeout;
d->NextDev = b->FirstDev;
b->FirstDev = d;
xf86DrvMsg(b->scrnIndex, X_INFO,
"I2C device \"%s:%s\" registered at address 0x%02X.\n",
b->BusName, d->DevName, d->SlaveAddr);
return TRUE;
}
I2CDevPtr
xf86I2CFindDev(I2CBusPtr b, I2CSlaveAddr addr)
{
I2CDevPtr d;
if (b) {
for (d = b->FirstDev; d != NULL; d = d->NextDev)
if (d->SlaveAddr == addr)
return d;
}
return NULL;
}
static I2CBusPtr I2CBusList;
/* Allocates an I2CBusRec for you and initializes with propper defaults
* you may modify before calling xf86I2CBusInit. Your I2CBusRec must
* contain at least a BusName, a scrnIndex (or -1), and a complete set
* of either high or low level I2C function pointers. You may pass
* bus-wide timeouts, otherwise inplausible values will be replaced
* with safe defaults.
*/
I2CBusPtr
xf86CreateI2CBusRec(void)
{
I2CBusPtr b;
b = (I2CBusPtr) calloc(1, sizeof(I2CBusRec));
if (b != NULL) {
b->scrnIndex = -1;
b->pScrn = NULL;
b->HoldTime = 5; /* 100 kHz bus */
b->BitTimeout = 5;
b->ByteTimeout = 5;
b->AcknTimeout = 5;
b->StartTimeout = 5;
b->RiseFallTime = RISEFALLTIME;
}
return b;
}
/* Unregister an I2C bus. If you got the I2CBusRec from xf86CreateI2CBusRec
* you should set <unalloc> to free it. If you set <devs_too>, the function
* xf86DestroyI2CDevRec will be called for all devices linked to the bus
* first, passing down the <unalloc> option.
*/
void
xf86DestroyI2CBusRec(I2CBusPtr b, Bool unalloc, Bool devs_too)
{
if (b) {
I2CBusPtr *p;
/* Remove this from the list of active I2C buses */
for (p = &I2CBusList; *p != NULL; p = &(*p)->NextBus)
if (*p == b) {
*p = (*p)->NextBus;
break;
}
if (b->FirstDev != NULL) {
if (devs_too) {
I2CDevPtr d;
while ((d = b->FirstDev) != NULL) {
b->FirstDev = d->NextDev;
xf86DestroyI2CDevRec(d, unalloc);
}
}
else {
if (unalloc) {
xf86Msg(X_ERROR,
"i2c bug: Attempt to remove I2C bus \"%s\", "
"but device list is not empty.\n", b->BusName);
return;
}
}
}
xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" removed.\n",
b->BusName);
if (unalloc)
free(b);
}
}
/* I2C masters have to register themselves using this function.
* It will not allocate an I2CBusRec for you, instead you may enter
* a pointer to a statically allocated I2CBusRec or the (modified)
* result of xf86CreateI2CBusRec. Returns TRUE on success.
*
* At this point there won't be any traffic on the I2C bus.
*/
Bool
xf86I2CBusInit(I2CBusPtr b)
{
/* I2C buses must be identified by a unique scrnIndex
* and name. If scrnIndex is unspecified (a negative value),
* then the name must be unique throughout the server.
*/
if (b->BusName == NULL || xf86I2CFindBus(b->scrnIndex, b->BusName) != NULL)
return FALSE;
/* If the high level functions are not
* supplied, use the generic functions.
* In this case we need the low-level
* function.
*/
if (b->I2CWriteRead == NULL) {
b->I2CWriteRead = I2CWriteRead;
if (b->I2CPutBits == NULL || b->I2CGetBits == NULL) {
if (b->I2CPutByte == NULL ||
b->I2CGetByte == NULL ||
b->I2CAddress == NULL ||
b->I2CStart == NULL || b->I2CStop == NULL)
return FALSE;
}
else {
b->I2CPutByte = I2CPutByte;
b->I2CGetByte = I2CGetByte;
b->I2CAddress = I2CAddress;
b->I2CStop = I2CStop;
b->I2CStart = I2CStart;
}
}
if (b->I2CUDelay == NULL)
b->I2CUDelay = I2CUDelay;
if (b->HoldTime < 2)
b->HoldTime = 5;
if (b->BitTimeout <= 0)
b->BitTimeout = b->HoldTime;
if (b->ByteTimeout <= 0)
b->ByteTimeout = b->HoldTime;
if (b->AcknTimeout <= 0)
b->AcknTimeout = b->HoldTime;
if (b->StartTimeout <= 0)
b->StartTimeout = b->HoldTime;
/* Put new bus on list. */
b->NextBus = I2CBusList;
I2CBusList = b;
xf86DrvMsg(b->scrnIndex, X_INFO, "I2C bus \"%s\" initialized.\n",
b->BusName);
return TRUE;
}
I2CBusPtr
xf86I2CFindBus(int scrnIndex, char *name)
{
I2CBusPtr p;
if (name != NULL)
for (p = I2CBusList; p != NULL; p = p->NextBus)
if (scrnIndex < 0 || p->scrnIndex == scrnIndex)
if (!strcmp(p->BusName, name))
return p;
return NULL;
}
/*
* Return an array of I2CBusPtr's related to a screen. The caller is
* responsible for freeing the array.
*/
int
xf86I2CGetScreenBuses(int scrnIndex, I2CBusPtr ** pppI2CBus)
{
I2CBusPtr pI2CBus;
int n = 0;
if (pppI2CBus)
*pppI2CBus = NULL;
for (pI2CBus = I2CBusList; pI2CBus; pI2CBus = pI2CBus->NextBus) {
if ((pI2CBus->scrnIndex >= 0) && (pI2CBus->scrnIndex != scrnIndex))
continue;
n++;
if (!pppI2CBus)
continue;
*pppI2CBus = xnfrealloc(*pppI2CBus, n * sizeof(I2CBusPtr));
(*pppI2CBus)[n - 1] = pI2CBus;
}
return n;
}