1c3e2dc6bSNicolas Souchu /*- 2c3e2dc6bSNicolas Souchu * Copyright (c) 1998 Nicolas Souchu 3c3e2dc6bSNicolas Souchu * All rights reserved. 4c3e2dc6bSNicolas Souchu * 5c3e2dc6bSNicolas Souchu * Redistribution and use in source and binary forms, with or without 6c3e2dc6bSNicolas Souchu * modification, are permitted provided that the following conditions 7c3e2dc6bSNicolas Souchu * are met: 8c3e2dc6bSNicolas Souchu * 1. Redistributions of source code must retain the above copyright 9c3e2dc6bSNicolas Souchu * notice, this list of conditions and the following disclaimer. 10c3e2dc6bSNicolas Souchu * 2. Redistributions in binary form must reproduce the above copyright 11c3e2dc6bSNicolas Souchu * notice, this list of conditions and the following disclaimer in the 12c3e2dc6bSNicolas Souchu * documentation and/or other materials provided with the distribution. 13c3e2dc6bSNicolas Souchu * 14c3e2dc6bSNicolas Souchu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15c3e2dc6bSNicolas Souchu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c3e2dc6bSNicolas Souchu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c3e2dc6bSNicolas Souchu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18c3e2dc6bSNicolas Souchu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c3e2dc6bSNicolas Souchu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c3e2dc6bSNicolas Souchu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c3e2dc6bSNicolas Souchu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c3e2dc6bSNicolas Souchu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c3e2dc6bSNicolas Souchu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c3e2dc6bSNicolas Souchu * SUCH DAMAGE. 25c3e2dc6bSNicolas Souchu * 26c3aac50fSPeter Wemm * $FreeBSD$ 27c3e2dc6bSNicolas Souchu * 28c3e2dc6bSNicolas Souchu */ 29c3e2dc6bSNicolas Souchu 30c3e2dc6bSNicolas Souchu /* 31c3e2dc6bSNicolas Souchu * I2C to SMB bridge 3204f89a63SNicolas Souchu * 3304f89a63SNicolas Souchu * Example: 3404f89a63SNicolas Souchu * 3504f89a63SNicolas Souchu * smb bttv 3604f89a63SNicolas Souchu * \ / 3704f89a63SNicolas Souchu * smbus 3804f89a63SNicolas Souchu * / \ 3904f89a63SNicolas Souchu * iicsmb bti2c 4004f89a63SNicolas Souchu * | 4104f89a63SNicolas Souchu * iicbus 4204f89a63SNicolas Souchu * / | \ 4304f89a63SNicolas Souchu * iicbb pcf ... 4404f89a63SNicolas Souchu * | 4504f89a63SNicolas Souchu * lpbb 46c3e2dc6bSNicolas Souchu */ 47c3e2dc6bSNicolas Souchu 48c3e2dc6bSNicolas Souchu #include <sys/param.h> 49c3e2dc6bSNicolas Souchu #include <sys/kernel.h> 50c3e2dc6bSNicolas Souchu #include <sys/systm.h> 51c3e2dc6bSNicolas Souchu #include <sys/module.h> 52c3e2dc6bSNicolas Souchu #include <sys/bus.h> 53c3e2dc6bSNicolas Souchu #include <sys/uio.h> 54c3e2dc6bSNicolas Souchu 55c3e2dc6bSNicolas Souchu 56c3e2dc6bSNicolas Souchu #include <dev/iicbus/iiconf.h> 57c3e2dc6bSNicolas Souchu #include <dev/iicbus/iicbus.h> 58c3e2dc6bSNicolas Souchu 59c3e2dc6bSNicolas Souchu #include <dev/smbus/smbconf.h> 60c3e2dc6bSNicolas Souchu 61c3e2dc6bSNicolas Souchu #include "iicbus_if.h" 62c3e2dc6bSNicolas Souchu #include "smbus_if.h" 63c3e2dc6bSNicolas Souchu 64c3e2dc6bSNicolas Souchu struct iicsmb_softc { 65c3e2dc6bSNicolas Souchu 66c3e2dc6bSNicolas Souchu #define SMB_WAITING_ADDR 0x0 67c3e2dc6bSNicolas Souchu #define SMB_WAITING_LOW 0x1 68c3e2dc6bSNicolas Souchu #define SMB_WAITING_HIGH 0x2 69c3e2dc6bSNicolas Souchu #define SMB_DONE 0x3 70c3e2dc6bSNicolas Souchu int state; 71c3e2dc6bSNicolas Souchu 72c3e2dc6bSNicolas Souchu u_char devaddr; /* slave device address */ 73c3e2dc6bSNicolas Souchu 74c3e2dc6bSNicolas Souchu char low; /* low byte received first */ 75c3e2dc6bSNicolas Souchu char high; /* high byte */ 76c3e2dc6bSNicolas Souchu 77c3e2dc6bSNicolas Souchu device_t smbus; 78c3e2dc6bSNicolas Souchu }; 79c3e2dc6bSNicolas Souchu 80c3e2dc6bSNicolas Souchu static int iicsmb_probe(device_t); 81c3e2dc6bSNicolas Souchu static int iicsmb_attach(device_t); 82c3e2dc6bSNicolas Souchu 83c3e2dc6bSNicolas Souchu static void iicsmb_intr(device_t dev, int event, char *buf); 8404f89a63SNicolas Souchu static int iicsmb_callback(device_t dev, int index, caddr_t data); 85c3e2dc6bSNicolas Souchu static int iicsmb_quick(device_t dev, u_char slave, int how); 86c3e2dc6bSNicolas Souchu static int iicsmb_sendb(device_t dev, u_char slave, char byte); 87c3e2dc6bSNicolas Souchu static int iicsmb_recvb(device_t dev, u_char slave, char *byte); 88c3e2dc6bSNicolas Souchu static int iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte); 89c3e2dc6bSNicolas Souchu static int iicsmb_writew(device_t dev, u_char slave, char cmd, short word); 90c3e2dc6bSNicolas Souchu static int iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte); 91c3e2dc6bSNicolas Souchu static int iicsmb_readw(device_t dev, u_char slave, char cmd, short *word); 92c3e2dc6bSNicolas Souchu static int iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata); 93c3e2dc6bSNicolas Souchu static int iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf); 94c3e2dc6bSNicolas Souchu static int iicsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf); 95c3e2dc6bSNicolas Souchu 96c3e2dc6bSNicolas Souchu static devclass_t iicsmb_devclass; 97c3e2dc6bSNicolas Souchu 98c3e2dc6bSNicolas Souchu static device_method_t iicsmb_methods[] = { 99c3e2dc6bSNicolas Souchu /* device interface */ 100c3e2dc6bSNicolas Souchu DEVMETHOD(device_probe, iicsmb_probe), 101c3e2dc6bSNicolas Souchu DEVMETHOD(device_attach, iicsmb_attach), 102f1c73142SJake Burkholder DEVMETHOD(device_detach, bus_generic_detach), 103c3e2dc6bSNicolas Souchu 104c3e2dc6bSNicolas Souchu /* bus interface */ 10515317dd8SMatthew N. Dodd DEVMETHOD(bus_print_child, bus_generic_print_child), 106c3e2dc6bSNicolas Souchu 107c3e2dc6bSNicolas Souchu /* iicbus interface */ 108c3e2dc6bSNicolas Souchu DEVMETHOD(iicbus_intr, iicsmb_intr), 109c3e2dc6bSNicolas Souchu 110c3e2dc6bSNicolas Souchu /* smbus interface */ 11104f89a63SNicolas Souchu DEVMETHOD(smbus_callback, iicsmb_callback), 112c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_quick, iicsmb_quick), 113c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_sendb, iicsmb_sendb), 114c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_recvb, iicsmb_recvb), 115c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_writeb, iicsmb_writeb), 116c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_writew, iicsmb_writew), 117c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_readb, iicsmb_readb), 118c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_readw, iicsmb_readw), 119c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_pcall, iicsmb_pcall), 120c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_bwrite, iicsmb_bwrite), 121c3e2dc6bSNicolas Souchu DEVMETHOD(smbus_bread, iicsmb_bread), 122c3e2dc6bSNicolas Souchu 123c3e2dc6bSNicolas Souchu { 0, 0 } 124c3e2dc6bSNicolas Souchu }; 125c3e2dc6bSNicolas Souchu 126c3e2dc6bSNicolas Souchu static driver_t iicsmb_driver = { 127c3e2dc6bSNicolas Souchu "iicsmb", 128c3e2dc6bSNicolas Souchu iicsmb_methods, 129c3e2dc6bSNicolas Souchu sizeof(struct iicsmb_softc), 130c3e2dc6bSNicolas Souchu }; 131c3e2dc6bSNicolas Souchu 132c3e2dc6bSNicolas Souchu static int 133c3e2dc6bSNicolas Souchu iicsmb_probe(device_t dev) 134c3e2dc6bSNicolas Souchu { 135c3e2dc6bSNicolas Souchu struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev); 136c3e2dc6bSNicolas Souchu 137c3e2dc6bSNicolas Souchu sc->smbus = smbus_alloc_bus(dev); 138c3e2dc6bSNicolas Souchu 139c3e2dc6bSNicolas Souchu if (!sc->smbus) 140c3e2dc6bSNicolas Souchu return (EINVAL); /* XXX don't know what to return else */ 141c3e2dc6bSNicolas Souchu 142c3e2dc6bSNicolas Souchu return (0); 143c3e2dc6bSNicolas Souchu } 144c3e2dc6bSNicolas Souchu 145c3e2dc6bSNicolas Souchu static int 146c3e2dc6bSNicolas Souchu iicsmb_attach(device_t dev) 147c3e2dc6bSNicolas Souchu { 148c3e2dc6bSNicolas Souchu struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev); 149c3e2dc6bSNicolas Souchu 150c3e2dc6bSNicolas Souchu /* probe and attach the smbus */ 151c3e2dc6bSNicolas Souchu device_probe_and_attach(sc->smbus); 152c3e2dc6bSNicolas Souchu 153c3e2dc6bSNicolas Souchu return (0); 154c3e2dc6bSNicolas Souchu } 155c3e2dc6bSNicolas Souchu 156c3e2dc6bSNicolas Souchu /* 157c3e2dc6bSNicolas Souchu * iicsmb_intr() 158c3e2dc6bSNicolas Souchu * 159c3e2dc6bSNicolas Souchu * iicbus interrupt handler 160c3e2dc6bSNicolas Souchu */ 161c3e2dc6bSNicolas Souchu static void 162c3e2dc6bSNicolas Souchu iicsmb_intr(device_t dev, int event, char *buf) 163c3e2dc6bSNicolas Souchu { 164c3e2dc6bSNicolas Souchu struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev); 165c3e2dc6bSNicolas Souchu 166c3e2dc6bSNicolas Souchu switch (event) { 167c3e2dc6bSNicolas Souchu case INTR_GENERAL: 168c3e2dc6bSNicolas Souchu case INTR_START: 169c3e2dc6bSNicolas Souchu sc->state = SMB_WAITING_ADDR; 170c3e2dc6bSNicolas Souchu break; 171c3e2dc6bSNicolas Souchu 172c3e2dc6bSNicolas Souchu case INTR_STOP: 173c3e2dc6bSNicolas Souchu /* call smbus intr handler */ 174c3e2dc6bSNicolas Souchu smbus_intr(sc->smbus, sc->devaddr, 175c3e2dc6bSNicolas Souchu sc->low, sc->high, SMB_ENOERR); 176c3e2dc6bSNicolas Souchu break; 177c3e2dc6bSNicolas Souchu 178c3e2dc6bSNicolas Souchu case INTR_RECEIVE: 179c3e2dc6bSNicolas Souchu switch (sc->state) { 180c3e2dc6bSNicolas Souchu case SMB_DONE: 181c3e2dc6bSNicolas Souchu /* XXX too much data, discard */ 1826e551fb6SDavid E. O'Brien printf("%s: too much data from 0x%x\n", __func__, 183c3e2dc6bSNicolas Souchu sc->devaddr & 0xff); 184c3e2dc6bSNicolas Souchu goto end; 185c3e2dc6bSNicolas Souchu 186c3e2dc6bSNicolas Souchu case SMB_WAITING_ADDR: 187c3e2dc6bSNicolas Souchu sc->devaddr = (u_char)*buf; 188c3e2dc6bSNicolas Souchu sc->state = SMB_WAITING_LOW; 189c3e2dc6bSNicolas Souchu break; 190c3e2dc6bSNicolas Souchu 191c3e2dc6bSNicolas Souchu case SMB_WAITING_LOW: 192c3e2dc6bSNicolas Souchu sc->low = *buf; 193c3e2dc6bSNicolas Souchu sc->state = SMB_WAITING_HIGH; 194c3e2dc6bSNicolas Souchu break; 195c3e2dc6bSNicolas Souchu 196c3e2dc6bSNicolas Souchu case SMB_WAITING_HIGH: 197c3e2dc6bSNicolas Souchu sc->high = *buf; 198c3e2dc6bSNicolas Souchu sc->state = SMB_DONE; 199c3e2dc6bSNicolas Souchu break; 200c3e2dc6bSNicolas Souchu } 201c3e2dc6bSNicolas Souchu end: 202c3e2dc6bSNicolas Souchu break; 203c3e2dc6bSNicolas Souchu 204c3e2dc6bSNicolas Souchu case INTR_TRANSMIT: 205c3e2dc6bSNicolas Souchu case INTR_NOACK: 206c3e2dc6bSNicolas Souchu break; 207c3e2dc6bSNicolas Souchu 208c3e2dc6bSNicolas Souchu case INTR_ERROR: 209c3e2dc6bSNicolas Souchu switch (*buf) { 210c3e2dc6bSNicolas Souchu case IIC_EBUSERR: 211c3e2dc6bSNicolas Souchu smbus_intr(sc->smbus, sc->devaddr, 0, 0, SMB_EBUSERR); 212c3e2dc6bSNicolas Souchu break; 213c3e2dc6bSNicolas Souchu 214c3e2dc6bSNicolas Souchu default: 2156e551fb6SDavid E. O'Brien printf("%s unknown error 0x%x!\n", __func__, 216c3e2dc6bSNicolas Souchu (int)*buf); 217c3e2dc6bSNicolas Souchu break; 218c3e2dc6bSNicolas Souchu } 219c3e2dc6bSNicolas Souchu break; 220c3e2dc6bSNicolas Souchu 221c3e2dc6bSNicolas Souchu default: 2226e551fb6SDavid E. O'Brien panic("%s: unknown event (%d)!", __func__, event); 223c3e2dc6bSNicolas Souchu } 224c3e2dc6bSNicolas Souchu 225c3e2dc6bSNicolas Souchu return; 226c3e2dc6bSNicolas Souchu } 227c3e2dc6bSNicolas Souchu 228c3e2dc6bSNicolas Souchu static int 22904f89a63SNicolas Souchu iicsmb_callback(device_t dev, int index, caddr_t data) 23004f89a63SNicolas Souchu { 23104f89a63SNicolas Souchu device_t parent = device_get_parent(dev); 23204f89a63SNicolas Souchu int error = 0; 23304f89a63SNicolas Souchu int how; 23404f89a63SNicolas Souchu 23504f89a63SNicolas Souchu switch (index) { 23604f89a63SNicolas Souchu case SMB_REQUEST_BUS: 23704f89a63SNicolas Souchu /* request underlying iicbus */ 23804f89a63SNicolas Souchu how = *(int *)data; 23904f89a63SNicolas Souchu error = iicbus_request_bus(parent, dev, how); 24004f89a63SNicolas Souchu break; 24104f89a63SNicolas Souchu 24204f89a63SNicolas Souchu case SMB_RELEASE_BUS: 24304f89a63SNicolas Souchu /* release underlying iicbus */ 24404f89a63SNicolas Souchu error = iicbus_release_bus(parent, dev); 24504f89a63SNicolas Souchu break; 24604f89a63SNicolas Souchu 24704f89a63SNicolas Souchu default: 24804f89a63SNicolas Souchu error = EINVAL; 24904f89a63SNicolas Souchu } 25004f89a63SNicolas Souchu 25104f89a63SNicolas Souchu return (error); 25204f89a63SNicolas Souchu } 25304f89a63SNicolas Souchu 25404f89a63SNicolas Souchu static int 255c3e2dc6bSNicolas Souchu iicsmb_quick(device_t dev, u_char slave, int how) 256c3e2dc6bSNicolas Souchu { 257c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 258c3e2dc6bSNicolas Souchu int error; 259c3e2dc6bSNicolas Souchu 260c3e2dc6bSNicolas Souchu switch (how) { 261c3e2dc6bSNicolas Souchu case SMB_QWRITE: 26204f89a63SNicolas Souchu error = iicbus_start(parent, slave & ~LSB, 0); 263c3e2dc6bSNicolas Souchu break; 264c3e2dc6bSNicolas Souchu 265c3e2dc6bSNicolas Souchu case SMB_QREAD: 26604f89a63SNicolas Souchu error = iicbus_start(parent, slave | LSB, 0); 267c3e2dc6bSNicolas Souchu break; 268c3e2dc6bSNicolas Souchu 269c3e2dc6bSNicolas Souchu default: 270c3e2dc6bSNicolas Souchu error = EINVAL; 271c3e2dc6bSNicolas Souchu break; 272c3e2dc6bSNicolas Souchu } 273c3e2dc6bSNicolas Souchu 274c3e2dc6bSNicolas Souchu if (!error) 275c3e2dc6bSNicolas Souchu error = iicbus_stop(parent); 276c3e2dc6bSNicolas Souchu 277c3e2dc6bSNicolas Souchu return (error); 278c3e2dc6bSNicolas Souchu } 279c3e2dc6bSNicolas Souchu 280c3e2dc6bSNicolas Souchu static int 281c3e2dc6bSNicolas Souchu iicsmb_sendb(device_t dev, u_char slave, char byte) 282c3e2dc6bSNicolas Souchu { 283c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 284c3e2dc6bSNicolas Souchu int error, sent; 285c3e2dc6bSNicolas Souchu 28604f89a63SNicolas Souchu error = iicbus_start(parent, slave & ~LSB, 0); 287c3e2dc6bSNicolas Souchu 288c3e2dc6bSNicolas Souchu if (!error) { 28904f89a63SNicolas Souchu error = iicbus_write(parent, &byte, 1, &sent, 0); 290c3e2dc6bSNicolas Souchu 291c3e2dc6bSNicolas Souchu iicbus_stop(parent); 292c3e2dc6bSNicolas Souchu } 293c3e2dc6bSNicolas Souchu 294c3e2dc6bSNicolas Souchu return (error); 295c3e2dc6bSNicolas Souchu } 296c3e2dc6bSNicolas Souchu 297c3e2dc6bSNicolas Souchu static int 298c3e2dc6bSNicolas Souchu iicsmb_recvb(device_t dev, u_char slave, char *byte) 299c3e2dc6bSNicolas Souchu { 300c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 301c3e2dc6bSNicolas Souchu int error, read; 302c3e2dc6bSNicolas Souchu 30304f89a63SNicolas Souchu error = iicbus_start(parent, slave | LSB, 0); 304c3e2dc6bSNicolas Souchu 30504f89a63SNicolas Souchu if (!error) { 30604f89a63SNicolas Souchu error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, 0); 30704f89a63SNicolas Souchu 30804f89a63SNicolas Souchu iicbus_stop(parent); 30904f89a63SNicolas Souchu } 310c3e2dc6bSNicolas Souchu 311c3e2dc6bSNicolas Souchu return (error); 312c3e2dc6bSNicolas Souchu } 313c3e2dc6bSNicolas Souchu 314c3e2dc6bSNicolas Souchu static int 315c3e2dc6bSNicolas Souchu iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte) 316c3e2dc6bSNicolas Souchu { 317c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 318c3e2dc6bSNicolas Souchu int error, sent; 319c3e2dc6bSNicolas Souchu 32004f89a63SNicolas Souchu error = iicbus_start(parent, slave & ~LSB, 0); 321c3e2dc6bSNicolas Souchu 322c3e2dc6bSNicolas Souchu if (!error) { 32304f89a63SNicolas Souchu if (!(error = iicbus_write(parent, &cmd, 1, &sent, 0))) 32404f89a63SNicolas Souchu error = iicbus_write(parent, &byte, 1, &sent, 0); 325c3e2dc6bSNicolas Souchu 326c3e2dc6bSNicolas Souchu iicbus_stop(parent); 327c3e2dc6bSNicolas Souchu } 328c3e2dc6bSNicolas Souchu 329c3e2dc6bSNicolas Souchu return (error); 330c3e2dc6bSNicolas Souchu } 331c3e2dc6bSNicolas Souchu 332c3e2dc6bSNicolas Souchu static int 333c3e2dc6bSNicolas Souchu iicsmb_writew(device_t dev, u_char slave, char cmd, short word) 334c3e2dc6bSNicolas Souchu { 335c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 336c3e2dc6bSNicolas Souchu int error, sent; 337c3e2dc6bSNicolas Souchu 338c3e2dc6bSNicolas Souchu char low = (char)(word & 0xff); 339c3e2dc6bSNicolas Souchu char high = (char)((word & 0xff00) >> 8); 340c3e2dc6bSNicolas Souchu 34104f89a63SNicolas Souchu error = iicbus_start(parent, slave & ~LSB, 0); 342c3e2dc6bSNicolas Souchu 343c3e2dc6bSNicolas Souchu if (!error) { 34404f89a63SNicolas Souchu if (!(error = iicbus_write(parent, &cmd, 1, &sent, 0))) 34504f89a63SNicolas Souchu if (!(error = iicbus_write(parent, &low, 1, &sent, 0))) 34604f89a63SNicolas Souchu error = iicbus_write(parent, &high, 1, &sent, 0); 347c3e2dc6bSNicolas Souchu 348c3e2dc6bSNicolas Souchu iicbus_stop(parent); 349c3e2dc6bSNicolas Souchu } 350c3e2dc6bSNicolas Souchu 351c3e2dc6bSNicolas Souchu return (error); 352c3e2dc6bSNicolas Souchu } 353c3e2dc6bSNicolas Souchu 354c3e2dc6bSNicolas Souchu static int 355c3e2dc6bSNicolas Souchu iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte) 356c3e2dc6bSNicolas Souchu { 357c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 358c3e2dc6bSNicolas Souchu int error, sent, read; 359c3e2dc6bSNicolas Souchu 36004f89a63SNicolas Souchu if ((error = iicbus_start(parent, slave & ~LSB, 0))) 36104f89a63SNicolas Souchu return (error); 36204f89a63SNicolas Souchu 36304f89a63SNicolas Souchu if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) 364c3e2dc6bSNicolas Souchu goto error; 365c3e2dc6bSNicolas Souchu 36604f89a63SNicolas Souchu if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) 367c3e2dc6bSNicolas Souchu goto error; 368c3e2dc6bSNicolas Souchu 36904f89a63SNicolas Souchu if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, 0))) 370c3e2dc6bSNicolas Souchu goto error; 371c3e2dc6bSNicolas Souchu 372c3e2dc6bSNicolas Souchu error: 37304f89a63SNicolas Souchu iicbus_stop(parent); 374c3e2dc6bSNicolas Souchu return (error); 375c3e2dc6bSNicolas Souchu } 376c3e2dc6bSNicolas Souchu 377c3e2dc6bSNicolas Souchu #define BUF2SHORT(low,high) \ 378c3e2dc6bSNicolas Souchu ((short)(((high) & 0xff) << 8) | (short)((low) & 0xff)) 379c3e2dc6bSNicolas Souchu 380c3e2dc6bSNicolas Souchu static int 381c3e2dc6bSNicolas Souchu iicsmb_readw(device_t dev, u_char slave, char cmd, short *word) 382c3e2dc6bSNicolas Souchu { 383c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 384c3e2dc6bSNicolas Souchu int error, sent, read; 385c3e2dc6bSNicolas Souchu char buf[2]; 386c3e2dc6bSNicolas Souchu 38704f89a63SNicolas Souchu if ((error = iicbus_start(parent, slave & ~LSB, 0))) 38804f89a63SNicolas Souchu return (error); 38904f89a63SNicolas Souchu 39004f89a63SNicolas Souchu if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) 391c3e2dc6bSNicolas Souchu goto error; 392c3e2dc6bSNicolas Souchu 39304f89a63SNicolas Souchu if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) 394c3e2dc6bSNicolas Souchu goto error; 395c3e2dc6bSNicolas Souchu 39604f89a63SNicolas Souchu if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, 0))) 397c3e2dc6bSNicolas Souchu goto error; 398c3e2dc6bSNicolas Souchu 399c3e2dc6bSNicolas Souchu /* first, receive low, then high byte */ 400c3e2dc6bSNicolas Souchu *word = BUF2SHORT(buf[0], buf[1]); 401c3e2dc6bSNicolas Souchu 402c3e2dc6bSNicolas Souchu error: 40304f89a63SNicolas Souchu iicbus_stop(parent); 404c3e2dc6bSNicolas Souchu return (error); 405c3e2dc6bSNicolas Souchu } 406c3e2dc6bSNicolas Souchu 407c3e2dc6bSNicolas Souchu static int 408c3e2dc6bSNicolas Souchu iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata) 409c3e2dc6bSNicolas Souchu { 410c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 411c3e2dc6bSNicolas Souchu int error, sent, read; 412c3e2dc6bSNicolas Souchu char buf[2]; 413c3e2dc6bSNicolas Souchu 41404f89a63SNicolas Souchu if ((error = iicbus_start(parent, slave & ~LSB, 0))) 41504f89a63SNicolas Souchu return (error); 416c3e2dc6bSNicolas Souchu 41704f89a63SNicolas Souchu if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) 418c3e2dc6bSNicolas Souchu goto error; 419c3e2dc6bSNicolas Souchu 420c3e2dc6bSNicolas Souchu /* first, send low, then high byte */ 421c3e2dc6bSNicolas Souchu buf[0] = (char)(sdata & 0xff); 422c3e2dc6bSNicolas Souchu buf[1] = (char)((sdata & 0xff00) >> 8); 423c3e2dc6bSNicolas Souchu 42404f89a63SNicolas Souchu if ((error = iicbus_write(parent, buf, 2, &sent, 0))) 425c3e2dc6bSNicolas Souchu goto error; 426c3e2dc6bSNicolas Souchu 42704f89a63SNicolas Souchu if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) 428c3e2dc6bSNicolas Souchu goto error; 429c3e2dc6bSNicolas Souchu 43004f89a63SNicolas Souchu if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, 0))) 431c3e2dc6bSNicolas Souchu goto error; 432c3e2dc6bSNicolas Souchu 433c3e2dc6bSNicolas Souchu /* first, receive low, then high byte */ 434c3e2dc6bSNicolas Souchu *rdata = BUF2SHORT(buf[0], buf[1]); 435c3e2dc6bSNicolas Souchu 436c3e2dc6bSNicolas Souchu error: 43704f89a63SNicolas Souchu iicbus_stop(parent); 438c3e2dc6bSNicolas Souchu return (error); 439c3e2dc6bSNicolas Souchu } 440c3e2dc6bSNicolas Souchu 441c3e2dc6bSNicolas Souchu static int 442c3e2dc6bSNicolas Souchu iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf) 443c3e2dc6bSNicolas Souchu { 444c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 445c3e2dc6bSNicolas Souchu int error, sent; 446c3e2dc6bSNicolas Souchu 44704f89a63SNicolas Souchu if ((error = iicbus_start(parent, slave & ~LSB, 0))) 448c3e2dc6bSNicolas Souchu goto error; 449c3e2dc6bSNicolas Souchu 45004f89a63SNicolas Souchu if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) 451c3e2dc6bSNicolas Souchu goto error; 452c3e2dc6bSNicolas Souchu 45304f89a63SNicolas Souchu if ((error = iicbus_write(parent, buf, (int)count, &sent, 0))) 454c3e2dc6bSNicolas Souchu goto error; 455c3e2dc6bSNicolas Souchu 456c3e2dc6bSNicolas Souchu if ((error = iicbus_stop(parent))) 457c3e2dc6bSNicolas Souchu goto error; 458c3e2dc6bSNicolas Souchu 459c3e2dc6bSNicolas Souchu error: 460c3e2dc6bSNicolas Souchu return (error); 461c3e2dc6bSNicolas Souchu } 462c3e2dc6bSNicolas Souchu 463c3e2dc6bSNicolas Souchu static int 464c3e2dc6bSNicolas Souchu iicsmb_bread(device_t dev, u_char slave, char cmd, u_char count, char *buf) 465c3e2dc6bSNicolas Souchu { 466c3e2dc6bSNicolas Souchu device_t parent = device_get_parent(dev); 467c3e2dc6bSNicolas Souchu int error, sent, read; 468c3e2dc6bSNicolas Souchu 46904f89a63SNicolas Souchu if ((error = iicbus_start(parent, slave & ~LSB, 0))) 47004f89a63SNicolas Souchu return (error); 47104f89a63SNicolas Souchu 47204f89a63SNicolas Souchu if ((error = iicbus_write(parent, &cmd, 1, &sent, 0))) 473c3e2dc6bSNicolas Souchu goto error; 474c3e2dc6bSNicolas Souchu 47504f89a63SNicolas Souchu if ((error = iicbus_repeated_start(parent, slave | LSB, 0))) 476c3e2dc6bSNicolas Souchu goto error; 477c3e2dc6bSNicolas Souchu 47804f89a63SNicolas Souchu if ((error = iicbus_read(parent, buf, (int)count, &read, 47904f89a63SNicolas Souchu IIC_LAST_READ, 0))) 480c3e2dc6bSNicolas Souchu goto error; 481c3e2dc6bSNicolas Souchu 482c3e2dc6bSNicolas Souchu error: 48304f89a63SNicolas Souchu iicbus_stop(parent); 484c3e2dc6bSNicolas Souchu return (error); 485c3e2dc6bSNicolas Souchu } 486c3e2dc6bSNicolas Souchu 487c3e2dc6bSNicolas Souchu DRIVER_MODULE(iicsmb, iicbus, iicsmb_driver, iicsmb_devclass, 0, 0); 488