Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.13
      1  1.13       dsl /*	$NetBSD: ki2c.c,v 1.13 2009/03/14 14:46:01 dsl 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 <uvm/uvm_extern.h>
     37   1.1     grant #include <machine/autoconf.h>
     38   1.1     grant 
     39   1.3  macallan #include <macppc/dev/ki2cvar.h>
     40   1.1     grant 
     41   1.1     grant int ki2c_match(struct device *, struct cfdata *, void *);
     42   1.1     grant void ki2c_attach(struct device *, struct device *, void *);
     43   1.1     grant inline u_int ki2c_readreg(struct ki2c_softc *, int);
     44   1.1     grant inline void ki2c_writereg(struct ki2c_softc *, int, u_int);
     45   1.1     grant u_int ki2c_getmode(struct ki2c_softc *);
     46   1.1     grant void ki2c_setmode(struct ki2c_softc *, u_int);
     47   1.1     grant u_int ki2c_getspeed(struct ki2c_softc *);
     48   1.1     grant void ki2c_setspeed(struct ki2c_softc *, u_int);
     49   1.1     grant int ki2c_intr(struct ki2c_softc *);
     50   1.1     grant int ki2c_poll(struct ki2c_softc *, int);
     51   1.1     grant int ki2c_start(struct ki2c_softc *, int, int, void *, int);
     52   1.1     grant int ki2c_read(struct ki2c_softc *, int, int, void *, int);
     53   1.3  macallan int ki2c_write(struct ki2c_softc *, int, int, void *, int);
     54  1.13       dsl int ki2c_print(void *, const char *);
     55   1.3  macallan 
     56   1.3  macallan /* I2C glue */
     57   1.3  macallan static int ki2c_i2c_acquire_bus(void *, int);
     58   1.3  macallan static void ki2c_i2c_release_bus(void *, int);
     59   1.3  macallan static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     60   1.3  macallan 		    void *, size_t, int);
     61   1.3  macallan 
     62   1.1     grant 
     63   1.9    dogcow CFATTACH_DECL(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
     64   1.9    dogcow 	NULL, NULL);
     65   1.1     grant 
     66   1.1     grant int
     67   1.1     grant ki2c_match(parent, match, aux)
     68   1.1     grant 	struct device *parent;
     69   1.1     grant 	struct cfdata *match;
     70   1.1     grant 	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.1     grant ki2c_attach(parent, self, aux)
     82   1.1     grant 	struct device *parent;
     83   1.1     grant 	struct device *self;
     84   1.1     grant 	void *aux;
     85   1.1     grant {
     86   1.1     grant 	struct ki2c_softc *sc = (struct ki2c_softc *)self;
     87   1.1     grant 	struct confargs *ca = aux;
     88   1.1     grant 	int node = ca->ca_node;
     89   1.4  macallan 	int rate, child, namelen, i2cbus;
     90   1.3  macallan 	struct ki2c_confargs ka;
     91   1.3  macallan 	struct i2cbus_attach_args iba;
     92   1.3  macallan 
     93   1.3  macallan 	char name[32];
     94   1.3  macallan 	u_int reg[20];
     95   1.3  macallan 
     96   1.1     grant 	ca->ca_reg[0] += ca->ca_baseaddr;
     97   1.1     grant 
     98   1.1     grant 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
     99   1.1     grant 		printf(": cannot get i2c-rate\n");
    100   1.1     grant 		return;
    101   1.1     grant 	}
    102   1.1     grant 	if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
    103   1.1     grant 		printf(": unable to find i2c address\n");
    104   1.1     grant 		return;
    105   1.1     grant 	}
    106   1.1     grant 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    107   1.1     grant 		printf(": unable to find i2c address step\n");
    108   1.1     grant 		return;
    109   1.1     grant 	}
    110   1.1     grant 
    111   1.1     grant 	printf("\n");
    112   1.1     grant 
    113   1.1     grant 	ki2c_writereg(sc, STATUS, 0);
    114   1.1     grant 	ki2c_writereg(sc, ISR, 0);
    115   1.1     grant 	ki2c_writereg(sc, IER, 0);
    116   1.1     grant 
    117   1.1     grant 	ki2c_setmode(sc, I2C_STDSUBMODE);
    118   1.1     grant 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    119   1.3  macallan 
    120  1.11        ad 	mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
    121   1.3  macallan 	ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
    122   1.3  macallan 
    123   1.3  macallan 	/* fill in the i2c tag */
    124   1.3  macallan 	sc->sc_i2c.ic_cookie = sc;
    125   1.3  macallan 	sc->sc_i2c.ic_acquire_bus = ki2c_i2c_acquire_bus;
    126   1.3  macallan 	sc->sc_i2c.ic_release_bus = ki2c_i2c_release_bus;
    127   1.3  macallan 	sc->sc_i2c.ic_send_start = NULL;
    128   1.3  macallan 	sc->sc_i2c.ic_send_stop = NULL;
    129   1.3  macallan 	sc->sc_i2c.ic_initiate_xfer = NULL;
    130   1.3  macallan 	sc->sc_i2c.ic_read_byte = NULL;
    131   1.3  macallan 	sc->sc_i2c.ic_write_byte = NULL;
    132   1.3  macallan 	sc->sc_i2c.ic_exec = ki2c_i2c_exec;
    133   1.3  macallan 
    134   1.3  macallan 	iba.iba_tag = &sc->sc_i2c;
    135   1.7  drochner 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    136   1.3  macallan 
    137   1.4  macallan 	/*
    138   1.4  macallan 	 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
    139   1.4  macallan 	 * directly to the ki2c node so we just check if we have a child named
    140   1.4  macallan 	 * 'i2c-bus' and if so we attach its children, not ours
    141   1.4  macallan 	 */
    142   1.4  macallan 	i2cbus = 0;
    143   1.4  macallan 	child = OF_child(node);
    144   1.4  macallan 	while ((child != 0) && (i2cbus == 0)) {
    145   1.4  macallan 		OF_getprop(child, "name", name, sizeof(name));
    146   1.4  macallan 		if (strcmp(name, "i2c-bus") == 0)
    147   1.4  macallan 			i2cbus = child;
    148   1.4  macallan 		child = OF_peer(child);
    149   1.4  macallan 	}
    150   1.4  macallan 	if (i2cbus == 0)
    151   1.4  macallan 		i2cbus = node;
    152   1.4  macallan 
    153   1.4  macallan 	for (child = OF_child(i2cbus); child; child = OF_peer(child)) {
    154  1.10   garbled 		int ok = 0;
    155   1.3  macallan 		namelen = OF_getprop(child, "name", name, sizeof(name));
    156   1.3  macallan 		if (namelen < 0)
    157   1.3  macallan 			continue;
    158   1.3  macallan 		if (namelen >= sizeof(name))
    159   1.3  macallan 			continue;
    160   1.3  macallan 
    161   1.3  macallan 		name[namelen] = 0;
    162   1.3  macallan 		ka.ka_name = name;
    163   1.3  macallan 		ka.ka_node = child;
    164  1.10   garbled 		ok = OF_getprop(child, "reg", reg, sizeof(reg));
    165  1.10   garbled 		if (ok <= 0) {
    166  1.10   garbled 			ok = OF_getprop(child, "i2c-address", reg,
    167  1.10   garbled 			    sizeof(reg));
    168  1.10   garbled 		}
    169  1.10   garbled 		if (ok > 0) {
    170   1.3  macallan 			ka.ka_addr = reg[0];
    171   1.3  macallan 			ka.ka_tag = &sc->sc_i2c;
    172   1.8  drochner 			config_found_ia(self, "ki2c", &ka, ki2c_print);
    173  1.10   garbled 		}
    174  1.10   garbled #ifdef DIAGNOSTIC
    175  1.10   garbled 		else {
    176  1.10   garbled 			printf("%s: device (%s) has no reg or i2c-address property.\n",
    177  1.10   garbled 			    sc->sc_dev.dv_xname, name);
    178   1.3  macallan 		}
    179  1.10   garbled #endif
    180   1.3  macallan 	}
    181   1.3  macallan }
    182   1.3  macallan 
    183   1.3  macallan int
    184   1.3  macallan ki2c_print(aux, ki2c)
    185   1.3  macallan 	void *aux;
    186   1.3  macallan 	const char *ki2c;
    187   1.3  macallan {
    188   1.3  macallan 	struct ki2c_confargs *ka = aux;
    189   1.3  macallan 
    190   1.3  macallan 	if (ki2c) {
    191   1.3  macallan 		aprint_normal("%s at %s", ka->ka_name, ki2c);
    192   1.3  macallan 		aprint_normal(" address 0x%x", ka->ka_addr);
    193   1.3  macallan 	}
    194   1.3  macallan 	return UNCONF;
    195   1.1     grant }
    196   1.1     grant 
    197   1.1     grant u_int
    198   1.1     grant ki2c_readreg(sc, reg)
    199   1.1     grant 	struct ki2c_softc *sc;
    200   1.1     grant 	int reg;
    201   1.1     grant {
    202   1.1     grant 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    203   1.1     grant 
    204   1.1     grant 	return *addr;
    205   1.1     grant }
    206   1.1     grant 
    207   1.1     grant void
    208   1.1     grant ki2c_writereg(sc, reg, val)
    209   1.1     grant 	struct ki2c_softc *sc;
    210   1.1     grant 	int reg;
    211   1.1     grant 	u_int val;
    212   1.1     grant {
    213   1.1     grant 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    214   1.1     grant 
    215   1.1     grant 	*addr = val;
    216   1.6     perry 	__asm volatile ("eieio");
    217   1.1     grant 	delay(10);
    218   1.1     grant }
    219   1.1     grant 
    220   1.1     grant u_int
    221   1.1     grant ki2c_getmode(sc)
    222   1.1     grant 	struct ki2c_softc *sc;
    223   1.1     grant {
    224   1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    225   1.1     grant }
    226   1.1     grant 
    227   1.1     grant void
    228   1.1     grant ki2c_setmode(sc, mode)
    229   1.1     grant 	struct ki2c_softc *sc;
    230   1.1     grant 	u_int mode;
    231   1.1     grant {
    232   1.1     grant 	u_int x;
    233   1.1     grant 
    234   1.1     grant 	KASSERT((mode & ~I2C_MODE) == 0);
    235   1.1     grant 	x = ki2c_readreg(sc, MODE);
    236   1.1     grant 	x &= ~I2C_MODE;
    237   1.1     grant 	x |= mode;
    238   1.1     grant 	ki2c_writereg(sc, MODE, x);
    239   1.1     grant }
    240   1.1     grant 
    241   1.1     grant u_int
    242   1.1     grant ki2c_getspeed(sc)
    243   1.1     grant 	struct ki2c_softc *sc;
    244   1.1     grant {
    245   1.1     grant 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    246   1.1     grant }
    247   1.1     grant 
    248   1.1     grant void
    249   1.1     grant ki2c_setspeed(sc, speed)
    250   1.1     grant 	struct ki2c_softc *sc;
    251   1.1     grant 	u_int speed;
    252   1.1     grant {
    253   1.1     grant 	u_int x;
    254   1.1     grant 
    255   1.1     grant 	KASSERT((speed & ~I2C_SPEED) == 0);
    256   1.1     grant 	x = ki2c_readreg(sc, MODE);
    257   1.1     grant 	x &= ~I2C_SPEED;
    258   1.1     grant 	x |= speed;
    259   1.1     grant 	ki2c_writereg(sc, MODE, x);
    260   1.1     grant }
    261   1.1     grant 
    262   1.1     grant int
    263   1.1     grant ki2c_intr(sc)
    264   1.1     grant 	struct ki2c_softc *sc;
    265   1.1     grant {
    266   1.1     grant 	u_int isr, x;
    267   1.1     grant 
    268   1.1     grant 	isr = ki2c_readreg(sc, ISR);
    269   1.1     grant 	if (isr & I2C_INT_ADDR) {
    270   1.1     grant #if 0
    271   1.1     grant 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    272   1.1     grant 			/* No slave responded. */
    273   1.1     grant 			sc->sc_flags |= I2C_ERROR;
    274   1.1     grant 			goto out;
    275   1.1     grant 		}
    276   1.1     grant #endif
    277   1.1     grant 
    278   1.1     grant 		if (sc->sc_flags & I2C_READING) {
    279   1.1     grant 			if (sc->sc_resid > 1) {
    280   1.1     grant 				x = ki2c_readreg(sc, CONTROL);
    281   1.1     grant 				x |= I2C_CT_AAK;
    282   1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    283   1.1     grant 			}
    284   1.1     grant 		} else {
    285   1.1     grant 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    286   1.1     grant 			sc->sc_resid--;
    287   1.1     grant 		}
    288   1.1     grant 	}
    289   1.1     grant 
    290   1.1     grant 	if (isr & I2C_INT_DATA) {
    291   1.1     grant 		if (sc->sc_flags & I2C_READING) {
    292   1.1     grant 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    293   1.1     grant 			sc->sc_resid--;
    294   1.1     grant 
    295   1.1     grant 			if (sc->sc_resid == 0) {	/* Completed */
    296   1.1     grant 				ki2c_writereg(sc, CONTROL, 0);
    297   1.1     grant 				goto out;
    298   1.1     grant 			}
    299   1.1     grant 		} else {
    300   1.1     grant #if 0
    301   1.1     grant 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    302   1.1     grant 				/* No slave responded. */
    303   1.1     grant 				sc->sc_flags |= I2C_ERROR;
    304   1.1     grant 				goto out;
    305   1.1     grant 			}
    306   1.1     grant #endif
    307   1.1     grant 
    308   1.1     grant 			if (sc->sc_resid == 0) {
    309   1.1     grant 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    310   1.1     grant 				ki2c_writereg(sc, CONTROL, x);
    311   1.1     grant 			} else {
    312   1.1     grant 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    313   1.1     grant 				sc->sc_resid--;
    314   1.1     grant 			}
    315   1.1     grant 		}
    316   1.1     grant 	}
    317   1.1     grant 
    318   1.1     grant out:
    319   1.1     grant 	if (isr & I2C_INT_STOP) {
    320   1.1     grant 		ki2c_writereg(sc, CONTROL, 0);
    321   1.1     grant 		sc->sc_flags &= ~I2C_BUSY;
    322   1.1     grant 	}
    323   1.1     grant 
    324   1.1     grant 	ki2c_writereg(sc, ISR, isr);
    325   1.1     grant 
    326   1.1     grant 	return 1;
    327   1.1     grant }
    328   1.1     grant 
    329   1.1     grant int
    330   1.1     grant ki2c_poll(sc, timo)
    331   1.1     grant 	struct ki2c_softc *sc;
    332   1.1     grant 	int timo;
    333   1.1     grant {
    334   1.1     grant 	while (sc->sc_flags & I2C_BUSY) {
    335   1.1     grant 		if (ki2c_readreg(sc, ISR))
    336   1.1     grant 			ki2c_intr(sc);
    337   1.1     grant 		timo -= 100;
    338   1.1     grant 		if (timo < 0) {
    339   1.1     grant 			printf("i2c_poll: timeout\n");
    340   1.1     grant 			return -1;
    341   1.1     grant 		}
    342   1.1     grant 		delay(100);
    343   1.1     grant 	}
    344   1.1     grant 	return 0;
    345   1.1     grant }
    346   1.1     grant 
    347   1.1     grant int
    348   1.1     grant ki2c_start(sc, addr, subaddr, data, len)
    349   1.1     grant 	struct ki2c_softc *sc;
    350   1.1     grant 	int addr, subaddr, len;
    351   1.1     grant 	void *data;
    352   1.1     grant {
    353   1.1     grant 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    354   1.1     grant 	int timo, x;
    355   1.1     grant 
    356   1.1     grant 	KASSERT((addr & 1) == 0);
    357   1.1     grant 
    358   1.1     grant 	sc->sc_data = data;
    359   1.1     grant 	sc->sc_resid = len;
    360   1.1     grant 	sc->sc_flags |= I2C_BUSY;
    361   1.1     grant 
    362   1.1     grant 	timo = 1000 + len * 200;
    363   1.1     grant 
    364   1.1     grant 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    365   1.1     grant 	/* if (addr == 0x68) */
    366   1.1     grant 		timo += 100000;
    367   1.1     grant 
    368   1.1     grant 	ki2c_writereg(sc, ADDR, addr | rw);
    369   1.1     grant 	ki2c_writereg(sc, SUBADDR, subaddr);
    370   1.1     grant 
    371   1.1     grant 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    372   1.1     grant 	ki2c_writereg(sc, CONTROL, x);
    373   1.1     grant 
    374   1.1     grant 	if (ki2c_poll(sc, timo))
    375   1.1     grant 		return -1;
    376   1.1     grant 	if (sc->sc_flags & I2C_ERROR) {
    377   1.1     grant 		printf("I2C_ERROR\n");
    378   1.1     grant 		return -1;
    379   1.1     grant 	}
    380   1.1     grant 	return 0;
    381   1.1     grant }
    382   1.1     grant 
    383   1.1     grant int
    384   1.1     grant ki2c_read(sc, addr, subaddr, data, len)
    385   1.1     grant 	struct ki2c_softc *sc;
    386   1.1     grant 	int addr, subaddr, len;
    387   1.1     grant 	void *data;
    388   1.1     grant {
    389   1.1     grant 	sc->sc_flags = I2C_READING;
    390   1.3  macallan 	#ifdef KI2C_DEBUG
    391   1.3  macallan 		printf("ki2c_read: %02x %d\n", addr, len);
    392   1.3  macallan 	#endif
    393   1.1     grant 	return ki2c_start(sc, addr, subaddr, data, len);
    394   1.1     grant }
    395   1.1     grant 
    396   1.1     grant int
    397   1.1     grant ki2c_write(sc, addr, subaddr, data, len)
    398   1.1     grant 	struct ki2c_softc *sc;
    399   1.1     grant 	int addr, subaddr, len;
    400   1.3  macallan 	void *data;
    401   1.1     grant {
    402   1.1     grant 	sc->sc_flags = 0;
    403   1.3  macallan 	#ifdef KI2C_DEBUG
    404   1.3  macallan 		printf("ki2c_write: %02x %d\n",addr,len);
    405   1.3  macallan 	#endif
    406   1.3  macallan 	return ki2c_start(sc, addr, subaddr, data, len);
    407   1.3  macallan }
    408   1.3  macallan 
    409   1.3  macallan static int
    410   1.3  macallan ki2c_i2c_acquire_bus(void *cookie, int flags)
    411   1.3  macallan {
    412   1.3  macallan 	struct ki2c_softc *sc = cookie;
    413   1.3  macallan 
    414  1.11        ad 	mutex_enter(&sc->sc_buslock);
    415  1.11        ad 	return 0;
    416   1.3  macallan }
    417   1.3  macallan 
    418   1.3  macallan static void
    419   1.3  macallan ki2c_i2c_release_bus(void *cookie, int flags)
    420   1.3  macallan {
    421   1.3  macallan 	struct ki2c_softc *sc = cookie;
    422   1.3  macallan 
    423  1.11        ad 	mutex_exit(&sc->sc_buslock);
    424   1.3  macallan }
    425   1.3  macallan 
    426   1.3  macallan int
    427   1.3  macallan ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    428   1.3  macallan     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    429   1.3  macallan {
    430   1.3  macallan 	struct ki2c_softc *sc = cookie;
    431  1.12  pgoyette 	int i;
    432  1.12  pgoyette 	size_t w_len;
    433  1.12  pgoyette 	uint8_t *wp;
    434  1.12  pgoyette 	uint8_t wrbuf[I2C_EXEC_MAX_CMDLEN + I2C_EXEC_MAX_CMDLEN];
    435  1.12  pgoyette 
    436  1.12  pgoyette 	/*
    437  1.12  pgoyette 	 * We don't have any idea if the ki2c controller can execute
    438  1.12  pgoyette 	 * i2c quick_{read,write} operations, so if someone tries one,
    439  1.12  pgoyette 	 * return an error.
    440  1.12  pgoyette 	 */
    441  1.12  pgoyette 	if (cmdlen == 0 && buflen == 0)
    442  1.12  pgoyette 		return -1;
    443  1.12  pgoyette 
    444   1.3  macallan 	/* we handle the subaddress stuff ourselves */
    445   1.3  macallan 	ki2c_setmode(sc, I2C_STDMODE);
    446   1.3  macallan 
    447  1.12  pgoyette 	/* Write-buffer defaults to vcmd */
    448  1.12  pgoyette 	wp = (uint8_t *)(__UNCONST(vcmd));
    449  1.12  pgoyette 	w_len = cmdlen;
    450  1.12  pgoyette 
    451  1.12  pgoyette 	/*
    452  1.12  pgoyette 	 * Concatenate vcmd and vbuf for write operations
    453  1.12  pgoyette 	 *
    454  1.12  pgoyette 	 * Drivers written specifically for ki2c might already do this,
    455  1.12  pgoyette 	 * but "generic" i2c drivers still provide separate arguments
    456  1.12  pgoyette 	 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
    457  1.12  pgoyette 	 */
    458  1.12  pgoyette 	if (I2C_OP_WRITE_P(op) && buflen != 0) {
    459  1.12  pgoyette 		if (cmdlen == 0) {
    460  1.12  pgoyette 			wp = (uint8_t *)vbuf;
    461  1.12  pgoyette 			w_len = buflen;
    462  1.12  pgoyette 		} else {
    463  1.12  pgoyette 			KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
    464  1.12  pgoyette 			wp = (uint8_t *)(__UNCONST(vcmd));
    465  1.12  pgoyette 			w_len = 0;
    466  1.12  pgoyette 			for (i = 0; i < cmdlen; i++)
    467  1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    468  1.12  pgoyette 			wp = (uint8_t *)vbuf;
    469  1.12  pgoyette 			for (i = 0; i < buflen; i++)
    470  1.12  pgoyette 				wrbuf[w_len++] = *wp++;
    471  1.12  pgoyette 			wp = wrbuf;
    472  1.12  pgoyette 		}
    473  1.12  pgoyette 	}
    474  1.12  pgoyette 
    475  1.12  pgoyette 	if (ki2c_write(sc, addr, 0, wp, w_len) !=0 )
    476   1.3  macallan 		return -1;
    477  1.10   garbled 
    478  1.10   garbled 	if (I2C_OP_READ_P(op)) {
    479   1.3  macallan 		if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
    480   1.3  macallan 			return -1;
    481   1.3  macallan 	}
    482   1.3  macallan 	return 0;
    483   1.1     grant }
    484