Home | History | Annotate | Line # | Download | only in usb
auvitek_i2c.c revision 1.6
      1 /* $NetBSD: auvitek_i2c.c,v 1.6 2021/04/24 23:36:59 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Auvitek AU0828 USB controller - I2C access ops
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: auvitek_i2c.c,v 1.6 2021/04/24 23:36:59 thorpej Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_usb.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/conf.h>
     44 #include <sys/bus.h>
     45 #include <sys/module.h>
     46 
     47 #include <dev/usb/usb.h>
     48 #include <dev/usb/usbdi.h>
     49 #include <dev/usb/usbdi_util.h>
     50 #include <dev/usb/usbdevs.h>
     51 
     52 #include <dev/i2c/i2cvar.h>
     53 
     54 #include <dev/usb/auvitekreg.h>
     55 #include <dev/usb/auvitekvar.h>
     56 
     57 /* #define AUVITEK_I2C_DEBUG */
     58 
     59 static int	auvitek_i2c_exec(void *, i2c_op_t, i2c_addr_t,
     60 				 const void *, size_t, void *, size_t, int);
     61 
     62 static int	auvitek_i2c_read(struct auvitek_softc *, i2c_addr_t,
     63 				 uint8_t *, size_t);
     64 static int	auvitek_i2c_write(struct auvitek_softc *, i2c_addr_t,
     65 				  const uint8_t *, size_t);
     66 static bool	auvitek_i2c_wait(struct auvitek_softc *);
     67 static bool	auvitek_i2c_wait_rdack(struct auvitek_softc *);
     68 static bool	auvitek_i2c_wait_rddone(struct auvitek_softc *);
     69 static bool	auvitek_i2c_wait_wrdone(struct auvitek_softc *);
     70 
     71 int
     72 auvitek_i2c_attach(struct auvitek_softc *sc)
     73 {
     74 	iic_tag_init(&sc->sc_i2c);
     75 	sc->sc_i2c.ic_cookie = sc;
     76 	sc->sc_i2c.ic_exec = auvitek_i2c_exec;
     77 
     78 	auvitek_i2c_rescan(sc, NULL, NULL);
     79 
     80 	return 0;
     81 }
     82 
     83 int
     84 auvitek_i2c_detach(struct auvitek_softc *sc, int flags)
     85 {
     86 	iic_tag_fini(&sc->sc_i2c);
     87 
     88 	if (sc->sc_i2cdev)
     89 		config_detach(sc->sc_i2cdev, flags);
     90 
     91 	return 0;
     92 }
     93 
     94 void
     95 auvitek_i2c_rescan(struct auvitek_softc *sc, const char *ifattr,
     96     const int *locs)
     97 {
     98 #ifdef AUVITEK_I2C_DEBUG
     99 	struct i2cbus_attach_args iba;
    100 
    101 	if (ifattr_match(ifattr, "i2cbus") && sc->sc_i2cdev == NULL) {
    102 		memset(&iba, 0, sizeof(iba));
    103 		iba.iba_tag = &sc->sc_i2c;
    104 		sc->sc_i2cdev = config_found(sc->sc_dev, &iba, iicbus_print,
    105 		    CFARG_IATTR, "i2cbus",
    106 		    CFARG_EOL);
    107 	}
    108 #endif
    109 }
    110 
    111 void
    112 auvitek_i2c_childdet(struct auvitek_softc *sc, device_t child)
    113 {
    114 	if (sc->sc_i2cdev == child)
    115 		sc->sc_i2cdev = NULL;
    116 }
    117 
    118 static int
    119 auvitek_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
    120     const void *cmd, size_t cmdlen, void *vbuf, size_t buflen, int flags)
    121 {
    122 	struct auvitek_softc *sc = opaque;
    123 
    124 	if (I2C_OP_READ_P(op))
    125 		return auvitek_i2c_read(sc, addr, vbuf, buflen);
    126 	else
    127 		return auvitek_i2c_write(sc, addr, cmd, cmdlen);
    128 }
    129 
    130 static int
    131 auvitek_i2c_read(struct auvitek_softc *sc, i2c_addr_t addr,
    132     uint8_t *buf, size_t buflen)
    133 {
    134 	uint8_t v;
    135 	unsigned int i;
    136 
    137 	auvitek_write_1(sc, AU0828_REG_I2C_MBMODE, 1);
    138 	auvitek_write_1(sc, AU0828_REG_I2C_CLKDIV, sc->sc_i2c_clkdiv);
    139 	auvitek_write_1(sc, AU0828_REG_I2C_DSTADDR, addr << 1);
    140 
    141 	if (buflen == 0) {
    142 		auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER,
    143 		    AU0828_I2C_TRIGGER_RD);
    144 		if (auvitek_i2c_wait_rdack(sc) == false)
    145 			return EBUSY;
    146 		return 0;
    147 	}
    148 
    149 	for (i = 0; i < buflen; i++) {
    150 		v = AU0828_I2C_TRIGGER_RD;
    151 		if (i < (buflen - 1))
    152 			v |= AU0828_I2C_TRIGGER_HOLD;
    153 		auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER, v);
    154 
    155 		if (auvitek_i2c_wait_rddone(sc) == false)
    156 			return EBUSY;
    157 
    158 		buf[i] = auvitek_read_1(sc, AU0828_REG_I2C_FIFORD);
    159 	}
    160 
    161 	if (auvitek_i2c_wait(sc) == false)
    162 		return EBUSY;
    163 
    164 	return 0;
    165 }
    166 
    167 static int
    168 auvitek_i2c_write(struct auvitek_softc *sc, i2c_addr_t addr,
    169     const uint8_t *buf, size_t buflen)
    170 {
    171 	uint8_t v;
    172 	unsigned int i, fifolen;
    173 
    174 	auvitek_write_1(sc, AU0828_REG_I2C_MBMODE, 1);
    175 	auvitek_write_1(sc, AU0828_REG_I2C_CLKDIV, sc->sc_i2c_clkdiv);
    176 	auvitek_write_1(sc, AU0828_REG_I2C_DSTADDR, addr << 1);
    177 
    178 	if (buflen == 0) {
    179 		auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER,
    180 		    AU0828_I2C_TRIGGER_RD);
    181 		if (auvitek_i2c_wait(sc) == false)
    182 			return EBUSY;
    183 		if (auvitek_i2c_wait_rdack(sc) == false)
    184 			return EBUSY;
    185 		return 0;
    186 	}
    187 
    188 	fifolen = 0;
    189 	for (i = 0; i < buflen; i++) {
    190 		v = AU0828_I2C_TRIGGER_WR;
    191 		if (i < (buflen - 1))
    192 			v |= AU0828_I2C_TRIGGER_HOLD;
    193 
    194 		auvitek_write_1(sc, AU0828_REG_I2C_FIFOWR, buf[i]);
    195 		++fifolen;
    196 
    197 		if (fifolen == 4 || i == (buflen - 1)) {
    198 			auvitek_write_1(sc, AU0828_REG_I2C_TRIGGER, v);
    199 			fifolen = 0;
    200 
    201 			if (auvitek_i2c_wait_wrdone(sc) == false)
    202 				return EBUSY;
    203 		}
    204 	}
    205 
    206 	if (auvitek_i2c_wait(sc) == false)
    207 		return EBUSY;
    208 
    209 	return 0;
    210 }
    211 
    212 static bool
    213 auvitek_i2c_wait(struct auvitek_softc *sc)
    214 {
    215 	uint8_t status;
    216 	int retry = 1000;
    217 
    218 	while (--retry > 0) {
    219 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
    220 		if (!(status & AU0828_I2C_STATUS_BUSY))
    221 			break;
    222 		delay(10);
    223 	}
    224 	if (retry == 0)
    225 		return false;
    226 
    227 	return true;
    228 }
    229 
    230 static bool
    231 auvitek_i2c_wait_rdack(struct auvitek_softc *sc)
    232 {
    233 	uint8_t status;
    234 	int retry = 1000;
    235 
    236 	while (--retry > 0) {
    237 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
    238 		if (!(status & AU0828_I2C_STATUS_NO_RD_ACK))
    239 			break;
    240 		delay(10);
    241 	}
    242 	if (retry == 0)
    243 		return false;
    244 
    245 	return true;
    246 }
    247 
    248 static bool
    249 auvitek_i2c_wait_rddone(struct auvitek_softc *sc)
    250 {
    251 	uint8_t status;
    252 	int retry = 1000;
    253 
    254 	while (--retry > 0) {
    255 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
    256 		if (status & AU0828_I2C_STATUS_RD_DONE)
    257 			break;
    258 		delay(10);
    259 	}
    260 	if (retry == 0)
    261 		return false;
    262 
    263 	return true;
    264 }
    265 
    266 static bool
    267 auvitek_i2c_wait_wrdone(struct auvitek_softc *sc)
    268 {
    269 	uint8_t status;
    270 	int retry = 1000;
    271 
    272 	while (--retry > 0) {
    273 		status = auvitek_read_1(sc, AU0828_REG_I2C_STATUS);
    274 		if (status & AU0828_I2C_STATUS_WR_DONE)
    275 			break;
    276 		delay(10);
    277 	}
    278 	if (retry == 0)
    279 		return false;
    280 
    281 	return true;
    282 }
    283