diff options
author | Alexander Yurchenko <grange@cvs.openbsd.org> | 2004-05-23 17:33:44 +0000 |
---|---|---|
committer | Alexander Yurchenko <grange@cvs.openbsd.org> | 2004-05-23 17:33:44 +0000 |
commit | 8dd5343703a9bba90dc5563eb0c42e3125752dae (patch) | |
tree | a3bccee5171c434fca4e7b791735786421522a96 | |
parent | a48ee7b51bafb017ef4e15547b4bcb18f33ad846 (diff) |
I2C framework originally written by Steve C. Woodford and Jason R. Thorpe
for NetBSD (r).
This framework supports various i2c master controllers:
- dumb bit-bang controllers
- a few styles of automated controllers that give you control
over sending start/stop conditions on the i2c bus
- automated controllers that are too smart for its own good,
giving software no control over start/stop conditions
- smbus controllers by emulating smbus protocol with i2c commands
i2c slave devices need their addresses to be specified in the kernel
config file, no device discovery presented.
ok deraadt@
-rw-r--r-- | sys/conf/files | 7 | ||||
-rw-r--r-- | sys/dev/i2c/files.i2c | 10 | ||||
-rw-r--r-- | sys/dev/i2c/i2c.c | 133 | ||||
-rw-r--r-- | sys/dev/i2c/i2c_bitbang.c | 179 | ||||
-rw-r--r-- | sys/dev/i2c/i2c_bitbang.h | 63 | ||||
-rw-r--r-- | sys/dev/i2c/i2c_exec.c | 157 | ||||
-rw-r--r-- | sys/dev/i2c/i2c_io.h | 101 | ||||
-rw-r--r-- | sys/dev/i2c/i2cvar.h | 144 |
8 files changed, 793 insertions, 1 deletions
diff --git a/sys/conf/files b/sys/conf/files index e4f4645cf41..7ddc5cf233a 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,4 +1,4 @@ -# $OpenBSD: files,v 1.297 2004/05/20 03:04:37 marco Exp $ +# $OpenBSD: files,v 1.298 2004/05/23 17:33:43 grange Exp $ # $NetBSD: files,v 1.87 1996/05/19 17:17:50 jonathan Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 @@ -16,6 +16,7 @@ define mii {[phy = -1]} define midibus { } define midisyn define radiobus {} +define i2cbus {} # audio device attributes define mulaw @@ -74,6 +75,10 @@ define wskbddev {[console = -1], [mux = -1]} define wsmousedev {[mux = -1]} define wsrasteremulops +# i2c device attributes +define i2cexec +define i2c_bitbang + # SMC 93Cx6 Serial EEPROM devices define smc93cx6 file dev/ic/smc93cx6.c smc93cx6 diff --git a/sys/dev/i2c/files.i2c b/sys/dev/i2c/files.i2c new file mode 100644 index 00000000000..c53863d6ec2 --- /dev/null +++ b/sys/dev/i2c/files.i2c @@ -0,0 +1,10 @@ +# $OpenBSD: files.i2c,v 1.1 2004/05/23 17:33:43 grange Exp $ +# $NetBSD: files.i2c,v 1.3 2003/10/20 16:24:10 briggs Exp $ + +device iic { addr, [size = -1] } +attach iic at i2cbus +file dev/i2c/i2c.c iic | i2cbus +file dev/i2c/i2c_exec.c iic | i2cbus | i2cexec + +# Common module for bit-bang'ing an I2C bus +file dev/i2c/i2c_bitbang.c i2c_bitbang diff --git a/sys/dev/i2c/i2c.c b/sys/dev/i2c/i2c.c new file mode 100644 index 00000000000..8cfd6c8cbba --- /dev/null +++ b/sys/dev/i2c/i2c.c @@ -0,0 +1,133 @@ +/* $OpenBSD: i2c.c,v 1.1 2004/05/23 17:33:43 grange Exp $ */ +/* $NetBSD: i2c.c,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/device.h> +#include <sys/event.h> +#include <sys/conf.h> + +#include <dev/i2c/i2cvar.h> + +#define IICCF_ADDR 0 +#define IICCF_SIZE 1 + +struct iic_softc { + struct device sc_dev; + i2c_tag_t sc_tag; +}; + +int iic_match(struct device *, void *, void *); +void iic_attach(struct device *, struct device *, void *); +int iic_search(struct device *, void *, void *); +int iic_print(void *, const char *); + +struct cfattach iic_ca = { + sizeof (struct iic_softc), + iic_match, + iic_attach +}; + +struct cfdriver iic_cd = { + NULL, "iic", DV_DULL +}; + +int +iicbus_print(void *aux, const char *pnp) +{ + struct i2cbus_attach_args *iba = aux; + + if (pnp != NULL) + printf("%s at %s", iba->iba_name, pnp); + + return (UNCONF); +} + +int +iic_print(void *aux, const char *pnp) +{ + struct i2c_attach_args *ia = aux; + + printf(" addr 0x%x", ia->ia_addr); + + return (UNCONF); +} + +int +iic_search(struct device *parent, void *arg, void *aux) +{ + struct iic_softc *sc = (void *) parent; + struct cfdata *cf = arg; + struct i2c_attach_args ia; + + ia.ia_tag = sc->sc_tag; + ia.ia_addr = cf->cf_loc[IICCF_ADDR]; + ia.ia_size = cf->cf_loc[IICCF_SIZE]; + + if (cf->cf_attach->ca_match(parent, cf, &ia) > 0) + config_attach(parent, cf, &ia, iic_print); + + return (0); +} + +int +iic_match(struct device *parent, void *arg, void *aux) +{ + struct cfdata *cf = arg; + struct i2cbus_attach_args *iba = aux; + + /* Just make sure we're looking for i2c. */ + return (strcmp(iba->iba_name, cf->cf_driver->cd_name) == 0); +} + +void +iic_attach(struct device *parent, struct device *self, void *aux) +{ + struct iic_softc *sc = (void *) self; + struct i2cbus_attach_args *iba = aux; + + sc->sc_tag = iba->iba_tag; + + printf("\n"); + + /* + * Attach all i2c devices described in the kernel + * configuration file. + */ + config_search(iic_search, self, NULL); +} diff --git a/sys/dev/i2c/i2c_bitbang.c b/sys/dev/i2c/i2c_bitbang.c new file mode 100644 index 00000000000..ba03967c0e8 --- /dev/null +++ b/sys/dev/i2c/i2c_bitbang.c @@ -0,0 +1,179 @@ +/* $OpenBSD: i2c_bitbang.c,v 1.1 2004/05/23 17:33:43 grange Exp $ */ +/* $NetBSD: i2c_bitbang.c,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Common module for bit-bang'ing an I2C bus. + */ + +#include <sys/param.h> + +#include <dev/i2c/i2cvar.h> +#include <dev/i2c/i2c_bitbang.h> + +#define BB_SET(x) ops->ibo_set_bits(v, (x)) +#define BB_DIR(x) ops->ibo_set_dir(v, (x)) +#define BB_READ ops->ibo_read_bits(v) + +#define SDA ops->ibo_bits[I2C_BIT_SDA] /* i2c signal */ +#define SCL ops->ibo_bits[I2C_BIT_SCL] /* i2c signal */ +#define OUTPUT ops->ibo_bits[I2C_BIT_OUTPUT] /* SDA is output */ +#define INPUT ops->ibo_bits[I2C_BIT_INPUT] /* SDA is input */ + +/*ARGSUSED*/ +int +i2c_bitbang_send_start(void *v, int flags, i2c_bitbang_ops_t ops) +{ + + BB_DIR(OUTPUT); + + BB_SET(SDA | SCL); + delay(5); /* bus free time (4.7 uS) */ + BB_SET( SCL); + delay(4); /* start hold time (4.0 uS) */ + BB_SET( 0); + delay(5); /* clock low time (4.7 uS) */ + + return (0); +} + +/*ARGSUSED*/ +int +i2c_bitbang_send_stop(void *v, int flags, i2c_bitbang_ops_t ops) +{ + + BB_DIR(OUTPUT); + + BB_SET( SCL); + delay(4); /* stop setup time (4.0 uS) */ + BB_SET(SDA | SCL); + + return (0); +} + +int +i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags, + i2c_bitbang_ops_t ops) +{ + int i2caddr; + + /* XXX Only support 7-bit addressing for now. */ + if ((addr & 0x78) == 0x78) + return (EINVAL); + + i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0); + + (void) i2c_bitbang_send_start(v, flags, ops); + return (i2c_bitbang_write_byte(v, i2caddr, flags & ~I2C_F_STOP, ops)); +} + +int +i2c_bitbang_read_byte(void *v, uint8_t *valp, int flags, + i2c_bitbang_ops_t ops) +{ + int i; + uint8_t val = 0; + uint32_t bit; + + BB_DIR(INPUT); + BB_SET(SDA ); + + for (i = 0; i < 8; i++) { + val <<= 1; + BB_SET(SDA | SCL); + delay(4); /* clock high time (4.0 uS) */ + if (BB_READ & SDA) + val |= 1; + BB_SET(SDA ); + delay(5); /* clock low time (4.7 uS) */ + } + + bit = (flags & I2C_F_LAST) ? SDA : 0; + BB_DIR(OUTPUT); + BB_SET(bit ); + delay(1); /* data setup time (250 nS) */ + BB_SET(bit | SCL); + delay(4); /* clock high time (4.0 uS) */ + BB_SET(bit ); + delay(5); /* clock low time (4.7 uS) */ + + BB_DIR(INPUT); + BB_SET(SDA ); + delay(5); + + if ((flags & (I2C_F_STOP | I2C_F_LAST)) == (I2C_F_STOP | I2C_F_LAST)) + (void) i2c_bitbang_send_stop(v, flags, ops); + + *valp = val; + return (0); +} + +int +i2c_bitbang_write_byte(void *v, uint8_t val, int flags, + i2c_bitbang_ops_t ops) +{ + uint32_t bit; + uint8_t mask; + int error; + + BB_DIR(OUTPUT); + + for (mask = 0x80; mask != 0; mask >>= 1) { + bit = (val & mask) ? SDA : 0; + BB_SET(bit ); + delay(1); /* data setup time (250 nS) */ + BB_SET(bit | SCL); + delay(4); /* clock high time (4.0 uS) */ + BB_SET(bit ); + delay(5); /* clock low time (4.7 uS) */ + } + + BB_DIR(INPUT); + + BB_SET(SDA ); + delay(5); + BB_SET(SDA | SCL); + delay(4); + error = (BB_READ & SDA) ? EIO : 0; + BB_SET(SDA ); + delay(5); + + if (flags & I2C_F_STOP) + (void) i2c_bitbang_send_stop(v, flags, ops); + + return (error); +} diff --git a/sys/dev/i2c/i2c_bitbang.h b/sys/dev/i2c/i2c_bitbang.h new file mode 100644 index 00000000000..a8311bb9daa --- /dev/null +++ b/sys/dev/i2c/i2c_bitbang.h @@ -0,0 +1,63 @@ +/* $OpenBSD: i2c_bitbang.h,v 1.1 2004/05/23 17:33:43 grange Exp $ */ +/* $NetBSD: i2c_bitbang.h,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DEV_I2C_I2C_BITBANG_H_ +#define _DEV_I2C_I2C_BITBANG_H_ + +#define I2C_BIT_SDA 0 /* SDA signal */ +#define I2C_BIT_SCL 1 /* SCL signal */ +#define I2C_BIT_OUTPUT 2 /* controller: SDA is output */ +#define I2C_BIT_INPUT 3 /* controller: SDA is input */ +#define I2C_NBITS 4 + +struct i2c_bitbang_ops { + void (*ibo_set_bits)(void *, uint32_t); + void (*ibo_set_dir)(void *, uint32_t); + uint32_t (*ibo_read_bits)(void *); + uint32_t ibo_bits[I2C_NBITS]; +}; + +typedef const struct i2c_bitbang_ops *i2c_bitbang_ops_t; + +int i2c_bitbang_send_start(void *, int, i2c_bitbang_ops_t); +int i2c_bitbang_send_stop(void *, int, i2c_bitbang_ops_t); +int i2c_bitbang_initiate_xfer(void *, i2c_addr_t, int, i2c_bitbang_ops_t); +int i2c_bitbang_read_byte(void *, uint8_t *, int, i2c_bitbang_ops_t); +int i2c_bitbang_write_byte(void *, uint8_t, int, i2c_bitbang_ops_t); + +#endif /* _DEV_I2C_I2C_BITBANG_H_ */ diff --git a/sys/dev/i2c/i2c_exec.c b/sys/dev/i2c/i2c_exec.c new file mode 100644 index 00000000000..f76d17cf949 --- /dev/null +++ b/sys/dev/i2c/i2c_exec.c @@ -0,0 +1,157 @@ +/* $OpenBSD: i2c_exec.c,v 1.1 2004/05/23 17:33:43 grange Exp $ */ +/* $NetBSD: i2c_exec.c,v 1.3 2003/10/29 00:34:58 mycroft Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/device.h> +#include <sys/event.h> +#include <sys/conf.h> + +#define _I2C_PRIVATE +#include <dev/i2c/i2cvar.h> + +/* + * iic_exec: + * + * Simplified I2C client interface engine. + * + * This and the SMBus routines are the preferred interface for + * client access to I2C/SMBus, since many automated controllers + * do not provide access to the low-level primitives of the I2C + * bus protocol. + */ +int +iic_exec(i2c_tag_t tag, i2c_op_t op, i2c_addr_t addr, const void *vcmd, + size_t cmdlen, void *vbuf, size_t buflen, int flags) +{ + const uint8_t *cmd = vcmd; + uint8_t *buf = vbuf; + int error; + size_t len; + + /* + * Defer to the controller if it provides an exec function. Use + * it if it does. + */ + if (tag->ic_exec != NULL) + return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd, + cmdlen, buf, buflen, flags)); + + if ((len = cmdlen) != 0) { + if ((error = iic_initiate_xfer(tag, addr, flags)) != 0) + goto bad; + while (len--) { + if ((error = iic_write_byte(tag, *cmd++, flags)) != 0) + goto bad; + } + } + + if (I2C_OP_READ_P(op)) + flags |= I2C_F_READ; + + len = buflen; + while (len--) { + if (len == 0 && I2C_OP_STOP_P(op)) + flags |= I2C_F_STOP; + if (I2C_OP_READ_P(op)) { + /* Send REPEATED START. */ + if ((len + 1) == buflen && + (error = iic_initiate_xfer(tag, addr, flags)) != 0) + goto bad; + /* NACK on last byte. */ + if (len == 0) + flags |= I2C_F_LAST; + if ((error = iic_read_byte(tag, buf++, flags)) != 0) + goto bad; + } else { + /* Maybe send START. */ + if ((len + 1) == buflen && cmdlen == 0 && + (error = iic_initiate_xfer(tag, addr, flags)) != 0) + goto bad; + if ((error = iic_write_byte(tag, *buf++, flags)) != 0) + goto bad; + } + } + + return (0); + bad: + iic_send_stop(tag, flags); + return (error); +} + +/* + * iic_smbus_write_byte: + * + * Perform an SMBus "write byte" operation. + */ +int +iic_smbus_write_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd, + uint8_t val, int flags) +{ + + return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1, + &val, 1, flags)); +} + +/* + * iic_smbus_read_byte: + * + * Perform an SMBus "read byte" operation. + */ +int +iic_smbus_read_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd, + uint8_t *valp, int flags) +{ + + return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1, + valp, 1, flags)); +} + +/* + * iic_smbus_receive_byte: + * + * Perform an SMBus "receive byte" operation. + */ +int +iic_smbus_receive_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t *valp, + int flags) +{ + + return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0, + valp, 1, flags)); +} diff --git a/sys/dev/i2c/i2c_io.h b/sys/dev/i2c/i2c_io.h new file mode 100644 index 00000000000..8a4aea81e12 --- /dev/null +++ b/sys/dev/i2c/i2c_io.h @@ -0,0 +1,101 @@ +/* $OpenBSD: i2c_io.h,v 1.1 2004/05/23 17:33:43 grange Exp $ */ +/* $NetBSD: i2c_io.h,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Jason R. Thorpe for Wasabi Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DEV_I2C_I2C_IO_H_ +#define _DEV_I2C_I2C_IO_H_ + +#include <sys/ioccom.h> + +/* I2C bus address. */ +typedef uint16_t i2c_addr_t; + +/* High-level I2C operations. */ +typedef enum { + I2C_OP_READ = 0, + I2C_OP_READ_WITH_STOP = 1, + I2C_OP_WRITE = 2, + I2C_OP_WRITE_WITH_STOP = 3, +} i2c_op_t; + +#define I2C_OP_READ_P(x) (((int)(x) & 2) == 0) +#define I2C_OP_WRITE_P(x) (! I2C_OP_READ_P(x)) +#define I2C_OP_STOP_P(x) (((int)(x) & 1) != 0) + +/* + * This structure describes a single I2C control script fragment. + * + * Note that use of this scripted API allows for support of automated + * SMBus controllers. The following table maps SMBus operations to + * script fragment configuration: + * + * SMBus "write byte": I2C_OP_WRITE_WITH_STOP + * cmdlen = 1 + * + * SMBus "read byte": I2C_OP_READ_WITH_STOP + * cmdlen = 1 + * + * SMBus "receive byte": I2C_OP_READ_WITH_STOP + * cmdlen = 0 + * + * Note that while each of these 3 SMBus operations implies a STOP + * (which an automated controller will likely perform automatically), + * non-SMBus clients may continue to function even if they issue + * I2C_OP_WRITE and I2C_OP_READ. + */ + +#ifdef notyet +/* + * I2C_IOCTL_EXEC: + * + * User ioctl to execute an i2c operation. + */ +typedef struct i2c_ioctl_exec { + i2c_op_t iie_op; /* operation to perform */ + i2c_addr_t iie_addr; /* address of device */ + const void *iie_cmd; /* pointer to command */ + size_t iie_cmdlen; /* length of command */ + void *iie_buf; /* pointer to data buffer */ + size_t iie_buflen; /* length of data buffer */ +} i2c_ioctl_exec_t; +#define I2C_EXEC_MAX_CMDLEN 32 +#define I2C_EXEC_MAX_BUFLEN 32 + +#define I2C_IOCTL_EXEC _IOW('I', 0, i2c_ioctl_exec_t) +#endif + +#endif /* _DEV_I2C_I2C_IO_H_ */ diff --git a/sys/dev/i2c/i2cvar.h b/sys/dev/i2c/i2cvar.h new file mode 100644 index 00000000000..7065df99932 --- /dev/null +++ b/sys/dev/i2c/i2cvar.h @@ -0,0 +1,144 @@ +/* $OpenBSD: i2cvar.h,v 1.1 2004/05/23 17:33:43 grange Exp $ */ +/* $NetBSD: i2cvar.h,v 1.1 2003/09/30 00:35:31 thorpej Exp $ */ + +/* + * Copyright (c) 2003 Wasabi Systems, Inc. + * All rights reserved. + * + * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed for the NetBSD Project by + * Wasabi Systems, Inc. + * 4. The name of Wasabi Systems, Inc. may not be used to endorse + * or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DEV_I2C_I2CVAR_H_ +#define _DEV_I2C_I2CVAR_H_ + +#include <dev/i2c/i2c_io.h> + +/* Flags passed to i2c routines. */ +#define I2C_F_WRITE 0x00 /* new transfer is a write */ +#define I2C_F_READ 0x01 /* new transfer is a read */ +#define I2C_F_LAST 0x02 /* last byte of read */ +#define I2C_F_STOP 0x04 /* send stop after byte */ +#define I2C_F_POLL 0x08 /* poll, don't sleep */ + +/* + * This structure provides the interface between the i2c framework + * and the underlying i2c controller. + * + * Note that this structure is designed specifically to allow us + * to either use the autoconfiguration framework or not. This + * allows a driver for a board with a private i2c bus use generic + * i2c client drivers for chips that might be on that board. + */ +typedef struct i2c_controller { + void *ic_cookie; /* controller private */ + + /* + * These provide synchronization in the presence of + * multiple users of the i2c bus. When a device + * driver wishes to perform transfers on the i2c + * bus, the driver should acquire the bus. When + * the driver is finished, it should release the + * bus. + * + * This is provided by the back-end since a single + * controller may present e.g. i2c and smbus views + * of the same set of i2c wires. + */ + int (*ic_acquire_bus)(void *, int); + void (*ic_release_bus)(void *, int); + + /* + * The preferred API for clients of the i2c interface + * is the scripted API. This handles i2c controllers + * that do not provide raw access to the i2c signals. + */ + int (*ic_exec)(void *, i2c_op_t, i2c_addr_t, const void *, size_t, + void *, size_t, int); + + int (*ic_send_start)(void *, int); + int (*ic_send_stop)(void *, int); + int (*ic_initiate_xfer)(void *, i2c_addr_t, int); + int (*ic_read_byte)(void *, uint8_t *, int); + int (*ic_write_byte)(void *, uint8_t, int); +} *i2c_tag_t; + +/* Used to attach the i2c framework to the controller. */ +struct i2cbus_attach_args { + const char *iba_name; /* bus name ("iic") */ + i2c_tag_t iba_tag; /* the controller */ +}; + +/* Used to attach devices on the i2c bus. */ +struct i2c_attach_args { + i2c_tag_t ia_tag; /* our controller */ + i2c_addr_t ia_addr; /* address of device */ + int ia_size; /* size (for EEPROMs) */ +}; + +/* + * API presented to i2c controllers. + */ +int iicbus_print(void *, const char *); + +#ifdef _I2C_PRIVATE +/* + * Macros used internally by the i2c framework. + */ +#define iic_send_start(ic, flags) \ + (*(ic)->ic_send_start)((ic)->ic_cookie, (flags)) +#define iic_send_stop(ic, flags) \ + (*(ic)->ic_send_stop)((ic)->ic_cookie, (flags)) +#define iic_initiate_xfer(ic, addr, flags) \ + (*(ic)->ic_initiate_xfer)((ic)->ic_cookie, (addr), (flags)) + +#define iic_read_byte(ic, bytep, flags) \ + (*(ic)->ic_read_byte)((ic)->ic_cookie, (bytep), (flags)) +#define iic_write_byte(ic, byte, flags) \ + (*(ic)->ic_write_byte)((ic)->ic_cookie, (byte), (flags)) +#endif /* _I2C_PRIVATE */ + +/* + * Simplified API for clients of the i2c framework. Definitions + * in <dev/i2c/i2c_io.h>. + */ +#define iic_acquire_bus(ic, flags) \ + (*(ic)->ic_acquire_bus)((ic)->ic_cookie, (flags)) +#define iic_release_bus(ic, flags) \ + (*(ic)->ic_release_bus)((ic)->ic_cookie, (flags)) + +int iic_exec(i2c_tag_t, i2c_op_t, i2c_addr_t, const void *, + size_t, void *, size_t, int); + +int iic_smbus_write_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t, int); +int iic_smbus_read_byte(i2c_tag_t, i2c_addr_t, uint8_t, uint8_t *, int); +int iic_smbus_receive_byte(i2c_tag_t, i2c_addr_t, uint8_t *, int); + +#endif /* _DEV_I2C_I2CVAR_H_ */ |