Home | History | Annotate | Line # | Download | only in usb
emdtv_i2c.c revision 1.1.56.1
      1       1.1  jmcneill /* $NetBSD: emdtv_i2c.c,v 1.1.56.1 2020/04/08 14:08:13 martin Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4       1.1  jmcneill  * Copyright (c) 2008, 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5       1.1  jmcneill  * All rights reserved.
      6       1.1  jmcneill  *
      7       1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8       1.1  jmcneill  * modification, are permitted provided that the following conditions
      9       1.1  jmcneill  * are met:
     10       1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12       1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15       1.1  jmcneill  *
     16       1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  jmcneill  */
     28       1.1  jmcneill 
     29       1.1  jmcneill #include <sys/cdefs.h>
     30       1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: emdtv_i2c.c,v 1.1.56.1 2020/04/08 14:08:13 martin Exp $");
     31       1.1  jmcneill 
     32       1.1  jmcneill #include <sys/param.h>
     33       1.1  jmcneill #include <sys/systm.h>
     34       1.1  jmcneill #include <sys/device.h>
     35       1.1  jmcneill #include <sys/conf.h>
     36       1.1  jmcneill 
     37       1.1  jmcneill #include <dev/usb/usb.h>
     38       1.1  jmcneill #include <dev/usb/usbdi.h>
     39       1.1  jmcneill #include <dev/usb/usbdi_util.h>
     40       1.1  jmcneill #include <dev/usb/usbdevs.h>
     41       1.1  jmcneill 
     42       1.1  jmcneill #include <dev/i2c/i2cvar.h>
     43       1.1  jmcneill 
     44       1.1  jmcneill #include <dev/usb/emdtvvar.h>
     45       1.1  jmcneill #include <dev/usb/emdtvreg.h>
     46       1.1  jmcneill 
     47       1.1  jmcneill static int	emdtv_i2c_exec(void *, i2c_op_t, i2c_addr_t,
     48       1.1  jmcneill 			       const void *, size_t, void *, size_t, int);
     49       1.1  jmcneill 
     50       1.1  jmcneill static int	emdtv_i2c_check(struct emdtv_softc *, i2c_addr_t);
     51       1.1  jmcneill static int	emdtv_i2c_recv(struct emdtv_softc *, i2c_addr_t,
     52       1.1  jmcneill 			       uint8_t *, size_t);
     53       1.1  jmcneill static int	emdtv_i2c_send(struct emdtv_softc *, i2c_addr_t,
     54       1.1  jmcneill 				const uint8_t *, size_t, bool);
     55       1.1  jmcneill 
     56       1.1  jmcneill int
     57       1.1  jmcneill emdtv_i2c_attach(struct emdtv_softc *sc)
     58       1.1  jmcneill {
     59  1.1.56.1    martin 	iic_tag_init(&sc->sc_i2c);
     60       1.1  jmcneill 	sc->sc_i2c.ic_cookie = sc;
     61       1.1  jmcneill 	sc->sc_i2c.ic_exec = emdtv_i2c_exec;
     62       1.1  jmcneill 
     63       1.1  jmcneill 	return 0;
     64       1.1  jmcneill }
     65       1.1  jmcneill 
     66       1.1  jmcneill int
     67       1.1  jmcneill emdtv_i2c_detach(struct emdtv_softc *sc, int flags)
     68       1.1  jmcneill {
     69  1.1.56.1    martin 	iic_tag_fini(&sc->sc_i2c);
     70       1.1  jmcneill 
     71       1.1  jmcneill 	return 0;
     72       1.1  jmcneill }
     73       1.1  jmcneill 
     74       1.1  jmcneill static int
     75       1.1  jmcneill emdtv_i2c_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
     76       1.1  jmcneill     const void *cmd, size_t cmdlen, void *vbuf, size_t buflen, int flags)
     77       1.1  jmcneill {
     78       1.1  jmcneill 	struct emdtv_softc *sc = opaque;
     79       1.1  jmcneill 	int error;
     80       1.1  jmcneill 
     81       1.1  jmcneill 	if (I2C_OP_READ_P(op)) {
     82       1.1  jmcneill 		if (buflen == 0)
     83       1.1  jmcneill 			error = emdtv_i2c_check(sc, addr);
     84       1.1  jmcneill 		else
     85       1.1  jmcneill 			error = emdtv_i2c_recv(sc, addr, vbuf, buflen);
     86       1.1  jmcneill 	} else {
     87       1.1  jmcneill 		error = emdtv_i2c_send(sc, addr, cmd, cmdlen,
     88       1.1  jmcneill 		    I2C_OP_STOP_P(op));
     89       1.1  jmcneill 		error = 0;
     90       1.1  jmcneill 	}
     91       1.1  jmcneill 
     92       1.1  jmcneill 	return error;
     93       1.1  jmcneill }
     94       1.1  jmcneill 
     95       1.1  jmcneill static int
     96       1.1  jmcneill emdtv_i2c_check(struct emdtv_softc *sc, i2c_addr_t addr)
     97       1.1  jmcneill {
     98       1.1  jmcneill 	emdtv_read_1(sc, EM28XX_UR_I2C, addr);
     99       1.1  jmcneill 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
    100       1.1  jmcneill 		device_printf(sc->sc_dev, "%s failed\n", __func__);
    101       1.1  jmcneill 		return ENXIO;
    102       1.1  jmcneill 	}
    103       1.1  jmcneill 	return 0;
    104       1.1  jmcneill }
    105       1.1  jmcneill 
    106       1.1  jmcneill static int
    107       1.1  jmcneill emdtv_i2c_recv(struct emdtv_softc *sc, i2c_addr_t addr, uint8_t *datap,
    108       1.1  jmcneill     size_t len)
    109       1.1  jmcneill {
    110       1.1  jmcneill 	emdtv_read_multi_1(sc, EM28XX_UR_I2C, addr, datap, len);
    111       1.1  jmcneill 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
    112       1.1  jmcneill 		device_printf(sc->sc_dev, "%s failed\n", __func__);
    113       1.1  jmcneill 		return ENXIO;
    114       1.1  jmcneill 	}
    115       1.1  jmcneill 	return 0;
    116       1.1  jmcneill }
    117       1.1  jmcneill 
    118       1.1  jmcneill static int
    119       1.1  jmcneill emdtv_i2c_send(struct emdtv_softc *sc, i2c_addr_t addr, const uint8_t *datap,
    120       1.1  jmcneill     size_t len, bool stop)
    121       1.1  jmcneill {
    122       1.1  jmcneill 	int off = (stop == false ? 1 : 0);
    123       1.1  jmcneill 	emdtv_write_multi_1(sc, EM28XX_UR_I2C + off, addr, datap, len);
    124       1.1  jmcneill 	if (emdtv_read_1(sc, UR_GET_STATUS, EM28XX_REG_I2C_STATUS) != 0) {
    125       1.1  jmcneill 		device_printf(sc->sc_dev, "%s failed\n", __func__);
    126       1.1  jmcneill 		return ENXIO;
    127       1.1  jmcneill 	}
    128       1.1  jmcneill 	return 0;
    129       1.1  jmcneill }
    130