[BACK]Return to i2c_exec.c CVS log [TXT][DIR] Up to [local] / sys / dev / i2c

Annotation of sys/dev/i2c/i2c_exec.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: i2c_exec.c,v 1.1 2004/05/23 17:33:43 grange Exp $     */
                      2: /*     $NetBSD: i2c_exec.c,v 1.3 2003/10/29 00:34:58 mycroft Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 2003 Wasabi Systems, Inc.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *      This product includes software developed for the NetBSD Project by
                     21:  *      Wasabi Systems, Inc.
                     22:  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
                     23:  *    or promote products derived from this software without specific prior
                     24:  *    written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
                     38:
                     39: #include <sys/param.h>
                     40: #include <sys/systm.h>
                     41: #include <sys/device.h>
                     42: #include <sys/event.h>
                     43: #include <sys/conf.h>
                     44:
                     45: #define        _I2C_PRIVATE
                     46: #include <dev/i2c/i2cvar.h>
                     47:
                     48: /*
                     49:  * iic_exec:
                     50:  *
                     51:  *     Simplified I2C client interface engine.
                     52:  *
                     53:  *     This and the SMBus routines are the preferred interface for
                     54:  *     client access to I2C/SMBus, since many automated controllers
                     55:  *     do not provide access to the low-level primitives of the I2C
                     56:  *     bus protocol.
                     57:  */
                     58: int
                     59: iic_exec(i2c_tag_t tag, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
                     60:     size_t cmdlen, void *vbuf, size_t buflen, int flags)
                     61: {
                     62:        const uint8_t *cmd = vcmd;
                     63:        uint8_t *buf = vbuf;
                     64:        int error;
                     65:        size_t len;
                     66:
                     67:        /*
                     68:         * Defer to the controller if it provides an exec function.  Use
                     69:         * it if it does.
                     70:         */
                     71:        if (tag->ic_exec != NULL)
                     72:                return ((*tag->ic_exec)(tag->ic_cookie, op, addr, cmd,
                     73:                                        cmdlen, buf, buflen, flags));
                     74:
                     75:        if ((len = cmdlen) != 0) {
                     76:                if ((error = iic_initiate_xfer(tag, addr, flags)) != 0)
                     77:                        goto bad;
                     78:                while (len--) {
                     79:                        if ((error = iic_write_byte(tag, *cmd++, flags)) != 0)
                     80:                                goto bad;
                     81:                }
                     82:        }
                     83:
                     84:        if (I2C_OP_READ_P(op))
                     85:                flags |= I2C_F_READ;
                     86:
                     87:        len = buflen;
                     88:        while (len--) {
                     89:                if (len == 0 && I2C_OP_STOP_P(op))
                     90:                        flags |= I2C_F_STOP;
                     91:                if (I2C_OP_READ_P(op)) {
                     92:                        /* Send REPEATED START. */
                     93:                        if ((len + 1) == buflen &&
                     94:                            (error = iic_initiate_xfer(tag, addr, flags)) != 0)
                     95:                                goto bad;
                     96:                        /* NACK on last byte. */
                     97:                        if (len == 0)
                     98:                                flags |= I2C_F_LAST;
                     99:                        if ((error = iic_read_byte(tag, buf++, flags)) != 0)
                    100:                                goto bad;
                    101:                } else  {
                    102:                        /* Maybe send START. */
                    103:                        if ((len + 1) == buflen && cmdlen == 0 &&
                    104:                            (error = iic_initiate_xfer(tag, addr, flags)) != 0)
                    105:                                goto bad;
                    106:                        if ((error = iic_write_byte(tag, *buf++, flags)) != 0)
                    107:                                goto bad;
                    108:                }
                    109:        }
                    110:
                    111:        return (0);
                    112:  bad:
                    113:        iic_send_stop(tag, flags);
                    114:        return (error);
                    115: }
                    116:
                    117: /*
                    118:  * iic_smbus_write_byte:
                    119:  *
                    120:  *     Perform an SMBus "write byte" operation.
                    121:  */
                    122: int
                    123: iic_smbus_write_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
                    124:     uint8_t val, int flags)
                    125: {
                    126:
                    127:        return (iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, &cmd, 1,
                    128:                         &val, 1, flags));
                    129: }
                    130:
                    131: /*
                    132:  * iic_smbus_read_byte:
                    133:  *
                    134:  *     Perform an SMBus "read byte" operation.
                    135:  */
                    136: int
                    137: iic_smbus_read_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t cmd,
                    138:     uint8_t *valp, int flags)
                    139: {
                    140:
                    141:        return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, &cmd, 1,
                    142:                         valp, 1, flags));
                    143: }
                    144:
                    145: /*
                    146:  * iic_smbus_receive_byte:
                    147:  *
                    148:  *     Perform an SMBus "receive byte" operation.
                    149:  */
                    150: int
                    151: iic_smbus_receive_byte(i2c_tag_t tag, i2c_addr_t addr, uint8_t *valp,
                    152:     int flags)
                    153: {
                    154:
                    155:        return (iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
                    156:                         valp, 1, flags));
                    157: }

CVSweb