1*4275c534Smlelstv /* $NetBSD: ki2c.c,v 1.33 2022/06/29 17:59:40 mlelstv Exp $ */
29427a0c7Sgrant /* Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp */
39427a0c7Sgrant
49427a0c7Sgrant /*-
59427a0c7Sgrant * Copyright (c) 2001 Tsubai Masanari. All rights reserved.
69427a0c7Sgrant *
79427a0c7Sgrant * Redistribution and use in source and binary forms, with or without
89427a0c7Sgrant * modification, are permitted provided that the following conditions
99427a0c7Sgrant * are met:
109427a0c7Sgrant * 1. Redistributions of source code must retain the above copyright
119427a0c7Sgrant * notice, this list of conditions and the following disclaimer.
129427a0c7Sgrant * 2. Redistributions in binary form must reproduce the above copyright
139427a0c7Sgrant * notice, this list of conditions and the following disclaimer in the
149427a0c7Sgrant * documentation and/or other materials provided with the distribution.
159427a0c7Sgrant * 3. The name of the author may not be used to endorse or promote products
169427a0c7Sgrant * derived from this software without specific prior written permission.
179427a0c7Sgrant *
189427a0c7Sgrant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199427a0c7Sgrant * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209427a0c7Sgrant * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
219427a0c7Sgrant * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
229427a0c7Sgrant * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
239427a0c7Sgrant * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249427a0c7Sgrant * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259427a0c7Sgrant * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269427a0c7Sgrant * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
279427a0c7Sgrant * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289427a0c7Sgrant */
299427a0c7Sgrant
309427a0c7Sgrant #include <sys/param.h>
319427a0c7Sgrant #include <sys/device.h>
329427a0c7Sgrant #include <sys/systm.h>
33065b6ba2Sad #include <sys/mutex.h>
349427a0c7Sgrant
359427a0c7Sgrant #include <dev/ofw/openfirm.h>
369427a0c7Sgrant #include <machine/autoconf.h>
379427a0c7Sgrant
38a6cd3ae8Smacallan #include "opt_ki2c.h"
3940f380a1Smacallan #include <macppc/dev/ki2cvar.h>
409427a0c7Sgrant
4112d26bd6Smacallan #ifdef KI2C_DEBUG
4212d26bd6Smacallan #define DPRINTF printf
4312d26bd6Smacallan #else
4412d26bd6Smacallan #define DPRINTF while (0) printf
4512d26bd6Smacallan #endif
4612d26bd6Smacallan
47*4275c534Smlelstv #define KI2C_EXEC_MAX_CMDLEN 32
48*4275c534Smlelstv #define KI2C_EXEC_MAX_BUFLEN 32
49*4275c534Smlelstv
5005b09539Smatt int ki2c_match(device_t, cfdata_t, void *);
5105b09539Smatt void ki2c_attach(device_t, device_t, void *);
52e6c5dacdSmacallan inline uint8_t ki2c_readreg(struct ki2c_softc *, int);
53e6c5dacdSmacallan inline void ki2c_writereg(struct ki2c_softc *, int, uint8_t);
549427a0c7Sgrant u_int ki2c_getmode(struct ki2c_softc *);
559427a0c7Sgrant void ki2c_setmode(struct ki2c_softc *, u_int);
569427a0c7Sgrant u_int ki2c_getspeed(struct ki2c_softc *);
579427a0c7Sgrant void ki2c_setspeed(struct ki2c_softc *, u_int);
589427a0c7Sgrant int ki2c_intr(struct ki2c_softc *);
599427a0c7Sgrant int ki2c_poll(struct ki2c_softc *, int);
609427a0c7Sgrant int ki2c_start(struct ki2c_softc *, int, int, void *, int);
619427a0c7Sgrant int ki2c_read(struct ki2c_softc *, int, int, void *, int);
6240f380a1Smacallan int ki2c_write(struct ki2c_softc *, int, int, void *, int);
6340f380a1Smacallan
6440f380a1Smacallan /* I2C glue */
6540f380a1Smacallan static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
6640f380a1Smacallan void *, size_t, int);
6740f380a1Smacallan
689427a0c7Sgrant
694d01e983Smacallan CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
700477b152Sdogcow NULL, NULL);
719427a0c7Sgrant
729427a0c7Sgrant int
ki2c_match(device_t parent,cfdata_t match,void * aux)7305b09539Smatt ki2c_match(device_t parent, cfdata_t match, void *aux)
749427a0c7Sgrant {
759427a0c7Sgrant struct confargs *ca = aux;
769427a0c7Sgrant
779427a0c7Sgrant if (strcmp(ca->ca_name, "i2c") == 0)
789427a0c7Sgrant return 1;
799427a0c7Sgrant
809427a0c7Sgrant return 0;
819427a0c7Sgrant }
829427a0c7Sgrant
839427a0c7Sgrant void
ki2c_attach(device_t parent,device_t self,void * aux)8405b09539Smatt ki2c_attach(device_t parent, device_t self, void *aux)
859427a0c7Sgrant {
8605b09539Smatt struct ki2c_softc *sc = device_private(self);
879427a0c7Sgrant struct confargs *ca = aux;
889427a0c7Sgrant int node = ca->ca_node;
8968414e0eSmacallan uint32_t addr, channel, reg;
90627d9cb4Smacallan int rate, child, /*namelen,*/ i2cbus[2] = {0, 0};
9140f380a1Smacallan struct i2cbus_attach_args iba;
9212d26bd6Smacallan prop_dictionary_t dict = device_properties(self);
9312d26bd6Smacallan prop_array_t cfg;
9468414e0eSmacallan int devs, devc;
9568414e0eSmacallan char compat[256], num[8], descr[32];
963fe6d9daSmacallan prop_dictionary_t dev;
973fe6d9daSmacallan prop_data_t data;
9840f380a1Smacallan char name[32];
999427a0c7Sgrant
1004d01e983Smacallan sc->sc_dev = self;
101e6c5dacdSmacallan sc->sc_tag = ca->ca_tag;
1029427a0c7Sgrant ca->ca_reg[0] += ca->ca_baseaddr;
1039427a0c7Sgrant
1049427a0c7Sgrant if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
10512d26bd6Smacallan aprint_error(": cannot get i2c-rate\n");
1069427a0c7Sgrant return;
1079427a0c7Sgrant }
10812d26bd6Smacallan if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
10912d26bd6Smacallan aprint_error(": unable to find i2c address\n");
1109427a0c7Sgrant return;
1119427a0c7Sgrant }
112e6c5dacdSmacallan if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
113e6c5dacdSmacallan aprint_error_dev(sc->sc_dev, "failed to map registers\n");
114e6c5dacdSmacallan return;
115e6c5dacdSmacallan }
116e6c5dacdSmacallan
1179427a0c7Sgrant if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
11812d26bd6Smacallan aprint_error(": unable to find i2c address step\n");
1199427a0c7Sgrant return;
1209427a0c7Sgrant }
1219427a0c7Sgrant
1229427a0c7Sgrant printf("\n");
1239427a0c7Sgrant
1249427a0c7Sgrant ki2c_writereg(sc, STATUS, 0);
1259427a0c7Sgrant ki2c_writereg(sc, ISR, 0);
1269427a0c7Sgrant ki2c_writereg(sc, IER, 0);
1279427a0c7Sgrant
1289427a0c7Sgrant ki2c_setmode(sc, I2C_STDSUBMODE);
1299427a0c7Sgrant ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
13040f380a1Smacallan
13140f380a1Smacallan ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
13240f380a1Smacallan
13312d26bd6Smacallan cfg = prop_array_create();
13412d26bd6Smacallan prop_dictionary_set(dict, "i2c-child-devices", cfg);
13512d26bd6Smacallan prop_object_release(cfg);
13612d26bd6Smacallan
137c2b05b7fSmacallan /*
138c2b05b7fSmacallan * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
139c2b05b7fSmacallan * directly to the ki2c node so we just check if we have a child named
140c2b05b7fSmacallan * 'i2c-bus' and if so we attach its children, not ours
1412daa0664Smacallan *
1422daa0664Smacallan * XXX
1432daa0664Smacallan * should probably check for multiple i2c-bus children
144c2b05b7fSmacallan */
145627d9cb4Smacallan
146627d9cb4Smacallan int found_busnode = 0;
1472daa0664Smacallan channel = 0;
148c2b05b7fSmacallan child = OF_child(node);
149627d9cb4Smacallan while (child != 0) {
150c2b05b7fSmacallan OF_getprop(child, "name", name, sizeof(name));
1512daa0664Smacallan if (strcmp(name, "i2c-bus") == 0) {
1522daa0664Smacallan OF_getprop(child, "reg", &channel, sizeof(channel));
153627d9cb4Smacallan i2cbus[channel] = child;
1542daa0664Smacallan DPRINTF("found channel %x\n", channel);
155627d9cb4Smacallan found_busnode = 1;
1562daa0664Smacallan }
157c2b05b7fSmacallan child = OF_peer(child);
158c2b05b7fSmacallan }
159627d9cb4Smacallan if (found_busnode == 0)
160627d9cb4Smacallan i2cbus[0] = node;
16140f380a1Smacallan
162627d9cb4Smacallan for (channel = 0; channel < 2; channel++) {
163627d9cb4Smacallan devs = OF_child(i2cbus[channel]);
1643fe6d9daSmacallan while (devs != 0) {
1652e8b74c2Smacallan if (OF_getprop(devs, "name", name, 32) <= 0)
1663fe6d9daSmacallan goto skip;
1672e8b74c2Smacallan if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
1683fe6d9daSmacallan /* some i2c device nodes don't have 'compatible' */
1693fe6d9daSmacallan memset(compat, 0, 256);
1703fe6d9daSmacallan strncpy(compat, name, 256);
171d974db0aSgarbled }
1722e8b74c2Smacallan if (OF_getprop(devs, "reg", &addr, 4) <= 0)
1732e8b74c2Smacallan if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
1743fe6d9daSmacallan goto skip;
1752daa0664Smacallan addr |= channel << 8;
1762daa0664Smacallan addr = addr >> 1;
1773fe6d9daSmacallan DPRINTF("-> %s@%x\n", name, addr);
1783fe6d9daSmacallan dev = prop_dictionary_create();
179dd42af5eSmacallan prop_dictionary_set_string(dev, "name", name);
180dd42af5eSmacallan data = prop_data_create_copy(compat, strlen(compat)+1);
1813fe6d9daSmacallan prop_dictionary_set(dev, "compatible", data);
1823fe6d9daSmacallan prop_object_release(data);
1833fe6d9daSmacallan prop_dictionary_set_uint32(dev, "addr", addr);
1847b642a57Smacallan prop_dictionary_set_uint64(dev, "cookie", devs);
18568414e0eSmacallan /* look for location info for sensors */
18668414e0eSmacallan devc = OF_child(devs);
18768414e0eSmacallan while (devc != 0) {
18868414e0eSmacallan if (OF_getprop(devc, "reg", ®, 4) < 4) goto nope;
18968414e0eSmacallan if (OF_getprop(devc, "location", descr, 32) <= 0)
19068414e0eSmacallan goto nope;
19168414e0eSmacallan DPRINTF("found '%s' at %02x\n", descr, reg);
19268414e0eSmacallan snprintf(num, 7, "s%02x", reg);
193dd42af5eSmacallan prop_dictionary_set_string(dev, num, descr);
19468414e0eSmacallan nope:
19568414e0eSmacallan devc = OF_peer(devc);
19668414e0eSmacallan }
1973fe6d9daSmacallan prop_array_add(cfg, dev);
1983fe6d9daSmacallan prop_object_release(dev);
1993fe6d9daSmacallan skip:
2003fe6d9daSmacallan devs = OF_peer(devs);
20140f380a1Smacallan }
202627d9cb4Smacallan }
20340f380a1Smacallan
2043fe6d9daSmacallan /* fill in the i2c tag */
205601e1783Sthorpej iic_tag_init(&sc->sc_i2c);
2063fe6d9daSmacallan sc->sc_i2c.ic_cookie = sc;
2073fe6d9daSmacallan sc->sc_i2c.ic_exec = ki2c_i2c_exec;
20840f380a1Smacallan
2093fe6d9daSmacallan memset(&iba, 0, sizeof(iba));
2103fe6d9daSmacallan iba.iba_tag = &sc->sc_i2c;
211c7fb772bSthorpej config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
2123fe6d9daSmacallan
2139427a0c7Sgrant }
2149427a0c7Sgrant
215e6c5dacdSmacallan uint8_t
ki2c_readreg(struct ki2c_softc * sc,int reg)216454af1c0Sdsl ki2c_readreg(struct ki2c_softc *sc, int reg)
2179427a0c7Sgrant {
2189427a0c7Sgrant
219e6c5dacdSmacallan return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
2209427a0c7Sgrant }
2219427a0c7Sgrant
2229427a0c7Sgrant void
ki2c_writereg(struct ki2c_softc * sc,int reg,uint8_t val)223e6c5dacdSmacallan ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
2249427a0c7Sgrant {
2259427a0c7Sgrant
226e6c5dacdSmacallan bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
2279427a0c7Sgrant delay(10);
2289427a0c7Sgrant }
2299427a0c7Sgrant
2309427a0c7Sgrant u_int
ki2c_getmode(struct ki2c_softc * sc)231454af1c0Sdsl ki2c_getmode(struct ki2c_softc *sc)
2329427a0c7Sgrant {
2339427a0c7Sgrant return ki2c_readreg(sc, MODE) & I2C_MODE;
2349427a0c7Sgrant }
2359427a0c7Sgrant
2369427a0c7Sgrant void
ki2c_setmode(struct ki2c_softc * sc,u_int mode)237454af1c0Sdsl ki2c_setmode(struct ki2c_softc *sc, u_int mode)
2389427a0c7Sgrant {
2392daa0664Smacallan ki2c_writereg(sc, MODE, mode);
2409427a0c7Sgrant }
2419427a0c7Sgrant
2429427a0c7Sgrant u_int
ki2c_getspeed(struct ki2c_softc * sc)243454af1c0Sdsl ki2c_getspeed(struct ki2c_softc *sc)
2449427a0c7Sgrant {
2459427a0c7Sgrant return ki2c_readreg(sc, MODE) & I2C_SPEED;
2469427a0c7Sgrant }
2479427a0c7Sgrant
2489427a0c7Sgrant void
ki2c_setspeed(struct ki2c_softc * sc,u_int speed)249454af1c0Sdsl ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
2509427a0c7Sgrant {
2519427a0c7Sgrant u_int x;
2529427a0c7Sgrant
2539427a0c7Sgrant KASSERT((speed & ~I2C_SPEED) == 0);
2549427a0c7Sgrant x = ki2c_readreg(sc, MODE);
2559427a0c7Sgrant x &= ~I2C_SPEED;
2569427a0c7Sgrant x |= speed;
2579427a0c7Sgrant ki2c_writereg(sc, MODE, x);
2589427a0c7Sgrant }
2599427a0c7Sgrant
2609427a0c7Sgrant int
ki2c_intr(struct ki2c_softc * sc)261454af1c0Sdsl ki2c_intr(struct ki2c_softc *sc)
2629427a0c7Sgrant {
2639427a0c7Sgrant u_int isr, x;
2649427a0c7Sgrant
2659427a0c7Sgrant isr = ki2c_readreg(sc, ISR);
2669427a0c7Sgrant if (isr & I2C_INT_ADDR) {
2679427a0c7Sgrant #if 0
2689427a0c7Sgrant if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
2699427a0c7Sgrant /* No slave responded. */
2709427a0c7Sgrant sc->sc_flags |= I2C_ERROR;
2719427a0c7Sgrant goto out;
2729427a0c7Sgrant }
2739427a0c7Sgrant #endif
2749427a0c7Sgrant
2759427a0c7Sgrant if (sc->sc_flags & I2C_READING) {
2769427a0c7Sgrant if (sc->sc_resid > 1) {
2779427a0c7Sgrant x = ki2c_readreg(sc, CONTROL);
2789427a0c7Sgrant x |= I2C_CT_AAK;
2799427a0c7Sgrant ki2c_writereg(sc, CONTROL, x);
2809427a0c7Sgrant }
2819427a0c7Sgrant } else {
2829427a0c7Sgrant ki2c_writereg(sc, DATA, *sc->sc_data++);
2839427a0c7Sgrant sc->sc_resid--;
2849427a0c7Sgrant }
2859427a0c7Sgrant }
2869427a0c7Sgrant
2879427a0c7Sgrant if (isr & I2C_INT_DATA) {
2889427a0c7Sgrant if (sc->sc_flags & I2C_READING) {
2899427a0c7Sgrant *sc->sc_data++ = ki2c_readreg(sc, DATA);
2909427a0c7Sgrant sc->sc_resid--;
2919427a0c7Sgrant
2929427a0c7Sgrant if (sc->sc_resid == 0) { /* Completed */
2939427a0c7Sgrant ki2c_writereg(sc, CONTROL, 0);
2949427a0c7Sgrant goto out;
2959427a0c7Sgrant }
2969427a0c7Sgrant } else {
2979427a0c7Sgrant #if 0
2989427a0c7Sgrant if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
2999427a0c7Sgrant /* No slave responded. */
3009427a0c7Sgrant sc->sc_flags |= I2C_ERROR;
3019427a0c7Sgrant goto out;
3029427a0c7Sgrant }
3039427a0c7Sgrant #endif
3049427a0c7Sgrant
3059427a0c7Sgrant if (sc->sc_resid == 0) {
3069427a0c7Sgrant x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
3079427a0c7Sgrant ki2c_writereg(sc, CONTROL, x);
3089427a0c7Sgrant } else {
3099427a0c7Sgrant ki2c_writereg(sc, DATA, *sc->sc_data++);
3109427a0c7Sgrant sc->sc_resid--;
3119427a0c7Sgrant }
3129427a0c7Sgrant }
3139427a0c7Sgrant }
3149427a0c7Sgrant
3159427a0c7Sgrant out:
3169427a0c7Sgrant if (isr & I2C_INT_STOP) {
3179427a0c7Sgrant ki2c_writereg(sc, CONTROL, 0);
3189427a0c7Sgrant sc->sc_flags &= ~I2C_BUSY;
3199427a0c7Sgrant }
3209427a0c7Sgrant
3219427a0c7Sgrant ki2c_writereg(sc, ISR, isr);
3229427a0c7Sgrant
3239427a0c7Sgrant return 1;
3249427a0c7Sgrant }
3259427a0c7Sgrant
3269427a0c7Sgrant int
ki2c_poll(struct ki2c_softc * sc,int timo)327454af1c0Sdsl ki2c_poll(struct ki2c_softc *sc, int timo)
3289427a0c7Sgrant {
3299427a0c7Sgrant while (sc->sc_flags & I2C_BUSY) {
3309427a0c7Sgrant if (ki2c_readreg(sc, ISR))
3319427a0c7Sgrant ki2c_intr(sc);
3329427a0c7Sgrant timo -= 100;
3339427a0c7Sgrant if (timo < 0) {
33412d26bd6Smacallan DPRINTF("i2c_poll: timeout\n");
3359427a0c7Sgrant return -1;
3369427a0c7Sgrant }
3379427a0c7Sgrant delay(100);
3389427a0c7Sgrant }
3399427a0c7Sgrant return 0;
3409427a0c7Sgrant }
3419427a0c7Sgrant
3429427a0c7Sgrant int
ki2c_start(struct ki2c_softc * sc,int addr,int subaddr,void * data,int len)34382357f6dSdsl ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
3449427a0c7Sgrant {
3459427a0c7Sgrant int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
3469427a0c7Sgrant int timo, x;
3479427a0c7Sgrant
3489427a0c7Sgrant KASSERT((addr & 1) == 0);
3499427a0c7Sgrant
3509427a0c7Sgrant sc->sc_data = data;
3519427a0c7Sgrant sc->sc_resid = len;
3529427a0c7Sgrant sc->sc_flags |= I2C_BUSY;
3539427a0c7Sgrant
3549427a0c7Sgrant timo = 1000 + len * 200;
3559427a0c7Sgrant
3569427a0c7Sgrant /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
3579427a0c7Sgrant /* if (addr == 0x68) */
3589427a0c7Sgrant timo += 100000;
3599427a0c7Sgrant
3609427a0c7Sgrant ki2c_writereg(sc, ADDR, addr | rw);
3619427a0c7Sgrant ki2c_writereg(sc, SUBADDR, subaddr);
3629427a0c7Sgrant
3639427a0c7Sgrant x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
3649427a0c7Sgrant ki2c_writereg(sc, CONTROL, x);
3659427a0c7Sgrant
3669427a0c7Sgrant if (ki2c_poll(sc, timo))
3679427a0c7Sgrant return -1;
3689427a0c7Sgrant if (sc->sc_flags & I2C_ERROR) {
36912d26bd6Smacallan DPRINTF("I2C_ERROR\n");
3709427a0c7Sgrant return -1;
3719427a0c7Sgrant }
3729427a0c7Sgrant return 0;
3739427a0c7Sgrant }
3749427a0c7Sgrant
3759427a0c7Sgrant int
ki2c_read(struct ki2c_softc * sc,int addr,int subaddr,void * data,int len)37682357f6dSdsl ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
3779427a0c7Sgrant {
3789427a0c7Sgrant sc->sc_flags = I2C_READING;
37912d26bd6Smacallan DPRINTF("ki2c_read: %02x %d\n", addr, len);
3809427a0c7Sgrant return ki2c_start(sc, addr, subaddr, data, len);
3819427a0c7Sgrant }
3829427a0c7Sgrant
3839427a0c7Sgrant int
ki2c_write(struct ki2c_softc * sc,int addr,int subaddr,void * data,int len)38482357f6dSdsl ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
3859427a0c7Sgrant {
3869427a0c7Sgrant sc->sc_flags = 0;
38712d26bd6Smacallan DPRINTF("ki2c_write: %02x %d\n",addr,len);
38840f380a1Smacallan return ki2c_start(sc, addr, subaddr, data, len);
38940f380a1Smacallan }
39040f380a1Smacallan
39140f380a1Smacallan int
ki2c_i2c_exec(void * cookie,i2c_op_t op,i2c_addr_t addr,const void * vcmd,size_t cmdlen,void * vbuf,size_t buflen,int flags)39240f380a1Smacallan ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
39340f380a1Smacallan size_t cmdlen, void *vbuf, size_t buflen, int flags)
39440f380a1Smacallan {
39540f380a1Smacallan struct ki2c_softc *sc = cookie;
396c8996319Spgoyette int i;
397c8996319Spgoyette size_t w_len;
398c8996319Spgoyette uint8_t *wp;
399*4275c534Smlelstv uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
4002daa0664Smacallan uint8_t channel;
401c8996319Spgoyette
402c8996319Spgoyette /*
403c8996319Spgoyette * We don't have any idea if the ki2c controller can execute
404c8996319Spgoyette * i2c quick_{read,write} operations, so if someone tries one,
405c8996319Spgoyette * return an error.
406c8996319Spgoyette */
407c8996319Spgoyette if (cmdlen == 0 && buflen == 0)
408c8996319Spgoyette return -1;
40940f380a1Smacallan
410*4275c534Smlelstv /*
411*4275c534Smlelstv * Transaction could be much larger now. Bail if it exceeds our
412*4275c534Smlelstv * small combining buffer, we don't expect such devices.
413*4275c534Smlelstv */
414*4275c534Smlelstv if (cmdlen + buflen > sizeof(wrbuf))
415*4275c534Smlelstv return -1;
416*4275c534Smlelstv
4172daa0664Smacallan channel = (addr & 0xf80) ? 0x10 : 0x00;
4182daa0664Smacallan addr &= 0x7f;
4192daa0664Smacallan
4202daa0664Smacallan
42140f380a1Smacallan /* we handle the subaddress stuff ourselves */
4222daa0664Smacallan ki2c_setmode(sc, channel | I2C_STDMODE);
423627d9cb4Smacallan ki2c_setspeed(sc, I2C_50kHz);
42440f380a1Smacallan
425c8996319Spgoyette /* Write-buffer defaults to vcmd */
426c8996319Spgoyette wp = (uint8_t *)(__UNCONST(vcmd));
427c8996319Spgoyette w_len = cmdlen;
428c8996319Spgoyette
429c8996319Spgoyette /*
430c8996319Spgoyette * Concatenate vcmd and vbuf for write operations
431c8996319Spgoyette *
432c8996319Spgoyette * Drivers written specifically for ki2c might already do this,
433c8996319Spgoyette * but "generic" i2c drivers still provide separate arguments
434c8996319Spgoyette * for the cmd and buf parts of iic_smbus_write_{byte,word}.
435c8996319Spgoyette */
436c8996319Spgoyette if (I2C_OP_WRITE_P(op) && buflen != 0) {
437c8996319Spgoyette if (cmdlen == 0) {
438c8996319Spgoyette wp = (uint8_t *)vbuf;
439c8996319Spgoyette w_len = buflen;
440c8996319Spgoyette } else {
441c8996319Spgoyette KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
442c8996319Spgoyette wp = (uint8_t *)(__UNCONST(vcmd));
443c8996319Spgoyette w_len = 0;
444c8996319Spgoyette for (i = 0; i < cmdlen; i++)
445c8996319Spgoyette wrbuf[w_len++] = *wp++;
446c8996319Spgoyette wp = (uint8_t *)vbuf;
447c8996319Spgoyette for (i = 0; i < buflen; i++)
448c8996319Spgoyette wrbuf[w_len++] = *wp++;
449c8996319Spgoyette wp = wrbuf;
450c8996319Spgoyette }
451c8996319Spgoyette }
452c8996319Spgoyette
4533fe6d9daSmacallan if (w_len > 0)
45412d26bd6Smacallan if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
45540f380a1Smacallan return -1;
456d974db0aSgarbled
457d974db0aSgarbled if (I2C_OP_READ_P(op)) {
45812d26bd6Smacallan if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
45940f380a1Smacallan return -1;
46040f380a1Smacallan }
46140f380a1Smacallan return 0;
4629427a0c7Sgrant }
463