Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.27.2.1
      1  1.27.2.1        ad /*	$NetBSD: ki2c.c,v 1.27.2.1 2020/01/17 21:47:26 ad Exp $	*/
      2       1.1     grant /*	Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp	*/
      3       1.1     grant 
      4       1.1     grant /*-
      5       1.1     grant  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
      6       1.1     grant  *
      7       1.1     grant  * Redistribution and use in source and binary forms, with or without
      8       1.1     grant  * modification, are permitted provided that the following conditions
      9       1.1     grant  * are met:
     10       1.1     grant  * 1. Redistributions of source code must retain the above copyright
     11       1.1     grant  *    notice, this list of conditions and the following disclaimer.
     12       1.1     grant  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1     grant  *    notice, this list of conditions and the following disclaimer in the
     14       1.1     grant  *    documentation and/or other materials provided with the distribution.
     15       1.1     grant  * 3. The name of the author may not be used to endorse or promote products
     16       1.1     grant  *    derived from this software without specific prior written permission.
     17       1.1     grant  *
     18       1.1     grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1     grant  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1     grant  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1     grant  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1     grant  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23       1.1     grant  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24       1.1     grant  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25       1.1     grant  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26       1.1     grant  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27       1.1     grant  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1     grant  */
     29       1.1     grant 
     30       1.1     grant #include <sys/param.h>
     31       1.1     grant #include <sys/device.h>
     32       1.1     grant #include <sys/systm.h>
     33      1.11        ad #include <sys/mutex.h>
     34       1.1     grant 
     35       1.1     grant #include <dev/ofw/openfirm.h>
     36       1.1     grant #include <machine/autoconf.h>
     37       1.1     grant 
     38  1.27.2.1        ad #include "opt_ki2c.h"
     39       1.3  macallan #include <macppc/dev/ki2cvar.h>
     40       1.1     grant 
     41      1.20  macallan #ifdef KI2C_DEBUG
     42      1.20  macallan #define DPRINTF printf
     43      1.20  macallan #else
     44      1.20  macallan #define DPRINTF while (0) printf
     45      1.20  macallan #endif
     46      1.20  macallan 
     47      1.17      matt int ki2c_match(device_t, cfdata_t, void *);
     48      1.17      matt void ki2c_attach(device_t, device_t, void *);
     49      1.21  macallan inline uint8_t ki2c_readreg(struct ki2c_softc *, int);
     50      1.21  macallan inline void ki2c_writereg(struct ki2c_softc *, int, uint8_t);
     51       1.1     grant u_int ki2c_getmode(struct ki2c_softc *);
     52       1.1     grant void ki2c_setmode(struct ki2c_softc *, u_int);
     53       1.1     grant u_int ki2c_getspeed(struct ki2c_softc *);
     54       1.1     grant void ki2c_setspeed(struct ki2c_softc *, u_int);
     55       1.1     grant int ki2c_intr(struct ki2c_softc *);
     56       1.1     grant int ki2c_poll(struct ki2c_softc *, int);
     57       1.1     grant int ki2c_start(struct ki2c_softc *, int, int, void *, int);
     58       1.1     grant int ki2c_read(struct ki2c_softc *, int, int, void *, int);
     59       1.3  macallan int ki2c_write(struct ki2c_softc *, int, int, void *, int);
     60       1.3  macallan 
     61       1.3  macallan /* I2C glue */
     62       1.3  macallan static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     63       1.3  macallan 		    void *, size_t, int);
     64       1.3  macallan 
     65       1.1     grant 
     66      1.18  macallan CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
     67       1.9    dogcow 	NULL, NULL);
     68       1.1     grant 
     69       1.1     grant int
     70      1.17      matt ki2c_match(device_t parent, cfdata_t match, void *aux)
     71       1.1     grant {
     72       1.1     grant 	struct confargs *ca = aux;
     73       1.1     grant 
     74       1.1     grant 	if (strcmp(ca->ca_name, "i2c") == 0)
     75       1.1     grant 		return 1;
     76       1.1     grant 
     77       1.1     grant 	return 0;
     78       1.1     grant }
     79       1.1     grant 
     80       1.1     grant void
     81      1.17      matt ki2c_attach(device_t parent, device_t self, void *aux)
     82       1.1     grant {
     83      1.17      matt 	struct ki2c_softc *sc = device_private(self);
     84       1.1     grant 	struct confargs *ca = aux;
     85       1.1     grant 	int node = ca->ca_node;
     86      1.26  macallan 	uint32_t addr, channel, reg;
     87  1.27.2.1        ad 	int rate, child, /*namelen,*/ i2cbus[2] = {0, 0};
     88       1.3  macallan 	struct i2cbus_attach_args iba;
     89      1.20  macallan 	prop_dictionary_t dict = device_properties(self);
     90      1.20  macallan 	prop_array_t cfg;
     91      1.26  macallan 	int devs, devc;
     92      1.26  macallan 	char compat[256], num[8], descr[32];
     93      1.22  macallan 	prop_dictionary_t dev;
     94      1.22  macallan 	prop_data_t data;
     95       1.3  macallan 	char name[32];
     96      1.18  macallan 
     97      1.18  macallan 	sc->sc_dev = self;
     98      1.21  macallan 	sc->sc_tag = ca->ca_tag;
     99       1.1     grant 	ca->ca_reg[0] += ca->ca_baseaddr;
    100       1.1     grant 
    101       1.1     grant 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
    102      1.20  macallan 		aprint_error(": cannot get i2c-rate\n");
    103       1.1     grant 		return;
    104       1.1     grant 	}
    105      1.20  macallan 	if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
    106      1.20  macallan 		aprint_error(": unable to find i2c address\n");
    107       1.1     grant 		return;
    108       1.1     grant 	}
    109      1.21  macallan 	if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
    110      1.21  macallan 		aprint_error_dev(sc->sc_dev, "failed to map registers\n");
    111      1.21  macallan 		return;
    112      1.21  macallan 	}
    113      1.21  macallan 
    114       1.1     grant 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    115      1.20  macallan 		aprint_error(": unable to find i2c address step\n");
    116       1.1     grant 		return;
    117       1.1     grant 	}
    118       1.1     grant 
    119       1.1     grant 	printf("\n");
    120       1.1     grant 
    121       1.1     grant 	ki2c_writereg(sc, STATUS, 0);
    122       1.1     grant 	ki2c_writereg(sc, ISR, 0);
    123       1.1     grant 	ki2c_writereg(sc, IER, 0);
    124       1.1     grant 
    125       1.1     grant 	ki2c_setmode(sc, I2C_STDSUBMODE);
    126       1.1     grant 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    127       1.3  macallan 
    128       1.3  macallan 	ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
    129       1.3  macallan 
    130      1.20  macallan 	cfg = prop_array_create();
    131      1.20  macallan 	prop_dictionary_set(dict, "i2c-child-devices", cfg);
    132      1.20  macallan 	prop_object_release(cfg);
    133      1.20  macallan 
    134       1.4  macallan 	/*
    135       1.4  macallan 	 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
    136       1.4  macallan 	 * directly to the ki2c node so we just check if we have a child named
    137      1.25  macallan 	 * 'i2c-bus' and if so we attach its children, not ours
    138      1.25  macallan 	 *
    139      1.25  macallan 	 * XXX
    140      1.25  macallan 	 * should probably check for multiple i2c-bus children
    141       1.4  macallan 	 */
    142  1.27.2.1        ad 
    143  1.27.2.1        ad 	int found_busnode = 0;
    144      1.25  macallan 	channel = 0;
    145       1.4  macallan 	child = OF_child(node);
    146  1.27.2.1        ad 	while (child != 0) {
    147       1.4  macallan 		OF_getprop(child, "name", name, sizeof(name));
    148      1.25  macallan 		if (strcmp(name, "i2c-bus") == 0) {
    149      1.25  macallan 			OF_getprop(child, "reg", &channel, sizeof(channel));
    150  1.27.2.1        ad 			i2cbus[channel] = child;
    151      1.25  macallan 			DPRINTF("found channel %x\n", channel);
    152  1.27.2.1        ad 			found_busnode = 1;
    153      1.25  macallan 		}
    154       1.4  macallan 		child = OF_peer(child);
    155       1.4  macallan 	}
    156  1.27.2.1        ad 	if (found_busnode == 0)
    157  1.27.2.1        ad 		i2cbus[0] = node;
    158      1.22  macallan 
    159  1.27.2.1        ad 	for (channel = 0; channel < 2; channel++) {
    160  1.27.2.1        ad 		devs = OF_child(i2cbus[channel]);
    161  1.27.2.1        ad 		while (devs != 0) {
    162  1.27.2.1        ad 			if (OF_getprop(devs, "name", name, 32) <= 0)
    163      1.22  macallan 				goto skip;
    164  1.27.2.1        ad 			if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
    165  1.27.2.1        ad 				/* some i2c device nodes don't have 'compatible' */
    166  1.27.2.1        ad 				memset(compat, 0, 256);
    167  1.27.2.1        ad 				strncpy(compat, name, 256);
    168  1.27.2.1        ad 			}
    169  1.27.2.1        ad 			if (OF_getprop(devs, "reg", &addr, 4) <= 0)
    170  1.27.2.1        ad 				if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
    171  1.27.2.1        ad 					goto skip;
    172  1.27.2.1        ad 			addr |= channel << 8;
    173  1.27.2.1        ad 			addr = addr >> 1;
    174  1.27.2.1        ad 			DPRINTF("-> %s@%x\n", name, addr);
    175  1.27.2.1        ad 			dev = prop_dictionary_create();
    176  1.27.2.1        ad 			prop_dictionary_set_cstring(dev, "name", name);
    177  1.27.2.1        ad 			data = prop_data_create_data(compat, strlen(compat)+1);
    178  1.27.2.1        ad 			prop_dictionary_set(dev, "compatible", data);
    179  1.27.2.1        ad 			prop_object_release(data);
    180  1.27.2.1        ad 			prop_dictionary_set_uint32(dev, "addr", addr);
    181  1.27.2.1        ad 			prop_dictionary_set_uint64(dev, "cookie", devs);
    182  1.27.2.1        ad 			/* look for location info for sensors */
    183  1.27.2.1        ad 			devc = OF_child(devs);
    184  1.27.2.1        ad 			while (devc != 0) {
    185  1.27.2.1        ad 				if (OF_getprop(devc, "reg", &reg, 4) < 4) goto nope;
    186  1.27.2.1        ad 				if (OF_getprop(devc, "location", descr, 32) <= 0)
    187  1.27.2.1        ad 					goto nope;
    188  1.27.2.1        ad 				DPRINTF("found '%s' at %02x\n", descr, reg);
    189  1.27.2.1        ad 				snprintf(num, 7, "s%02x", reg);
    190  1.27.2.1        ad 				prop_dictionary_set_cstring(dev, num, descr);
    191  1.27.2.1        ad 			nope:
    192  1.27.2.1        ad 				devc = OF_peer(devc);
    193  1.27.2.1        ad 			}
    194  1.27.2.1        ad 			prop_array_add(cfg, dev);
    195  1.27.2.1        ad 			prop_object_release(dev);
    196  1.27.2.1        ad 		skip:
    197  1.27.2.1        ad 			devs = OF_peer(devs);
    198      1.26  macallan 		}
    199       1.3  macallan 	}
    200       1.3  macallan 
    201      1.22  macallan 	/* fill in the i2c tag */
    202      1.27   thorpej 	iic_tag_init(&sc->sc_i2c);
    203      1.22  macallan 	sc->sc_i2c.ic_cookie = sc;
    204      1.22  macallan 	sc->sc_i2c.ic_exec = ki2c_i2c_exec;
    205       1.3  macallan 
    206      1.22  macallan 	memset(&iba, 0, sizeof(iba));
    207      1.22  macallan 	iba.iba_tag = &sc->sc_i2c;
    208      1.22  macallan 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
    209      1.22  macallan 
    210       1.1     grant }
    211       1.1     grant 
    212      1.21  macallan uint8_t
    213      1.14       dsl ki2c_readreg(struct ki2c_softc *sc, int reg)
    214       1.1     grant {
    215       1.1     grant 
    216      1.21  macallan 	return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
    217       1.1     grant }
    218       1.1     grant 
    219       1.1     grant void
    220      1.21  macallan ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
    221       1.1     grant {
    222      1.21  macallan 
    223      1.21  macallan 	bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
    224       1.1     grant 	delay(10);
    225       1.1     grant }
    226       1.1     grant 
    227       1.1     grant u_int
    228      1.14       dsl ki2c_getmode(struct ki2c_softc *sc)
    229       1.1     grant {
    230       1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    231       1.1     grant }
    232       1.1     grant 
    233       1.1     grant void
    234      1.14       dsl ki2c_setmode(struct ki2c_softc *sc, u_int mode)
    235       1.1     grant {
    236      1.25  macallan 	ki2c_writereg(sc, MODE, mode);
    237       1.1     grant }
    238       1.1     grant 
    239       1.1     grant u_int
    240      1.14       dsl ki2c_getspeed(struct ki2c_softc *sc)
    241       1.1     grant {
    242       1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    243       1.1     grant }
    244       1.1     grant 
    245       1.1     grant void
    246      1.14       dsl ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
    247       1.1     grant {
    248       1.1     grant 	u_int x;
    249       1.1     grant 
    250       1.1     grant 	KASSERT((speed & ~I2C_SPEED) == 0);
    251       1.1     grant 	x = ki2c_readreg(sc, MODE);
    252       1.1     grant 	x &= ~I2C_SPEED;
    253       1.1     grant 	x |= speed;
    254       1.1     grant 	ki2c_writereg(sc, MODE, x);
    255       1.1     grant }
    256       1.1     grant 
    257       1.1     grant int
    258      1.14       dsl ki2c_intr(struct ki2c_softc *sc)
    259       1.1     grant {
    260       1.1     grant 	u_int isr, x;
    261       1.1     grant 
    262       1.1     grant 	isr = ki2c_readreg(sc, ISR);
    263       1.1     grant 	if (isr & I2C_INT_ADDR) {
    264       1.1     grant #if 0
    265       1.1     grant 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    266       1.1     grant 			/* No slave responded. */
    267       1.1     grant 			sc->sc_flags |= I2C_ERROR;
    268       1.1     grant 			goto out;
    269       1.1     grant 		}
    270       1.1     grant #endif
    271       1.1     grant 
    272       1.1     grant 		if (sc->sc_flags & I2C_READING) {
    273       1.1     grant 			if (sc->sc_resid > 1) {
    274       1.1     grant 				x = ki2c_readreg(sc, CONTROL);
    275       1.1     grant 				x |= I2C_CT_AAK;
    276       1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    277       1.1     grant 			}
    278       1.1     grant 		} else {
    279       1.1     grant 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    280       1.1     grant 			sc->sc_resid--;
    281       1.1     grant 		}
    282       1.1     grant 	}
    283       1.1     grant 
    284       1.1     grant 	if (isr & I2C_INT_DATA) {
    285       1.1     grant 		if (sc->sc_flags & I2C_READING) {
    286       1.1     grant 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    287       1.1     grant 			sc->sc_resid--;
    288       1.1     grant 
    289       1.1     grant 			if (sc->sc_resid == 0) {	/* Completed */
    290       1.1     grant 				ki2c_writereg(sc, CONTROL, 0);
    291       1.1     grant 				goto out;
    292       1.1     grant 			}
    293       1.1     grant 		} else {
    294       1.1     grant #if 0
    295       1.1     grant 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    296       1.1     grant 				/* No slave responded. */
    297       1.1     grant 				sc->sc_flags |= I2C_ERROR;
    298       1.1     grant 				goto out;
    299       1.1     grant 			}
    300       1.1     grant #endif
    301       1.1     grant 
    302       1.1     grant 			if (sc->sc_resid == 0) {
    303       1.1     grant 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    304       1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    305       1.1     grant 			} else {
    306       1.1     grant 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    307       1.1     grant 				sc->sc_resid--;
    308       1.1     grant 			}
    309       1.1     grant 		}
    310       1.1     grant 	}
    311       1.1     grant 
    312       1.1     grant out:
    313       1.1     grant 	if (isr & I2C_INT_STOP) {
    314       1.1     grant 		ki2c_writereg(sc, CONTROL, 0);
    315       1.1     grant 		sc->sc_flags &= ~I2C_BUSY;
    316       1.1     grant 	}
    317       1.1     grant 
    318       1.1     grant 	ki2c_writereg(sc, ISR, isr);
    319       1.1     grant 
    320       1.1     grant 	return 1;
    321       1.1     grant }
    322       1.1     grant 
    323       1.1     grant int
    324      1.14       dsl ki2c_poll(struct ki2c_softc *sc, int timo)
    325       1.1     grant {
    326       1.1     grant 	while (sc->sc_flags & I2C_BUSY) {
    327       1.1     grant 		if (ki2c_readreg(sc, ISR))
    328       1.1     grant 			ki2c_intr(sc);
    329       1.1     grant 		timo -= 100;
    330       1.1     grant 		if (timo < 0) {
    331      1.20  macallan 			DPRINTF("i2c_poll: timeout\n");
    332       1.1     grant 			return -1;
    333       1.1     grant 		}
    334       1.1     grant 		delay(100);
    335       1.1     grant 	}
    336       1.1     grant 	return 0;
    337       1.1     grant }
    338       1.1     grant 
    339       1.1     grant int
    340      1.15       dsl ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    341       1.1     grant {
    342       1.1     grant 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    343       1.1     grant 	int timo, x;
    344       1.1     grant 
    345       1.1     grant 	KASSERT((addr & 1) == 0);
    346       1.1     grant 
    347       1.1     grant 	sc->sc_data = data;
    348       1.1     grant 	sc->sc_resid = len;
    349       1.1     grant 	sc->sc_flags |= I2C_BUSY;
    350       1.1     grant 
    351       1.1     grant 	timo = 1000 + len * 200;
    352       1.1     grant 
    353       1.1     grant 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    354       1.1     grant 	/* if (addr == 0x68) */
    355       1.1     grant 		timo += 100000;
    356       1.1     grant 
    357       1.1     grant 	ki2c_writereg(sc, ADDR, addr | rw);
    358       1.1     grant 	ki2c_writereg(sc, SUBADDR, subaddr);
    359       1.1     grant 
    360       1.1     grant 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    361       1.1     grant 	ki2c_writereg(sc, CONTROL, x);
    362       1.1     grant 
    363       1.1     grant 	if (ki2c_poll(sc, timo))
    364       1.1     grant 		return -1;
    365       1.1     grant 	if (sc->sc_flags & I2C_ERROR) {
    366      1.20  macallan 		DPRINTF("I2C_ERROR\n");
    367       1.1     grant 		return -1;
    368       1.1     grant 	}
    369       1.1     grant 	return 0;
    370       1.1     grant }
    371       1.1     grant 
    372       1.1     grant int
    373      1.15       dsl ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    374       1.1     grant {
    375       1.1     grant 	sc->sc_flags = I2C_READING;
    376      1.20  macallan 	DPRINTF("ki2c_read: %02x %d\n", addr, len);
    377       1.1     grant 	return ki2c_start(sc, addr, subaddr, data, len);
    378       1.1     grant }
    379       1.1     grant 
    380       1.1     grant int
    381      1.15       dsl ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
    382       1.1     grant {
    383       1.1     grant 	sc->sc_flags = 0;
    384      1.20  macallan 	DPRINTF("ki2c_write: %02x %d\n",addr,len);
    385       1.3  macallan 	return ki2c_start(sc, addr, subaddr, data, len);
    386       1.3  macallan }
    387       1.3  macallan 
    388       1.3  macallan int
    389       1.3  macallan ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    390       1.3  macallan     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    391       1.3  macallan {
    392       1.3  macallan 	struct ki2c_softc *sc = cookie;
    393      1.12  pgoyette 	int i;
    394      1.12  pgoyette 	size_t w_len;
    395      1.12  pgoyette 	uint8_t *wp;
    396      1.12  pgoyette 	uint8_t wrbuf[I2C_EXEC_MAX_CMDLEN + I2C_EXEC_MAX_CMDLEN];
    397      1.25  macallan 	uint8_t channel;
    398      1.12  pgoyette 
    399      1.12  pgoyette 	/*
    400      1.12  pgoyette 	 * We don't have any idea if the ki2c controller can execute
    401      1.12  pgoyette 	 * i2c quick_{read,write} operations, so if someone tries one,
    402      1.12  pgoyette 	 * return an error.
    403      1.12  pgoyette 	 */
    404      1.12  pgoyette 	if (cmdlen == 0 && buflen == 0)
    405      1.12  pgoyette 		return -1;
    406      1.12  pgoyette 
    407      1.25  macallan 	channel = (addr & 0xf80) ? 0x10 : 0x00;
    408      1.25  macallan 	addr &= 0x7f;
    409      1.25  macallan 
    410      1.25  macallan 
    411       1.3  macallan 	/* we handle the subaddress stuff ourselves */
    412      1.25  macallan 	ki2c_setmode(sc, channel | I2C_STDMODE);
    413  1.27.2.1        ad 	ki2c_setspeed(sc, I2C_50kHz);
    414       1.3  macallan 
    415      1.12  pgoyette 	/* Write-buffer defaults to vcmd */
    416      1.12  pgoyette 	wp = (uint8_t *)(__UNCONST(vcmd));
    417      1.12  pgoyette 	w_len = cmdlen;
    418      1.12  pgoyette 
    419      1.12  pgoyette 	/*
    420      1.12  pgoyette 	 * Concatenate vcmd and vbuf for write operations
    421      1.12  pgoyette 	 *
    422      1.12  pgoyette 	 * Drivers written specifically for ki2c might already do this,
    423      1.12  pgoyette 	 * but "generic" i2c drivers still provide separate arguments
    424      1.12  pgoyette 	 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
    425      1.12  pgoyette 	 */
    426      1.12  pgoyette 	if (I2C_OP_WRITE_P(op) && buflen != 0) {
    427      1.12  pgoyette 		if (cmdlen == 0) {
    428      1.12  pgoyette 			wp = (uint8_t *)vbuf;
    429      1.12  pgoyette 			w_len = buflen;
    430      1.12  pgoyette 		} else {
    431      1.12  pgoyette 			KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
    432      1.12  pgoyette 			wp = (uint8_t *)(__UNCONST(vcmd));
    433      1.12  pgoyette 			w_len = 0;
    434      1.12  pgoyette 			for (i = 0; i < cmdlen; i++)
    435      1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    436      1.12  pgoyette 			wp = (uint8_t *)vbuf;
    437      1.12  pgoyette 			for (i = 0; i < buflen; i++)
    438      1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    439      1.12  pgoyette 			wp = wrbuf;
    440      1.12  pgoyette 		}
    441      1.12  pgoyette 	}
    442      1.12  pgoyette 
    443      1.22  macallan 	if (w_len > 0)
    444      1.22  macallan 		if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
    445      1.22  macallan 			return -1;
    446      1.10   garbled 
    447      1.10   garbled 	if (I2C_OP_READ_P(op)) {
    448      1.20  macallan 		if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
    449       1.3  macallan 			return -1;
    450       1.3  macallan 	}
    451       1.3  macallan 	return 0;
    452       1.1     grant }
    453